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 );
87 listen : function( type, arg1, arg2, arg3 ){
88 if( type <= X_ViewPort_readyState ){
90 * X.Event.XDOM_READY 以後に listen した場合の対策
92 X_ViewPort.asyncDispatch( type );
94 // ie8-では keydown -> documentへ
95 type && arg1 && X_ViewPort.listen( type, arg1, arg2, arg3 );
100 listenOnce : function( type, arg1, arg2, arg3 ){
101 if( type <= X_ViewPort_readyState ){
103 * X.Event.XDOM_READY 以後に listen した場合の対策
105 X_ViewPort.asyncDispatch( type );
107 type && arg1 && X_ViewPort.listenOnce( type, arg1, arg2, arg3 );
111 unlisten : function( type, arg1, arg2, arg3 ){
112 type && arg1 && X_ViewPort.unlisten( type, arg1, arg2, arg3 );
116 listening : function( type, arg1, arg2, arg3 ){
117 return X_ViewPort.listening( type, arg1, arg2, arg3 );
120 // hammer のテストファイルだけが使用
121 asyncDispatch : function(){
122 return X_ViewPort.asyncDispatch.apply( X_ViewPort, arguments );
125 getPointerPosition : function(){
129 /* 要素が視界に入った http://remysharp.com/2009/01/26/element-in-view-event-plugin/ */
130 inView : function( elm ){
134 getSize : function(){
135 return [ X_ViewPort_width, X_ViewPort_height ];
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
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
148 X_Node_updateTimerID && X_Node_startUpdate();
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]' )
155 X_ViewPort_rootElement.scrollWidth || X_ViewPort_rootElement.offsetWidth,
156 X_ViewPort_rootElement.scrollHeight || X_ViewPort_rootElement.offsetHeight
161 window.pageXOffset !== undefined ?
163 X_Node_updateTimerID && X_Node_startUpdate();
164 return[ window.pageXOffset, window.pageYOffset ];
166 window.scrollLeft !== undefined ?
168 X_Node_updateTimerID && X_Node_startUpdate();
169 return[ window.scrollLeft, window.scrollTop ];
172 X_Node_updateTimerID && X_Node_startUpdate();
174 return[ X_ViewPort_rootElement.scrollLeft || document.body.scrollLeft, X_ViewPort_rootElement.scrollTop || document.body.scrollTop ];
177 getScrollbarSize : function(){
178 return [ X_ViewPort_vScrollbarSize, X_ViewPort_hScrollbarSize ];
181 getBaseFontSize : function(){
182 if( X_Node_updateTimerID ){
183 X_Node_startUpdate();
184 return X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
186 return X_ViewPort_baseFontSize;
193 /* -----------------------------------------------
195 * original : uupaa.js
198 * https://w3g.jp/blog/studies/ios7_1_minimal-ui_warning
199 * iOS7.0からあったiPad Safariの高さ100%コンテンツでlandscape(横向き)時に起きる不具合
201 * http://looxu.blogspot.jp/2013/11/ios7mobile-safariwindowinnerheight.html
202 * iOS7にて、Mobile Safariのwindow.innerHeightを正しく取得する方法
204 * http://tenderfeel.xsrv.jp/javascript/1182/
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
213 * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/ss-12718639
216 * getBoundingClientRect で fontsize の調査
218 var X_ViewPort_resize =
219 // iOS もループで回す,,,iOS3.1.3, iOS6 で確認
220 X_UA.IE < 9 || X_UA.iOS ?
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;
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;
241 console.log( '-- resize : ' + X_Timer_now() );
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;
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 );
254 console.log( '-- detectFinishResizing : ' + X_Timer_now() );
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 } );
265 X_TEMP.onDomContentLoaded = function(){
266 var s, size, elmHtml, elmHead, elmBody, html, head, body;
268 console.log( '> X_TEMP.onDomContentLoaded rs:' + X_ViewPort_readyState );
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;
273 // DOMContentLoaded の無いブラウザで X_TEMP.onDomContentLoaded への参照が残り続けるのを回避
274 X_TEMP.onDomContentLoaded && X_ViewPort_document.unlisten( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
275 delete X_TEMP.onDomContentLoaded;
277 elmHtml = document.documentElement ||
278 X_UA_DOM.W3C ? document.getElementsByTagName( 'html' )[ 0 ] :
279 X_UA_DOM.IE4 ? document.all.tags( 'html' )[ 0 ] : null;
282 X_UA_DOM.W3C ? document.getElementsByTagName( 'head' )[ 0 ] :
283 X_UA_DOM.IE4 ? document.all.tags( 'head' )[ 0 ] : null;
285 elmBody = document.body;
287 X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
289 html = X.Doc.html = X_Node_html = elmHtml && new Node( elmHtml );
290 html._flags |= X_Node_State.IN_TREE;
292 head = X.Doc.head = X_Node_head = elmHead && new Node( elmHead );
294 body = X.Doc.body = X_Node_body = new Node( elmBody );
296 body.parent = head.parent = html;
297 html._xnodes = [ head, body ];
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' );
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 );
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' )
319 X_Node_startUpdate();
321 X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_INIT );
324 X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
325 var size = X_ViewPort_getWindowSize(),
327 // http://jsdo.it/imaya/kTYg
328 body = document.body,
329 defaultOverflow = document.body.style.overflow,
332 body.style.overflow = 'hidden';
333 w = body.clientWidth;
334 h = body.clientHeight;
336 body.style.overflow = 'scroll';
337 w -= body.clientWidth;
338 h -= body.clientHeight;
340 if( !w ) w = body.offsetWidth - body.clientWidth;
341 if( !h ) h = body.offsetHeight - body.clientHeight;
342 body.style.overflow = defaultOverflow;
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;
352 if( X_Dom_orientationchange ){
353 X_EventDispatcher_systemListen( X_ViewPort, 'orientationchange', X_Dom_orientationchange );
356 if( X_Dom_detectFontSize ){
357 X_EventDispatcher_systemListen( X_ViewPort, 'resize', X_ViewPort_resize );
358 X.Timer.add( 333, X_Dom_detectFontSize );
360 X.Timer.add( 333, X_ViewPort_resize );
363 X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
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 ] } );
369 X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_PRE_INIT );
372 X_EventDispatcher_systemListen( X_ViewPort, [ 'beforeunload', 'unload' ] );
374 //ブラウザの戻るボタンで戻ったときに呼ばれるイベントとかキャッシュとかそこらへんのこと
375 //http://d.hatena.ne.jp/koumiya/20080916/1221580149
377 if( document[ 'hidden' ] !== undefined ){// iOS 7+
378 X_EventDispatcher_systemListen( X_ViewPort_document, 'visibilitychange', X_ViewPort );
380 if( document[ 'mozHidden' ] !== undefined ){
381 X_EventDispatcher_systemListen( X_ViewPort_document, 'mozvisibilitychange', X_ViewPort );
383 if( document[ 'webkitHidden' ] !== undefined ){
384 X_EventDispatcher_systemListen( X_ViewPort_document, 'webkitvisibilitychange', X_ViewPort );
386 if( X_UA.iOS && window[ 'onpageshow' ] !== undefined ){
387 X_EventDispatcher_systemListen( X_ViewPort, [ 'pageshow', 'pagehide' ] );
389 X_EventDispatcher_systemListen( X_ViewPort, [ 'focus', 'blur' ] );
392 return X_Callback_UN_LISTEN;
395 function X_ViewPort_getWindowSize(){
397 [ X_ViewPort_rootElement.clientWidth, X_ViewPort_rootElement.clientHeight ] :
398 [ window.innerWidth, window.innerHeight ];
402 console.log( 'X.Dom dom:w3c=' + X_UA_DOM.W3C + ' ev:w3c=' + X_UA_EVENT.W3C );
404 /* -----------------------------------------------
406 * Dean Edwards/Matthias Miller/John Resig
409 // SafariでJavaScriptのデバッグをする方法
411 // http://shimax.cocolog-nifty.com/search/2006/09/safarijavascrip_c54d.html
413 /* for ie9+/Mozilla/Opera9 */
414 if( X_UA_EVENT.W3C ){
415 X_ViewPort_document.listenOnce( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
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();
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();
441 /* for other browsers */
442 X_ViewPort.listenOnce( 'load', X_TEMP.onDomContentLoaded );