UNPKG

@senx/discovery-plugin-marauder

Version:

Discovery plugin - Marauder's map

307 lines (303 loc) 16.2 kB
import { r as registerInstance, c as createEvent, h, g as getElement } from './index-7d8b9e81.js'; import { P as Param, U as Utils } from './utils-2db7dc60.js'; import { l as lodash, L as Logger, v as v4, G as GTSLib } from './lodash-d98cd4b5.js'; import { L as LangUtils } from './lang-utils-ab937efb.js'; const discoveryTileCss = "/*!\n * Copyright 2022-2024 SenX S.A.S.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *//*!\n * Copyright 2022-2024 SenX S.A.S.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */:host{width:100%;height:100%;position:relative}:host>div{width:100%;height:100%;position:relative}:host #ws{color:transparent;font-size:0;height:0;overflow:hidden}:host .discovery-tile-spinner{position:absolute;top:0;left:0;width:100%;display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;height:100%}:host .discovery-tile-error{background-color:var(--warp-view-error-bg-color, #dc3545);color:var(--warp-view-error-color, #ffffff);text-align:center}:host .discovery-tile-status{background-color:var(--warp-view-status-bg-color, #d1ecf1);color:var(--warp-view-status-color, #0c5460);text-align:center;position:relative;padding:5px 10px;width:calc(100% - 40px);margin-top:12px}"; const DiscoveryTileStyle0 = discoveryTileCss; const DiscoveryTileComponent = class { constructor(hostRef) { registerInstance(this, hostRef); this.statusHeaders = createEvent(this, "statusHeaders", 7); this.statusError = createEvent(this, "statusError", 7); this.execResult = createEvent(this, "execResult", 7); this.selfType = createEvent(this, "selfType", 7); this.innerVars = {}; this.innerOptions = new Param(); this.firstExec = false; this.url = undefined; this.chartTitle = undefined; this.chartDescription = undefined; this.type = undefined; this.options = new Param(); this.language = 'warpscript'; this.debug = false; this.unit = ''; this.autoRefresh = -1; this.vars = '{}'; this.loaded = false; this.result = '[]'; this.width = undefined; this.height = undefined; this.headers = undefined; this.start = undefined; this.showLoader = false; this.hasError = false; this.errorMessage = ''; this.statusMessage = ''; } optionsUpdate(newValue, oldValue) { var _a, _b; (_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['optionsUpdate'], newValue, oldValue); let opts = newValue; if (!!newValue && typeof newValue === 'string') { opts = JSON.parse(newValue); } if (!lodash.exports.isEqual(opts, this.innerOptions)) { this.innerOptions = Object.assign({}, opts); (_b = this.LOG) === null || _b === void 0 ? void 0 : _b.debug(['optionsUpdate 2'], { options: this.innerOptions, newValue, oldValue }); } console.log("optionsUpdate", this.type, this.innerOptions); } async varsUpdate(newValue, oldValue) { var _a; let vars = this.vars; if (!!this.vars && typeof this.vars === 'string') { vars = JSON.parse(this.vars); } if (!lodash.exports.isEqual(vars, this.innerVars)) { this.innerVars = vars; await this.exec(true); } if (this.LOG) { (_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['varsUpdate'], { vars: this.vars, newValue, oldValue }); } } async discoveryEventHandler(event) { const res = Utils.parseEventData(event.detail, this.innerOptions.eventHandler, this.componentId); if (res.vars) { this.innerVars = Object.assign(Object.assign({}, (this.innerVars || {})), res.vars); if (!(this.innerOptions.mutedVars || []).includes(event.detail.selector)) { await this.exec(true); } } if (res.selected) { this.innerVars = Object.assign(Object.assign({}, (this.innerVars || {})), res.selected); if (!(this.innerOptions.mutedVars || []).includes(event.detail.selector)) { await this.exec(true); } } } async resize() { var _a; const dims = Utils.getContentBounds(this.el.parentElement); this.width = dims.w - 15; this.height = dims.h; (_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['componentDidLoad'], 'Tile - resize', this.tileResult); if (this.tileResult) { return this.tileResult.resize(); } } async show(regexp) { if (this.tileResult) { await this.tileResult.show(regexp); } } async showById(id) { if (this.tileResult) { await this.tileResult.showById(id); } } async hide(regexp) { if (this.tileResult) { await this.tileResult.hide(regexp); } } async hideById(id) { if (this.tileResult) { await this.tileResult.hideById(id); } } async setFocus(regexp, ts, value) { if (this.tileResult) { await this.tileResult.setFocus(regexp, ts, value); } } async unFocus() { if (this.tileResult) { await this.tileResult.unFocus(); } } // noinspection JSUnusedGlobalSymbols componentWillLoad() { var _a, _b; this.LOG = new Logger(DiscoveryTileComponent, this.debug); this.componentId = v4(); (_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['componentWillLoad'], { url: this.url, type: this.type, options: this.options, language: this.language, innerVars: this.innerVars, }); if (!!this.options && typeof this.options === 'string' && this.options !== 'undefined') { this.innerOptions = JSON.parse(this.options); } else { this.innerOptions = Object.assign({}, (_b = this.options) !== null && _b !== void 0 ? _b : new Param()); } this.innerVars = JSON.parse(this.vars || '{}'); const dims = Utils.getContentBounds(this.el.parentElement); this.width = dims.w - 15; this.height = dims.h; } async componentDidLoad() { if (!this.firstExec) { await this.exec(); } } // noinspection JSUnusedGlobalSymbols disconnectedCallback() { var _a; (_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['disconnectedCallback'], 'disconnected'); if (this.timer) { window.clearInterval(this.timer); } if (!!this.socket) { this.socket.close(); } } async export(type = 'png') { if (this.tileResult) { return this.tileResult.export(type); } else { return undefined; } } async exec(refresh = false) { return new Promise(resolve => { var _a, _b, _c; if (((_a = this.el) === null || _a === void 0 ? void 0 : _a.innerHTML) !== undefined) { if (!refresh) { setTimeout(() => this.loaded = false); } this.ws = LangUtils.prepare(Utils.unsescape(this.el.innerHTML), this.innerVars || {}, ((_b = this.innerOptions) === null || _b === void 0 ? void 0 : _b.skippedVars) || [], this.type, this.language); if (!!window) { const win = window; let registry = win.DiscoveryPluginRegistry; registry = registry || {}; if (!!(registry || {})[this.type] && !!registry[this.type].scriptWrapper && typeof registry[this.type].scriptWrapper === 'function') { this.ws = registry[this.type].scriptWrapper(this.ws); } } (_c = this.LOG) === null || _c === void 0 ? void 0 : _c.debug(['exec'], this.ws, this.type); this.url = Utils.getUrl(this.url); if (this.url.toLowerCase().startsWith('http')) { setTimeout(() => { this.hasError = false; this.errorMessage = ''; this.statusMessage = undefined; if (this.innerOptions.showLoader) { this.showLoader = true; } }); Utils.httpPost(this.url, this.ws, this.innerOptions.httpHeaders) .then((res) => { var _a; const toRefresh = this.result === res.data; if ((this.type || '').startsWith('input')) { this.result = ''; } this.headers = res.headers; this.headers.statusText = `Your script execution took ${GTSLib.formatElapsedTime(res.status.elapsed)} serverside, fetched ${res.status.fetched} datapoints and performed ${res.status.ops} WarpLib operations.`; (_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['exec', 'headers'], this.headers); this.statusHeaders.emit(this.headers); if (this.innerOptions.showStatus) { this.statusMessage = this.headers.statusText; } this.start = new Date().getTime(); if (this.autoRefresh !== this.innerOptions.autoRefresh) { this.autoRefresh = this.innerOptions.autoRefresh; if (this.timer) { window.clearInterval(this.timer); } if (this.autoRefresh && this.autoRefresh > 0) { this.timer = window.setInterval(() => void this.exec(true), this.autoRefresh * 1000); } } setTimeout(() => { void (async () => { var _a; this.loaded = true; this.showLoader = false; (_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['exec', 'result'], this.result); this.result = res.data; this.execResult.emit(this.result); if (toRefresh && refresh && !!this.tileResult) { await this.tileResult.parseEvents(); } resolve(true); })(); }); }) .catch(e => { var _a; this.statusError.emit(e); setTimeout(() => { this.loaded = true; this.showLoader = false; this.hasError = this.innerOptions.showErrors; this.errorMessage = e.message || e.statusText; }); (_a = this.LOG) === null || _a === void 0 ? void 0 : _a.error(['exec'], e); resolve(true); }); } else if (this.url.toLowerCase().startsWith('ws')) { // Web Socket if (!!this.socket) { this.socket.close(); } this.socket = new WebSocket(this.url); this.socket.onopen = () => { this.socket.onmessage = event => { var _a; const res = event.data; setTimeout(() => { this.loaded = true; this.showLoader = false; }); if (res.startsWith('["Exception at \'EVERY')) { this.hasError = this.innerOptions.showErrors; this.errorMessage = JSON.parse(res)[0] || 'Error'; this.statusError.emit(this.errorMessage); } else { this.result = res; this.hasError = false; this.errorMessage = ''; (_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['exec', 'result'], this.result); this.execResult.emit(this.result); } }; this.socket.send(`<% ${this.ws} %> ${(this.innerOptions.autoRefresh || 1000)} EVERY`); resolve(true); }; } } }); } async setZoom(dataZoom) { await this.tileResult.setZoom(dataZoom); } handleSelfType(type) { this.selfType.emit(type); } render() { return h("div", { key: '919e5adf56b4263b461a1ff2877c7bc4b47362f2' }, this.loaded ? this.hasError ? h("div", { class: "discovery-tile-error" }, this.errorMessage) : h("div", { style: { width: '100%', height: '100%', display: 'flex', flexDirection: 'column' } }, h("discovery-tile-result", { url: this.url, start: this.start, result: this.result, type: this.type, options: this.innerOptions, unit: this.unit, debug: this.debug, height: this.height, width: this.width, language: this.language, "chart-title": this.chartTitle, "chart-description": this.chartDescription, onSelfType: type => this.handleSelfType(type), vars: JSON.stringify(this.innerVars), ref: (el) => this.tileResult = el, id: this.componentId }), this.statusMessage ? h("div", { class: "discovery-tile-status" }, this.statusMessage) : '') : h("div", { class: "discovery-tile-spinner" }, this.showLoader ? h("discovery-spinner", null, "Requesting data...") : ''), this.showLoader ? h("div", { class: "discovery-tile-spinner" }, h("discovery-spinner", null, "Requesting data...")) : '', h("pre", { key: 'b406cafb81b093bf5b2150dd55ae2be3ff1fdbb1', id: "ws" }, h("slot", { key: 'e07f266a931e56bbfeed86f3ec71daf8cd5074e2' }))); } get el() { return getElement(this); } static get watchers() { return { "options": ["optionsUpdate"], "vars": ["varsUpdate"] }; } }; DiscoveryTileComponent.style = DiscoveryTileStyle0; export { DiscoveryTileComponent as discovery_tile }; //# sourceMappingURL=discovery-tile.entry.js.map