From: Alexander Ebert Date: Tue, 18 Feb 2014 18:45:30 +0000 (+0100) Subject: Added WCF.PageVisibilityHandler X-Git-Tag: 2.0.3~32 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=03fcd56094c896e6f49952aeea372d4615136a89;p=GitHub%2FWoltLab%2FWCF.git Added WCF.PageVisibilityHandler --- diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index 3cc1c9672c..61d2400e14 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -5292,6 +5292,107 @@ WCF.DOMNodeRemovedHandler = { } }; +WCF.PageVisibilityHandler = { + /** + * list of callbacks + * @var WCF.Dictionary + */ + _callbacks: new WCF.Dictionary(), + + /** + * indicates that event listeners are bound + * @var boolean + */ + _isListening: false, + + /** + * name of window's hidden property + * @var string + */ + _hiddenFieldName: '', + + /** + * Adds a new callback. + * + * @param string identifier + * @param object callback + */ + addCallback: function(identifier, callback) { + this._bindListener(); + + if (this._callbacks.isset(identifier)) { + console.debug("[WCF.PageVisibilityHandler] identifier '" + identifier + "' is already bound to a callback"); + return false; + } + + this._callbacks.add(identifier, callback); + }, + + /** + * Removes a callback from list. + * + * @param string identifier + */ + removeCallback: function(identifier) { + if (this._callbacks.isset(identifier)) { + this._callbacks.remove(identifier); + } + }, + + /** + * Binds click event handler. + */ + _bindListener: function() { + if (this._isListening) return; + + var $eventName = null; + if (typeof document.hidden !== "undefined") { + this._hiddenFieldName = "hidden"; + $eventName = "visibilitychange"; + } + else if (typeof document.mozHidden !== "undefined") { + this._hiddenFieldName = "mozHidden"; + $eventName = "mozvisibilitychange"; + } + else if (typeof document.msHidden !== "undefined") { + this._hiddenFieldName = "msHidden"; + $eventName = "msvisibilitychange"; + } + else if (typeof document.webkitHidden !== "undefined") { + this._hiddenFieldName = "webkitHidden"; + $eventName = "webkitvisibilitychange"; + } + + if ($eventName === null) { + console.debug("[WCF.PageVisibilityHandler] This browser does not support the page visibility API."); + } + else { + $(document).on($eventName, $.proxy(this._executeCallbacks, this)); + } + + this._isListening = true; + }, + + /** + * Executes callbacks if page is hidden/visible again. + */ + _executeCallbacks: function(event) { + if (this._isExecuting) return; + + // do not track events while executing callbacks + this._isExecuting = true; + + var $state = document[this._hiddenFieldName]; + this._callbacks.each(function(pair) { + // execute callback + pair.value($state); + }); + + // enable listener again + this._isExecuting = false; + } +}; + /** * Namespace for table related classes. */