OSDN Git Service

Version 0.6.106, bugfix for X.EventDispatcher, cleanup X.Node, Image JSONP for Opera1...
[pettanr/clientJs.git] / 0.6.x / js / 05_util / 01_XNinjaIframe.js
1 /*\r
2  * http://msdn.microsoft.com/ja-jp/library/ie/hh180174%28v=vs.85%29.aspx\r
3  * 孤立するとウィンドウ オブジェクトのプロパティが消去される\r
4  */\r
5 \r
6 X.Util.NinjaIframe = X.EventDispatcher.inherits(\r
7         'NinjaIframe',\r
8         {\r
9                 autoRefresh  : 0,\r
10                 \r
11                 xnodeIframe  : null,\r
12                 \r
13                 _iwin        : null,\r
14                 \r
15                 _html        : '',\r
16                 _name        : '',\r
17                 _ready       : false,\r
18                 \r
19                 Constructor : function( html ){\r
20                         \r
21                         this._name = 'hidden-iframe-' + X_Timer_now();\r
22                         // https://github.com/polygonplanet/Pot.js/blob/master/src/Worker.js\r
23 \r
24                         this.xnodeIframe = X_Node_body.create(\r
25                                 'iframe',\r
26                                 {\r
27                                         className         : 'hidden-iframe',\r
28                                         scrolling         : 'no',\r
29                                         frameborder       : 0,\r
30                                         allowtransparency : 'no',\r
31                                         name              : this._name,\r
32                                         id                : this._name\r
33                                 }\r
34                         );\r
35                         \r
36                         X.ViewPort.listenOnce( X.Event.AFTER_UPDATE, this );\r
37                         \r
38                         // http://nanto.asablo.jp/blog/2011/12/08/6237308\r
39                         // IE 6/7 で文書間通信を実現するための一案\r
40                         if( X_UA.IE < 9 ){\r
41                                 this.xnodeIframe.attr( 'src', 'about:blank' );\r
42                         };\r
43                         // Safari 2.0.* bug: iframe's absolute position and src set.\r
44                         if( !X_UA.Webkit ){\r
45                                 this.xnodeIframe.css( { position : 'absolute' } );\r
46                         };\r
47                         \r
48                         if( html ) this._html = html;\r
49                 },\r
50                 \r
51                 handleEvent : function( e ){\r
52                         var raw = this.xnodeIframe._rawObject,\r
53                                 html, idoc;\r
54                         \r
55                         switch( e.type ){\r
56                                 case X.Event.AFTER_UPDATE :\r
57                                         this._iwin = raw.contentWindow || ( raw.contentDocument && raw.contentDocument.parentWindow ) || window.frames[ this._name ];\r
58                                         // http://d.hatena.ne.jp/NeoCat/20080921/1221940658\r
59                                         // こちらに名前をsetしないとtargetが動作しない\r
60                                         this._iwin.name = this._name;\r
61                                         \r
62                                         this.xnodeIframe.listen( X_UA.IE < 9 ? [ 'readystatechange', 'error' ] : [ 'load', 'error' ], this );\r
63                                         \r
64                                         if( !( X_UA.IE < 9 ) ){\r
65                                                 this._html && X_Util_NinjaIframe_writeToIframe( this );\r
66                                                 this._ready = true;\r
67                                                 return;\r
68                                         };\r
69                                         \r
70                                 case 'readystatechange' :\r
71                                         if( ( raw.readyState !== 'complete' && raw.readyState !== 'loaded' ) ) return X.Callback.STOP_PROPAGATION;\r
72                                         // ie9-\r
73                                         if( !this._ready ){\r
74                                                 this._html && X_Util_NinjaIframe_writeToIframe( this );\r
75                                                 this._ready = true;\r
76                                                 return;                 \r
77                                         };\r
78                                         // onload\r
79                                 case 'load' :\r
80                                         console.log( 'iframe load.' );\r
81                                         this.asyncDispatch( X.Event.SUCCESS );\r
82                                         break;\r
83                                 case 'error' :\r
84                                         this.asyncDispatch( X.Event.ERROR );\r
85                                         break;\r
86                         };\r
87                         \r
88                         return X.Callback.STOP_PROPAGATION;                     \r
89                 },\r
90                 \r
91                 refresh : function( opt_html ){\r
92                         var raw = this.xnodeIframe._rawObject,\r
93                                 idoc;\r
94                                 \r
95                         this._ready = false;\r
96                         \r
97                         if( !this._iwin ){\r
98                                 this._html = opt_html;\r
99                                 return this;\r
100                         };\r
101                         \r
102                         if( X_UA.IE5 || X_UA.IE55 ){\r
103                                 this._iwin.location.href = 'about:blank'; // reload() では、IE5.5(IETester)で2回目移行の操作でerrorが出る(doc取得やopen,writeで)\r
104                         } else {\r
105                                 this._iwin.location.reload();\r
106                         };\r
107                         \r
108                         if( !opt_html ) return this;\r
109                         \r
110                         this._html = opt_html;\r
111                         X_UA.IE < 9 || X_Util_NinjaIframe_writeToIframe( this );\r
112                         \r
113                         return this;\r
114                 },\r
115                 \r
116                 close : function(){\r
117                         X_ViewPort.unlisten( X.Event.AFTER_UPDATE, this );\r
118                         this.xnodeIframe.call( 'close' );\r
119                         this.xnodeIframe.destroy();\r
120                 }\r
121                 \r
122         }\r
123 );\r
124 \r
125 function X_Util_NinjaIframe_writeToIframe( that ){\r
126         var raw  = that.xnodeIframe._rawObject,\r
127                 idoc = raw.contentDocument || that._iwin.document,\r
128                 html = that._html;\r
129                 \r
130         delete that._html;\r
131         that._ready = true;\r
132 \r
133         idoc.open();\r
134         idoc.writeln( html );\r
135         idoc.close();\r
136 };\r
137 \r