OSDN Git Service

Version 0.6.31, async parser, add X.Dom.Builder.js.
[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 = img.width;\r
61                         memH = img.height;\r
62                         // keep current style\r
63                         img.removeAttribute( 'width' );\r
64                         img.removeAttribute( 'height' );\r
65                         w = img.width;\r
66                         h = img.height;\r
67                         img.width  = memW;\r
68                         // restore\r
69                         img.height = memH;\r
70                 };\r
71                 \r
72                 ret = X.Dom.Image._actualSize[ img.src ] = [ w, h ];\r
73                 \r
74                 remove && xnode.destroy();\r
75                 \r
76                 return ret;\r
77         },\r
78         \r
79         /* \r
80          * original\r
81          *  LICENSE: MIT?\r
82          *  URL: http://d.hatena.ne.jp/uupaa/20080413/1208067631\r
83          *  AUTHOR: uupaa.js@gmail.com\r
84          * \r
85          */\r
86         Loader : X.EventDispatcher.inherits(\r
87                 'ImageLoader',\r
88                 X.Class.POOL_OBJECT,\r
89                 {\r
90                         xnode   : null,\r
91                         size    : null,\r
92                         tick    : 0,\r
93                         timerID : 0,\r
94                         finish  : false,\r
95                         abspath : null,\r
96                         delay   : null,\r
97                         timeout : 0,\r
98                         Constructor : function( abspath, delay, timeout ){\r
99                                 var img;\r
100                                 \r
101                                 this.abspath = abspath;\r
102                                 this.delay   = delay || 100;\r
103                                 this.timeout = timeout || 10000;\r
104                                 this.xnode =\r
105                                         (\r
106                                                 window.Image ?\r
107                                                         X.Dom.Node( img = new Image() ) :\r
108                                                         X.Dom.Node._systemNode.create( 'img', { src : abspath } )\r
109                                         )\r
110                                         .listen( 'load', this )\r
111                                         .listen( 'error', this )\r
112                                         .listen( 'abort', this )\r
113                                         .listen( X.Dom.Event.LOAD_ASSET_COMPLETE, this )\r
114                                         .listen( X.Dom.Event.LOAD_ASSET_ERROR, this );\r
115         \r
116                                 img && ( img.src = abspath );\r
117 \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.Dom.Event.LOAD_ASSET_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._rawNode.complete ){\r
135                                                         this.timerID = this.asyncDispatch( 0, { type : X.Dom.Event.LOAD_ASSET_ERROR } );\r
136                                                         return;\r
137                                                 };\r
138                                                 size = X.Dom.Image.getActualDimension( X.UA.IE && X.UA.IE < 9 && window.Image ? this.abspath : this.xnode );\r
139                                                 this.timerID = this.asyncDispatch( 0, {\r
140                                                         type : X.Dom.Event.LOAD_ASSET_COMPLETE,\r
141                                                         src  : this.abspath,\r
142                                                         w    : size[ 0 ],\r
143                                                         h    : size[ 1 ]\r
144                                                 } );\r
145                                                 break;\r
146                                         case X.Dom.Event.LOAD_ASSET_COMPLETE :\r
147                                         case X.Dom.Event.LOAD_ASSET_ERROR :\r
148                                                 delete this.timerID;\r
149                                                 X.Timer.once( 0, this, this.kill );\r
150                                                 break;\r
151                                 };\r
152                         },      \r
153                         _detect : function(){\r
154                                 if( this.finish === true ) return;\r
155                                 if( this.xnode._rawNode.complete ){\r
156                                         this.finish = true;\r
157                                         if( this.xnode._rawNode.width ) return;\r
158                                         this.timerID = this.asyncDispatch( 0, { type : X.Dom.Event.LOAD_ASSET_ERROR } );\r
159                                         return;\r
160                                 };\r
161                                 if( ( this.tick += this.delay ) > this.timeout ){\r
162                                         this.finish = true;\r
163                                         this.timerID = this.asyncDispatch( 0, { type : X.Dom.Event.LOAD_ASSET_ERROR } );\r
164                                         return;\r
165                                 };\r
166                                 this.timerID = X.Timer.once( this.delay, this, this._detect );\r
167                         },\r
168                         stop : function(){\r
169                                 // if( this.dispatch( { type : X.Dom.Event.LOAD_BEFORE_STOP } ) & X.Callback.CANCEL_NOW ) return;\r
170                                 \r
171                                 // this.dispacth( { type : X.Dom.Event.LOAD_ASSET_STOPED } );\r
172                                 this.kill();\r
173                         },\r
174                         onKill : function(){\r
175                                 this.timerID && X.Timer.remove( this.timerID );\r
176                                 this.xnode.destroy();\r
177                                 this.unlisten();\r
178                         }\r
179                 }\r
180         )\r
181 };\r