OSDN Git Service

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