OSDN Git Service

Version 0.6.106, bugfix for X.EventDispatcher, cleanup X.Node, Image JSONP for Opera1...
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 02_XNetJSONP.js
index 13bd9e5..806396c 100644 (file)
@@ -37,7 +37,9 @@ X.Net.JSONP = {
 
 var X_NET_JSONP_ACCESS_KEY = Math.random(),
        
-       X_NET_JSONP_NinjaIframe;
+       X_NET_JSONP_NinjaIframe,
+       
+       X_Net_JSONP_onloadCount;
 
 
 function X_NET_JSONP_loadScriptInNinjaIframe( url ){
@@ -48,35 +50,56 @@ function X_NET_JSONP_loadScriptInNinjaIframe( url ){
        X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = new X.Util.NinjaIframe() );
        
        // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
-       
+       // TODO postMessage の利用
+
+       // numonLoad
+
+       if( X_UA.Opera ){
+               html = [
+                       ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
+                       '<script>',
+                               'onunload=function(){im.onload=im.onerror=""};',
+                               'nw=+new Date;',
+                               'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
+                       '</script>',    
+                       '<script id="jp"></script>',
+                       '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
+               ];
+               X_Net_JSONP_onloadCount = 2;
+       } else
        if( X_UA.IE8 ){
                html = [
                        // http://blog.livedoor.jp/dankogai/archives/51503830.html              
                        // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
                        // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
-                       /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
-                       '<script>',
-                               'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
-                       '</script>',
-                       '<script src="', url, '"></script>' */
                        '<script id="jp"></script>',
                        '<script>',
+                               'onunload=function(){clearTimeout(id)};',
                                'nw=0;', // なぜか必要,,,
                                'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',parent.JSON.stringify(o),-nw)}',
                                'function tm(){jp.src="', url ,'";nw=+new Date}',
-                               'setTimeout(tm,16);',
+                               'id=setTimeout(tm,16);',
                        '</script>'
+                       
+                       /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
+                       '<script>',
+                               'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
+                       '</script>',
+                       '<script src="', url, '"></script>' */
                ];
+               X_Net_JSONP_onloadCount = 2;
        } else
        if( X_UA.IE9 ){
                html = [
                        '<script id="jp"></script>',
                        '<script>',
+                               'onunload=function(){clearTimeout(id)};',
                                'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
                                'function tm(){jp.src="', url ,'";nw=+new Date}',
-                               'setTimeout(tm,16);',
+                               'id=setTimeout(tm,16);',
                        '</script>'
                ];
+               X_Net_JSONP_onloadCount = 2;
        } else
        if( window[ 'JSON' ] ){
                html = [        
@@ -86,30 +109,35 @@ function X_NET_JSONP_loadScriptInNinjaIframe( url ){
                        '</script>',
                        '<script src="', url, '"></script>'
                ];
+               X_Net_JSONP_onloadCount = 1;
        } else
        if( X_UA.IE4 || X_UA.MacIE ){
                html = [
                        '<script id="jn"></script>',
                        '<script id="jp"></script>',
                        '<script>',
+                               'onunload=function(){clearTimeout(id)};',
                                'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
-                               'function t1(){document.all.jn.src="', json2Path ,'";setTimeout("t2()",16);nw=+new Date}',
-                               'setTimeout("t1()",16);',
-                               'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{setTimeout("t2()",16)}}',
+                               'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
+                               'id=setTimeout("t1()",16);',
+                               'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
                        '</script>'
                ];
+               X_Net_JSONP_onloadCount = 3;
        } else
        if( X_UA.IE < 8 ){ // ie5-7
                html = [
                        '<script id="jn"></script>',
                        '<script id="jp"></script>',
                        '<script>',
+                               'onunload=function(){clearTimeout(id)};',
                                'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
-                               'function t1(){jn.src="', json2Path ,'";setTimeout(t2,16);nw=+new Date}',
-                               'setTimeout(t1,16);',
-                               'function t2(){if(window.JSON){jp.src="', url ,'"}else{setTimeout(t2,16)}}',
+                               'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
+                               'id=setTimeout(t1,16);',
+                               'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
                        '</script>'
                ];
+               X_Net_JSONP_onloadCount = 3;
        } else {
                html = [
                        '<script>',
@@ -119,6 +147,7 @@ function X_NET_JSONP_loadScriptInNinjaIframe( url ){
                        '<script src="', json2Path, '"></script>',
                        '<script src="', url, '"></script>'
                ];
+               X_Net_JSONP_onloadCount = 1;
        };
        
        X_NET_JSONP_NinjaIframe
@@ -130,14 +159,18 @@ function X_NET_JSONP_loadScriptInNinjaIframe( url ){
 function X_NET_JSONP_iframeListener( e ){
        switch( e.type ){
                case X.Event.SUCCESS :
-                       console.log( 'iframe onload' );
-
+                       console.log( 'iframe onload, but' );
+                       if( ++X_NET_JSONPWrapper._onloadCount < X_Net_JSONP_onloadCount ) return;
+                       // TODO callback が無ければ error
+                       X_NET_JSONPWrapper.asyncDispatch( 1000, X.Event.ERROR );
                        break;
                case X.Event.ERROR :
                        console.log( 'iframe onerror' );
-                       X_NET_JSONPWrapper.asyncDispatch( 0, X.Event.ERROR );
+                       X_NET_JSONPWrapper.asyncDispatch( X.Event.ERROR );
                        break;
        };
+       X_NET_JSONP_NinjaIframe.unlisten();
+       return X.Callback.UN_LISTEN;
 };
 
 
@@ -145,51 +178,24 @@ X_NET_JSONPWrapper = X_Class_override(
        new X.EventDispatcher(),
        {
 
-               _operaImage   : null, // X.Net.Image();
                _busy         : false,
                _canceled     : false,
+               _onloadCount  : 0,
                
                load : function( url, data, timeout ){
                        //createURL
-                       if( X_NET_JSONP_operaImageHandleEvent ){
-                               this._operaImage = X.Net.Image( url, false )
-                                       .listenOnce( [ X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_JSONP_operaImageHandleEvent );
-                       } else {
-                               X_NET_JSONP_loadScriptInNinjaIframe( url );
-                       };
+                       X_NET_JSONP_loadScriptInNinjaIframe( url );
                                                
                        this._busy = true;
                },
                
                cancel : function(){
-                       if( this._operaImage ){
-                               this._operaImage
-                                       .unlisten( [ X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_JSONP_operaImageHandleEvent )
-                                       .cancel();
-                               delete this._operaImage;
-                       } else {
-                               this._canceled = true;
-                       };
+                       this._canceled = true;
                },
                
                reset : function(){
                        this._busy = this._canceled = false;
+                       this._onloadCount = 0;
                }
        }
 );
-
-function X_NET_JSONP_operaImageHandleEvent( e ){
-       switch( e.type ){
-               case X.Event.SUCCESS :
-               case X.Event.ERROR :
-                       X_NET_JSONP_loadScriptInNinjaIframe( e.src );
-                       break;
-               case X.Event.TIMEOUT :
-                       X_NET_JSONPWrapper._operaImage
-                               .unlisten( [ X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_JSONP_operaImageHandleEvent )
-                               .reset();
-                       X_NET_JSONPWrapper.asyncDispatch( 0, X.Event.ERROR );
-                       break;
-       };
-};
-if( !X_UA.Opera ) X_NET_JSONP_operaImageHandleEvent = null;
\ No newline at end of file