OSDN Git Service

Version 0.6.132, fix X.Node._flags & fix X.EventDispatcher._listeners & start to...
[pettanr/clientJs.git] / 0.6.x / js / 06_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 Image(),\r
11         // IE では厳密には HTMLImageElement ではなく、appendChild してもサイズが取れず、removeChild に失敗する\r
12         X_Net_Image_isElement = !( X_UA.IE < 9 ) && X.Type.isHTMLElement( X_Net_Image_image );\r
13         \r
14 \r
15 if( !X_Net_Image_hasImage ){\r
16         alert( 'no Image!' );\r
17 } else\r
18 if( X_Net_Image_isElement ){\r
19         //alert( 'X_Net_Image_isElement ' + X_Net_Image_isElement );\r
20 };\r
21 \r
22 /*\r
23  * TODO\r
24  * new Image() のときに Image オブジェクトを作るもの(IE8-)と、HTMLImageElement を作るものがある。\r
25  * Image は、X.EventDispatcher で、<img> は X.Node で。 \r
26  * \r
27  * Opera7 では毎回 image を作る必要あり、src が異なればOK?\r
28  */\r
29 X_NET_ImageWrapper = X_Class_override(\r
30         !X_Net_Image_isElement ? new X.EventDispatcher( X_Net_Image_image ) : new X.Node( X_Net_Image_image ),\r
31         {\r
32 \r
33                 _busy      : false,\r
34                 tick       : 0,\r
35                 timerID    : 0,\r
36                 finish     : false,\r
37                 abspath    : '',\r
38                 delay      : 0,\r
39                 timeout    : 0,\r
40                 \r
41                 load : function( data ){\r
42                         this._busy   = true;\r
43                         this.abspath = X.URL.toAbsolutePath( data.url );\r
44                         this.delay   = data.delay || 100;\r
45                         this.timeout = data.timeout || 5000;\r
46 \r
47                         this._rawObject.src = this.abspath;\r
48 \r
49                         if( X_UA.Opera7 && this._rawObject.complete ){\r
50                                 this.asyncDispatch( 'load' );\r
51                         } else {\r
52                                 this.timerID = X.Timer.add( this.delay, 0, this, this._detect );\r
53                         };\r
54                 },\r
55                 \r
56                 handleEvent : function( e ){\r
57                         var size;\r
58                         switch( e.type ){\r
59                                 case 'error' :\r
60                                 //case 'abort' : // TODO ??\r
61                                         if( this.finish ) return;\r
62                                         this._busy  = false;\r
63                                         this.finish  = true;\r
64                                         this.timerID && X.Timer.remove( this.timerID );\r
65                                         this.timerID = this.asyncDispatch( /*e.type === 'error' ?*/ X.Event.ERROR /*: X.Event.CANCELED*/ );\r
66                                         break;\r
67                                 case 'load' :\r
68                                 // if( finish === true ) return; // これがあると firefox3.6 で駄目、、、\r
69                                 // if( timer ) return; // これがあると safari3.2 で駄目、、、\r
70                                         this._busy  = false;\r
71                                         this.finish = true;\r
72                                         this.timerID && X.Timer.remove( this.timerID );\r
73                                         if( X_UA.Opera && !this._rawObject.complete ){\r
74                                                 this.timerID = this.asyncDispatch( X.Event.ERROR );\r
75                                                 return;\r
76                                         };\r
77                                         size = X.Util.Image.getActualDimension( !X_Net_Image_isElement ? this.abspath : this );\r
78                                         this.timerID = this.asyncDispatch( {\r
79                                                 type : X.Event.SUCCESS,\r
80                                                 src  : this.abspath,\r
81                                                 w    : size[ 0 ],\r
82                                                 h    : size[ 1 ]\r
83                                                 // TODO feedback net speed\r
84                                                 // time , this._rawObject.fileSize\r
85                                         } );\r
86                                         break;\r
87                                 case X.Event.KILL_INSTANCE :\r
88                                         this.reset();\r
89                                         !X_Net_Image_hasImage && this.destroy(); // if xnode\r
90                                         break;\r
91                         };\r
92                 },\r
93                 \r
94                 _detect : function(){\r
95                         if( this.finish ) return;\r
96                         if( this._rawObject && this._rawObject.complete ){\r
97                                 this._busy  = false;\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( X.Event.ERROR );\r
102                         } else\r
103                         if( this.timeout < ( this.tick += this.delay ) ){\r
104                                 this._busy  = false;\r
105                                 this.finish = true;\r
106                                 X.Timer.remove( this.timerID );\r
107                                 this.timerID = this.asyncDispatch( X.Event.TIMEOUT );\r
108                         };\r
109                 },\r
110                 \r
111                 cancel : function(){\r
112                         // abort がある?\r
113                         this._rawObject && this._rawObject.abort && this._rawObject.abort();\r
114                         // this._rawObject.src = '';\r
115                         this._busy  = false;\r
116                         this.finish = true;\r
117                         this.asyncDispatch( X.Event.CANCELED );\r
118                 },\r
119                 \r
120                 reset : function(){\r
121                         this.timerID && X.Timer.remove( this.timerID );\r
122                         //X_Net_Image_isElement ? this._rawObject.removeAttribute( 'src' ) : ( this._rawObject.src = '' );\r
123                         this._rawObject.src = '';\r
124                         this.timerID  = 0;\r
125                         this._busy    = false;\r
126                         this.finished = false;\r
127                         this.abspath  = '';\r
128                 }\r
129         }\r
130 );\r
131 \r
132 X_NET_ImageWrapper.listen( [ 'load', 'error' /*, 'abort'*/, X.Event.KILL_INSTANCE ] );\r
133 \r
134 // X_Net_Image_isElement && X_NET_ImageWrapper.appendTo( X.X_Node_systemNode );\r