UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

53 lines (42 loc) 1.37 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); /** * This class is a surger to handle window beforeunload event. * * Example . * ```javascript * beforeunload.block(); // block user exit browser. * * doSomething() * * beforeunload.unblock(); // unblock user exit browser. * ``` */ class Beforeunload { constructor() { (0, _defineProperty2.default)(this, "onbeforeunload", void 0); } block(options) { if (this.onbeforeunload) return; this.onbeforeunload = e => { // Cancel the event as stated by the standard. e.preventDefault(); // Chrome requires returnValue to be set. e.returnValue = options === null || options === void 0 ? void 0 : options.dialogText; return options === null || options === void 0 ? void 0 : options.dialogText; }; window.addEventListener('beforeunload', this.onbeforeunload); } unblock() { if (!this.onbeforeunload) return; window.removeEventListener('beforeunload', this.onbeforeunload); this.onbeforeunload = undefined; } } const Instance = new Beforeunload(); var _default = Instance; exports.default = _default;