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,
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;
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 ] );
25 X_ViewPort_document = new X.EventDispatcher( document ),
27 X_ViewPort = X.Class._override(
28 new X.EventDispatcher( window ),
31 handleEvent : function( e ){
36 // ie では a href="javascript" な要素でも beforeunload が起こる
37 href = e.target && e.target.attr && e.target.attr( 'href' );
38 if( href && href.indexOf && href.indexOf( 'javascript:' ) === 0 ) return X.Callback.PREVENT_DEFAULT | X.Callback.STOP_PROPAGATION;
40 return X_ViewPort.dispatch( X.Event.BEFORE_UNLOAD );
42 X_ViewPort.dispatch( X.Event.UNLOAD );
44 X_ViewPort.unlisten();
45 X_ViewPort_document.unlisten();
46 Node._html._actualRemove( true );
48 case 'visibilitychange' :
49 X_ViewPort.dispatch( { type : ( X_ViewPort_active = document[ 'hidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE } );
51 case 'webkitvisibilitychange' :
52 X_ViewPort.dispatch( { type : ( X_ViewPort_active = document[ 'webkitHidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE } );
56 X_ViewPort_active = true;
57 X_ViewPort.dispatch( X.Event.VIEW_ACTIVATE );
61 X_ViewPort_active = false;
62 X_ViewPort.dispatch( X.Event.VIEW_DEACTIVATE );
73 listen : function( type, arg1, arg2, arg3 ){
74 if( type <= X_ViewPort_readyState ){
76 * X.Event.XDOM_READY 以後に listen した場合の対策
78 X_ViewPort.asyncDispatch( { type : type, w : X_ViewPort_width, h : X_ViewPort_height } );
80 type && arg1 && X_ViewPort.listen( type, arg1, arg2, arg3 );
85 listenOnce : function( type, arg1, arg2, arg3 ){
86 if( type <= X_ViewPort_readyState ){
88 * X.Event.XDOM_READY 以後に listen した場合の対策
90 X_ViewPort.asyncDispatch( { type : type, w : X_ViewPort_width, h : X_ViewPort_height } );
92 type && arg1 && X_ViewPort.listenOnce( type, arg1, arg2, arg3 );
96 unlisten : function( type, arg1, arg2, arg3 ){
97 type && arg1 && X_ViewPort.unlisten( type, arg1, arg2, arg3 );
101 listening : function( type, arg1, arg2, arg3 ){
102 return X_ViewPort.listening( type, arg1, arg2, arg3 );
105 // hammer のテストファイルだけが使用
106 asyncDispatch : function(){
107 return X_ViewPort.asyncDispatch.apply( X_ViewPort, arguments );
110 getPointerPosition : function(){
114 /* 要素が視界に入った http://remysharp.com/2009/01/26/element-in-view-event-plugin/ */
115 inView : function( elm ){
119 getSize : function(){
120 return [ X_ViewPort_width, X_ViewPort_height ];
122 //https://developer.mozilla.org/en-US/docs/Web/API/Element.getBoundingClientRect
123 //use window.pageXOffset and window.pageYOffset instead of window.scrollX and window.scrollY
124 //(((t = document.documentElement) || (t = document.body.parentNode)) && typeof t.ScrollLeft == 'number' ? t : document.body).ScrollLeft;
125 //(((t = document.documentElement) || (t = document.body.parentNode)) && typeof t.ScrollTop == 'number' ? t : document.body).ScrollTop
127 getDocumentSize : function(){
128 // Opera は互換モードでは document.body.scrollHeight、標準モードでは document.documentElement.scrollHeight でページの高さが取れる。と思ってたんだけど、例外があった。
129 // http://orera.g.hatena.ne.jp/edvakf/20100515/1273908051
130 //http://onozaty.hatenablog.com/entry/20060803/p1
131 // Safari2.0.4では標準・互換どちらも document.body
133 Node._body._updateTimerID && Node._body._startUpdate();
135 ( document.documentElement && document.documentElement.clientWidth ?
136 new Function( 'return[document.documentElement.clientWidth,document.documentElement.clientHeight]' ) :
137 new Function( 'return[document.body.clientWidth,document.body.clientHeight]' )
140 X_ViewPort_rootElement.scrollWidth || X_ViewPort_rootElement.offsetWidth,
141 X_ViewPort_rootElement.scrollHeight || X_ViewPort_rootElement.offsetHeight
146 window.pageXOffset !== undefined ?
148 Node._body._updateTimerID && Node._body._startUpdate();
149 return[ window.pageXOffset, window.pageYOffset ];
151 window.scrollLeft !== undefined ?
153 Node._body._updateTimerID && Node._body._startUpdate();
154 return[ window.scrollLeft, window.scrollTop ];
157 Node._body._updateTimerID && Node._body._startUpdate();
159 return[ X_ViewPort_rootElement.scrollLeft || document.body.scrollLeft, X_ViewPort_rootElement.scrollTop || document.body.scrollTop ];
162 getScrollbarSize : function(){
163 return [ X.Dom.BoxModel.vScrollbarSize, X.Dom.BoxModel.hScrollbarSize ];
166 getBaseFontSize : function(){
167 if( Node._body._updateTimerID ){
168 Node._body._startUpdate();
169 return X_ViewPort_baseFontSize = Node._fontSizeNode._rawObject.offsetHeight;
171 return X_ViewPort_baseFontSize;
178 /* -----------------------------------------------
180 * original : uupaa.js
183 * https://w3g.jp/blog/studies/ios7_1_minimal-ui_warning
184 * iOS7.0からあったiPad Safariの高さ100%コンテンツでlandscape(横向き)時に起きる不具合
186 * http://looxu.blogspot.jp/2013/11/ios7mobile-safariwindowinnerheight.html
187 * iOS7にて、Mobile Safariのwindow.innerHeightを正しく取得する方法
189 * http://tenderfeel.xsrv.jp/javascript/1182/
192 * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/javascript-14514208
193 * 1. 傾き時の画面サイズ取得ー 対処方法の例
194 * (1)ー? タイマーを使って、画面サイズ取得の タイミングを少しだけ遅延させる
195 * window.onorientationchange = function() { setTimeout(function() { alert("W: " + window.innerWidth + ", H: " + window.innerHeight); }, 200); }
196 * 横に傾けた時、正常に横向け時の 幅と高さが取得できている ただし、端末によってはうまく取得できな い場合がある(Galaxy S IIIとか・・・) 7
198 * http://sssslide.com/www.slideshare.net/hiroakiwakamatsu/ss-12718639
201 * getBoundingClientRect で fontsize の調査
203 var X_ViewPort_resize =
204 // iOS もループで回す,,,iOS3.1.3, iOS6 で確認
205 X.UA.IE < 9 || X.UA.iOS ?
208 if( !X_ViewPort_lock ){
209 size = X_ViewPort_getWindowSize();
210 if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
211 X_ViewPort_width = size[ 0 ];
212 X_ViewPort_height = size[ 1 ];
213 X.Timer.once( 100, X_Dom_detectFinishResizing );
214 X_ViewPort_lock = true;
218 size = Node._fontSizeNode._rawObject.offsetHeight;
219 if( X_ViewPort_baseFontSize !== size ){
220 X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( { type : X.Event.BASE_FONT_RESIZED, fontSize : size, w : X_ViewPort_width, h : X_ViewPort_height } );
221 X_ViewPort_baseFontSize = size;
226 console.log( '-- resize : ' + X_Timer_now() );
228 !X_ViewPort_lock && ( X_ViewPort_lock = true ) && X.Timer.once( 100, X_Dom_detectFinishResizing );
229 return X.Callback.PREVENT_DEFAULT | X.Callback.STOP_PROPAGATION;
232 function X_Dom_detectFinishResizing(){
233 var size = X_ViewPort_getWindowSize();
234 if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
235 X_ViewPort_width = size[ 0 ];
236 X_ViewPort_height = size[ 1 ];
237 X.Timer.once( 100, X_Dom_detectFinishResizing );
239 console.log( '-- detectFinishResizing : ' + X_Timer_now() );
241 X_ViewPort.asyncDispatch( { type : X.Event.VIEW_RESIZED, fontSize : X_ViewPort_baseFontSize, w : X_ViewPort_width, h : X_ViewPort_height } );
242 X_ViewPort_lock = false;
243 if( X_ViewPort_orientationFlag ){
244 X_ViewPort_orientationFlag = false;
245 X_ViewPort.asyncDispatch( 100, { type : X.Event.VIEW_TURNED, orientation : window.orientation } );
250 X_TEMP.onDomContentLoaded = function(){
251 var s, size, elmHtml, elmHead, elmBody, html, head, body;
253 if( X_TEMP.SYSTEM_EVENT_PRE_INIT <= X_ViewPort_readyState ) return X_Callback_UN_LISTEN;
254 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_PRE_INIT;
256 // DOMContentLoaded の無いブラウザで X_TEMP.onDomContentLoaded への参照が残り続けるのを回避
257 X_TEMP.onDomContentLoaded && X_ViewPort_document.unlisten( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
258 delete X_TEMP.onDomContentLoaded;
260 elmHtml = document.documentElement ||
261 X_UA_DOM.W3C ? document.getElementsByTagName( 'html' )[ 0 ] :
262 X_UA_DOM.IE4 ? document.all.tags( 'html' )[ 0 ] : null;
265 X_UA_DOM.W3C ? document.getElementsByTagName( 'head' )[ 0 ] :
266 X_UA_DOM.IE4 ? document.all.tags( 'head' )[ 0 ] : null;
268 elmBody = document.body;
270 X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
272 html = Node._html = elmHtml && new Node( elmHtml );
274 head = Node._head = elmHead && new Node( elmHead );
276 body = Node._body = new Node( elmBody );
278 body._root = html._root = head._root = body;// _root は html だろ
279 body.parent = head.parent = html;
280 html._xnodes = [ head, body ];
282 Node.root = body; // 後方互換
284 html.appendTo = html.appendToRoot = html.before = html.after = html.clone = html.remove = html.destroy = html.prevNode = html.nextNode =
285 html.create = html.createText = html.createAt = html.createTextAt = html.append = html.appendAt = html.empty = html.html = html.text =
286 head.appendTo = head.appendToRoot = head.before = head.after = head.clone = head.remove = head.destroy = head.prevNode = head.nextNode =
287 head.empty = head.html = head.text =
288 body.appendTo = body.appendToRoot = body.before = body.after = body.clone = body.remove = body.destroy = body.prevNode = body.nextNode = new Function( 'return this' );
290 X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_PRE_INIT, function(){
291 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_XTREE;
292 if( X_TEMP.X_Dom_useBuilder ) X_TEMP.X_Dom_useBuilder = !!document.body.children.length;
293 !X_TEMP.X_Dom_useBuilder && X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_XTREE );
296 X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_XTREE, function(){
297 X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_INIT;
298 //X.UA.Opera7 && alert( 'bc' );
299 Node._body.appendAt( 0,
300 Node._systemNode = Node.create( 'div', { 'class' : 'hidden-system-node' } ),
301 Node._fontSizeNode = Node.create( 'div', { 'class' : 'hidden-system-node' } ).cssText( 'line-height:1;height:1em;' ).text( 'X' )
304 X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_INIT );
307 X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
308 var size = X_ViewPort_getWindowSize();
310 if( X_Dom_orientationchange ){
311 X_ViewPort.listen( 'orientationchange', X_Dom_orientationchange );
314 if( X_Dom_detectFontSize ){
315 X_ViewPort.listen( 'resize', X_ViewPort_resize );
316 X.Timer.add( 333, X_Dom_detectFontSize );
318 X.Timer.add( 333, X_ViewPort_resize );
321 X_ViewPort_baseFontSize = Node._fontSizeNode._rawObject.offsetHeight;
323 X_ViewPort_readyState = X.Event.XDOM_READY;
324 X_ViewPort.asyncDispatch( { type : X.Event.XDOM_READY, w : X_ViewPort_width = size[ 0 ], h : X_ViewPort_height = size[ 1 ] } );
327 console.log( X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_PRE_INIT ) );
330 .listen( 'beforeunload', X_ViewPort )
331 .listenOnce( 'unload', X_ViewPort );
333 //ブラウザの戻るボタンで戻ったときに呼ばれるイベントとかキャッシュとかそこらへんのこと
334 //http://d.hatena.ne.jp/koumiya/20080916/1221580149
336 if( document[ 'hidden' ] !== undefined ) {// iOS 7+
337 X_ViewPort_document.listen( 'visibilitychange', X_ViewPort );
339 if( document[ 'webkitHidden' ] !== undefined ) {
340 X_ViewPort_document.listen( 'webkitvisibilitychange', X_ViewPort );
342 if( X.UA.iOS && window[ 'onpageshow' ] !== undefined ) {
343 X_ViewPort.listen( 'pageshow', X_ViewPort )
344 .listen( 'pagehide', X_ViewPort );
346 X_ViewPort.listen( 'focus', X_ViewPort )
347 .listen( 'blur', X_ViewPort );
350 return X_Callback_UN_LISTEN;
353 function X_ViewPort_getWindowSize(){
355 [ X_ViewPort_rootElement.clientWidth, X_ViewPort_rootElement.clientHeight ] :
356 [ window.innerWidth, window.innerHeight ];
360 console.log( 'X.Dom dom:w3c=' + X_UA_DOM.W3C + ' ev:w3c=' + X_UA_EVENT.W3C );
362 /* -----------------------------------------------
364 * Dean Edwards/Matthias Miller/John Resig
367 // SafariでJavaScriptのデバッグをする方法
369 // http://shimax.cocolog-nifty.com/search/2006/09/safarijavascrip_c54d.html
371 /* for ie9+/Mozilla/Opera9 */
372 if( X_UA_EVENT.W3C ){
373 X_ViewPort_document.listenOnce( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
375 if( 6 <= X.UA.IE && X.inHead ){
376 // if this script in Head
377 document.write( "<script id=__ie_onload defer src=javascript:void(0)><\/script>" );
378 X_TEMP._script = document.getElementById( "__ie_onload" );
379 X_TEMP._script.onreadystatechange = function(){
380 var s = X_TEMP._script;
381 if( s && s.readyState === 'complete' ){
382 s.onreadystatechange = X.emptyFunction;
383 s.onreadystatechange = null;
384 s.parentNode.removeChild( s );
385 delete X_TEMP._script;
386 X_TEMP.onDomContentLoaded && X_TEMP.onDomContentLoaded();
390 // Re: onLoad doesn't work with Safari?
391 // http://lists.apple.com/archives/web-dev/2003/Oct/msg00036.html
392 if( X.UA.WebKit || X.UA.Safari < 3 ){ // sniff
393 X.Timer.add( 16, function(){
394 if( !X_TEMP.onDomContentLoaded ) return X_Callback_UN_LISTEN;
395 if( document.readyState === 'loaded' || document.readyState === 'complete' ) return X_TEMP.onDomContentLoaded();
399 /* for other browsers */
400 X_ViewPort.listenOnce( 'load', X_TEMP.onDomContentLoaded );