4 * URL: http://d.hatena.ne.jp/uupaa/20080413/1208067631
\r
5 * AUTHOR: uupaa.js@gmail.com
\r
9 var X_Net_Image_hasImage = !!window[ 'Image' ],
\r
10 X_Net_Image_image = X_Net_Image_hasImage && new Image(),
\r
11 // IE では厳密には HTMLImageElement ではなく、appendChild してもサイズが取れず、removeChild に失敗する
\r
12 X_Net_Image_isElement = !( X_UA.IE < 9 ) && X.Type.isHTMLElement( X_Net_Image_image );
\r
15 if( !X_Net_Image_hasImage ){
\r
16 alert( 'no Image!' );
\r
18 if( X_Net_Image_isElement ){
\r
19 //alert( 'X_Net_Image_isElement ' + X_Net_Image_isElement );
\r
24 * new Image() のときに Image オブジェクトを作るもの(IE8-)と、HTMLImageElement を作るものがある。
\r
25 * Image は、X.EventDispatcher で、<img> は X.Node で。
\r
27 * Opera7 では毎回 image を作る必要あり、src が異なればOK?
\r
29 X_NET_ImageWrapper = X_Class_override(
\r
30 !X_Net_Image_isElement ? new X.EventDispatcher( X_Net_Image_image ) : new X.Node( X_Net_Image_image ),
\r
41 load : function( data ){
\r
43 this.abspath = X.URL.toAbsolutePath( data.url );
\r
44 this.delay = data.delay || 100;
\r
45 this.timeout = data.timeout || 5000;
\r
47 this._rawObject.src = this.abspath;
\r
49 if( X_UA.Opera7 && this._rawObject.complete ){
\r
50 this.asyncDispatch( 'load' );
\r
52 this.timerID = X.Timer.add( this.delay, 0, this, this._detect );
\r
56 handleEvent : function( e ){
\r
60 //case 'abort' : // TODO ??
\r
61 if( this.finish ) return;
\r
64 this.timerID && X.Timer.remove( this.timerID );
\r
65 this.timerID = this.asyncDispatch( /*e.type === 'error' ?*/ X.Event.ERROR /*: X.Event.CANCELED*/ );
\r
68 // if( finish === true ) return; // これがあると firefox3.6 で駄目、、、
\r
69 // if( timer ) return; // これがあると safari3.2 で駄目、、、
\r
72 this.timerID && X.Timer.remove( this.timerID );
\r
73 if( X_UA.Opera && !this._rawObject.complete ){
\r
74 this.timerID = this.asyncDispatch( X.Event.ERROR );
\r
77 size = X.Util.Image.getActualDimension( !X_Net_Image_isElement ? this.abspath : this );
\r
78 this.timerID = this.asyncDispatch( {
\r
79 type : X.Event.SUCCESS,
\r
83 // TODO feedback net speed
\r
84 // time , this._rawObject.fileSize
\r
87 case X.Event.KILL_INSTANCE :
\r
89 !X_Net_Image_hasImage && this.destroy(); // if xnode
\r
94 _detect : function(){
\r
95 if( this.finish ) return;
\r
96 if( this._rawObject && this._rawObject.complete ){
\r
99 if( this._rawObject.width ) return;
\r
100 X.Timer.remove( this.timerID );
\r
101 this.timerID = this.asyncDispatch( X.Event.ERROR );
\r
103 if( this.timeout < ( this.tick += this.delay ) ){
\r
104 this._busy = false;
\r
105 this.finish = true;
\r
106 X.Timer.remove( this.timerID );
\r
107 this.timerID = this.asyncDispatch( X.Event.TIMEOUT );
\r
111 cancel : function(){
\r
113 this._rawObject && this._rawObject.abort && this._rawObject.abort();
\r
114 // this._rawObject.src = '';
\r
115 this._busy = false;
\r
116 this.finish = true;
\r
117 this.asyncDispatch( X.Event.CANCELED );
\r
120 reset : function(){
\r
121 this.timerID && X.Timer.remove( this.timerID );
\r
122 //X_Net_Image_isElement ? this._rawObject.removeAttribute( 'src' ) : ( this._rawObject.src = '' );
\r
123 this._rawObject.src = '';
\r
125 this._busy = false;
\r
126 this.finished = false;
\r
132 X_NET_ImageWrapper.listen( [ 'load', 'error' /*, 'abort'*/, X.Event.KILL_INSTANCE ] );
\r
134 // X_Net_Image_isElement && X_NET_ImageWrapper.appendTo( X.X_Node_systemNode );
\r