UNPKG

@bigfishtv/cockpit

Version:

55 lines (47 loc) 1.37 kB
'use strict'; exports.__esModule = true; exports.windowVisible = windowVisible; exports.onVisibilityChange = onVisibilityChange; /** * Window Visible Utility * @module Utilities/windowVisible */ var stateKey = null; var eventKey = null; var keys = { hidden: 'visibilitychange', webkitHidden: 'webkitvisibilitychange', mozHidden: 'mozvisibilitychange', msHidden: 'msvisibilitychange' }; for (stateKey in keys) { if (stateKey in document) { eventKey = keys[stateKey]; break; } } /** * Returns true/false if window is visible * @param {Object} _window - defaults to window * @return {Boolean} */ function windowVisible() { var _window = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window; return !_window.document[stateKey]; } /** * Adds 'visibilitychange' listener to window and binds to provided callback * @param {Function} callback * @param {Object} _window - defaults to window * @return {Function} */ function onVisibilityChange(callback) { var _window = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window; var func = function func() { return callback(windowVisible(_window)); }; _window.document.addEventListener(eventKey, func); return function () { return _window && _window.document && _window.document.removeEventListener(eventKey, func); }; }