OSDN Git Service

Version 0.6.90, files & folders renamed.
[pettanr/clientJs.git] / 0.6.x / js / 05_net / 04_XNetImage.js
1 /* \r
2  * original\r
3  *  LICENSE: MIT?\r
4  *  URL: http://d.hatena.ne.jp/uupaa/20080413/1208067631\r
5  *  AUTHOR: uupaa.js@gmail.com\r
6  * \r
7  */\r
8 \r
9 var X_Net_Image_hasImage  = !!window[ 'Image' ],\r
10         X_Net_Image_image     = X_Net_Image_hasImage && new window[ 'Image' ](),\r
11         X_Net_Image_isElement = X.Type.isHTMLElement( X_Net_Image_image );\r
12         \r
13 \r
14 if( !X_Net_Image_hasImage ){\r
15         alert( 'no Image!' );\r
16 };\r
17 \r
18 /*\r
19  * TODO\r
20  * new Image() のときに Image オブジェクトを作るもの(IE8-)と、HTMLImageElement を作るものがある。\r
21  * Image は、X.EventDispatcher で、<img> は X.Dom.Node で。\r
22  */\r
23 X_NET_ImageWrapper = ( !X_Net_Image_isElement ? X.EventDispatcher : X.Dom.Node ).inherits(\r
24         'X.Net.Image',\r
25         X.Class.POOL_OBJECT,\r
26         {\r
27                 _rawObject : X_Net_Image_image,\r
28                 \r
29                 tick       : 0,\r
30                 timerID    : 0,\r
31                 finish     : false,\r
32                 abspath    : '',\r
33                 delay      : 0,\r
34                 timeout    : 0,\r
35                 \r
36                 Constructor : X_Net_Image_hasImage ?\r
37                         (function(){\r
38                                 this.listen( [ 'load', 'error', 'abort', X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT, X.Event.KILL_INSTANCE ] );\r
39                         }) :\r
40                         (function(){\r
41                                 this.SuperConstructor( document.createElement( 'img' ) )\r
42                                         .appendTo( X.Dom.Node._systemNode )\r
43                                         .listen( [ 'load', 'error', 'abort', X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT, X.Event.KILL_INSTANCE ] );\r
44                         }),\r
45                 \r
46                 load : function( abspath, opt_delay, opt_timeout ){\r
47                         this.abspath = abspath;\r
48                         this.delay   = opt_delay || 100;\r
49                         this.timeout = opt_timeout || 10000;\r
50                         this.timerID = X.Timer.add( this.delay, 0, this, this._detect );\r
51                         \r
52                         X_Net_Image_hasImage ? ( this._rawObject.src = abspath ) : this.attr( 'src', abspath );\r
53                 },\r
54                 \r
55                 handleEvent : function( e ){\r
56                         var size;\r
57                         switch( e.type ){\r
58                                 case 'error' :\r
59                                 case 'abort' : // TODO ??\r
60                                         if( this.finish ) return;\r
61                                         this.finish  = true;\r
62                                         this.timerID && X.Timer.remove( this.timerID );\r
63                                         this.timerID = this.asyncDispatch( 0, { type : X.Event.ERROR } );\r
64                                         break;\r
65                                 case 'load' :\r
66                                 // if( finish === true ) return; // これがあると firefox3.6 で駄目、、、\r
67                                 // if( timer ) return; // これがあると safari3.2 で駄目、、、\r
68                                         this.finish = true;\r
69                                         this.timerID && X.Timer.remove( this.timerID );\r
70                                         if( X.UA.Opera && !this._rawObject.complete ){\r
71                                                 this.timerID = this.asyncDispatch( 0, { type : X.Event.ERROR } );\r
72                                                 return;\r
73                                         };\r
74                                         size = X.Dom.Image.getActualDimension( !X_Net_Image_isElement ? this.abspath : this );\r
75                                         this.timerID = this.asyncDispatch( 0, {\r
76                                                 type : X.Event.SUCCESS,\r
77                                                 src  : this.abspath,\r
78                                                 w    : size[ 0 ],\r
79                                                 h    : size[ 1 ]\r
80                                         } );\r
81                                         break;\r
82                                 case X.Event.SUCCESS :\r
83                                 case X.Event.ERROR :\r
84                                 case X.Event.TIMEOUT :\r
85                                         this.timerID && X.Timer.remove( this.timerID );\r
86                                         this.timerID = this.asyncDispatch( 0, X.Event.COMPLETE );\r
87                                         break;\r
88                                 case X.Event.KILL_INSTANCE :\r
89                                         this.reset();\r
90                                         !X_Net_Image_hasImage && this.destroy(); // if xnode\r
91                                         this.unlisten();\r
92                                         break;\r
93                         };\r
94                 },      \r
95                 _detect : function(){\r
96                         if( this.finish ) return;\r
97                         if( this._rawObject && this._rawObject.complete ){\r
98                                 this.finish = true;\r
99                                 if( this._rawObject.width ) return;\r
100                                 X.Timer.remove( this.timerID );\r
101                                 this.timerID = this.asyncDispatch( 0, X.Event.ERROR );\r
102                         } else\r
103                         if( this.timeout < ( this.tick += this.delay ) ){\r
104                                 this.finish = true;\r
105                                 X.Timer.remove( this.timerID );\r
106                                 this.timerID = this.asyncDispatch( 0, X.Event.TIMEOUT );\r
107                         };\r
108                 },\r
109                 cancel : function(){\r
110                         this._rawObject && this._rawObject.abort();\r
111                 },\r
112                 \r
113                 reset : function(){\r
114                         this.timerID && X.Timer.remove( this.timerID );\r
115                         this.timerID  = 0;\r
116                         this.finished = false;\r
117                         this.abspath  = '';\r
118                 }\r
119         }\r
120 );\r