OSDN Git Service

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