OSDN Git Service

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