OSDN Git Service

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