UNPKG

@bigfishtv/cockpit

Version:

50 lines (43 loc) 1.25 kB
/** * 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} */ export 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} */ export 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); }; }