OSDN Git Service

version 0.6.100, X.ViewPort working.
[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         
10         X_Dom_detectFontSize = !( X.UA.IE < 9 || X.UA.iOS ) && function(){
11                         var size = Node._fontSizeNode._rawObject.offsetHeight;
12                         if( X_ViewPort_baseFontSize !== size ){
13                                 X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( { type : X.Event.BASE_FONT_RESIZED, fontSize : size, w : X_ViewPort_width, h : X_ViewPort_height } );
14                                 X_ViewPort_baseFontSize = size;
15                         };
16         },
17
18         X_ViewPort_orientationFlag,     
19         X_Dom_orientationchange = window[ 'orientation' ] !== undefined && function( e ){
20                 X_ViewPort_orientationFlag = true;
21                 !X.UA.Android && X_ViewPort_resize();
22                 //console.log( '-- orientationchange : ' + X.ViewPort.getSize[ 0 ] + ' ' + X.ViewPort.getSize[ 1 ] );
23         };
24
25 X_ViewPort = X.Class._override(
26         new X.EventDispatcher( window ),
27         {
28
29                 handleEvent : function( e ){
30                         var href;
31                         switch( e.type ){
32                                 case 'beforeunload' :
33                                         
34                                         // ie では a href="javascript" な要素でも beforeunload が起こる
35                                         href = e.target && e.target.attr && e.target.attr( 'href' );
36                                         if( href && href.indexOf && href.indexOf( 'javascript:' ) === 0 ) return X.Callback.PREVENT_DEFAULT | X.Callback.STOP_PROPAGATION;
37                                         
38                                         return X_ViewPort.dispatch( X.Event.BEFORE_UNLOAD );
39                                 case 'unload' :
40                                         X_ViewPort.dispatch( X.Event.UNLOAD );
41                                         
42                                         X.ViewPort.unlisten();
43                                         Node._document.unlisten();
44                                         Node._html._actualRemove( true );
45                                         break;
46                                 case 'visibilitychange' :
47                                         X_ViewPort.dispatch( { type : ( X_ViewPort_active = document[ 'hidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE } );
48                                         break;
49                                 case 'webkitvisibilitychange' :
50                                         X_ViewPort.dispatch( { type : ( X_ViewPort_active = document[ 'webkitHidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE } );
51                                         break;
52                                 case 'pageshow' :
53                                 case 'focus' :
54                                         X_ViewPort_active = true;
55                                         X_ViewPort.dispatch( X.Event.VIEW_ACTIVATE );
56                                         break;
57                                 case 'pagehide' :
58                                 case 'blur' :
59                                         X_ViewPort_active = false;
60                                         X_ViewPort.dispatch( X.Event.VIEW_DEACTIVATE );
61                                         break;
62                         };
63                 }
64                 
65         }
66 );
67
68
69 X.ViewPort = {
70         
71         listen : function( type, arg1, arg2, arg3 ){
72                 if( type <= X_ViewPort_readyState ){
73                         /*
74                          * X.Event.XDOM_READY 以後に listen した場合の対策
75                          */
76                         X_ViewPort.asyncDispatch( { type : type, w : X_ViewPort_width, h : X_ViewPort_height } );
77                 };
78                 type && arg1 && X_ViewPort.listen( type, arg1, arg2, arg3 );
79                 return X.ViewPort;
80         },
81         
82         
83         listenOnce : function( type, arg1, arg2, arg3 ){
84                 if( type <= X_ViewPort_readyState ){
85                         /*
86                          * X.Event.XDOM_READY 以後に listen した場合の対策
87                          */
88                         X_ViewPort.asyncDispatch( { type : type, w : X_ViewPort_width, h : X_ViewPort_height } );
89                 };
90                 type && arg1 && X_ViewPort.listenOnce( type, arg1, arg2, arg3 );
91                 return X.ViewPort;
92         },
93         
94         unlisten : function( type, arg1, arg2, arg3 ){
95                 type && arg1 && X_ViewPort.unlisten( type, arg1, arg2, arg3 );
96                 return X.ViewPort;
97         },
98         
99         listening : function( type, arg1, arg2, arg3 ){
100                 return X_ViewPort.listening( type, arg1, arg2, arg3 );
101         },
102         
103         asyncDispatch : function(){
104                 return X_ViewPort.asyncDispatch.apply( X_ViewPort, arguments );
105         },
106         
107         getPointerPosition : function(){
108                 
109         },
110         
111         /* 要素が視界に入った  http://remysharp.com/2009/01/26/element-in-view-event-plugin/ */
112         inView : function( elm ){
113                 
114         },
115         
116         getSize : function(){
117                 return [ X_ViewPort_width, X_ViewPort_height ];
118         },
119 //https://developer.mozilla.org/en-US/docs/Web/API/Element.getBoundingClientRect
120 //use window.pageXOffset and window.pageYOffset instead of window.scrollX and window.scrollY
121 //(((t = document.documentElement) || (t = document.body.parentNode)) && typeof t.ScrollLeft == 'number' ? t : document.body).ScrollLeft;
122 //(((t = document.documentElement) || (t = document.body.parentNode)) && typeof t.ScrollTop == 'number' ? t : document.body).ScrollTop
123         
124         getDocumentSize : function(){
125                 // Opera は互換モードでは document.body.scrollHeight、標準モードでは document.documentElement.scrollHeight でページの高さが取れる。と思ってたんだけど、例外があった。
126                 // http://orera.g.hatena.ne.jp/edvakf/20100515/1273908051
127         //http://onozaty.hatenablog.com/entry/20060803/p1
128         // Safari2.0.4では標準・互換どちらも document.body
129                 
130                 Node._body._updateTimerID && Node._body._startUpdate();
131                 /*X.UA.Opera ?
132                         ( document.documentElement && document.documentElement.clientWidth ?
133                                 new Function( 'return[document.documentElement.clientWidth,document.documentElement.clientHeight]' ) :
134                                 new Function( 'return[document.body.clientWidth,document.body.clientHeight]' )
135                         ) :*/                   
136                 return [
137                         X_ViewPort_rootElement.scrollWidth  || X_ViewPort_rootElement.offsetWidth,
138                         X_ViewPort_rootElement.scrollHeight || X_ViewPort_rootElement.offsetHeight
139                 ];
140         },
141         
142         getScrollPosition :
143                 window.pageXOffset !== undefined ?
144                         ( function(){
145                                 Node._body._updateTimerID && Node._body._startUpdate();
146                                 return[ window.pageXOffset, window.pageYOffset ];
147                         } ) :
148                 window.scrollLeft  !== undefined ?
149                         ( function(){
150                                 Node._body._updateTimerID && Node._body._startUpdate();
151                                 return[ window.scrollLeft, window.scrollTop ];
152                         } ) :
153                         ( function(){
154                                 Node._body._updateTimerID && Node._body._startUpdate();
155                                 // body は Safari2-
156                                 return[ X_ViewPort_rootElement.scrollLeft || document.body.scrollLeft, X_ViewPort_rootElement.scrollTop || document.body.scrollTop ];
157                         } ),
158
159         getScrollbarSize : function(){
160                 return [ X.Dom.BoxModel.vScrollbarSize, X.Dom.BoxModel.hScrollbarSize ];        
161         },
162         
163         getBaseFontSize : function(){
164                 if( Node._body._updateTimerID ){
165                         Node._body._startUpdate();
166                         return X_ViewPort_baseFontSize = Node._fontSizeNode._rawObject.offsetHeight;
167                 };
168                 return X_ViewPort_baseFontSize;
169         }
170         
171 };
172
173
174
175 /* -----------------------------------------------
176  * Resize
177  *  original : uupaa.js
178  * 
179  * TODO
180  *  https://w3g.jp/blog/studies/ios7_1_minimal-ui_warning
181  *  iOS7.0からあったiPad Safariの高さ100%コンテンツでlandscape(横向き)時に起きる不具合
182  * 
183  * http://looxu.blogspot.jp/2013/11/ios7mobile-safariwindowinnerheight.html
184  * iOS7にて、Mobile Safariのwindow.innerHeightを正しく取得する方法
185  * 
186  *  http://tenderfeel.xsrv.jp/javascript/1182/
187  *  アドレスバーの高さの算出
188  * 
189  * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/javascript-14514208
190  * 1. 傾き時の画面サイズ取得ー 対処方法の例
191  * (1)ー? タイマーを使って、画面サイズ取得の タイミングを少しだけ遅延させる
192  * window.onorientationchange = function() { setTimeout(function() { alert("W: " + window.innerWidth + ", H: " + window.innerHeight); }, 200); }
193  * 横に傾けた時、正常に横向け時の 幅と高さが取得できている ただし、端末によってはうまく取得できな い場合がある(Galaxy S IIIとか・・・) 7 
194  * 
195  * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/ss-12718639
196  * 
197  * 
198  * getBoundingClientRect で fontsize の調査
199  */
200                 var X_ViewPort_resize =
201                         // iOS もループで回す,,,iOS3.1.3, iOS6 で確認
202                         X.UA.IE < 9 || X.UA.iOS ?
203                                 (function(){
204                                         var size;
205                                         if( !X_ViewPort_lock ){
206                                                 size = X_ViewPort_getWindowSize();
207                                                 if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
208                                                         X_ViewPort_width = size[ 0 ];
209                                                         X_ViewPort_height = size[ 1 ];
210                                                         X.Timer.once( 100, X_Dom_detectFinishResizing );
211                                                         X_ViewPort_lock = true;
212                                                 };
213                                         };
214                                         
215                                         size = Node._fontSizeNode._rawObject.offsetHeight;
216                                         if( X_ViewPort_baseFontSize !== size ){
217                                                 X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( { type : X.Event.BASE_FONT_RESIZED, fontSize : size, w : X_ViewPort_width, h : X_ViewPort_height  } );
218                                                 X_ViewPort_baseFontSize = size;
219                                         };
220                                         
221                                 }) :
222                                 (function( e ){
223                                         console.log( '-- resize : ' + X_Timer_now() );
224                                         
225                                         !X_ViewPort_lock && ( X_ViewPort_lock = true ) && X.Timer.once( 100, X_Dom_detectFinishResizing );
226                                         return X.Callback.PREVENT_DEFAULT | X.Callback.STOP_PROPAGATION;
227                                 });
228                 
229                 function X_Dom_detectFinishResizing(){
230                         var size = X_ViewPort_getWindowSize();
231                         if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
232                                 X_ViewPort_width  = size[ 0 ];
233                                 X_ViewPort_height = size[ 1 ];
234                                 X.Timer.once( 100, X_Dom_detectFinishResizing );
235                         } else {
236                                 console.log( '-- detectFinishResizing : ' + X_Timer_now() );
237                                 
238                                 X_ViewPort.asyncDispatch( { type : X.Event.VIEW_RESIZED, fontSize : X_ViewPort_baseFontSize, w : X_ViewPort_width, h : X_ViewPort_height } );
239                                 X_ViewPort_lock = false;
240                                 if( X_ViewPort_orientationFlag ){
241                                         X_ViewPort_orientationFlag = false;
242                                         X_ViewPort.asyncDispatch( 100, { type : X.Event.VIEW_TURNED, orientation : window.orientation } );
243                                 };
244                         };
245                 };
246
247                 X_TEMP.onDomContentLoaded = function(){
248                         var s, size, elmHtml, elmHead, elmBody, html, head, body;
249                         
250                         if( X_TEMP.SYSTEM_EVENT_PRE_INIT <= X_ViewPort_readyState ) return X_Callback_UN_LISTEN;
251                         X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_PRE_INIT;
252                         
253                         // DOMContentLoaded の無いブラウザで X_TEMP.onDomContentLoaded への参照が残り続けるのを回避
254                         X_TEMP.onDomContentLoaded && Node._document.unlisten( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
255                         delete X_TEMP.onDomContentLoaded;
256                         
257                         elmHtml = document.documentElement ||
258                                 X_UA_DOM.W3C ? document.getElementsByTagName( 'html' )[ 0 ] :
259                                 X_UA_DOM.IE4 ? document.all.tags( 'html' )[ 0 ] : null;
260                         
261                         elmHead = 
262                                 X_UA_DOM.W3C ? document.getElementsByTagName( 'head' )[ 0 ] :
263                                 X_UA_DOM.IE4 ? document.all.tags( 'head' )[ 0 ] : null;
264
265                         elmBody = document.body;
266
267                         X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
268
269                         html = Node._html = elmHtml && new Node( elmHtml );
270                 
271                         head = Node._head = elmHead && new Node( elmHead );
272                 
273                         body = Node._body = new Node( elmBody );
274                         
275                         body._root   = html._root = head._root = body;// _root は html だろ
276                         body.parent  = head.parent = html;
277                         html._xnodes = [ head, body ];                  
278                         
279                         Node.root = body; // 後方互換
280                         
281                         html.appendTo = html.appendToRoot = html.before = html.after = html.clone = html.remove = html.destroy = html.prevNode = html.nextNode =
282                         html.create = html.createText = html.createAt = html.createTextAt = html.append = html.appendAt = html.empty = html.html = html.text =
283                         head.appendTo = head.appendToRoot = head.before = head.after = head.clone = head.remove = head.destroy = head.prevNode = head.nextNode =
284                         head.empty = head.html = head.text =
285                         body.appendTo = body.appendToRoot = body.before = body.after = body.clone = body.remove = body.destroy = body.prevNode = body.nextNode = new Function( 'return this' );
286
287                         X.ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_PRE_INIT, function(){
288                                 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_XTREE;
289                                 if( X_TEMP.X_Dom_useBuilder ) X_TEMP.X_Dom_useBuilder = !!document.body.children.length;
290                                 !X_TEMP.X_Dom_useBuilder && X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_XTREE );
291                         } );
292                         
293                         X.ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_XTREE, function(){
294                                 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_INIT;
295                                 //X.UA.Opera7 && alert( 'bc' );
296                                 Node._body.appendAt( 0,
297                                         Node._systemNode = Node.create( 'div', { 'class' : 'hidden-system-node' } ),
298                                         Node._fontSizeNode = Node.create( 'div', { 'class' : 'hidden-system-node' } ).cssText( 'line-height:1;height:1em;' ).text( 'X' )
299                                 )._startUpdate();
300
301                                 X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_INIT );
302                         } );
303
304                         X.ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
305                                 var size = X_ViewPort_getWindowSize();
306                                 
307                                 if( X_Dom_orientationchange ){
308                                         X.ViewPort.listen( 'orientationchange', X_Dom_orientationchange );
309                                 };
310                                 
311                                 if( X_Dom_detectFontSize ){
312                                         X.ViewPort.listen( 'resize', X_ViewPort_resize );
313                                         X.Timer.add( 333, X_Dom_detectFontSize );
314                                 } else {
315                                         X.Timer.add( 333, X_ViewPort_resize );
316                                 };
317                                 
318                                 X_ViewPort_baseFontSize = Node._fontSizeNode._rawObject.offsetHeight;
319                                 
320                                 X_ViewPort_readyState = X.Event.XDOM_READY;
321                                 X_ViewPort.asyncDispatch( { type : X.Event.XDOM_READY, w : X_ViewPort_width = size[ 0 ], h : X_ViewPort_height = size[ 1 ] } );
322                         } );
323
324                         console.log( X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_PRE_INIT ) );        
325
326                         X.ViewPort
327                                 .listen( 'beforeunload', X_ViewPort )
328                                 .listenOnce( 'unload', X_ViewPort );
329
330 //ブラウザの戻るボタンで戻ったときに呼ばれるイベントとかキャッシュとかそこらへんのこと
331 //http://d.hatena.ne.jp/koumiya/20080916/1221580149
332
333                         if( document[ 'hidden' ] !== undefined ) {// iOS 7+
334                                 Node._document.listen( 'visibilitychange', X_ViewPort );
335                         } else
336                         if( document[ 'webkitHidden' ] !== undefined ) {
337                                 Node._document.listen( 'webkitvisibilitychange', X_ViewPort );
338                         } else
339                         if( X.UA.iOS && window[ 'onpageshow' ] !== undefined ) {
340                                 X.ViewPort.listen( 'pageshow', X_ViewPort )
341                                                         .listen( 'pagehide', X_ViewPort );      
342                         } else {
343                                 X.ViewPort.listen( 'focus', X_ViewPort )
344                                                         .listen( 'blur', X_ViewPort );
345                         };
346
347                         return X_Callback_UN_LISTEN;
348                 };
349
350                 function X_ViewPort_getWindowSize(){
351                         return X.UA.IE ?
352                                 [ X_ViewPort_rootElement.clientWidth, X_ViewPort_rootElement.clientHeight ] :
353                                 [ window.innerWidth, window.innerHeight ];
354                 };
355
356
357 console.log( 'X.Dom dom:w3c=' + X_UA_DOM.W3C + ' ev:w3c=' + X_UA_EVENT.W3C );