OSDN Git Service

806396c92ab96bc3faef6b98bec501a94975ab63
[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         X_Net_JSONP_onloadCount;
43
44
45 function X_NET_JSONP_loadScriptInNinjaIframe( url ){
46         var json2Path     = 'js/libs/json2.js',
47                 json2FileSize = 18103,
48                 html;
49         
50         X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = new X.Util.NinjaIframe() );
51         
52         // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
53         // TODO postMessage の利用
54
55         // numonLoad
56
57         if( X_UA.Opera ){
58                 html = [
59                         ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
60                         '<script>',
61                                 'onunload=function(){im.onload=im.onerror=""};',
62                                 'nw=+new Date;',
63                                 'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
64                         '</script>',    
65                         '<script id="jp"></script>',
66                         '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
67                 ];
68                 X_Net_JSONP_onloadCount = 2;
69         } else
70         if( X_UA.IE8 ){
71                 html = [
72                         // http://blog.livedoor.jp/dankogai/archives/51503830.html              
73                         // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
74                         // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
75                         '<script id="jp"></script>',
76                         '<script>',
77                                 'onunload=function(){clearTimeout(id)};',
78                                 'nw=0;', // なぜか必要,,,
79                                 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',parent.JSON.stringify(o),-nw)}',
80                                 'function tm(){jp.src="', url ,'";nw=+new Date}',
81                                 'id=setTimeout(tm,16);',
82                         '</script>'
83                         
84                         /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
85                         '<script>',
86                                 'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
87                         '</script>',
88                         '<script src="', url, '"></script>' */
89                 ];
90                 X_Net_JSONP_onloadCount = 2;
91         } else
92         if( X_UA.IE9 ){
93                 html = [
94                         '<script id="jp"></script>',
95                         '<script>',
96                                 'onunload=function(){clearTimeout(id)};',
97                                 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
98                                 'function tm(){jp.src="', url ,'";nw=+new Date}',
99                                 'id=setTimeout(tm,16);',
100                         '</script>'
101                 ];
102                 X_Net_JSONP_onloadCount = 2;
103         } else
104         if( window[ 'JSON' ] ){
105                 html = [        
106                         '<script>',
107                                 'nw=+new Date;',
108                                 'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
109                         '</script>',
110                         '<script src="', url, '"></script>'
111                 ];
112                 X_Net_JSONP_onloadCount = 1;
113         } else
114         if( X_UA.IE4 || X_UA.MacIE ){
115                 html = [
116                         '<script id="jn"></script>',
117                         '<script id="jp"></script>',
118                         '<script>',
119                                 'onunload=function(){clearTimeout(id)};',
120                                 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
121                                 'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
122                                 'id=setTimeout("t1()",16);',
123                                 'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
124                         '</script>'
125                 ];
126                 X_Net_JSONP_onloadCount = 3;
127         } else
128         if( X_UA.IE < 8 ){ // ie5-7
129                 html = [
130                         '<script id="jn"></script>',
131                         '<script id="jp"></script>',
132                         '<script>',
133                                 'onunload=function(){clearTimeout(id)};',
134                                 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
135                                 'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
136                                 'id=setTimeout(t1,16);',
137                                 'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
138                         '</script>'
139                 ];
140                 X_Net_JSONP_onloadCount = 3;
141         } else {
142                 html = [
143                         '<script>',
144                                 '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}}',
145                                 'nw=+new Date;',
146                         '</script>',
147                         '<script src="', json2Path, '"></script>',
148                         '<script src="', url, '"></script>'
149                 ];
150                 X_Net_JSONP_onloadCount = 1;
151         };
152         
153         X_NET_JSONP_NinjaIframe
154                 .refresh( html.join( '' ) )
155                 .listen( [ X.Event.SUCCESS, X.Event.ERROR ], X_NET_JSONPWrapper, X_NET_JSONP_iframeListener );
156 };
157
158
159 function X_NET_JSONP_iframeListener( e ){
160         switch( e.type ){
161                 case X.Event.SUCCESS :
162                         console.log( 'iframe onload, but' );
163                         if( ++X_NET_JSONPWrapper._onloadCount < X_Net_JSONP_onloadCount ) return;
164                         // TODO callback が無ければ error
165                         X_NET_JSONPWrapper.asyncDispatch( 1000, X.Event.ERROR );
166                         break;
167                 case X.Event.ERROR :
168                         console.log( 'iframe onerror' );
169                         X_NET_JSONPWrapper.asyncDispatch( X.Event.ERROR );
170                         break;
171         };
172         X_NET_JSONP_NinjaIframe.unlisten();
173         return X.Callback.UN_LISTEN;
174 };
175
176
177 X_NET_JSONPWrapper = X_Class_override(
178         new X.EventDispatcher(),
179         {
180
181                 _busy         : false,
182                 _canceled     : false,
183                 _onloadCount  : 0,
184                 
185                 load : function( url, data, timeout ){
186                         //createURL
187                         X_NET_JSONP_loadScriptInNinjaIframe( url );
188                                                 
189                         this._busy = true;
190                 },
191                 
192                 cancel : function(){
193                         this._canceled = true;
194                 },
195                 
196                 reset : function(){
197                         this._busy = this._canceled = false;
198                         this._onloadCount = 0;
199                 }
200         }
201 );