OSDN Git Service

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