2 var X_ViewPort_readyState,
3 X_ViewPort_active = !!window.parent || !!document.activeElement, // parent は frameに読み込まれた場合のieのerror回避
4 X_ViewPort_rootElement,
8 X_ViewPort_baseFontSize,
9 X_ViewPort_vScrollbarSize,
10 X_ViewPort_hScrollbarSize,
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;
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 ] );
27 X_ViewPort_document = new X.EventDispatcher( document ),
29 X_ViewPort = X_Class_override(
30 new X.EventDispatcher( window ),
33 handleEvent : function( e ){
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;
42 return X_ViewPort.dispatch( X_Event.BEFORE_UNLOAD );
45 X_ViewPort.dispatch( X_Event.UNLOAD );
47 X_ViewPort_document.kill();
50 //X_System.dispatch( X_Event.SHUT_DOWN );
53 case 'visibilitychange' :
54 X_ViewPort.dispatch( ( X_ViewPort_active = document[ 'hidden' ] ) ? X_Event.VIEW_DEACTIVATE : X_Event.VIEW_ACTIVATE );
56 case 'mozvisibilitychange' :
57 X_ViewPort.dispatch( ( X_ViewPort_active = document[ 'mozHidden' ] ) ? X_Event.VIEW_DEACTIVATE : X_Event.VIEW_ACTIVATE );
59 case 'webkitvisibilitychange' :
60 X_ViewPort.dispatch( ( X_ViewPort_active = document[ 'webkitHidden' ] ) ? X_Event.VIEW_DEACTIVATE : X_Event.VIEW_ACTIVATE );
65 if( !X_ViewPort_active ){
66 X_ViewPort_active = true;
67 X_ViewPort.dispatch( X_Event.VIEW_ACTIVATE );
73 if( X_ViewPort_active ){
74 X_ViewPort_active = false;
75 X_ViewPort.dispatch( X_Event.VIEW_DEACTIVATE );
86 * window に相当する ViewPort 情報を提供するオブジェクト。
87 * @namespace X.ViewPort
92 listen : function( type, arg1, arg2, arg3 ){
93 if( type <= X_ViewPort_readyState ){
95 * X_Event.XDOM_READY 以後に listen した場合の対策
97 X_ViewPort.asyncDispatch( type );
99 // ie8-では keydown -> documentへ
100 type && arg1 && X_ViewPort.listen( type, arg1, arg2, arg3 );
105 listenOnce : function( type, arg1, arg2, arg3 ){
106 if( type <= X_ViewPort_readyState ){
108 * X.Event.XDOM_READY 以後に listen した場合の対策
110 X_ViewPort.asyncDispatch( type );
112 type && arg1 && X_ViewPort.listenOnce( type, arg1, arg2, arg3 );
116 unlisten : function( type, arg1, arg2, arg3 ){
117 type && arg1 && X_ViewPort.unlisten( type, arg1, arg2, arg3 );
121 listening : function( type, arg1, arg2, arg3 ){
122 return X_ViewPort.listening( type, arg1, arg2, arg3 );
125 // hammer のテストファイルだけが使用
126 asyncDispatch : function(){
127 return X_ViewPort.asyncDispatch.apply( X_ViewPort, arguments );
130 getPointerPosition : function(){
134 /* 要素が視界に入った http://remysharp.com/2009/01/26/element-in-view-event-plugin/
135 * TODO -> Node.call('inView')
137 inView : function( elm ){
141 getSize : function(){
142 return [ X_ViewPort_width, X_ViewPort_height ];
144 //https://developer.mozilla.org/en-US/docs/Web/API/Element.getBoundingClientRect
145 //use window.pageXOffset and window.pageYOffset instead of window.scrollX and window.scrollY
146 //(((t = document.documentElement) || (t = document.body.parentNode)) && typeof t.ScrollLeft == 'number' ? t : document.body).ScrollLeft;
147 //(((t = document.documentElement) || (t = document.body.parentNode)) && typeof t.ScrollTop == 'number' ? t : document.body).ScrollTop
149 getDocumentSize : function(){
150 // Opera は互換モードでは document.body.scrollHeight、標準モードでは document.documentElement.scrollHeight でページの高さが取れる。と思ってたんだけど、例外があった。
151 // http://orera.g.hatena.ne.jp/edvakf/20100515/1273908051
152 //http://onozaty.hatenablog.com/entry/20060803/p1
153 // Safari2.0.4では標準・互換どちらも document.body
155 X_Node_updateTimerID && X_Node_startUpdate();
157 ( document.documentElement && document.documentElement.clientWidth ?
158 new Function( 'return[document.documentElement.clientWidth,document.documentElement.clientHeight]' ) :
159 new Function( 'return[document.body.clientWidth,document.body.clientHeight]' )
162 X_ViewPort_rootElement.scrollWidth || X_ViewPort_rootElement.offsetWidth,
163 X_ViewPort_rootElement.scrollHeight || X_ViewPort_rootElement.offsetHeight
168 window.pageXOffset !== undefined ?
170 X_Node_updateTimerID && X_Node_startUpdate();
171 return[ window.pageXOffset, window.pageYOffset ];
173 window.scrollLeft !== undefined ?
175 X_Node_updateTimerID && X_Node_startUpdate();
176 return[ window.scrollLeft, window.scrollTop ];
179 X_Node_updateTimerID && X_Node_startUpdate();
181 return[ X_ViewPort_rootElement.scrollLeft || document.body.scrollLeft, X_ViewPort_rootElement.scrollTop || document.body.scrollTop ];
184 getScrollbarSize : function(){
185 return [ X_ViewPort_vScrollbarSize, X_ViewPort_hScrollbarSize ];
188 getBaseFontSize : function(){
189 if( X_Node_updateTimerID ){
190 X_Node_startUpdate();
191 return X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
193 return X_ViewPort_baseFontSize;
200 /* -----------------------------------------------
202 * original : uupaa.js
205 * https://w3g.jp/blog/studies/ios7_1_minimal-ui_warning
206 * iOS7.0からあったiPad Safariの高さ100%コンテンツでlandscape(横向き)時に起きる不具合
208 * http://looxu.blogspot.jp/2013/11/ios7mobile-safariwindowinnerheight.html
209 * iOS7にて、Mobile Safariのwindow.innerHeightを正しく取得する方法
211 * http://tenderfeel.xsrv.jp/javascript/1182/
214 * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/javascript-14514208
215 * 1. 傾き時の画面サイズ取得ー 対処方法の例
216 * (1)ー? タイマーを使って、画面サイズ取得の タイミングを少しだけ遅延させる
217 * window.onorientationchange = function() { setTimeout(function() { alert("W: " + window.innerWidth + ", H: " + window.innerHeight); }, 200); }
218 * 横に傾けた時、正常に横向け時の 幅と高さが取得できている ただし、端末によってはうまく取得できな い場合がある(Galaxy S IIIとか・・・) 7
220 * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/ss-12718639
223 * getBoundingClientRect で fontsize の調査
225 var X_ViewPort_resize =
226 // iOS もループで回す,,,iOS3.1.3, iOS6 で確認
227 X_UA[ 'IE' ] < 9 || X_UA[ 'iOS' ] ?
230 if( !X_ViewPort_lock ){
231 size = X_ViewPort_getWindowSize();
232 if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
233 X_ViewPort_width = size[ 0 ];
234 X_ViewPort_height = size[ 1 ];
235 X.Timer.once( 100, X_Dom_detectFinishResizing );
236 X_ViewPort_lock = true;
240 size = X_Node_fontSizeNode._rawObject.offsetHeight;
241 if( X_ViewPort_baseFontSize !== size ){
242 X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( X_Event.BASE_FONT_RESIZED );
243 X_ViewPort_baseFontSize = size;
248 console.log( '-- resize : ' + X_Timer_now() );
250 !X_ViewPort_lock && ( X_ViewPort_lock = true ) && X.Timer.once( 100, X_Dom_detectFinishResizing );
251 return X.Callback.PREVENT_DEFAULT | X.Callback.STOP_PROPAGATION;
254 function X_Dom_detectFinishResizing(){
255 var size = X_ViewPort_getWindowSize();
256 if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
257 X_ViewPort_width = size[ 0 ];
258 X_ViewPort_height = size[ 1 ];
259 X.Timer.once( 100, X_Dom_detectFinishResizing );
261 console.log( '-- detectFinishResizing : ' + X_Timer_now() );
263 X_ViewPort.asyncDispatch( X_Event.VIEW_RESIZED );
264 X_ViewPort_lock = false;
265 if( X_ViewPort_orientationFlag ){
266 X_ViewPort_orientationFlag = false;
267 X_ViewPort.asyncDispatch( 100, { type : X_Event.VIEW_TURNED, orientation : window.orientation } );
272 X_TEMP.onDomContentLoaded = function(){
273 var s, size, elmHtml, elmHead, elmBody, html, head, body;
275 console.log( '> X_TEMP.onDomContentLoaded rs:' + X_ViewPort_readyState );
277 if( X_TEMP.SYSTEM_EVENT_PRE_INIT <= X_ViewPort_readyState ) return X_Callback_UN_LISTEN;
278 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_PRE_INIT;
280 // DOMContentLoaded の無いブラウザで X_TEMP.onDomContentLoaded への参照が残り続けるのを回避
281 X_TEMP.onDomContentLoaded && X_ViewPort_document.unlisten( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
282 delete X_TEMP.onDomContentLoaded;
284 elmHtml = document.documentElement ||
285 X_UA_DOM.W3C ? document.getElementsByTagName( 'html' )[ 0 ] :
286 X_UA_DOM.IE4 ? document.all.tags( 'html' )[ 0 ] : null;
289 X_UA_DOM.W3C ? document.getElementsByTagName( 'head' )[ 0 ] :
290 X_UA_DOM.IE4 ? document.all.tags( 'head' )[ 0 ] : null;
292 elmBody = document.body;
294 X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
296 html = X.Doc.html = X_Node_html = elmHtml && new Node( elmHtml );
297 html._flags |= X_Node_State.IN_TREE;
299 head = X.Doc.head = X_Node_head = elmHead && new Node( elmHead );
301 body = X.Doc.body = X_Node_body = new Node( elmBody );
303 body.parent = head.parent = html;
304 html._xnodes = [ head, body ];
306 html.appendTo = html.appendToRoot = html.before = html.after = html.clone = html.remove = html.destroy = html.prev = html.next =
307 html.create = html.createText = html.createAt = html.createTextAt = html.append = html.appendAt = html.empty = html.html = html.text =
308 html.css = html.cssText =
309 head.appendTo = head.appendToRoot = head.before = head.after = head.clone = head.remove = head.destroy =
310 head.createText = head.createTextAt = head.empty = head.html = head.text = head.css = head.cssText =
311 body.appendTo = body.appendToRoot = body.before = body.after = body.clone = body.remove = body.destroy = new Function( 'return this' );
313 X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_PRE_INIT, function(){
314 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_XTREE;
315 if( X_TEMP.X_Dom_useBuilder ) X_TEMP.X_Dom_useBuilder = !!document.body.children.length;
316 !X_TEMP.X_Dom_useBuilder && X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_XTREE );
319 X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_XTREE, function(){
320 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_INIT;
321 //X_UA[ 'Opera7' ] && alert( 'bc' );
322 X_Node_body.appendAt( 0,
323 X_Node_systemNode = X_Doc_create( 'div', { 'class' : 'hidden-system-node' } ),
324 X_Node_fontSizeNode = X_Doc_create( 'div', { 'class' : 'hidden-system-node' } ).cssText( 'line-height:1;height:1em;' ).text( 'X' )
326 X_Node_startUpdate();
328 X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_INIT );
331 X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
332 var size = X_ViewPort_getWindowSize(),
334 // http://jsdo.it/imaya/kTYg
335 body = document.body,
336 defaultOverflow = document.body.style.overflow,
339 body.style.overflow = 'hidden';
340 w = body.clientWidth;
341 h = body.clientHeight;
343 body.style.overflow = 'scroll';
344 w -= body.clientWidth;
345 h -= body.clientHeight;
347 if( !w ) w = body.offsetWidth - body.clientWidth;
348 if( !h ) h = body.offsetHeight - body.clientHeight;
349 body.style.overflow = defaultOverflow;
351 X_ViewPort_vScrollbarSize = w;
352 X_ViewPort_hScrollbarSize = h;
353 if( h <= 0 ){ // ie6, ie11, firefox で 負の値が返る
354 console.log( 'invalid hScrollbarSize: ' + h );
355 X_ViewPort_hScrollbarSize = w;
359 if( X_Dom_orientationchange ){
360 X_EventDispatcher_systemListen( X_ViewPort, 'orientationchange', X_Dom_orientationchange );
363 if( X_Dom_detectFontSize ){
364 X_EventDispatcher_systemListen( X_ViewPort, 'resize', X_ViewPort_resize );
365 X.Timer.add( 333, X_Dom_detectFontSize );
367 X.Timer.add( 333, X_ViewPort_resize );
370 X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
372 X_ViewPort_readyState = X_Event.XDOM_READY;
373 X_ViewPort.asyncDispatch( { type : X_Event.XDOM_READY, w : X_ViewPort_width = size[ 0 ], h : X_ViewPort_height = size[ 1 ] } );
376 X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_PRE_INIT );
379 X_EventDispatcher_systemListen( X_ViewPort, [ 'beforeunload', 'unload' ] );
381 //ブラウザの戻るボタンで戻ったときに呼ばれるイベントとかキャッシュとかそこらへんのこと
382 //http://d.hatena.ne.jp/koumiya/20080916/1221580149
384 if( document[ 'hidden' ] !== undefined ){// iOS 7+
385 X_EventDispatcher_systemListen( X_ViewPort_document, 'visibilitychange', X_ViewPort );
387 if( document[ 'mozHidden' ] !== undefined ){
388 X_EventDispatcher_systemListen( X_ViewPort_document, 'mozvisibilitychange', X_ViewPort );
390 if( document[ 'webkitHidden' ] !== undefined ){
391 X_EventDispatcher_systemListen( X_ViewPort_document, 'webkitvisibilitychange', X_ViewPort );
393 if( X_UA[ 'iOS' ] && window[ 'onpageshow' ] !== undefined ){
394 X_EventDispatcher_systemListen( X_ViewPort, [ 'pageshow', 'pagehide' ] );
396 X_EventDispatcher_systemListen( X_ViewPort, [ 'focus', 'blur' ] );
399 return X_Callback_UN_LISTEN;
402 function X_ViewPort_getWindowSize(){
403 return X_UA[ 'IE' ] ?
404 [ X_ViewPort_rootElement.clientWidth, X_ViewPort_rootElement.clientHeight ] :
405 [ window.innerWidth, window.innerHeight ];
409 console.log( 'X.Dom dom:w3c=' + X_UA_DOM.W3C + ' ev:w3c=' + X_UA_EVENT.W3C );
411 /* -----------------------------------------------
413 * Dean Edwards/Matthias Miller/John Resig
416 // SafariでJavaScriptのデバッグをする方法
418 // http://shimax.cocolog-nifty.com/search/2006/09/safarijavascrip_c54d.html
420 /* for ie9+/Mozilla/Opera9 */
421 if( X_UA_EVENT.W3C ){
422 X_ViewPort_document.listenOnce( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
424 if( 6 <= X_UA[ 'IE' ] && X.inHead ){
425 // if this script in Head
426 document.write( "<script id=__ie_onload defer src=javascript:void(0)><\/script>" );
427 X_TEMP._script = document.getElementById( "__ie_onload" );
428 X_TEMP._script.onreadystatechange = function(){
429 var s = X_TEMP._script;
430 if( s && s.readyState === 'complete' ){
431 s.onreadystatechange = X.emptyFunction;
432 s.onreadystatechange = null;
433 s.parentNode.removeChild( s );
434 delete X_TEMP._script;
435 X_TEMP.onDomContentLoaded && X_TEMP.onDomContentLoaded();
439 // Re: onLoad doesn't work with Safari?
440 // http://lists.apple.com/archives/web-dev/2003/Oct/msg00036.html
441 if( X_UA[ 'WebKit' ] || X_UA[ 'Safari' ] < 3 ){ // sniff
442 X.Timer.add( 16, function(){
443 if( !X_TEMP.onDomContentLoaded ) return X_Callback_UN_LISTEN;
444 if( document.readyState === 'loaded' || document.readyState === 'complete' ) return X_TEMP.onDomContentLoaded();
448 /* for other browsers */
449 X_ViewPort.listenOnce( 'load', X_TEMP.onDomContentLoaded );