// JSX
// hide/close tab event -> save localStorage
var pageVisibilityEvent = new PageVisibilityEvent();
pageVisibilityEvent.on(function(pageHide:boolean):void {
if (pageHide) {
;
}
});
// JSX
// ----------------------------------------------
/*
+-------------------+------------------+----------+----------+------------------+
| OS, Browser | load | hide tab | show tab | close tab |
+-------------------+------------------+----------+----------+------------------+
| iOS 5.1 Safari | pageshow | pagehide | pageshow | |
| iOS 6.1 Safari | pageshow | pagehide | pageshow | |
| Mac Safari 6.0.4 | pageshow | blur | focus | |
| Mac Chrome 26 | visibilitychange | blur | focus | visibilitychange |
| Android Chrome 27 | visibilitychange | blur | focus | visibilitychange |
| Android 4.0.1 | pageshow | blur | focus | pagehide |
| Android 2.3.6 | pageshow | (none) | (none) | pagehide |
+-------------------+------------------+----------+----------+------------------+
*/
/*
// var pageVisibilityEvent = new PageVisibilityEvent();
//
// pageVisibilityEvent.on(function(pageHide:boolean):void {
// log(pageHide ? "page hide" : "page show");
// });
*/
class PageVisibilityEvent {
var _hidden = false; // Boolean - current PageVisibility state
var _callback = null:function(:boolean):void; // Function - callback
function on(callback:function(pageHide:boolean):void):void {
this._callback = callback;
this._handler(true);
}
function off():void {
this._callback = null;
this._handler(false);
}
function isHidden():boolean {
return this._hidden;
}
function _handler(attach:boolean):void { // @arg Boolean(= false): true is attach event
var that = this;
var window = js.global["window"] as __noconvert__ Window;
var document = js.global["document"] as __noconvert__ HTMLDocument;
function _handlePageVisibility(event:Event):void {
that._callback(that._hidden = document["hidden"] || document["webkitHidden"] || false);
}
function _handlePageShow(event:Event):void {
that._hidden = event.type == "pagehide";
that._callback(that._hidden);
}
function _handleFocus(event:Event):void {
that._hidden = event.type == "blur";
that._callback(that._hidden);
}
if ( "hidden" in document as __noconvert__ Map.<variant> ) {
if (attach) { document.addEventListener("visibilitychange", _handlePageVisibility, false); }
else { document.removeEventListener("visibilitychange", _handlePageVisibility, false); }
} else if ( "webkitHidden" in document as __noconvert__ Map.<variant> ) {
if (attach) { document.addEventListener("webkitvisibilitychange", _handlePageVisibility, false); }
else { document.removeEventListener("webkitvisibilitychange", _handlePageVisibility, false); }
} else {
if ( /iPhone|iPad|iPod/.test(window.navigator.userAgent) ) {
if (attach) { window.addEventListener("pageshow", _handlePageShow); }
else { window.removeEventListener("pageshow", _handlePageShow); }
if (attach) { window.addEventListener("pagehide", _handlePageShow); }
else { window.removeEventListener("pagehide", _handlePageShow); }
} else {
if (attach) { window.addEventListener("focus", _handlePageShow); }
else { window.removeEventListener("focus", _handlePageShow); }
if (attach) { window.addEventListener("blur", _handleFocus); }
else { window.removeEventListener("blur", _handleFocus); }
}
}
}
}