OSDN Git Service

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