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( { type : X.Event.BASE_FONT_RESIZED, fontSize : size, w : X_ViewPort_width, h : X_ViewPort_height } );
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 );
44 X_ViewPort.dispatch( X.Event.UNLOAD );
46 X_ViewPort.unlisten();
47 X_ViewPort_document.unlisten();
48 X_Node__actualRemove( X_Node_html, true );
50 case 'visibilitychange' :
51 X_ViewPort.dispatch( { type : ( X_ViewPort_active = document[ 'hidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE } );
53 case 'webkitvisibilitychange' :
54 X_ViewPort.dispatch( { type : ( X_ViewPort_active = document[ 'webkitHidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE } );
58 X_ViewPort_active = true;
59 X_ViewPort.dispatch( X.Event.VIEW_ACTIVATE );
63 X_ViewPort_active = false;
64 X_ViewPort.dispatch( X.Event.VIEW_DEACTIVATE );
75 listen : function( type, arg1, arg2, arg3 ){
76 if( type <= X_ViewPort_readyState ){
78 * X.Event.XDOM_READY 以後に listen した場合の対策
80 X_ViewPort.asyncDispatch( { type : type, w : X_ViewPort_width, h : X_ViewPort_height } );
82 type && arg1 && X_ViewPort.listen( type, arg1, arg2, arg3 );
87 listenOnce : function( type, arg1, arg2, arg3 ){
88 if( type <= X_ViewPort_readyState ){
90 * X.Event.XDOM_READY 以後に listen した場合の対策
92 X_ViewPort.asyncDispatch( { type : type, w : X_ViewPort_width, h : X_ViewPort_height } );
94 type && arg1 && X_ViewPort.listenOnce( type, arg1, arg2, arg3 );
98 unlisten : function( type, arg1, arg2, arg3 ){
99 type && arg1 && X_ViewPort.unlisten( type, arg1, arg2, arg3 );
103 listening : function( type, arg1, arg2, arg3 ){
104 return X_ViewPort.listening( type, arg1, arg2, arg3 );
107 // hammer のテストファイルだけが使用
108 asyncDispatch : function(){
109 return X_ViewPort.asyncDispatch.apply( X_ViewPort, arguments );
112 getPointerPosition : function(){
116 /* 要素が視界に入った http://remysharp.com/2009/01/26/element-in-view-event-plugin/ */
117 inView : function( elm ){
121 getSize : function(){
122 return [ X_ViewPort_width, X_ViewPort_height ];
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
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
135 X_Node_body._updateTimerID && X_Node_startUpdate();
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]' )
142 X_ViewPort_rootElement.scrollWidth || X_ViewPort_rootElement.offsetWidth,
143 X_ViewPort_rootElement.scrollHeight || X_ViewPort_rootElement.offsetHeight
148 window.pageXOffset !== undefined ?
150 X_Node_body._updateTimerID && X_Node_startUpdate();
151 return[ window.pageXOffset, window.pageYOffset ];
153 window.scrollLeft !== undefined ?
155 X_Node_body._updateTimerID && X_Node_startUpdate();
156 return[ window.scrollLeft, window.scrollTop ];
159 X_Node_body._updateTimerID && X_Node_startUpdate();
161 return[ X_ViewPort_rootElement.scrollLeft || document.body.scrollLeft, X_ViewPort_rootElement.scrollTop || document.body.scrollTop ];
164 getScrollbarSize : function(){
165 return [ X_ViewPort_vScrollbarSize, X_ViewPort_hScrollbarSize ];
168 getBaseFontSize : function(){
169 if( X_Node_body._updateTimerID ){
170 X_Node_startUpdate();
171 return X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
173 return X_ViewPort_baseFontSize;
180 /* -----------------------------------------------
182 * original : uupaa.js
185 * https://w3g.jp/blog/studies/ios7_1_minimal-ui_warning
186 * iOS7.0からあったiPad Safariの高さ100%コンテンツでlandscape(横向き)時に起きる不具合
188 * http://looxu.blogspot.jp/2013/11/ios7mobile-safariwindowinnerheight.html
189 * iOS7にて、Mobile Safariのwindow.innerHeightを正しく取得する方法
191 * http://tenderfeel.xsrv.jp/javascript/1182/
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
200 * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/ss-12718639
203 * getBoundingClientRect で fontsize の調査
205 var X_ViewPort_resize =
206 // iOS もループで回す,,,iOS3.1.3, iOS6 で確認
207 X_UA.IE < 9 || X_UA.iOS ?
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;
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;
228 console.log( '-- resize : ' + X_Timer_now() );
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;
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 );
241 console.log( '-- detectFinishResizing : ' + X_Timer_now() );
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 } );
252 X_TEMP.onDomContentLoaded = function(){
253 var s, size, elmHtml, elmHead, elmBody, html, head, body;
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;
258 // DOMContentLoaded の無いブラウザで X_TEMP.onDomContentLoaded への参照が残り続けるのを回避
259 X_TEMP.onDomContentLoaded && X_ViewPort_document.unlisten( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
260 delete X_TEMP.onDomContentLoaded;
262 elmHtml = document.documentElement ||
263 X_UA_DOM.W3C ? document.getElementsByTagName( 'html' )[ 0 ] :
264 X_UA_DOM.IE4 ? document.all.tags( 'html' )[ 0 ] : null;
267 X_UA_DOM.W3C ? document.getElementsByTagName( 'head' )[ 0 ] :
268 X_UA_DOM.IE4 ? document.all.tags( 'head' )[ 0 ] : null;
270 elmBody = document.body;
272 X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
274 html = X.Doc.html = X_Node_html = elmHtml && new Node( elmHtml );
276 head = X.Doc.head = X_Node_head = elmHead && new Node( elmHead );
278 body = X.Doc.body = X_Node_body = new Node( elmBody );
280 body._root = html._root = head._root = body;// _root は html だろ
281 body.parent = head.parent = html;
282 html._xnodes = [ head, body ];
284 Node.root = body; // 後方互換
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' );
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 );
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' )
305 X_Node_startUpdate();
307 X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_INIT );
310 X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
311 var size = X_ViewPort_getWindowSize(),
313 // http://jsdo.it/imaya/kTYg
314 body = document.body,
315 defaultOverflow = document.body.style.overflow,
318 body.style.overflow = 'hidden';
319 w = body.clientWidth;
320 h = body.clientHeight;
322 body.style.overflow = 'scroll';
323 w -= body.clientWidth;
324 h -= body.clientHeight;
326 if( !w ) w = body.offsetWidth - body.clientWidth;
327 if( !h ) h = body.offsetHeight - body.clientHeight;
328 body.style.overflow = defaultOverflow;
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;
338 if( X_Dom_orientationchange ){
339 X_ViewPort.listen( 'orientationchange', X_Dom_orientationchange );
342 if( X_Dom_detectFontSize ){
343 X_ViewPort.listen( 'resize', X_ViewPort_resize );
344 X.Timer.add( 333, X_Dom_detectFontSize );
346 X.Timer.add( 333, X_ViewPort_resize );
349 X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
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 ] } );
355 console.log( X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_PRE_INIT ) );
358 .listen( 'beforeunload', X_ViewPort )
359 .listenOnce( 'unload', X_ViewPort );
361 //ブラウザの戻るボタンで戻ったときに呼ばれるイベントとかキャッシュとかそこらへんのこと
362 //http://d.hatena.ne.jp/koumiya/20080916/1221580149
364 if( document[ 'hidden' ] !== undefined ) {// iOS 7+
365 X_ViewPort_document.listen( 'visibilitychange', X_ViewPort );
367 if( document[ 'webkitHidden' ] !== undefined ) {
368 X_ViewPort_document.listen( 'webkitvisibilitychange', X_ViewPort );
370 if( X_UA.iOS && window[ 'onpageshow' ] !== undefined ) {
371 X_ViewPort.listen( 'pageshow', X_ViewPort )
372 .listen( 'pagehide', X_ViewPort );
374 X_ViewPort.listen( 'focus', X_ViewPort )
375 .listen( 'blur', X_ViewPort );
378 return X_Callback_UN_LISTEN;
381 function X_ViewPort_getWindowSize(){
383 [ X_ViewPort_rootElement.clientWidth, X_ViewPort_rootElement.clientHeight ] :
384 [ window.innerWidth, window.innerHeight ];
388 console.log( 'X.Dom dom:w3c=' + X_UA_DOM.W3C + ' ev:w3c=' + X_UA_EVENT.W3C );
390 /* -----------------------------------------------
392 * Dean Edwards/Matthias Miller/John Resig
395 // SafariでJavaScriptのデバッグをする方法
397 // http://shimax.cocolog-nifty.com/search/2006/09/safarijavascrip_c54d.html
399 /* for ie9+/Mozilla/Opera9 */
400 if( X_UA_EVENT.W3C ){
401 X_ViewPort_document.listenOnce( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
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();
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();
427 /* for other browsers */
428 X_ViewPort.listenOnce( 'load', X_TEMP.onDomContentLoaded );