UNPKG

molstar

Version:

A comprehensive macromolecular library.

85 lines 3.25 kB
"use strict"; /** * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info. * * Adapted from LiteMol (c) David Sehnal * * @author David Sehnal <david.sehnal@gmail.com> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginToastManager = void 0; var tslib_1 = require("tslib"); var component_1 = require("../../mol-plugin-state/component"); var immutable_1 = require("immutable"); var commands_1 = require("../commands"); var PluginToastManager = /** @class */ (function (_super) { (0, tslib_1.__extends)(PluginToastManager, _super); function PluginToastManager(plugin) { var _this = _super.call(this, { entries: (0, immutable_1.OrderedMap)() }) || this; _this.events = { changed: _this.ev() }; _this.serialNumber = 0; _this.serialId = 0; commands_1.PluginCommands.Toast.Show.subscribe(plugin, function (e) { return _this.show(e); }); commands_1.PluginCommands.Toast.Hide.subscribe(plugin, function (e) { return _this.hide(_this.findByKey(e.key)); }); return _this; } PluginToastManager.prototype.findByKey = function (key) { return this.state.entries.find(function (e) { return !!e && e.key === key; }); }; PluginToastManager.prototype.show = function (toast) { var _this = this; var entries = this.state.entries; var e = void 0; var id = ++this.serialId; var serialNumber; if (toast.key && (e = this.findByKey(toast.key))) { if (e.timeout !== void 0) clearTimeout(e.timeout); serialNumber = e.serialNumber; entries = entries.remove(e.id); } else { serialNumber = ++this.serialNumber; } e = { id: id, serialNumber: serialNumber, key: toast.key, title: toast.title, message: toast.message, timeout: this.timeout(id, toast.timeoutMs), hide: function () { return _this.hideId(id); } }; if (this.updateState({ entries: entries.set(id, e) })) this.events.changed.next(void 0); }; PluginToastManager.prototype.timeout = function (id, delay) { var _this = this; if (delay === void 0) return void 0; if (delay < 0) delay = 500; return setTimeout(function () { var e = _this.state.entries.get(id); e.timeout = void 0; _this.hide(e); }, delay); }; PluginToastManager.prototype.hideId = function (id) { this.hide(this.state.entries.get(id)); }; PluginToastManager.prototype.hide = function (e) { if (!e) return; if (e.timeout !== void 0) clearTimeout(e.timeout); e.hide = void 0; if (this.updateState({ entries: this.state.entries.delete(e.id) })) this.events.changed.next(void 0); }; return PluginToastManager; }(component_1.StatefulPluginComponent)); exports.PluginToastManager = PluginToastManager; //# sourceMappingURL=toast.js.map