OSDN Git Service

Version 0.6.79, fixed X.Net.
[pettanr/clientJs.git] / 0.6.x / js / 04_net / 02_XNetJSONP.js
1 /*
2  * Operaでも非同期リクエストが並列処理できる img-JSONP
3  * http://developer.cybozu.co.jp/takesako/2007/06/opera_img-jsonp.html
4  * 
5  * IE9でiframe内で遷移した場合window.parentのメソッドを呼べない 
6  * http://kozo002.blogspot.jp/2012/07/ie9iframewindowparent.html
7  */
8
9 X.Net.JSONP = {
10         cb : function( accessKey, jsonString, time, opt_json2FileSize ){
11                         if( accessKey !== X_NET_JSONP_ACCESS_KEY ) return;
12                         
13                         X_NET_JSONPWrapper
14                                 .asyncDispatch( 0, {
15                                         type : jsonString ? X.Event.SUCCESS : X.Event.ERROR,
16                                         data : jsonString //eval( jsonString )
17                                 } );
18                         
19                         console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
20                 }
21 };
22
23 var X_NET_JSONP_ACCESS_KEY = Math.random(),
24         
25         X_NET_JSONP_NinjaIframe,
26         
27         X_NET_JSONP_imageFixListener = X.UA.Opera && function( e ){
28                 switch( e.type ){
29                         case X.Event.SUCCESS :
30                         case X.Event.ERROR :
31                                 X_NET_JSONP_loadScriptInNinjaIframe( e.src );
32                         case X.Event.TIMEOUT :
33                         case X.Event.CANCELED :
34                 };
35         };
36
37 function X_NET_JSONP_loadScriptInNinjaIframe( url ){
38         X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = new X.Util.NinjaIframe() );
39         
40         var json2Path     = 'js/libs/json2.js',
41                 json2FileSize = 18103,
42                 html;
43         
44         if( X.UA.IE8 ){
45                 html = [
46                         // http://blog.livedoor.jp/dankogai/archives/51503830.html              
47                         // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
48                         // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
49                         /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
50                         '<script>',
51                                 'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
52                         '</script>',
53                         '<script src="', url, '"></script>' */
54                         '<script id="jp"></script>',
55                         '<script>',
56                                 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',parent.JSON.stringify(o),-nw)}',
57                                 'function tm(){jp.src="', url ,'";nw=+new Date}',
58                                 'setTimeout(tm,16);',
59                         '</script>'
60                 ];
61         } else
62         if( X.UA.IE9 ){
63                 html = [
64                         '<script id="jp"></script>',
65                         '<script>',
66                                 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
67                                 'function tm(){jp.src="', url ,'";nw=+new Date}',
68                                 'setTimeout(tm,16);',
69                         '</script>'
70                 ];
71         } else
72         if( window[ 'JSON' ] ){
73                 html = [        
74                         '<script>',
75                                 'nw=+new Date;',
76                                 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
77                         '</script>',
78                         '<script src="', url, '"></script>'
79                 ];
80         } else
81         if( X.UA.IE < 5 || X.UA.MacIE ){
82                 html = [
83                         '<script id="jn"></script>',
84                         '<script id="jp"></script>',
85                         '<script>',
86                                 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
87                                 'function t1(){document.all.jn.src="', json2Path ,'";setTimeout("t2()",16);nw=+new Date}',
88                                 'setTimeout("t1()",16);',
89                                 'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{setTimeout("t2()",16)}}',
90                         '</script>'
91                 ];
92         } else
93         if( X.UA.IE < 8 ){ // ie5-7
94                 html = [
95                         '<script id="jn"></script>',
96                         '<script id="jp"></script>',
97                         '<script>',
98                                 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
99                                 'function t1(){jn.src="', json2Path ,'";setTimeout(t2,16);nw=+new Date}',
100                                 'setTimeout(t1,16);',
101                                 'function t2(){if(window.JSON){jp.src="', url ,'"}else{setTimeout(t2,16)}}',
102                         '</script>'
103                 ];
104         } else {
105                 html = [
106                         '<script>',
107                                 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw,', json2FileSize, ')}',
108                                 'nw=+new Date;',
109                         '</script>',
110                         '<script src="', json2Path, '"></script>',
111                         '<script src="', url, '"></script>'
112                 ];
113         };
114         
115         X_NET_JSONP_NinjaIframe
116                 .refresh( html.join( '' ) )
117                 .listen( [ X.Event.SUCCESS, X.Event.ERROR ], X_NET_JSONPWrapper, X_NET_JSONP_iframeListener );
118 };
119
120
121 function X_NET_JSONP_iframeListener( e ){
122         switch( e.type ){
123                 case X.Event.SUCCESS :
124                         console.log( 'iframe onload' );
125
126                         break;
127                 case X.Event.ERROR :
128                         console.log( 'iframe onerror' );
129                         X_NET_JSONPWrapper.asyncDispatch( 0, X.Event.ERROR );
130                         break;
131         };
132 };
133
134
135 X_NET_JSONPWrapper = X.Class._override(
136         new X.EventDispatcher(),
137         {
138
139                 _operaImage   : null, // X.Dom.Image.Loader();
140                 _busy         : false,
141                 _canceled     : false,
142                 
143                 load : function( url, data, timeout ){
144                         
145                         //createURL
146                         if( X_NET_JSONP_imageFixListener ){
147                                 this._operaImage = X.Dom.Image.Loader( url )
148                                         .listenOnce( [ X.Event.SUCCESS, X.Event.ERROR ], X_NET_JSONP_imageFixListener );
149                         } else {
150                                 X_NET_JSONP_loadScriptInNinjaIframe( url );
151                         };
152                                                 
153                         this._busy = true;
154                 },
155                 
156                 cancel : function(){
157                         if( this._operaImage ){
158                                 this._operaImage
159                                         .unlisten()
160                                         .abort();
161                                 delete this._operaImage;
162                         } else {
163                                 this._canceled = true;
164                         };
165                 },
166                 
167                 reset : function(){
168                         this._busy = this._canceled = false;
169                 }
170         }
171 );
172