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
46 //this.timerID = X.Timer.add( this.delay, 0, this, this._detect );
\r
48 this._rawObject.src = this.abspath;
\r
50 if( X_UA.Opera7 && this._rawObject.complete ){
\r
51 this.asyncDispatch( 'load' );
\r
53 this.timerID = X.Timer.add( this.delay, 0, this, this._detect );
\r
57 handleEvent : function( e ){
\r
61 //case 'abort' : // TODO ??
\r
62 if( this.finish ) return;
\r
65 this.timerID && X.Timer.remove( this.timerID );
\r
66 this.timerID = this.asyncDispatch( /*e.type === 'error' ?*/ X.Event.ERROR /*: X.Event.CANCELED*/ );
\r
69 // if( finish === true ) return; // これがあると firefox3.6 で駄目、、、
\r
70 // if( timer ) return; // これがあると safari3.2 で駄目、、、
\r
73 this.timerID && X.Timer.remove( this.timerID );
\r
74 if( X_UA.Opera && !this._rawObject.complete ){
\r
75 this.timerID = this.asyncDispatch( X.Event.ERROR );
\r
78 size = X.Util.Image.getActualDimension( !X_Net_Image_isElement ? this.abspath : this );
\r
79 this.timerID = this.asyncDispatch( {
\r
80 type : X.Event.SUCCESS,
\r
84 // TODO feedback net speed
\r
85 // time , this._rawObject.fileSize
\r
88 case X.Event.KILL_INSTANCE :
\r
90 !X_Net_Image_hasImage && this.destroy(); // if xnode
\r
95 _detect : function(){
\r
96 if( this.finish ) return;
\r
97 if( this._rawObject && this._rawObject.complete ){
\r
100 if( this._rawObject.width ) return;
\r
101 X.Timer.remove( this.timerID );
\r
102 this.timerID = this.asyncDispatch( X.Event.ERROR );
\r
104 if( this.timeout < ( this.tick += this.delay ) ){
\r
105 this._busy = false;
\r
106 this.finish = true;
\r
107 X.Timer.remove( this.timerID );
\r
108 this.timerID = this.asyncDispatch( X.Event.TIMEOUT );
\r
112 cancel : function(){
\r
114 this._rawObject && this._rawObject.abort && this._rawObject.abort();
\r
115 // this._rawObject.src = '';
\r
116 this._busy = false;
\r
117 this.finish = true;
\r
118 this.asyncDispatch( X.Event.CANCELED );
\r
121 reset : function(){
\r
122 this.timerID && X.Timer.remove( this.timerID );
\r
123 //X_Net_Image_isElement ? this._rawObject.removeAttribute( 'src' ) : ( this._rawObject.src = '' );
\r
124 this._rawObject.src = '';
\r
126 this._busy = false;
\r
127 this.finished = false;
\r
133 X_NET_ImageWrapper.listen( [ 'load', 'error' /*, 'abort'*/, X.Event.KILL_INSTANCE ] );
\r
135 // X_Net_Image_isElement && X_NET_ImageWrapper.appendTo( X.X_Node_systemNode );
\r