OSDN Git Service

d1d417f0f1a6d6277363fde6ea5e77fe70f8e1fd
[pettanr/clientJs.git] / 0.6.x / js / 01_dom / 20_XDomImage.js
1 \r
2 X.Dom.Image = {\r
3         _actualSize : {},\r
4         \r
5 /*\r
6  * original\r
7  * LICENSE: MIT\r
8  * AUTHOR: uupaa.js@gmail.com\r
9  */     \r
10         getActualDimension : function( XnodeOrImageElemOrSrc ){\r
11                 var xnode, img, remove, ret, run, memW, memH, w, h;\r
12         \r
13                 if( X.Type.isString( XnodeOrImageElemOrSrc ) ){\r
14                         if( ret = X.Dom.Image._actualSize[ X.Dom.getAbsolutePath( XnodeOrImageElemOrSrc ) ] ) return ret;\r
15                         \r
16                         xnode = X.Dom.Node._systemNode.create(\r
17                                 'img',\r
18                                 {\r
19                                         src : XnodeOrImageElemOrSrc\r
20                                 },\r
21                                 {\r
22                                         position   : 'absolute'\r
23                                 }\r
24                         );\r
25                         Node._body._startUpdate();\r
26                         img    = xnode._ie4getRawNode ? xnode._ie4getRawNode() : xnode._rawObject;\r
27                         remove = true;\r
28                 } else {\r
29                         if( XnodeOrImageElemOrSrc.constructor === X.Dom.Node ){\r
30                                 xnode = XnodeOrImageElemOrSrc;\r
31                                 img   = xnode._ie4getRawNode ? xnode._ie4getRawNode() : xnode._rawObject;\r
32                         } else\r
33                         if( X.Type.isHTMLElement( XnodeOrImageElemOrSrc ) ){\r
34                                 img = XnodeOrImageElemOrSrc;\r
35                         } else {\r
36                                 return;\r
37                         };\r
38                         if( ret = X.Dom.Image._actualSize[ img.src ] ) return ret;\r
39                 };\r
40 \r
41                 // for Firefox, Safari, Google Chrome\r
42                 if( img.naturalWidth ) return [ img.naturalWidth, img.naturalHeight ];\r
43         \r
44                 if( X.UA.IE && 5 <= X.UA.IE ){// for IE\r
45                         run  = img.runtimeStyle;\r
46                         memW = run.width;\r
47                         memH = run.height;\r
48         \r
49                         // keep runtimeStyle\r
50                         run.width  = 'auto';\r
51                         // override\r
52                         run.height = 'auto';\r
53                         w = img.width;\r
54                         h = img.height;\r
55                         run.width  = memW;\r
56                         // restore\r
57                         run.height = memH;\r
58                 } else {// for Opera and Other\r
59                         \r
60                         memW = w = img.width;\r
61                         memH = h = img.height;\r
62                         \r
63                         if( img.removeAttribute ){ // Safari1.3 の Image は removeAttribute がない\r
64                                 // keep current style\r
65                                 img.removeAttribute( 'width' );\r
66                                 img.removeAttribute( 'height' );\r
67                                 \r
68                                 w = img.width;\r
69                                 h = img.height;\r
70                                 \r
71                                 // restore\r
72                                 img.width  = memW;\r
73                                 img.height = memH;\r
74                         };\r
75                 };\r
76                 \r
77                 ret = X.Dom.Image._actualSize[ img.src ] = [ w, h ];\r
78                 \r
79                 remove && xnode.destroy();\r
80                 \r
81                 return ret;\r
82         },\r
83         \r
84         /* \r
85          * original\r
86          *  LICENSE: MIT?\r
87          *  URL: http://d.hatena.ne.jp/uupaa/20080413/1208067631\r
88          *  AUTHOR: uupaa.js@gmail.com\r
89          * \r
90          */\r
91         Loader : X.EventDispatcher.inherits(\r
92                 'ImageLoader',\r
93                 X.Class.POOL_OBJECT,\r
94                 {\r
95                         xnode   : null,\r
96                         size    : null,\r
97                         tick    : 0,\r
98                         timerID : 0,\r
99                         finish  : false,\r
100                         abspath : null,\r
101                         delay   : null,\r
102                         timeout : 0,\r
103                         \r
104                         Constructor : function( abspath, delay, timeout ){\r
105                                 var img;\r
106 \r
107                                 this.abspath = abspath;\r
108                                 this.delay   = delay || 100;\r
109                                 this.timeout = timeout || 10000;\r
110                                 this.xnode =\r
111                                         (\r
112                                                 window[ 'Image' ] ?\r
113                                                         X.EventDispatcher( img = new Image() ) :\r
114                                                         X.Dom.Node._systemNode.create( 'img', { src : abspath } )\r
115                                         )\r
116                                         .listen( [ 'load', 'error', 'abort', X.Event.SUCCESS, X.Event.ERROR, X.Event.KILL_INSTANCE ], this );\r
117                                 img && ( img.src = abspath );\r
118                                 this._detect();\r
119                         },\r
120                         handleEvent : function( e ){\r
121                                 var size;\r
122                                 switch( e.type ){\r
123                                         case 'error' :\r
124                                         case 'abort' :\r
125                                                 if( this.finish ) return;\r
126                                                 this.finish = true;\r
127                                                 this.timerID = this.asyncDispatch( 0, { type : X.Event.ERROR } );\r
128                                                 break;\r
129                                         case 'load' :\r
130                                         // if( finish === true ) return; // これがあると firefox3.6 で駄目、、、\r
131                                         // if( timer ) return; // これがあると safari3.2 で駄目、、、\r
132                                                 this.finish = true;\r
133                                                 this.timerID && X.Timer.remove( this.timerID );\r
134                                                 if( window.opera && !this.xnode._rawObject.complete ){\r
135                                                         this.timerID = this.asyncDispatch( 0, { type : X.Event.ERROR } );\r
136                                                         return;\r
137                                                 };\r
138                                                 size = X.Dom.Image.getActualDimension( X.UA.IE < 9 ? this.abspath : this.xnode );\r
139                                                 this.timerID = this.asyncDispatch( 0, {\r
140                                                         type : X.Event.SUCCESS,\r
141                                                         src  : this.abspath,\r
142                                                         w    : size[ 0 ],\r
143                                                         h    : size[ 1 ]\r
144                                                 } );\r
145                                                 break;\r
146                                         case X.Event.SUCCESS :\r
147                                         case X.Event.ERROR :\r
148                                                 delete this.timerID;\r
149                                                 X.Timer.once( 0, this, this.kill );\r
150                                                 break;\r
151                                         case X.Event.KILL_INSTANCE :\r
152                                                 this.timerID && X.Timer.remove( this.timerID );\r
153                                                 this.xnode.destroy && this.xnode.destroy();\r
154                                                 this.unlisten();\r
155                                                 break;\r
156                                 };\r
157                         },      \r
158                         _detect : function(){\r
159                                 if( this.finish === true ) return;\r
160                                 if( this.xnode._rawObject && this.xnode._rawObject.complete ){\r
161                                         this.finish = true;\r
162                                         if( this.xnode._rawObject.width ) return;\r
163                                         this.timerID = this.asyncDispatch( 0, { type : X.Event.ERROR } );\r
164                                         return;\r
165                                 };\r
166                                 if( ( this.tick += this.delay ) > this.timeout ){\r
167                                         this.finish = true;\r
168                                         this.timerID = this.asyncDispatch( 0, { type : X.Event.ERROR, msg : 'timeout' } );\r
169                                         return;\r
170                                 };\r
171                                 this.timerID = X.Timer.once( this.delay, this, this._detect );\r
172                         },\r
173                         stop : function(){\r
174                                 // if( this.dispatch( { type : X.Event.BEFORE_CANCEL } ) & X.Callback.CANCEL_NOW ) return;\r
175                                 \r
176                                 // this.dispacth( { type : X.Event.CANCELED } );\r
177                                 this.kill();\r
178                         }\r
179                 }\r
180         )\r
181 };\r