OSDN Git Service

Version 0.6.135, fix for closure compiler.
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 02_XNetJSONP.js
index 806396c..6c008f0 100644 (file)
  * TODO postMessage
  */
 
-X.Net.JSONP = {
-       cb : function( accessKey, jsonString, time, opt_json2FileSize ){
+X[ 'Net' ][ 'JSONP' ] = {
+       'cb' : function( accessKey, jsonString, time, opt_json2FileSize ){
                        if( accessKey !== X_NET_JSONP_ACCESS_KEY || !X_NET_JSONPWrapper._busy ) return;
                        
                        X_NET_JSONPWrapper._busy = false;
                        
                        X_NET_JSONPWrapper
-                               .asyncDispatch( 0, {
-                                       type : jsonString ? X.Event.SUCCESS : X.Event.ERROR,
-                                       data : jsonString //eval( jsonString )
+                               [ 'asyncDispatch' ]( {
+                                       type : jsonString ? X_EVENT_SUCCESS : X_EVENT_ERROR,
+                                       data : window.JSON ? JSON.parse( jsonString ) : eval( 'var a=' + jsonString + ';a' )
                                } );
                        
                        console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
                }
 };
 
+function X_Net_JSONP_reciveMessage( data ){
+       console.log( data );
+};
+
 var X_NET_JSONP_ACCESS_KEY = Math.random(),
        
+       X_NET_JSONP_SEND_MSG_KEY = X_System.message( 'X.Net.JSONP', X_Net_JSONP_reciveMessage ),
+       
        X_NET_JSONP_NinjaIframe,
        
        X_Net_JSONP_onloadCount;
 
+       
 
 function X_NET_JSONP_loadScriptInNinjaIframe( url ){
        var json2Path     = 'js/libs/json2.js',
                json2FileSize = 18103,
                html;
        
-       X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = new X.Util.NinjaIframe() );
+       X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = X[ 'Util' ][ 'NinjaIframe' ]() );
        
        // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
        // TODO postMessage の利用
+       // document.postMessage()→window.postMessage() (Opera 9.50 build 9841 -)
+       // http://d.hatena.ne.jp/cnrd/20080518/1211099169
+       // 最近の仕様変更(引数のtargetOriginとかMessageEventのoriginとか)にはまだ対応してないみたい 
 
-       // numonLoad
-
-       if( X_UA.Opera ){
+       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}}',
+                               'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw', window[ 'JSON' ] ? json2FileSize : 0 ,');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 ){
+       if( X_UA[ 'IE8' ] ){
                html = [
+                       // JavaScriptでunicode文字列をunescapeする
+                       // http://perutago.seesaa.net/article/202801583.html
+                       
                        // http://blog.livedoor.jp/dankogai/archives/51503830.html              
                        // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
                        // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
@@ -76,7 +87,8 @@ function X_NET_JSONP_loadScriptInNinjaIframe( url ){
                        '<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 cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',parent.JSON.stringify(o).replace(/\\\\u([a-fA-F0-9]{4})/g,function(a,b){return String.fromCharCode(parseInt(b,16))}),-nw)}',
+                               //'function cb(o){if(nw){nw-=+new Date;postMessage("', X_NET_JSONP_SEND_MSG_KEY,' "+nw+"|"+parent.JSON.stringify(o).replace(/\\\\u([a-fA-F0-9]{4})/g,function(a,b){return String.fromCharCode(parseInt(b,16))}),"*");nw=0}}',                   
                                'function tm(){jp.src="', url ,'";nw=+new Date}',
                                'id=setTimeout(tm,16);',
                        '</script>'
@@ -89,7 +101,7 @@ function X_NET_JSONP_loadScriptInNinjaIframe( url ){
                ];
                X_Net_JSONP_onloadCount = 2;
        } else
-       if( X_UA.IE9 ){
+       if( X_UA[ 'IE9' ] ){
                html = [
                        '<script id="jp"></script>',
                        '<script>',
@@ -106,12 +118,13 @@ function X_NET_JSONP_loadScriptInNinjaIframe( url ){
                        '<script>',
                                '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}}',
+                               //'function cb(o){if(nw){nw-=+new Date;parent.postMessage("', X_NET_JSONP_SEND_MSG_KEY,' "+nw+"|"+JSON.stringify(o),"', location.origin, '");nw=0}}',
                        '</script>',
                        '<script src="', url, '"></script>'
                ];
                X_Net_JSONP_onloadCount = 1;
        } else
-       if( X_UA.IE4 || X_UA.MacIE ){
+       if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){
                html = [
                        '<script id="jn"></script>',
                        '<script id="jp"></script>',
@@ -125,7 +138,7 @@ function X_NET_JSONP_loadScriptInNinjaIframe( url ){
                ];
                X_Net_JSONP_onloadCount = 3;
        } else
-       if( X_UA.IE < 8 ){ // ie5-7
+       if( X_UA[ 'IE' ] < 8 ){ // ie5-7
                html = [
                        '<script id="jn"></script>',
                        '<script id="jp"></script>',
@@ -151,31 +164,31 @@ function X_NET_JSONP_loadScriptInNinjaIframe( url ){
        };
        
        X_NET_JSONP_NinjaIframe
-               .refresh( html.join( '' ) )
-               .listen( [ X.Event.SUCCESS, X.Event.ERROR ], X_NET_JSONPWrapper, X_NET_JSONP_iframeListener );
+               [ 'refresh' ]( html.join( '' ) )
+               [ 'listen' ]( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], X_NET_JSONPWrapper, X_NET_JSONP_iframeListener );
 };
 
 
 function X_NET_JSONP_iframeListener( e ){
        switch( e.type ){
-               case X.Event.SUCCESS :
-                       console.log( 'iframe onload, but' );
+               case X_EVENT_SUCCESS :
+                       console.log( 'iframe onload, but ' + X_NET_JSONPWrapper._onloadCount + ' < ' + X_Net_JSONP_onloadCount );
                        if( ++X_NET_JSONPWrapper._onloadCount < X_Net_JSONP_onloadCount ) return;
                        // TODO callback が無ければ error
-                       X_NET_JSONPWrapper.asyncDispatch( 1000, X.Event.ERROR );
+                       X_NET_JSONPWrapper[ 'asyncDispatch' ]( 1000, X_EVENT_ERROR );
                        break;
-               case X.Event.ERROR :
+               case X_EVENT_ERROR :
                        console.log( 'iframe onerror' );
-                       X_NET_JSONPWrapper.asyncDispatch( X.Event.ERROR );
+                       X_NET_JSONPWrapper[ 'asyncDispatch' ]( X_EVENT_ERROR );
                        break;
        };
-       X_NET_JSONP_NinjaIframe.unlisten();
-       return X.Callback.UN_LISTEN;
+       X_NET_JSONP_NinjaIframe[ 'unlisten' ]();
+       return X_Callback_UN_LISTEN;
 };
 
-
+// TODO extend NinjaIframe
 X_NET_JSONPWrapper = X_Class_override(
-       new X.EventDispatcher(),
+       X_EventDispatcher(),
        {
 
                _busy         : false,