OSDN Git Service

21a3547bed7f2457d394d89a0849fe3424526bae
[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 = X_Class_override(
30         new X.EventDispatcher( window ),
31         {
32
33                 handleEvent : function( e ){
34                         var href, i, name;
35                         
36                         switch( e.type ){
37                                 case 'beforeunload' :
38                                         // ie では a href="javascript" な要素でも beforeunload が起こる
39                                         href = e.target && e.target.attr && e.target.attr( 'href' );
40                                         if( href && href.indexOf && href.indexOf( 'javascript:' ) === 0 ) return X.Callback.PREVENT_DEFAULT | X.Callback.STOP_PROPAGATION;
41                                         
42                                         return X_ViewPort.dispatch( X_Event.BEFORE_UNLOAD );
43                                         
44                                 case 'unload' :
45                                         X_ViewPort.dispatch( X_Event.UNLOAD );
46                                 //alert('unload');
47                                         X_ViewPort_document.kill();
48                                         this.kill();
49                                         
50                                         //X_System.dispatch( X_Event.SHUT_DOWN );
51                                         break;
52
53                                 case 'visibilitychange' :
54                                         X_ViewPort.dispatch( ( X_ViewPort_active = document[ 'hidden' ] ) ? X_Event.VIEW_DEACTIVATE : X_Event.VIEW_ACTIVATE );
55                                         break;
56                                 case 'mozvisibilitychange' :
57                                         X_ViewPort.dispatch( ( X_ViewPort_active = document[ 'mozHidden' ] ) ? X_Event.VIEW_DEACTIVATE : X_Event.VIEW_ACTIVATE );
58                                         break;
59                                 case 'webkitvisibilitychange' :
60                                         X_ViewPort.dispatch( ( X_ViewPort_active = document[ 'webkitHidden' ] ) ? X_Event.VIEW_DEACTIVATE : X_Event.VIEW_ACTIVATE );
61                                         break;
62                                         
63                                 case 'pageshow' :
64                                 case 'focus' :
65                                         if( !X_ViewPort_active ){
66                                                 X_ViewPort_active = true;
67                                                 X_ViewPort.dispatch( X_Event.VIEW_ACTIVATE );
68                                         };
69                                         break;
70
71                                 case 'pagehide' :
72                                 case 'blur' :
73                                         if( X_ViewPort_active ){
74                                                 X_ViewPort_active = false;
75                                                 X_ViewPort.dispatch( X_Event.VIEW_DEACTIVATE );                         
76                                         };
77                                         break;
78                         };
79                 }
80                 
81         }
82 );
83
84
85 /**
86  * window に相当する ViewPort 情報を提供するオブジェクト。
87  * @namespace X.ViewPort
88  * @alias X.ViewPort
89  */
90 X.ViewPort = {
91         
92         listen : function( type, arg1, arg2, arg3 ){
93                 if( type <= X_ViewPort_readyState ){
94                         /*
95                          * X_Event.XDOM_READY 以後に listen した場合の対策
96                          */
97                         X_ViewPort.asyncDispatch( type );
98                 };
99                 // ie8-では keydown -> documentへ
100                 type && arg1 && X_ViewPort.listen( type, arg1, arg2, arg3 );
101                 return X.ViewPort;
102         },
103         
104         
105         listenOnce : function( type, arg1, arg2, arg3 ){
106                 if( type <= X_ViewPort_readyState ){
107                         /*
108                          * X.Event.XDOM_READY 以後に listen した場合の対策
109                          */
110                         X_ViewPort.asyncDispatch( type );
111                 };
112                 type && arg1 && X_ViewPort.listenOnce( type, arg1, arg2, arg3 );
113                 return X.ViewPort;
114         },
115         
116         unlisten : function( type, arg1, arg2, arg3 ){
117                 type && arg1 && X_ViewPort.unlisten( type, arg1, arg2, arg3 );
118                 return X.ViewPort;
119         },
120         
121         listening : function( type, arg1, arg2, arg3 ){
122                 return X_ViewPort.listening( type, arg1, arg2, arg3 );
123         },
124         
125         // hammer のテストファイルだけが使用
126         asyncDispatch : function(){
127                 return X_ViewPort.asyncDispatch.apply( X_ViewPort, arguments );
128         },
129         
130         getPointerPosition : function(){
131                 
132         },
133         
134         /* 要素が視界に入った  http://remysharp.com/2009/01/26/element-in-view-event-plugin/
135          * TODO -> Node.call('inView')
136          */
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                         console.log( '> X_TEMP.onDomContentLoaded rs:' + X_ViewPort_readyState );
276                         
277                         if( X_TEMP.SYSTEM_EVENT_PRE_INIT <= X_ViewPort_readyState ) return X_Callback_UN_LISTEN;
278                         X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_PRE_INIT;
279                         
280                         // DOMContentLoaded の無いブラウザで X_TEMP.onDomContentLoaded への参照が残り続けるのを回避
281                         X_TEMP.onDomContentLoaded && X_ViewPort_document.unlisten( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
282                         delete X_TEMP.onDomContentLoaded;
283                         
284                         elmHtml = document.documentElement ||
285                                 X_UA_DOM.W3C ? document.getElementsByTagName( 'html' )[ 0 ] :
286                                 X_UA_DOM.IE4 ? document.all.tags( 'html' )[ 0 ] : null;
287                         
288                         elmHead = 
289                                 X_UA_DOM.W3C ? document.getElementsByTagName( 'head' )[ 0 ] :
290                                 X_UA_DOM.IE4 ? document.all.tags( 'head' )[ 0 ] : null;
291
292                         elmBody = document.body;
293
294                         X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
295
296                         html = X.Doc.html = X_Node_html = elmHtml && new Node( elmHtml );
297                         html._flags |= X_Node_State.IN_TREE;
298                         
299                         head = X.Doc.head = X_Node_head = elmHead && new Node( elmHead );
300                 
301                         body = X.Doc.body = X_Node_body = new Node( elmBody );
302
303                         body.parent  = head.parent = html;
304                         html._xnodes = [ head, body ];
305                         
306                         html.appendTo = html.appendToRoot = html.before = html.after = html.clone = html.remove = html.destroy = html.prev = html.next =
307                         html.create = html.createText = html.createAt = html.createTextAt = html.append = html.appendAt = html.empty = html.html = html.text =
308                         html.css = html.cssText =
309                         head.appendTo = head.appendToRoot = head.before = head.after = head.clone = head.remove = head.destroy =
310                         head.createText = head.createTextAt = head.empty = head.html = head.text = head.css = head.cssText =
311                         body.appendTo = body.appendToRoot = body.before = body.after = body.clone = body.remove = body.destroy = new Function( 'return this' );
312
313                         X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_PRE_INIT, function(){
314                                 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_XTREE;
315                                 if( X_TEMP.X_Dom_useBuilder ) X_TEMP.X_Dom_useBuilder = !!document.body.children.length;
316                                 !X_TEMP.X_Dom_useBuilder && X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_XTREE );
317                         } );
318                         
319                         X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_XTREE, function(){
320                                 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_INIT;
321                                 //X_UA[ 'Opera7' ] && alert( 'bc' );
322                                 X_Node_body.appendAt( 0,
323                                         X_Node_systemNode = X_Doc_create( 'div', { 'class' : 'hidden-system-node' } ),
324                                         X_Node_fontSizeNode = X_Doc_create( 'div', { 'class' : 'hidden-system-node' } ).cssText( 'line-height:1;height:1em;' ).text( 'X' )
325                                 );
326                                 X_Node_startUpdate();
327
328                                 X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_INIT );
329                         } );
330
331                         X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
332                                 var size = X_ViewPort_getWindowSize(),
333                                 
334                                 // http://jsdo.it/imaya/kTYg
335                                         body = document.body,
336                                         defaultOverflow = document.body.style.overflow,
337                                         w, h;
338                         
339                                 body.style.overflow = 'hidden';
340                                 w = body.clientWidth;
341                                 h = body.clientHeight;
342                         
343                                 body.style.overflow = 'scroll';
344                                 w -= body.clientWidth;
345                                 h -= body.clientHeight;
346                         
347                                 if( !w ) w = body.offsetWidth  - body.clientWidth;
348                                 if( !h ) h = body.offsetHeight - body.clientHeight;
349                                 body.style.overflow = defaultOverflow; 
350                         
351                                 X_ViewPort_vScrollbarSize = w;
352                                 X_ViewPort_hScrollbarSize = h;
353                                 if( h <= 0 ){ // ie6, ie11, firefox で 負の値が返る
354                                         console.log( 'invalid hScrollbarSize: ' + h );
355                                         X_ViewPort_hScrollbarSize = w;
356                                 };
357                                 
358                                 //
359                                 if( X_Dom_orientationchange ){
360                                         X_EventDispatcher_systemListen( X_ViewPort, 'orientationchange', X_Dom_orientationchange );
361                                 };
362                                 
363                                 if( X_Dom_detectFontSize ){
364                                         X_EventDispatcher_systemListen( X_ViewPort, 'resize', X_ViewPort_resize );
365                                         X.Timer.add( 333, X_Dom_detectFontSize );
366                                 } else {
367                                         X.Timer.add( 333, X_ViewPort_resize );
368                                 };
369                                 
370                                 X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
371                                 
372                                 X_ViewPort_readyState = X_Event.XDOM_READY;
373                                 X_ViewPort.asyncDispatch( { type : X_Event.XDOM_READY, w : X_ViewPort_width = size[ 0 ], h : X_ViewPort_height = size[ 1 ] } );
374                         } );
375
376                         X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_PRE_INIT );       
377
378
379                         X_EventDispatcher_systemListen( X_ViewPort, [ 'beforeunload', 'unload' ] );
380
381 //ブラウザの戻るボタンで戻ったときに呼ばれるイベントとかキャッシュとかそこらへんのこと
382 //http://d.hatena.ne.jp/koumiya/20080916/1221580149
383
384                         if( document[ 'hidden' ] !== undefined ){// iOS 7+
385                                 X_EventDispatcher_systemListen( X_ViewPort_document, 'visibilitychange', X_ViewPort );
386                         } else
387                         if( document[ 'mozHidden' ] !== undefined ){
388                                 X_EventDispatcher_systemListen( X_ViewPort_document, 'mozvisibilitychange', X_ViewPort );
389                         } else
390                         if( document[ 'webkitHidden' ] !== undefined ){
391                                 X_EventDispatcher_systemListen( X_ViewPort_document, 'webkitvisibilitychange', X_ViewPort );
392                         } else
393                         if( X_UA[ 'iOS' ] && window[ 'onpageshow' ] !== undefined ){
394                                 X_EventDispatcher_systemListen( X_ViewPort, [ 'pageshow', 'pagehide' ] );
395                         } else {
396                                 X_EventDispatcher_systemListen( X_ViewPort, [ 'focus', 'blur' ] );
397                         };
398
399                         return X_Callback_UN_LISTEN;
400                 };
401
402                 function X_ViewPort_getWindowSize(){
403                         return X_UA[ 'IE' ] ?
404                                 [ X_ViewPort_rootElement.clientWidth, X_ViewPort_rootElement.clientHeight ] :
405                                 [ window.innerWidth, window.innerHeight ];
406                 };
407
408
409 console.log( 'X.Dom dom:w3c=' + X_UA_DOM.W3C + ' ev:w3c=' + X_UA_EVENT.W3C );
410
411 /* -----------------------------------------------
412  * Document Ready
413  *  Dean Edwards/Matthias Miller/John Resig
414  */
415
416 // SafariでJavaScriptのデバッグをする方法
417 // safari1.3 可
418 // http://shimax.cocolog-nifty.com/search/2006/09/safarijavascrip_c54d.html
419
420 /* for ie9+/Mozilla/Opera9 */
421 if( X_UA_EVENT.W3C ){
422         X_ViewPort_document.listenOnce( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
423 } else
424 if( 6 <= X_UA[ 'IE' ] && X.inHead ){
425         // if this script in Head
426         document.write( "<script id=__ie_onload defer src=javascript:void(0)><\/script>" );
427         X_TEMP._script = document.getElementById( "__ie_onload" );
428         X_TEMP._script.onreadystatechange = function(){
429                 var s = X_TEMP._script;
430                 if( s && s.readyState === 'complete' ){
431                         s.onreadystatechange = X.emptyFunction;
432                         s.onreadystatechange = null;
433                         s.parentNode.removeChild( s );
434                         delete X_TEMP._script;
435                         X_TEMP.onDomContentLoaded && X_TEMP.onDomContentLoaded();
436                 };
437         };
438 };
439 // Re: onLoad doesn't work with Safari?
440 // http://lists.apple.com/archives/web-dev/2003/Oct/msg00036.html
441 if( X_UA[ 'WebKit' ] || X_UA[ 'Safari' ]  < 3 ){ // sniff
442         X.Timer.add( 16, function(){
443                 if( !X_TEMP.onDomContentLoaded ) return X_Callback_UN_LISTEN;
444                 if( document.readyState === 'loaded' || document.readyState === 'complete' ) return X_TEMP.onDomContentLoaded();
445         });
446 };
447
448 /* for other browsers */
449 X_ViewPort.listenOnce( 'load', X_TEMP.onDomContentLoaded );