X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=0.6.x%2Fjs%2F05_util%2F01_XNinjaIframe.js;h=8507c5e3a644d50c757da75ef7b611231406a8b1;hb=094b0536bafe5efd70540698cf74ab13ece03ebb;hp=6be609a5aea752b333224ee8742054c5ef918cd6;hpb=572e81e88d5e6f8e3c5d73df7c95e61f1c4544e0;p=pettanr%2FclientJs.git diff --git a/0.6.x/js/05_util/01_XNinjaIframe.js b/0.6.x/js/05_util/01_XNinjaIframe.js index 6be609a..8507c5e 100644 --- a/0.6.x/js/05_util/01_XNinjaIframe.js +++ b/0.6.x/js/05_util/01_XNinjaIframe.js @@ -1,29 +1,32 @@ /* * http://msdn.microsoft.com/ja-jp/library/ie/hh180174%28v=vs.85%29.aspx * 孤立するとウィンドウ オブジェクトのプロパティが消去される + * + * http://qiita.com/sou/items/3380d4fa9b08b27bb387 + * モバイルブラウザでの iframe の挙動(Mobile Safari, Chrome for Android) */ // TODO Node.inherits -X.Util.NinjaIframe = X.EventDispatcher.inherits( +X[ 'Util' ][ 'NinjaIframe' ] = Node[ 'inherits' ]( 'NinjaIframe', { autoRefresh : 0, - xnodeIframe : null, - _iwin : null, _contentHTML : '', _name : '', _ready : false, - Constructor : function( html ){ + 'Constructor' : function( html ){ this._name = 'hidden-iframe-' + X_Timer_now(); // https://github.com/polygonplanet/Pot.js/blob/master/src/Worker.js - this.xnodeIframe = X_Node_body.create( - 'iframe', + X_Node_newByTag = true; + + this[ 'Super' ]( + 'IFRAME', { className : 'hidden-iframe', scrolling : 'no', @@ -35,62 +38,25 @@ X.Util.NinjaIframe = X.EventDispatcher.inherits( } ); - X_ViewPort.listenOnce( X.Event.AFTER_UPDATE, this ); - // http://nanto.asablo.jp/blog/2011/12/08/6237308 // IE 6/7 で文書間通信を実現するための一案 - if( X_UA.IE < 9 ){ - this.xnodeIframe.attr( 'src', 'about:blank' ); + if( X_UA[ 'IE' ] < 9 ){ + this[ 'attr' ]( 'src', 'about:blank' ); }; // Safari 2.0.* bug: iframe's absolute position and src set. - if( !X_UA.Webkit ){ - this.xnodeIframe.css( { position : 'absolute' } ); + if( !X_UA[ 'Webkit' ] ){ + this[ 'css' ]( { position : 'absolute' } ); }; - if( html ) this._contentHTML = html; - }, - - handleEvent : function( e ){ - var raw = this.xnodeIframe._rawObject; + if( html ) this._contentHTML = html; - switch( e.type ){ - case X.Event.AFTER_UPDATE : - this._iwin = raw.contentWindow || ( raw.contentDocument && raw.contentDocument.parentWindow ) || window.frames[ this._name ]; - // http://d.hatena.ne.jp/NeoCat/20080921/1221940658 - // こちらに名前をsetしないとtargetが動作しない - this._iwin.name = this._name; - - this.xnodeIframe.listen( [ X_UA.IE < 9 ? 'readystatechange' : 'load', 'error' ], this ); - - if( !( X_UA.IE < 9 ) ){ - this._contentHTML && X_Util_NinjaIframe_writeToIframe( this ); - this._ready = true; - return; - }; - - case 'readystatechange' : - if( ( raw.readyState !== 'complete' && raw.readyState !== 'loaded' ) ) break; - // ie9- - if( !this._ready ){ - this._contentHTML && X_Util_NinjaIframe_writeToIframe( this ); - this._ready = true; - break; - }; - // onload - case 'load' : - console.log( 'iframe load.' ); - this.asyncDispatch( X.Event.SUCCESS ); - break; - case 'error' : - this.asyncDispatch( X.Event.ERROR ); - break; - }; + this[ 'appendTo' ]( X_Node_body /* X_Node_systemNode */ ); - return X.Callback.STOP_PROPAGATION; + X_ViewPort[ 'listenOnce' ]( X_EVENT_AFTER_UPDATE, this, X_Util_NinjaIframe_handleEvent ); }, - refresh : function( opt_contentHTML ){ - var raw = this.xnodeIframe._rawObject, + 'refresh' : function( opt_contentHTML ){ + var raw = this[ '_rawObject' ], idoc; this._ready = false; @@ -100,36 +66,82 @@ X.Util.NinjaIframe = X.EventDispatcher.inherits( return this; }; - if( X_UA.IE5 || X_UA.IE55 ){ + if( X_UA[ 'IE5x' ] ){ this._iwin.location.href = 'about:blank'; // reload() では、IE5.5(IETester)で2回目移行の操作でerrorが出る(doc取得やopen,writeで) } else { this._iwin.location.reload(); }; - if( !opt_contentHTML ) return this; + if( !opt_contentHTML && opt_contentHTML !== '' ) return this; this._contentHTML = opt_contentHTML; - X_UA.IE < 9 || X_Util_NinjaIframe_writeToIframe( this ); + + if( !( X_UA[ 'IE' ] < 9 ) ){ + X_Util_NinjaIframe_writeToIframe( this ); + this._ready = true; + }; return this; }, - close : function(){ - X_ViewPort.unlisten( X.Event.AFTER_UPDATE, this ); - this.xnodeIframe.call( 'close' ); - this.xnodeIframe.destroy(); + // TODO close -> kill + 'close' : function(){ + X_ViewPort[ 'unlisten' ]( X_EVENT_AFTER_UPDATE, this, X_Util_NinjaIframe_handleEvent ); + this.call( 'close' ); + this[ 'kill' ](); } } ); + +function X_Util_NinjaIframe_handleEvent( e ){ + var raw = this[ '_rawObject' ]; + + switch( e.type ){ + case X_EVENT_AFTER_UPDATE : + this._iwin = raw.contentWindow || ( raw.contentDocument && raw.contentDocument.parentWindow ) || window.frames[ this._name ]; + // http://d.hatena.ne.jp/NeoCat/20080921/1221940658 + // こちらに名前をsetしないとtargetが動作しない + if( X_UA[ 'IE' ] ) this._iwin.name = this._name; + + this[ 'listen' ]( [ X_UA[ 'IE' ] < 9 ? 'readystatechange' : 'load', 'error' ], this, X_Util_NinjaIframe_handleEvent ); + + if( !( X_UA[ 'IE' ] < 9 ) ){ + ( this._contentHTML || this._contentHTML === '' ) && X_Util_NinjaIframe_writeToIframe( this ); + this._ready = true; + return; + }; + //break; これあると IE8 で駄目! + + case 'readystatechange' : + if( ( raw.readyState !== 'complete' && raw.readyState !== 'loaded' ) ) break; + // ie9- + if( !this._ready ){ + ( this._contentHTML || this._contentHTML === '' ) && X_Util_NinjaIframe_writeToIframe( this ); + this._ready = true; + break; + }; + // onload + case 'load' : + console.log( 'iframe load.' ); + this[ 'asyncDispatch' ]( 'ninjaload' ); + break; + + case 'error' : + this[ 'asyncDispatch' ]( 'ninjaerror' ); + break; + }; + + return X_Callback_STOP_PROPAGATION; +}; + function X_Util_NinjaIframe_writeToIframe( that ){ - var raw = that.xnodeIframe._rawObject, + var raw = that[ '_rawObject' ], idoc = raw.contentDocument || that._iwin.document, html = that._contentHTML; delete that._contentHTML; - that._ready = true; idoc.open(); idoc.writeln( html );