UNPKG

vue-use-query

Version:

vue use query

88 lines (87 loc) 3.44 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.focusManager = void 0; var subscribable_1 = require("./subscribable"); var utils_1 = require("./utils"); /** * 聚焦管理 */ var FocusManager = /** @class */ (function (_super) { __extends(FocusManager, _super); function FocusManager() { return _super !== null && _super.apply(this, arguments) || this; } FocusManager.prototype.onSubscribe = function () { if (!this.removeEventListener) { this.setDefaultEventListener(); } }; FocusManager.prototype.setEventListener = function (setup) { var _this = this; if (this.removeEventListener) { this.removeEventListener(); } this.removeEventListener = setup(function (focused) { if (typeof focused === 'boolean') { _this.setFocused(focused); } else { _this.onFocus(); } }); }; FocusManager.prototype.setFocused = function (focused) { this.focused = focused; if (focused) { this.onFocus(); } }; FocusManager.prototype.onFocus = function () { this.listeners.forEach(function (listener) { listener(); }); }; FocusManager.prototype.isFocused = function () { if (typeof this.focused === 'boolean') { return this.focused; } // 非浏览器环境 if (typeof document === 'undefined') { return true; } // 浏览器环境 return [undefined, 'visible', 'prerender'].includes(document.visibilityState); }; FocusManager.prototype.setDefaultEventListener = function () { if (!utils_1.isServer && (window === null || window === void 0 ? void 0 : window.addEventListener)) { this.setEventListener(function (onFocus) { var listener = function () { return onFocus(); }; // Listen to visibillitychange and focus window.addEventListener('visibilitychange', listener, false); window.addEventListener('focus', listener, false); return function () { // Be sure to unsubscribe if a new handler is set window.removeEventListener('visibilitychange', listener); window.removeEventListener('focus', listener); }; }); } }; return FocusManager; }(subscribable_1.Subscribable)); exports.focusManager = new FocusManager();