OSDN Git Service

Version 0.6.38, fix for safari2-.
[pettanr/clientJs.git] / 0.6.x / js / 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.root._startUpdate();\r
26                         img    = xnode._ie4getRawNode ? xnode._ie4getRawNode() : xnode._rawNode;\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._rawNode;\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                         Constructor : function( abspath, delay, timeout ){\r
104                                 var img, s, p;\r
105                                 \r
106                                 this.abspath = abspath;\r
107                                 this.delay   = delay || 100;\r
108                                 this.timeout = timeout || 10000;\r
109                                 this.xnode =\r
110                                         (\r
111                                                 window[ 'Image' ] ?\r
112                                                         X.Dom.Node( img = new Image() ) :\r
113                                                         X.Dom.Node._systemNode.create( 'img', { src : abspath } )\r
114                                         )\r
115                                         .listen( 'load', this )\r
116                                         .listen( 'error', this )\r
117                                         .listen( 'abort', this )\r
118                                         .listen( X.Dom.Event.LOAD_ASSET_COMPLETE, this )\r
119                                         .listen( X.Dom.Event.LOAD_ASSET_ERROR, this );\r
120                                 img && ( img.src = abspath );\r
121                                 this._detect();\r
122                         },\r
123                         handleEvent : function( e ){\r
124                                 var size;\r
125                                 switch( e.type ){\r
126                                         case 'error' :\r
127                                         case 'abort' :\r
128                                                 if( this.finish ) return;\r
129                                                 this.finish = true;\r
130                                                 this.timerID = this.asyncDispatch( 0, { type : X.Dom.Event.LOAD_ASSET_ERROR } );\r
131                                                 break;\r
132                                         case 'load' :\r
133                                         // if( finish === true ) return; // これがあると firefox3.6 で駄目、、、\r
134                                         // if( timer ) return; // これがあると safari3.2 で駄目、、、\r
135                                                 this.finish = true;\r
136                                                 this.timerID && X.Timer.remove( this.timerID );\r
137                                                 if( window.opera && !this.xnode._rawNode.complete ){\r
138                                                         this.timerID = this.asyncDispatch( 0, { type : X.Dom.Event.LOAD_ASSET_ERROR } );\r
139                                                         return;\r
140                                                 };\r
141                                                 size = X.Dom.Image.getActualDimension( X.UA.IE && X.UA.IE < 9 && window.Image ? this.abspath : this.xnode );\r
142                                                 this.timerID = this.asyncDispatch( 0, {\r
143                                                         type : X.Dom.Event.LOAD_ASSET_COMPLETE,\r
144                                                         src  : this.abspath,\r
145                                                         w    : size[ 0 ],\r
146                                                         h    : size[ 1 ]\r
147                                                 } );\r
148                                                 break;\r
149                                         case X.Dom.Event.LOAD_ASSET_COMPLETE :\r
150                                         case X.Dom.Event.LOAD_ASSET_ERROR :\r
151                                                 delete this.timerID;\r
152                                                 X.Timer.once( 0, this, this.kill );\r
153                                                 break;\r
154                                 };\r
155                         },      \r
156                         _detect : function(){\r
157                                 if( this.finish === true ) return;\r
158                                 if( this.xnode._rawNode && this.xnode._rawNode.complete ){\r
159                                         this.finish = true;\r
160                                         if( this.xnode._rawNode.width ) return;\r
161                                         this.timerID = this.asyncDispatch( 0, { type : X.Dom.Event.LOAD_ASSET_ERROR } );\r
162                                         return;\r
163                                 };\r
164                                 if( ( this.tick += this.delay ) > this.timeout ){\r
165                                         this.finish = true;\r
166                                         this.timerID = this.asyncDispatch( 0, { type : X.Dom.Event.LOAD_ASSET_ERROR, msg : 'timeout' } );\r
167                                         return;\r
168                                 };\r
169                                 this.timerID = X.Timer.once( this.delay, this, this._detect );\r
170                         },\r
171                         stop : function(){\r
172                                 // if( this.dispatch( { type : X.Dom.Event.LOAD_BEFORE_STOP } ) & X.Callback.CANCEL_NOW ) return;\r
173                                 \r
174                                 // this.dispacth( { type : X.Dom.Event.LOAD_ASSET_STOPED } );\r
175                                 this.kill();\r
176                         },\r
177                         onKill : function(){\r
178                                 this.timerID && X.Timer.remove( this.timerID );\r
179                                 this.xnode.destroy();\r
180                                 this.unlisten();\r
181                         }\r
182                 }\r
183         )\r
184 };\r