OSDN Git Service

Version 0.6.157, add X.Net.Form & fix X.EventDispatcher.
[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 iframe 内を他ドメインにして jsonp のセキュリティを強化。他ドメインに jsonp を読み込んで 正しい JSON か?検証後にフレーム間通信で戻す。
20  */
21
22         // TODO chashe
23         // TODO iframe useful or not. TODO check dynamicIframe
24         // TODO file: では http: は使えない
25
26 X[ 'Net' ][ 'JSONP' ] = {
27         'cb' : function( accessKey, jsonString, time, opt_json2FileSize ){
28                         if( accessKey !== X_NET_JSONP_ACCESS_KEY || !X_NET_JSONPWrapper._busy ) return;
29                         
30                         X_NET_JSONPWrapper._busy = false;
31                         
32                         X_NET_JSONPWrapper
33                                 [ 'asyncDispatch' ]( {
34                                         type : jsonString ? X_EVENT_SUCCESS : X_EVENT_ERROR,
35                                         data : X_String_parseTrustedJsonString( jsonString )
36                                 } );
37                         
38                         X_Net_JSONP_errorTimerID && X_Timer_remove( X_Net_JSONP_errorTimerID );
39                         
40                         console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
41                 }
42 };
43
44 var X_NET_JSONP_ACCESS_KEY = Math.random(),
45         
46         X_Net_JSONP_maxOnloadCount,
47         
48         X_Net_JSONP_onloadCount = 0,
49         
50         X_Net_JSONP_errorTimerID;
51
52 X_TEMP.X_NET_JSONP_init = function(){
53         X_NET_JSONPWrapper = X_Class_override( X[ 'Util' ][ 'NinjaIframe' ](), X_TEMP.X_NET_JSONP_params );
54         
55         delete X_TEMP.X_NET_JSONP_init;
56         delete X_TEMP.X_NET_JSONP_params;
57         
58         return X_NET_JSONPWrapper;
59 };
60
61 X_TEMP.X_NET_JSONP_params = {
62         
63                         _busy         : false,
64                         _canceled     : false,
65                         
66                         load : function( option ){
67                                 //createURL
68                                 var url           = option[ 'url' ],
69                                         json2Path     = 'js/libs/json2.js',
70                                         json2FileSize = 18103,
71                                         html;
72                                 
73                                 // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
74                                 // document.postMessage()→window.postMessage() (Opera 9.50 build 9841 -)
75                                 // http://d.hatena.ne.jp/cnrd/20080518/1211099169
76                                 // 最近の仕様変更(引数のtargetOriginとかMessageEventのoriginとか)にはまだ対応してないみたい 
77                         
78                                 if( X_UA[ 'Opera' ] ){
79                                         html = [
80                                                 ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
81                                                 '<script>',
82                                                         'onunload=function(){im.onload=im.onerror=""};',
83                                                         'nw=+new Date;',
84                                                         '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}}',
85                                                 '</script>',    
86                                                 '<script id="jp"></script>',
87                                                 '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
88                                         ];
89                                         X_Net_JSONP_maxOnloadCount = 2;
90                                 } else
91                                 if( X_UA[ 'IE8' ] ){
92                                         html = [
93                                                 // JavaScriptでunicode文字列をunescapeする
94                                                 // http://perutago.seesaa.net/article/202801583.html
95                                                 
96                                                 // http://blog.livedoor.jp/dankogai/archives/51503830.html              
97                                                 // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
98                                                 // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
99                                                 '<script id="jp"></script>',
100                                                 '<script>',
101                                                         'onunload=function(){clearTimeout(id)};',
102                                                         'nw=0;', // なぜか必要,,,
103                                                         '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)}',
104                                                         //'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}}',                   
105                                                         'function tm(){jp.src="', url ,'";nw=+new Date}',
106                                                         'id=setTimeout(tm,16);',
107                                                 '</script>'
108                                                 
109                                                 /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
110                                                 '<script>',
111                                                         'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
112                                                 '</script>',
113                                                 '<script src="', url, '"></script>' */
114                                         ];
115                                         X_Net_JSONP_maxOnloadCount = 2;
116                                 } else
117                                 if( X_UA[ 'IE9' ] ){
118                                         html = [
119                                                 '<script id="jp"></script>',
120                                                 '<script>',
121                                                         'onunload=function(){clearTimeout(id)};',
122                                                         'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
123                                                         'function tm(){jp.src="', url ,'";nw=+new Date}',
124                                                         'id=setTimeout(tm,16);',
125                                                 '</script>'
126                                         ];
127                                         X_Net_JSONP_maxOnloadCount = 2;
128                                 } else
129                                 if( window[ 'JSON' ] ){
130                                         html = [        
131                                                 '<script>',
132                                                         'nw=+new Date;',
133                                                         'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
134                                                         //'function cb(o){if(nw){nw-=+new Date;parent.postMessage("', X_NET_JSONP_SEND_MSG_KEY,' "+nw+"|"+JSON.stringify(o),"', location.origin, '");nw=0}}',
135                                                 '</script>',
136                                                 '<script src="', url, '"></script>'
137                                         ];
138                                         X_Net_JSONP_maxOnloadCount = 1;
139                                 } else
140                                 if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){
141                                         html = [
142                                                 '<script id="jn"></script>',
143                                                 '<script id="jp"></script>',
144                                                 '<script>',
145                                                         'onunload=function(){clearTimeout(id)};',
146                                                         'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
147                                                         'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
148                                                         'id=setTimeout("t1()",16);',
149                                                         'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
150                                                 '</script>'
151                                         ];
152                                         X_Net_JSONP_maxOnloadCount = 3;
153                                 } else
154                                 if( X_UA[ 'IE' ] < 8 ){ // ie5-7
155                                         html = [
156                                                 '<script id="jn"></script>',
157                                                 '<script id="jp"></script>',
158                                                 '<script>',
159                                                         'onunload=function(){clearTimeout(id)};',
160                                                         'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
161                                                         'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
162                                                         'id=setTimeout(t1,16);',
163                                                         'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
164                                                 '</script>'
165                                         ];
166                                         X_Net_JSONP_maxOnloadCount = 3;
167                                 } else {
168                                         html = [
169                                                 '<script>',
170                                                         '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}}',
171                                                         'nw=+new Date;',
172                                                 '</script>',
173                                                 '<script src="', json2Path, '"></script>',
174                                                 '<script src="', url, '"></script>'
175                                         ];
176                                         X_Net_JSONP_maxOnloadCount = 2;
177                                 };
178                                 
179                                 X_NET_JSONPWrapper
180                                         [ 'refresh' ]( html.join( '' ) )
181                                         [ 'listen' ]( [ 'ninjaload', 'ninjaerror' ], X_NET_JSONP_iframeListener );
182                                                         
183                                 X_NET_JSONPWrapper._busy = true;
184                         },
185                         
186                         cancel : function(){
187                                 X_NET_JSONPWrapper.reset();
188                                 X_NET_JSONPWrapper._canceled = true;
189                         },
190                         
191                         reset : function(){
192                                 X_NET_JSONPWrapper._busy = X_NET_JSONPWrapper._canceled = false;
193                                 X_NET_JSONPWrapper[ 'unlisten' ]( [ 'ninjaload', 'ninjaerror' ], X_NET_JSONP_iframeListener );
194                                 X_NET_JSONPWrapper[ 'refresh' ]( '' );
195                                 X_Net_JSONP_errorTimerID && X_Timer_remove( X_Net_JSONP_errorTimerID );
196                                 X_Net_JSONP_errorTimerID = X_Net_JSONP_onloadCount = 0;
197                         }
198                 };
199
200 function X_NET_JSONP_iframeListener( e ){
201         switch( e.type ){
202                 case 'ninjaload' :
203                         console.log( 'iframe onload, but ' + X_Net_JSONP_onloadCount + ' < ' + X_Net_JSONP_maxOnloadCount );
204                         if( ++X_Net_JSONP_onloadCount < X_Net_JSONP_maxOnloadCount ) return;
205                         
206                         // TODO callback が無ければ error -> timeout を観る?
207                         X_Net_JSONP_errorTimerID = X_NET_JSONPWrapper[ 'asyncDispatch' ]( 1000, X_EVENT_ERROR );
208                         break;
209                 case 'ninjaerror' :
210                         console.log( 'iframe onerror' );
211                         X_NET_JSONPWrapper[ 'asyncDispatch' ]( X_EVENT_ERROR );
212                         break;
213         };
214         return X_Callback_UN_LISTEN;
215 };