OSDN Git Service

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