OSDN Git Service

Version 0.6.105, rename folders.
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 04_XNetImage.js
diff --git a/0.6.x/js/06_net/04_XNetImage.js b/0.6.x/js/06_net/04_XNetImage.js
new file mode 100644 (file)
index 0000000..6a90a46
--- /dev/null
@@ -0,0 +1,141 @@
+/* \r
+ * original\r
+ *  LICENSE: MIT?\r
+ *  URL: http://d.hatena.ne.jp/uupaa/20080413/1208067631\r
+ *  AUTHOR: uupaa.js@gmail.com\r
+ * \r
+ */\r
+\r
+var X_Net_Image_hasImage  = !!window[ 'Image' ],\r
+       X_Net_Image_image     = X_Net_Image_hasImage && new Image(),\r
+       // IE では厳密には HTMLImageElement ではなく、appendChild してもサイズが取れず、removeChild に失敗する\r
+       X_Net_Image_isElement = !( X_UA.IE < 9 ) && X.Type.isHTMLElement( X_Net_Image_image );\r
+       \r
+\r
+if( !X_Net_Image_hasImage ){\r
+       alert( 'no Image!' );\r
+} else\r
+if( X_Net_Image_isElement ){\r
+       //alert( 'X_Net_Image_isElement ' + X_Net_Image_isElement );\r
+};\r
+\r
+/*\r
+ * TODO\r
+ * new Image() のときに Image オブジェクトを作るもの(IE8-)と、HTMLImageElement を作るものがある。\r
+ * Image は、X.EventDispatcher で、<img> は X.Node で。 \r
+ * \r
+ * Opera7 では毎回 image を作る必要あり、src が異なればOK?\r
+ */\r
+X_NET_ImageWrapper = X_Class_override(\r
+       !X_Net_Image_isElement ? new X.EventDispatcher( X_Net_Image_image ) : new X.Node( X_Net_Image_image ),\r
+       {\r
+\r
+               _busy      : false,\r
+               tick       : 0,\r
+               timerID    : 0,\r
+               finish     : false,\r
+               abspath    : '',\r
+               delay      : 0,\r
+               timeout    : 0,\r
+               \r
+               load : function( data ){\r
+                       this._busy   = true;\r
+                       this.abspath = X.URL.toAbsolutePath( data.url );\r
+                       this.delay   = data.delay || 100;\r
+                       this.timeout = data.timeout || 5000;\r
+                       this.timerID = X.Timer.add( this.delay, 0, this, this._detect );\r
+                       \r
+                       //X_Net_Image_hasImage ? ( this._rawObject.src = this.abspath ) : this.attr( 'src', this.abspath );\r
+                       /*\r
+                       if( X_UA.Opera7 ){\r
+                               X_EventDispatcher_toggleAllEvents( this, false );\r
+                               this._rawObject = new Image();\r
+                               X_EventDispatcher_toggleAllEvents( this, true );\r
+                       }; */\r
+                       \r
+                       this._rawObject.src = this.abspath;\r
+                       //alert( this._rawObject.src + ' ' + this._rawObject.complete );\r
+                       if( X_UA.Opera7 && this._rawObject.complete ){\r
+                               this.asyncDispatch( 'load' );\r
+                       } else {\r
+                               this.timerID = X.Timer.add( this.delay, 0, this, this._detect );\r
+                       };\r
+                       //alert( this._rawObject.src + ' ' + this._rawObject.complete );\r
+               },\r
+               \r
+               handleEvent : function( e ){\r
+                       var size;\r
+                       switch( e.type ){\r
+                               case 'error' :\r
+                               //case 'abort' : // TODO ??\r
+                                       if( this.finish ) return;\r
+                                       this._busy  = false;\r
+                                       this.finish  = true;\r
+                                       this.timerID && X.Timer.remove( this.timerID );\r
+                                       this.timerID = this.asyncDispatch( /*e.type === 'error' ?*/ X.Event.ERROR /*: X.Event.CANCELED*/ );\r
+                                       break;\r
+                               case 'load' :\r
+                               // if( finish === true ) return; // これがあると firefox3.6 で駄目、、、\r
+                               // if( timer ) return; // これがあると safari3.2 で駄目、、、\r
+                                       this._busy  = false;\r
+                                       this.finish = true;\r
+                                       this.timerID && X.Timer.remove( this.timerID );\r
+                                       if( X_UA.Opera && !this._rawObject.complete ){\r
+                                               this.timerID = this.asyncDispatch( X.Event.ERROR );\r
+                                               return;\r
+                                       };\r
+                                       size = X.Util.Image.getActualDimension( !X_Net_Image_isElement ? this.abspath : this );\r
+                                       this.timerID = this.asyncDispatch( {\r
+                                               type : X.Event.SUCCESS,\r
+                                               src  : this.abspath,\r
+                                               w    : size[ 0 ],\r
+                                               h    : size[ 1 ]\r
+                                       } );\r
+                                       break;\r
+                               case X.Event.KILL_INSTANCE :\r
+                                       this.reset();\r
+                                       !X_Net_Image_hasImage && this.destroy(); // if xnode\r
+                                       break;\r
+                       };\r
+               },\r
+               \r
+               _detect : function(){\r
+                       if( this.finish ) return;\r
+                       if( this._rawObject && this._rawObject.complete ){\r
+                               this._busy  = false;\r
+                               this.finish = true;\r
+                               if( this._rawObject.width ) return;\r
+                               X.Timer.remove( this.timerID );\r
+                               this.timerID = this.asyncDispatch( X.Event.ERROR );\r
+                       } else\r
+                       if( this.timeout < ( this.tick += this.delay ) ){\r
+                               this._busy  = false;\r
+                               this.finish = true;\r
+                               X.Timer.remove( this.timerID );\r
+                               this.timerID = this.asyncDispatch( X.Event.TIMEOUT );\r
+                       };\r
+               },\r
+               \r
+               cancel : function(){\r
+                       // abort がある?\r
+                       this._rawObject && this._rawObject.abort && this._rawObject.abort();\r
+                       this._busy  = false;\r
+                       this.finish = true;\r
+                       this.asyncDispatch( X.Event.CANCELED );\r
+               },\r
+               \r
+               reset : function(){\r
+                       this.timerID && X.Timer.remove( this.timerID );\r
+                       //X_Net_Image_isElement ? this._rawObject.removeAttribute( 'src' ) : ( this._rawObject.src = '' );\r
+                       this._rawObject.src = '';\r
+                       this.timerID  = 0;\r
+                       this._busy    = false;\r
+                       this.finished = false;\r
+                       this.abspath  = '';\r
+               }\r
+       }\r
+);\r
+\r
+X_NET_ImageWrapper.listen( [ 'load', 'error' /*, 'abort'*/, X.Event.KILL_INSTANCE ] );\r
+\r
+// X_Net_Image_isElement && X_NET_ImageWrapper.appendTo( X.X_Node_systemNode );\r