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 X_Net_Image_isElement = X.Type.isHTMLElement( X_Net_Image_image );
\r
14 if( !X_Net_Image_hasImage ){
\r
15 alert( 'no Image!' );
\r
20 * new Image() のときに Image オブジェクトを作るもの(IE8-)と、HTMLImageElement を作るものがある。
\r
21 * Image は、X.EventDispatcher で、<img> は X.Dom.Node で。
\r
23 X_NET_ImageWrapper = ( !X_Net_Image_isElement ? X.EventDispatcher : X.Dom.Node ).inherits(
\r
25 X.Class.POOL_OBJECT,
\r
27 _rawObject : X_Net_Image_image,
\r
36 Constructor : X_Net_Image_hasImage ?
\r
38 this.listen( [ 'load', 'error', 'abort', X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT, X.Event.KILL_INSTANCE ] );
\r
41 this.SuperConstructor( document.createElement( 'img' ) )
\r
42 .appendTo( X.Dom.Node._systemNode )
\r
43 .listen( [ 'load', 'error', 'abort', X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT, X.Event.KILL_INSTANCE ] );
\r
46 load : function( abspath, opt_delay, opt_timeout ){
\r
47 this.abspath = abspath;
\r
48 this.delay = opt_delay || 100;
\r
49 this.timeout = opt_timeout || 10000;
\r
50 this.timerID = X.Timer.add( this.delay, 0, this, this._detect );
\r
52 X_Net_Image_hasImage ? ( this._rawObject.src = abspath ) : this.attr( 'src', abspath );
\r
55 handleEvent : function( e ){
\r
59 case 'abort' : // TODO ??
\r
60 if( this.finish ) return;
\r
62 this.timerID && X.Timer.remove( this.timerID );
\r
63 this.timerID = this.asyncDispatch( { type : X.Event.ERROR } );
\r
66 // if( finish === true ) return; // これがあると firefox3.6 で駄目、、、
\r
67 // if( timer ) return; // これがあると safari3.2 で駄目、、、
\r
69 this.timerID && X.Timer.remove( this.timerID );
\r
70 if( X.UA.Opera && !this._rawObject.complete ){
\r
71 this.timerID = this.asyncDispatch( { type : X.Event.ERROR } );
\r
74 size = X.Dom.Image.getActualDimension( !X_Net_Image_isElement ? this.abspath : this );
\r
75 this.timerID = this.asyncDispatch( {
\r
76 type : X.Event.SUCCESS,
\r
82 case X.Event.SUCCESS :
\r
83 case X.Event.ERROR :
\r
84 case X.Event.TIMEOUT :
\r
85 this.timerID && X.Timer.remove( this.timerID );
\r
86 this.timerID = this.asyncDispatch( X.Event.COMPLETE );
\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
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.finish = true;
\r
105 X.Timer.remove( this.timerID );
\r
106 this.timerID = this.asyncDispatch( X.Event.TIMEOUT );
\r
109 cancel : function(){
\r
110 this._rawObject && this._rawObject.abort();
\r
113 reset : function(){
\r
114 this.timerID && X.Timer.remove( this.timerID );
\r
116 this.finished = false;
\r