OSDN Git Service

48d437183c3e0d479c20045156b1560520e67614
[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._busy = false;
14                         
15                         X_NET_JSONPWrapper
16                                 .asyncDispatch( 0, {
17                                         type : jsonString ? X.Event.SUCCESS : X.Event.ERROR,
18                                         data : jsonString //eval( jsonString )
19                                 } );
20                         
21                         console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
22                 }
23 };
24
25 var X_NET_JSONP_ACCESS_KEY = Math.random(),
26         
27         X_NET_JSONP_NinjaIframe;
28
29
30 function X_NET_JSONP_operaImageHandleEvent( e ){
31         switch( e.type ){
32                 case X.Event.SUCCESS :
33                 case X.Event.ERROR :
34                         X_NET_JSONP_loadScriptInNinjaIframe( e.src );
35                         break;
36                 case X.Event.TIMEOUT :
37                 case X.Event.CANCELED :
38                         X_NET_JSONPWrapper._operaImage.unlisten().reset();
39                         X_NET_JSONPWrapper.asyncDispatch( 0, X.Event.ERROR );
40                         break;
41         };
42 };
43 if( !X.UA.Opera ) X_NET_JSONP_operaImageHandleEvent = null;
44
45
46 function X_NET_JSONP_loadScriptInNinjaIframe( url ){
47         var json2Path     = 'js/libs/json2.js',
48                 json2FileSize = 18103,
49                 html;
50         
51         X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = new X.Util.NinjaIframe() );
52         
53         if( X.UA.IE8 ){
54                 html = [
55                         // http://blog.livedoor.jp/dankogai/archives/51503830.html              
56                         // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
57                         // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
58                         /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
59                         '<script>',
60                                 'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
61                         '</script>',
62                         '<script src="', url, '"></script>' */
63                         '<script id="jp"></script>',
64                         '<script>',
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){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
86                         '</script>',
87                         '<script src="', url, '"></script>'
88                 ];
89         } else
90         if( X.UA.IE < 5 || 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){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw,', json2FileSize, ')}',
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 )
156                                         .listenOnce( [ X.Event.SUCCESS, X.Event.ERROR ], 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()
168                                         .abort();
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