@senx/discovery-plugin-marauder
Version:
Discovery plugin - Marauder's map
334 lines (328 loc) • 19.6 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-df4aa1cc.js');
const utils = require('./utils-c9264fdc.js');
const langUtils = require('./lang-utils-08582de0.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;z-index:9999;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%;color:#ffffff;font-weight:bold}:host .discovery-tile-error{background-color:var(--warp-view-error-bg-color, #dc3545);color:var(--warp-view-error-color, #ffffff);text-align:center;position:absolute;width:calc(100% - 40px);padding:20px;height:calc(100% - 40px);z-index:9;display:grid;-ms-flex-align:center;align-items: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}:host .hidden-by-ws{opacity:0;-webkit-transition:opacity 1s ease-in-out;transition:opacity 1s ease-in-out}";
const DiscoveryTileStyle0 = discoveryTileCss;
const DiscoveryTileComponent = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.statusHeaders = index.createEvent(this, "statusHeaders", 7);
this.statusError = index.createEvent(this, "statusError", 7);
this.execResult = index.createEvent(this, "execResult", 7);
this.selfType = index.createEvent(this, "selfType", 7);
this.draw = index.createEvent(this, "draw", 7);
this.options = new utils.Param();
this.language = 'warpscript';
this.debug = false;
this.unit = '';
this.autoRefresh = -1;
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
this.vars = '{}';
this.loaded = false;
this.result = '[]';
this.showLoader = false;
this.hasError = false;
this.errorMessage = '';
this.statusMessage = '';
this.hiddenByWs = false;
this.innerVars = {};
this.innerOptions = new utils.Param();
this.firstExec = false;
}
async optionsUpdate(newValue, oldValue) {
var _a, _b, _c, _d;
(_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 (!utils.Utils.deepEqual(opts, this.innerOptions)) {
this.innerOptions = utils.Utils.clone(opts);
if (utils.Utils.deepEqual((_b = opts.httpHeaders) !== null && _b !== void 0 ? _b : {}, (_c = this.innerOptions.httpHeaders) !== null && _c !== void 0 ? _c : {})) {
await this.exec(true);
}
(_d = this.LOG) === null || _d === void 0 ? void 0 : _d.debug(['optionsUpdate 2'], this.type, { options: this.innerOptions, newValue, oldValue });
}
}
async varsUpdate(newValue, oldValue) {
var _a;
let vars = this.vars;
if (!!this.vars && typeof this.vars === 'string') {
vars = JSON.parse(this.vars);
}
if (!utils.Utils.deepEqual(vars, this.innerVars)) {
this.innerVars = utils.Utils.clone(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) {
var _a, _b, _c, _d, _e;
const res = utils.Utils.parseEventData(event.detail, this.innerOptions.eventHandler, this.componentId);
if (res.vars) {
this.innerVars = utils.Utils.clone(Object.assign(Object.assign({}, ((_a = this.innerVars) !== null && _a !== void 0 ? _a : {})), res.vars));
if (!((_b = this.innerOptions.mutedVars) !== null && _b !== void 0 ? _b : []).includes(event.detail.selector)) {
await this.exec(true);
}
}
if (res.selected) {
const vars = utils.Utils.clone(Object.assign(Object.assign({}, ((_c = this.innerVars) !== null && _c !== void 0 ? _c : {})), res.selected));
if (!utils.Utils.deepEqual((_d = this.innerVars) !== null && _d !== void 0 ? _d : {}, vars)) {
this.innerVars = utils.Utils.clone(vars);
if (!((_e = this.innerOptions.mutedVars) !== null && _e !== void 0 ? _e : []).includes(event.detail.selector)) {
await this.exec(true);
}
}
}
}
async resize() {
var _a;
const dims = utils.Utils.getContentBounds(this.el.parentElement);
this.height = dims.h;
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['componentDidLoad'], 'Tile - resize', this.tileResult);
if (this.tileResult && this.width !== (dims.w - 2)) {
this.width = dims.w - 2;
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, _c;
this.LOG = new utils.Logger(DiscoveryTileComponent, this.debug);
this.componentId = utils.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 utils.Param());
}
this.innerVars = JSON.parse((_c = this.vars) !== null && _c !== void 0 ? _c : '{}');
const dims = utils.Utils.getContentBounds(this.el.parentElement);
this.width = dims.w - 2;
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);
window.clearInterval(this.timerFadeOut);
}
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, _d, _e;
if (((_a = this.el) === null || _a === void 0 ? void 0 : _a.innerHTML) !== undefined) {
if (!refresh) {
setTimeout(() => this.loaded = false);
}
this.ws = langUtils.LangUtils.prepare(utils.Utils.unsescape(this.el.innerHTML), (_b = this.innerVars) !== null && _b !== void 0 ? _b : {}, (_d = (_c = this.innerOptions) === null || _c === void 0 ? void 0 : _c.skippedVars) !== null && _d !== void 0 ? _d : [], this.type, this.language);
if (!!window) {
const win = window;
let registry = win.DiscoveryPluginRegistry;
registry = registry !== null && registry !== void 0 ? registry : {};
if (!!(registry !== null && registry !== void 0 ? registry : {})[this.type] && !!registry[this.type].scriptWrapper && typeof registry[this.type].scriptWrapper === 'function') {
this.ws = registry[this.type].scriptWrapper(this.ws);
}
}
(_e = this.LOG) === null || _e === void 0 ? void 0 : _e.debug(['exec'], this.chartTitle, this.ws, this.type);
this.url = utils.Utils.getUrl(this.url);
if (this.url.toLowerCase().startsWith('http')) {
const thisRequestTs = Date.now();
this.latestRequestTs = thisRequestTs;
setTimeout(() => {
this.hasError = false;
this.errorMessage = '';
this.statusMessage = undefined;
this.showLoader = !!this.innerOptions.showLoader;
});
utils.Utils.httpPost(this.url, this.ws, this.innerOptions.httpHeaders)
.then((res) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
if (this.latestRequestTs !== thisRequestTs) {
// When requests pile up, the oldests must be ignored.
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['exec', 'liloControl'], 'This request result arrived later than the latest request, discard result');
resolve(true);
}
else {
this.hiddenByWs = false;
if (((_b = this.type) !== null && _b !== void 0 ? _b : '').startsWith('input') || ((_c = this.type) !== null && _c !== void 0 ? _c : '').startsWith('svg')) {
this.result = '';
}
this.headers = (_d = res === null || res === void 0 ? void 0 : res.headers) !== null && _d !== void 0 ? _d : {};
this.headers.statusText = `Your script execution took ${utils.GTSLib.formatElapsedTime(res.status.elapsed)} serverside, fetched ${res.status.fetched} datapoints and performed ${res.status.ops} WarpLib operations.`;
(_e = this.LOG) === null || _e === void 0 ? void 0 : _e.debug(['exec', 'headers'], this.headers);
this.statusHeaders.emit(this.headers);
if (this.innerOptions.showStatus) {
this.statusMessage = this.headers.statusText;
}
this.start = window.performance.now();
const rws = utils.GTSLib.getData(res.data);
let autoRefreshFeedBack = (_g = (_f = rws.globalParams) === null || _f === void 0 ? void 0 : _f.autoRefresh) !== null && _g !== void 0 ? _g : -1;
const fadeOutAfter = (_h = rws.globalParams) === null || _h === void 0 ? void 0 : _h.fadeOutAfter;
if (rws.localvars) {
utils.Utils.mergeDeep(this.innerVars, rws.localvars);
}
if (autoRefreshFeedBack < 0) {
autoRefreshFeedBack = undefined;
}
if (this.autoRefresh !== this.innerOptions.autoRefresh || autoRefreshFeedBack) {
this.autoRefresh = autoRefreshFeedBack ? autoRefreshFeedBack : 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);
}
}
if (fadeOutAfter) {
if (this.timerFadeOut) {
window.clearInterval(this.timerFadeOut);
}
if (fadeOutAfter > 0) {
this.timerFadeOut = window.setInterval(() => {
this.hiddenByWs = true;
window.clearInterval(this.timerFadeOut);
}, fadeOutAfter * 1000);
}
}
requestAnimationFrame(() => {
var _a;
this.loaded = true;
this.showLoader = false;
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['exec', 'result'], this.chartTitle, this.result);
this.result = res.data;
this.execResult.emit(this.result);
this.hasError = false;
resolve(true);
});
}
})
.catch(e => {
this.displayError(e);
this.loaded = true;
this.showLoader = false;
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);
}
displayError(e) {
setTimeout(() => {
var _a, _b;
this.statusError.emit(e);
this.hasError = !!this.innerOptions.showErrors;
this.errorMessage = (_a = e.message) !== null && _a !== void 0 ? _a : e.statusText;
(_b = this.LOG) === null || _b === void 0 ? void 0 : _b.error(['exec'], e, this.innerOptions.showErrors, this.errorMessage);
});
}
render() {
return index.h("div", { key: 'd4ad0d939e766b6f493f228ab76021b1b3803ea7' }, this.loaded ?
index.h("div", { style: { width: '100%', height: '100%', display: 'flex', flexDirection: 'column' }, class: this.hiddenByWs ? 'hidden-by-ws' : '' }, this.hasError ? index.h("div", { class: "discovery-tile-error" }, this.errorMessage) : '', index.h("discovery-tile-result", { url: this.url, start: this.start, result: this.result, type: this.type, options: JSON.stringify(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), onExecError: (e) => this.displayError(e.detail), onDraw: () => this.draw.emit(), vars: JSON.stringify(this.innerVars), ref: (el) => this.tileResult = el, id: this.componentId, standalone: true }), this.statusMessage ? index.h("div", { class: "discovery-tile-status" }, this.statusMessage) : '')
: index.h("div", { class: "discovery-tile-spinner" }, this.showLoader ? index.h("discovery-spinner", { backdrop: true }, "Requesting data...") : ''), this.showLoader ? index.h("div", { class: "discovery-tile-spinner" }, index.h("discovery-spinner", { backdrop: true }, "Requesting data...")) : '', index.h("pre", { key: '891f9ed0225695209c49932fa3d57814ad86d8a9', id: "ws" }, index.h("slot", { key: '9d22b8f2afb4a28cc557da18dcf199d0170b20fb' })));
}
get el() { return index.getElement(this); }
static get watchers() { return {
"options": ["optionsUpdate"],
"vars": ["varsUpdate"]
}; }
};
DiscoveryTileComponent.style = DiscoveryTileStyle0;
exports.discovery_tile = DiscoveryTileComponent;
//# sourceMappingURL=discovery-tile.cjs.entry.js.map