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 / 01_core / 16_XViewPort.js
1
2 var X_ViewPort_readyState,
3         X_ViewPort_active = !!window.parent || !!document.activeElement, // parent は frameに読み込まれた場合のieのerror回避
4         X_ViewPort_rootElement,
5         X_ViewPort_lock,
6         X_ViewPort_width,
7         X_ViewPort_height,
8         X_ViewPort_baseFontSize,
9         X_ViewPort_vScrollbarSize,
10         X_ViewPort_hScrollbarSize,
11         
12         X_Dom_detectFontSize = !( X_UA.IE < 9 || X_UA.iOS ) && function(){
13                         var size = X_Node_fontSizeNode._rawObject.offsetHeight;
14                         if( X_ViewPort_baseFontSize !== size ){
15                                 X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( X.Event.BASE_FONT_RESIZED );
16                                 X_ViewPort_baseFontSize = size;
17                         };
18         },
19
20         X_ViewPort_orientationFlag,     
21         X_Dom_orientationchange = window[ 'orientation' ] !== undefined && function( e ){
22                 X_ViewPort_orientationFlag = true;
23                 !X_UA.Android && X_ViewPort_resize();
24                 //console.log( '-- orientationchange : ' + X.ViewPort.getSize[ 0 ] + ' ' + X.ViewPort.getSize[ 1 ] );
25         },
26         
27         X_ViewPort_document = new X.EventDispatcher( document ),
28         
29         X_ViewPort_postMessageAccessKey = Math.random() * 10000 | 0,
30
31 X_ViewPort = X_Class_override(
32         new X.EventDispatcher( window ),
33         {
34
35                 handleEvent : function( e ){
36                         var href;
37                         switch( e.type ){
38                                 case 'beforeunload' :
39                                         // ie では a href="javascript" な要素でも beforeunload が起こる
40                                         href = e.target && e.target.attr && e.target.attr( 'href' );
41                                         if( href && href.indexOf && href.indexOf( 'javascript:' ) === 0 ) return X.Callback.PREVENT_DEFAULT | X.Callback.STOP_PROPAGATION;
42                                         
43                                         return X_ViewPort.dispatch( X.Event.BEFORE_UNLOAD );
44                                         
45                                 case 'unload' :
46                                         X_ViewPort.dispatch( X.Event.UNLOAD );
47                                 //alert('unload');
48                                         X_ViewPort_document.kill();
49                                         this.kill();
50                                         
51                                         //X_System.dispatch( X.Event.SHUT_DOWN );
52                                         break;
53
54                                 case 'visibilitychange' :
55                                         X_ViewPort.asyncDispatch( ( X_ViewPort_active = document[ 'hidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE );
56                                         break;
57                                 case 'webkitvisibilitychange' :
58                                         X_ViewPort.asyncDispatch( ( X_ViewPort_active = document[ 'webkitHidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE );
59                                         break;
60                                         
61                                 case 'pageshow' :
62                                 case 'focus' :
63                                         if( !X_ViewPort_active ){
64                                                 X_ViewPort_active = true;
65                                                 X_ViewPort.asyncDispatch( X.Event.VIEW_ACTIVATE );                                              
66                                         };
67                                         break;
68
69                                 case 'pagehide' :
70                                 case 'blur' :
71                                         if( X_ViewPort_active ){
72                                                 X_ViewPort_active = false;
73                                                 X_ViewPort.asyncDispatch( X.Event.VIEW_DEACTIVATE );                                            
74                                         };
75                                         break;
76                                 
77                                 // TODO
78                                 case 'message' :
79                                         if( e.origin !== X.URL.BASE_URL ){
80                                                 
81                                         } else {
82                                                 e.data;
83                                         };
84                                         break;
85                         };
86                 }
87                 
88         }
89 );
90
91
92 X.ViewPort = {
93         
94         listen : function( type, arg1, arg2, arg3 ){
95                 if( type <= X_ViewPort_readyState ){
96                         /*
97                          * X.Event.XDOM_READY 以後に listen した場合の対策
98                          */
99                         X_ViewPort.asyncDispatch( type );
100                 };
101                 // ie8-では keydown -> documentへ
102                 type && arg1 && X_ViewPort.listen( type, arg1, arg2, arg3 );
103                 return X.ViewPort;
104         },
105         
106         
107         listenOnce : function( type, arg1, arg2, arg3 ){
108                 if( type <= X_ViewPort_readyState ){
109                         /*
110                          * X.Event.XDOM_READY 以後に listen した場合の対策
111                          */
112                         X_ViewPort.asyncDispatch( type );
113                 };
114                 type && arg1 && X_ViewPort.listenOnce( type, arg1, arg2, arg3 );
115                 return X.ViewPort;
116         },
117         
118         unlisten : function( type, arg1, arg2, arg3 ){
119                 type && arg1 && X_ViewPort.unlisten( type, arg1, arg2, arg3 );
120                 return X.ViewPort;
121         },
122         
123         listening : function( type, arg1, arg2, arg3 ){
124                 return X_ViewPort.listening( type, arg1, arg2, arg3 );
125         },
126         
127         // hammer のテストファイルだけが使用
128         asyncDispatch : function(){
129                 return X_ViewPort.asyncDispatch.apply( X_ViewPort, arguments );
130         },
131         
132         getPointerPosition : function(){
133                 
134         },
135         
136         /* 要素が視界に入った  http://remysharp.com/2009/01/26/element-in-view-event-plugin/ */
137         inView : function( elm ){
138                 
139         },
140         
141         getSize : function(){
142                 return [ X_ViewPort_width, X_ViewPort_height ];
143         },
144 //https://developer.mozilla.org/en-US/docs/Web/API/Element.getBoundingClientRect
145 //use window.pageXOffset and window.pageYOffset instead of window.scrollX and window.scrollY
146 //(((t = document.documentElement) || (t = document.body.parentNode)) && typeof t.ScrollLeft == 'number' ? t : document.body).ScrollLeft;
147 //(((t = document.documentElement) || (t = document.body.parentNode)) && typeof t.ScrollTop == 'number' ? t : document.body).ScrollTop
148         
149         getDocumentSize : function(){
150                 // Opera は互換モードでは document.body.scrollHeight、標準モードでは document.documentElement.scrollHeight でページの高さが取れる。と思ってたんだけど、例外があった。
151                 // http://orera.g.hatena.ne.jp/edvakf/20100515/1273908051
152         //http://onozaty.hatenablog.com/entry/20060803/p1
153         // Safari2.0.4では標準・互換どちらも document.body
154                 
155                 X_Node_updateTimerID && X_Node_startUpdate();
156                 /*X_UA.Opera ?
157                         ( document.documentElement && document.documentElement.clientWidth ?
158                                 new Function( 'return[document.documentElement.clientWidth,document.documentElement.clientHeight]' ) :
159                                 new Function( 'return[document.body.clientWidth,document.body.clientHeight]' )
160                         ) :*/                   
161                 return [
162                         X_ViewPort_rootElement.scrollWidth  || X_ViewPort_rootElement.offsetWidth,
163                         X_ViewPort_rootElement.scrollHeight || X_ViewPort_rootElement.offsetHeight
164                 ];
165         },
166         
167         getScrollPosition :
168                 window.pageXOffset !== undefined ?
169                         ( function(){
170                                 X_Node_updateTimerID && X_Node_startUpdate();
171                                 return[ window.pageXOffset, window.pageYOffset ];
172                         } ) :
173                 window.scrollLeft  !== undefined ?
174                         ( function(){
175                                 X_Node_updateTimerID && X_Node_startUpdate();
176                                 return[ window.scrollLeft, window.scrollTop ];
177                         } ) :
178                         ( function(){
179                                 X_Node_updateTimerID && X_Node_startUpdate();
180                                 // body は Safari2-
181                                 return[ X_ViewPort_rootElement.scrollLeft || document.body.scrollLeft, X_ViewPort_rootElement.scrollTop || document.body.scrollTop ];
182                         } ),
183
184         getScrollbarSize : function(){
185                 return [ X_ViewPort_vScrollbarSize, X_ViewPort_hScrollbarSize ];        
186         },
187         
188         getBaseFontSize : function(){
189                 if( X_Node_updateTimerID ){
190                         X_Node_startUpdate();
191                         return X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
192                 };
193                 return X_ViewPort_baseFontSize;
194         }
195         
196 };
197
198
199
200 /* -----------------------------------------------
201  * Resize
202  *  original : uupaa.js
203  * 
204  * TODO
205  *  https://w3g.jp/blog/studies/ios7_1_minimal-ui_warning
206  *  iOS7.0からあったiPad Safariの高さ100%コンテンツでlandscape(横向き)時に起きる不具合
207  * 
208  * http://looxu.blogspot.jp/2013/11/ios7mobile-safariwindowinnerheight.html
209  * iOS7にて、Mobile Safariのwindow.innerHeightを正しく取得する方法
210  * 
211  *  http://tenderfeel.xsrv.jp/javascript/1182/
212  *  アドレスバーの高さの算出
213  * 
214  * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/javascript-14514208
215  * 1. 傾き時の画面サイズ取得ー 対処方法の例
216  * (1)ー? タイマーを使って、画面サイズ取得の タイミングを少しだけ遅延させる
217  * window.onorientationchange = function() { setTimeout(function() { alert("W: " + window.innerWidth + ", H: " + window.innerHeight); }, 200); }
218  * 横に傾けた時、正常に横向け時の 幅と高さが取得できている ただし、端末によってはうまく取得できな い場合がある(Galaxy S IIIとか・・・) 7 
219  * 
220  * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/ss-12718639
221  * 
222  * 
223  * getBoundingClientRect で fontsize の調査
224  */
225                 var X_ViewPort_resize =
226                         // iOS もループで回す,,,iOS3.1.3, iOS6 で確認
227                         X_UA.IE < 9 || X_UA.iOS ?
228                                 (function(){
229                                         var size;
230                                         if( !X_ViewPort_lock ){
231                                                 size = X_ViewPort_getWindowSize();
232                                                 if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
233                                                         X_ViewPort_width = size[ 0 ];
234                                                         X_ViewPort_height = size[ 1 ];
235                                                         X.Timer.once( 100, X_Dom_detectFinishResizing );
236                                                         X_ViewPort_lock = true;
237                                                 };
238                                         };
239                                         
240                                         size = X_Node_fontSizeNode._rawObject.offsetHeight;
241                                         if( X_ViewPort_baseFontSize !== size ){
242                                                 X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( X.Event.BASE_FONT_RESIZED );
243                                                 X_ViewPort_baseFontSize = size;
244                                         };
245                                         
246                                 }) :
247                                 (function( e ){
248                                         console.log( '-- resize : ' + X_Timer_now() );
249                                         
250                                         !X_ViewPort_lock && ( X_ViewPort_lock = true ) && X.Timer.once( 100, X_Dom_detectFinishResizing );
251                                         return X.Callback.PREVENT_DEFAULT | X.Callback.STOP_PROPAGATION;
252                                 });
253                 
254                 function X_Dom_detectFinishResizing(){
255                         var size = X_ViewPort_getWindowSize();
256                         if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
257                                 X_ViewPort_width  = size[ 0 ];
258                                 X_ViewPort_height = size[ 1 ];
259                                 X.Timer.once( 100, X_Dom_detectFinishResizing );
260                         } else {
261                                 console.log( '-- detectFinishResizing : ' + X_Timer_now() );
262                                 
263                                 X_ViewPort.asyncDispatch( X.Event.VIEW_RESIZED );
264                                 X_ViewPort_lock = false;
265                                 if( X_ViewPort_orientationFlag ){
266                                         X_ViewPort_orientationFlag = false;
267                                         X_ViewPort.asyncDispatch( 100, { type : X.Event.VIEW_TURNED, orientation : window.orientation } );
268                                 };
269                         };
270                 };
271
272                 X_TEMP.onDomContentLoaded = function(){
273                         var s, size, elmHtml, elmHead, elmBody, html, head, body;
274                         
275                         if( X_TEMP.SYSTEM_EVENT_PRE_INIT <= X_ViewPort_readyState ) return X_Callback_UN_LISTEN;
276                         X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_PRE_INIT;
277                         
278                         // DOMContentLoaded の無いブラウザで X_TEMP.onDomContentLoaded への参照が残り続けるのを回避
279                         X_TEMP.onDomContentLoaded && X_ViewPort_document.unlisten( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
280                         delete X_TEMP.onDomContentLoaded;
281                         
282                         elmHtml = document.documentElement ||
283                                 X_UA_DOM.W3C ? document.getElementsByTagName( 'html' )[ 0 ] :
284                                 X_UA_DOM.IE4 ? document.all.tags( 'html' )[ 0 ] : null;
285                         
286                         elmHead = 
287                                 X_UA_DOM.W3C ? document.getElementsByTagName( 'head' )[ 0 ] :
288                                 X_UA_DOM.IE4 ? document.all.tags( 'head' )[ 0 ] : null;
289
290                         elmBody = document.body;
291
292                         X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
293
294                         html = X.Doc.html = X_Node_html = elmHtml && new Node( elmHtml );
295                 
296                         head = X.Doc.head = X_Node_head = elmHead && new Node( elmHead );
297                 
298                         body = X.Doc.body = X_Node_body = new Node( elmBody );
299                         
300                         body._root   = html._root = head._root = body;// _root は html だろ
301                         body.parent  = head.parent = html;
302                         html._xnodes = [ head, body ];                  
303                         
304                         Node.root = body; // 後方互換
305                         
306                         html.appendTo = html.appendToRoot = html.before = html.after = html.clone = html.remove = html.destroy = html.prevNode = html.nextNode =
307                         html.create = html.createText = html.createAt = html.createTextAt = html.append = html.appendAt = html.empty = html.html = html.text =
308                         head.appendTo = head.appendToRoot = head.before = head.after = head.clone = head.remove = head.destroy = head.prevNode = head.nextNode =
309                         head.empty = head.html = head.text =
310                         body.appendTo = body.appendToRoot = body.before = body.after = body.clone = body.remove = body.destroy = body.prevNode = body.nextNode = new Function( 'return this' );
311
312                         X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_PRE_INIT, function(){
313                                 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_XTREE;
314                                 if( X_TEMP.X_Dom_useBuilder ) X_TEMP.X_Dom_useBuilder = !!document.body.children.length;
315                                 !X_TEMP.X_Dom_useBuilder && X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_XTREE );
316                         } );
317                         
318                         X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_XTREE, function(){
319                                 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_INIT;
320                                 //X_UA.Opera7 && alert( 'bc' );
321                                 X_Node_body.appendAt( 0,
322                                         X_Node_systemNode = Node.create( 'div', { 'class' : 'hidden-system-node' } ),
323                                         X_Node_fontSizeNode = Node.create( 'div', { 'class' : 'hidden-system-node' } ).cssText( 'line-height:1;height:1em;' ).text( 'X' )
324                                 );
325                                 X_Node_startUpdate();
326
327                                 X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_INIT );
328                         } );
329
330                         X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
331                                 var size = X_ViewPort_getWindowSize(),
332                                 
333                                 // http://jsdo.it/imaya/kTYg
334                                         body = document.body,
335                                         defaultOverflow = document.body.style.overflow,
336                                         w, h;
337                         
338                                 body.style.overflow = 'hidden';
339                                 w = body.clientWidth;
340                                 h = body.clientHeight;
341                         
342                                 body.style.overflow = 'scroll';
343                                 w -= body.clientWidth;
344                                 h -= body.clientHeight;
345                         
346                                 if( !w ) w = body.offsetWidth  - body.clientWidth;
347                                 if( !h ) h = body.offsetHeight - body.clientHeight;
348                                 body.style.overflow = defaultOverflow; 
349                         
350                                 X_ViewPort_vScrollbarSize = w;
351                                 X_ViewPort_hScrollbarSize = h;
352                                 if( h <= 0 ){ // ie6, ie11, firefox で 負の値が返る
353                                         console.log( 'invalid hScrollbarSize: ' + h );
354                                         X_ViewPort_hScrollbarSize = w;
355                                 };
356                                 
357                                 //
358                                 if( X_Dom_orientationchange ){
359                                         X_EventDispatcher_systemListen( X_ViewPort, 'orientationchange', X_Dom_orientationchange );
360                                 };
361                                 
362                                 if( X_Dom_detectFontSize ){
363                                         X_EventDispatcher_systemListen( X_ViewPort, 'resize', X_ViewPort_resize );
364                                         X.Timer.add( 333, X_Dom_detectFontSize );
365                                 } else {
366                                         X.Timer.add( 333, X_ViewPort_resize );
367                                 };
368                                 
369                                 X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
370                                 
371                                 X_ViewPort_readyState = X.Event.XDOM_READY;
372                                 X_ViewPort.asyncDispatch( { type : X.Event.XDOM_READY, w : X_ViewPort_width = size[ 0 ], h : X_ViewPort_height = size[ 1 ] } );
373                         } );
374
375                         console.log( X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_PRE_INIT ) );        
376
377
378                         X_EventDispatcher_systemListen( X_ViewPort, [ 'beforeunload', 'unload' ] );
379
380 //ブラウザの戻るボタンで戻ったときに呼ばれるイベントとかキャッシュとかそこらへんのこと
381 //http://d.hatena.ne.jp/koumiya/20080916/1221580149
382
383                         if( document[ 'hidden' ] !== undefined ) {// iOS 7+
384                                 X_EventDispatcher_systemListen( X_ViewPort_document, 'visibilitychange', X_ViewPort );
385                         } else
386                         if( document[ 'webkitHidden' ] !== undefined ) {
387                                 X_EventDispatcher_systemListen( X_ViewPort_document, 'webkitvisibilitychange', X_ViewPort );
388                         } else
389                         if( X_UA.iOS && window[ 'onpageshow' ] !== undefined ) {
390                                 X_EventDispatcher_systemListen( X_ViewPort, [ 'pageshow', 'pagehide' ] );
391                         } else {
392                                 X_EventDispatcher_systemListen( X_ViewPort, [ 'focus', 'blur' ] );
393                         };
394
395                         return X_Callback_UN_LISTEN;
396                 };
397
398                 function X_ViewPort_getWindowSize(){
399                         return X_UA.IE ?
400                                 [ X_ViewPort_rootElement.clientWidth, X_ViewPort_rootElement.clientHeight ] :
401                                 [ window.innerWidth, window.innerHeight ];
402                 };
403
404
405 console.log( 'X.Dom dom:w3c=' + X_UA_DOM.W3C + ' ev:w3c=' + X_UA_EVENT.W3C );
406
407 /* -----------------------------------------------
408  * Document Ready
409  *  Dean Edwards/Matthias Miller/John Resig
410  */
411
412 // SafariでJavaScriptのデバッグをする方法
413 // safari1.3 可
414 // http://shimax.cocolog-nifty.com/search/2006/09/safarijavascrip_c54d.html
415
416 /* for ie9+/Mozilla/Opera9 */
417 if( X_UA_EVENT.W3C ){
418         X_ViewPort_document.listenOnce( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
419 } else
420 if( 6 <= X_UA.IE && X.inHead ){
421         // if this script in Head
422         document.write( "<script id=__ie_onload defer src=javascript:void(0)><\/script>" );
423         X_TEMP._script = document.getElementById( "__ie_onload" );
424         X_TEMP._script.onreadystatechange = function(){
425                 var s = X_TEMP._script;
426                 if( s && s.readyState === 'complete' ){
427                         s.onreadystatechange = X.emptyFunction;
428                         s.onreadystatechange = null;
429                         s.parentNode.removeChild( s );
430                         delete X_TEMP._script;
431                         X_TEMP.onDomContentLoaded && X_TEMP.onDomContentLoaded();
432                 };
433         };
434 };
435 // Re: onLoad doesn't work with Safari?
436 // http://lists.apple.com/archives/web-dev/2003/Oct/msg00036.html
437 if( X_UA.WebKit || X_UA.Safari < 3 ){ // sniff
438         X.Timer.add( 16, function(){
439                 if( !X_TEMP.onDomContentLoaded ) return X_Callback_UN_LISTEN;
440                 if( document.readyState === 'loaded' || document.readyState === 'complete' ) return X_TEMP.onDomContentLoaded();
441         });
442 };
443
444 /* for other browsers */
445 X_ViewPort.listenOnce( 'load', X_TEMP.onDomContentLoaded );