UNPKG

@ibsheet/loader

Version:

Dynamically load support module for IBSheet

169 lines 6.1 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.LoaderRegistry = void 0; var lodash_1 = require("../shared/lodash"); var custom_1 = require("../custom"); var ibsheet_1 = require("../ibsheet"); var item_1 = require("./item"); var utils_1 = require("./utils"); var for_ibsheet_1 = require("./for-ibsheet"); var LoaderRegistry = (function (_super) { __extends(LoaderRegistry, _super); function LoaderRegistry(uber) { var _this = _super.call(this) || this; _this._list = []; _this._uber = uber; return _this; } Object.defineProperty(LoaderRegistry.prototype, "debug", { get: function () { return this._uber.debug; }, enumerable: false, configurable: true }); Object.defineProperty(LoaderRegistry.prototype, "length", { get: function () { return this._list.length; }, enumerable: false, configurable: true }); LoaderRegistry.prototype.getUberOption = function (sPath, def) { return this._uber.getOption(sPath, def); }; LoaderRegistry.prototype.add = function (data, overwrite) { if (overwrite === void 0) { overwrite = false; } var item; try { item = new item_1.RegistryItem(data); } catch (err) { console.warn(err); return; } var _a = item.raw, alias = _a.alias, urls = _a.urls; var bIBSheet = (0, ibsheet_1.isIBSheet)(item.name); var existItem = this.get(alias); if (!(0, lodash_1.isNil)(existItem)) { if (overwrite) { if (this.debug) { console.log("%c[registry.add] update \"".concat(alias, "\":"), 'color: royalblue', data); } existItem.update(data); return; } else if (existItem.raw.urls[0] === urls[0]) { if (this.debug) { console.warn("ignore duplicate script \"".concat(alias, "\"")); } return; } item.version = (0, utils_1.generateVersion)(item); } else if (bIBSheet) { for_ibsheet_1.defaultsIBSheetEvents.call(this, item); } this._list.push(item); return item; }; LoaderRegistry.prototype.addAll = function (params, overwrite) { var _this = this; if (overwrite === void 0) { overwrite = false; } return (0, lodash_1.castArray)(params) .map(function (data) { return _this.add(data, overwrite); }) .filter(Boolean); }; LoaderRegistry.prototype.exists = function (alias) { return !(0, lodash_1.isNil)(this.get(alias)); }; LoaderRegistry.prototype.get = function (alias) { var ndx = this.getIndexByAlias(alias); if (ndx < 0) return null; return this._list[ndx]; }; LoaderRegistry.prototype.info = function (alias) { var res = this.getAll(alias).map(function (item) { return item.raw; }); if (!res.length) return; if (res.length === 1) res = res[0]; return JSON.stringify(res, null, 2); }; LoaderRegistry.prototype.getAll = function (query) { var items = this._list; if ((0, lodash_1.isNil)(query)) { return items; } var hasVersion = (0, lodash_1.lastIndexOf)(query, '@') > 0; return items.filter(function (item) { if (hasVersion) { return item.alias === query; } return item.name === query; }); }; LoaderRegistry.prototype.findOne = function (query) { var items = this.getAll(query); if (items.length) return items[0]; return; }; LoaderRegistry.prototype.findLoadedOne = function (query) { var items = this.getAll(query); var loadedItems = items.filter(function (item) { return item.loaded; }); if (loadedItems.length) return loadedItems[0]; return; }; LoaderRegistry.prototype.getIndexByAlias = function (alias) { return (0, lodash_1.findIndex)(this._list, { alias: alias }); }; LoaderRegistry.prototype.update = function (alias, data) { var item = this.get(alias); if ((0, lodash_1.isNil)(item)) return; item.update(data); }; LoaderRegistry.prototype.remove = function (alias) { var items = this.getAll(alias); if (!items.length) return; var ids = items.map(function (item) { return item.id; }); var result = []; (0, lodash_1.remove)(this._list, function (item) { var match = (0, lodash_1.includes)(ids, item.id); if (match) result.push(item); return match; }); if (result.length === 1) return result[0]; return result; }; LoaderRegistry.prototype.list = function () { return this._list.map(function (item) { return item.alias; }); }; return LoaderRegistry; }(custom_1.CustomEventEmitter)); exports.LoaderRegistry = LoaderRegistry; exports.default = LoaderRegistry; //# sourceMappingURL=registry.js.map