UNPKG

@senx/discovery-plugin-marauder

Version:

Discovery plugin - Marauder's map

345 lines (340 loc) 13.7 kB
'use strict'; const lodash = require('./lodash-d2ae8efc.js'); /* * Copyright 2022-2024 SenX S.A.S. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ class Param { constructor() { this.scheme = 'WARP10'; this.datasetNoAlpha = false; this.timeZone = 'UTC'; this.showRangeSelector = false; this.showDots = false; this.timeUnit = 'us'; this.hideYAxis = false; this.hideXAxis = false; this.showLegend = false; this.showValues = false; this.fullDateDisplay = false; this.showControls = false; this.expandAnnotation = false; this.displayExpander = true; this.leftMargin = 0; this.showLoader = false; this.noDataLabel = 'No data'; this.poiLine = 'dotted'; this.dotSize = 5; this.strokeWidth = 1; } } /* * Copyright 2022-2024 SenX S.A.S. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ class Utils { static clone(inObject) { return lodash.lodash.exports.cloneDeep(inObject); } static throttle(func, delay, ctx) { let isRunning; return (...args) => { // eslint-disable-next-line @typescript-eslint/no-this-alias const context = ctx || this; // store the context of the object that owns this function if (!isRunning) { isRunning = true; func.apply(context, args); // execute the function with the context of the object that owns it setTimeout(() => isRunning = false, delay); } }; } static httpPost(theUrl, payload, headers) { return new Promise((resolve, reject) => { const xmlHttp = new XMLHttpRequest(); const resHeaders = {}; xmlHttp.onreadystatechange = () => { xmlHttp.getAllResponseHeaders().split('\n').forEach(header => { if (header.trim() !== '') { const h = header.split(':'); resHeaders[h[0].trim()] = h[1].trim().replace('\r', ''); } }); if (xmlHttp.readyState === 4 && xmlHttp.status === 200) { resolve({ data: xmlHttp.responseText, headers: resHeaders, status: { ops: parseInt(resHeaders['x-warp10-ops'], 10), elapsed: parseInt(resHeaders['x-warp10-elapsed'], 10), fetched: parseInt(resHeaders['x-warp10-fetched'], 10), }, }); } else if (xmlHttp.readyState === 4 && xmlHttp.status >= 400) { reject({ statusText: xmlHttp.statusText, status: xmlHttp.status, url: theUrl, headers: resHeaders, message: `line #${resHeaders['x-warp10-error-line']}: ${resHeaders['x-warp10-error-message']}`, detail: { mess: resHeaders['x-warp10-error-message'], line: resHeaders['x-warp10-error-line'], }, }); } else if (xmlHttp.readyState === 4 && xmlHttp.status === 0) { reject({ statusText: theUrl + ' is unreachable', status: 404, url: theUrl, headers: resHeaders, message: theUrl + ' is unreachable', detail: { mess: theUrl + ' is unreachable', line: -1 }, }); } }; xmlHttp.open('POST', theUrl, true); // true for asynchronous xmlHttp.setRequestHeader('Content-Type', 'text/plain; charset=utf-8'); Object.keys(headers || {}) .filter(h => h.toLowerCase() !== 'accept' && h.toLowerCase() !== 'content-type') .forEach(h => xmlHttp.setRequestHeader(h, headers[h])); xmlHttp.send(payload); }); } static merge(options, options2) { if (typeof options === 'string') { options = JSON.parse(options); } return Object.assign(Object.assign(Object.assign({}, new Param()), options), options2); } static sanitize(data) { if (typeof data === 'string') return '["' + data + '"]'; else return data; } static mergeDeep(base, ext) { const obj = Object.assign({}, base); const extended = Object.assign({}, ext); for (const prop in extended || {}) { // If property is an object, merge properties if (Object.prototype.toString.call(extended[prop]) === '[object Object]') { obj[prop] = Utils.mergeDeep(obj[prop], extended[prop]); } else { obj[prop] = extended[prop]; } } return obj; } static getLabelColor(el) { return Utils.getCSSColor(el, '--warp-view-chart-label-color', '#8e8e8e').trim(); } static getGridColor(el) { return Utils.getCSSColor(el, '--warp-view-chart-grid-color', '#8e8e8e').trim(); } static getCSSColor(el, property, defColor) { const color = getComputedStyle(el).getPropertyValue(property).trim(); return color === '' ? defColor : color; } static getContentBounds(el) { return { h: el.clientHeight - parseInt(getComputedStyle(el, null).getPropertyValue('padding-top'), 10) - parseInt(getComputedStyle(el, null).getPropertyValue('padding-bottom'), 10), w: el.clientWidth - parseInt(getComputedStyle(el, null).getPropertyValue('padding-left'), 10) - parseInt(getComputedStyle(el, null).getPropertyValue('padding-right'), 10) }; } static unsescape(str) { return str.replace(/&gt;/g, '>') .replace(/&lt;/g, '<') .replace(/&quot;/g, '"') .replace(/&apos;/g, '\'') .replace(/&amp;/g, '&'); } static parseEventData(evt, eventHandler, id) { const parsed = { style: undefined, data: undefined, xpath: undefined, popup: undefined, vars: undefined, audio: undefined, zoom: undefined, focus: undefined, margin: undefined, bounds: undefined, title: undefined, description: undefined, selected: undefined, link: undefined, hasEvent: false, poi: [] }; if (eventHandler && evt.source !== id) { let tag = '.*'; let type = '.*'; eventHandler.split(',').forEach(eh => { if (eh.startsWith('tag')) { tag = eh.split('=')[1]; } if (eh.startsWith('type')) { type = eh.split('=')[1]; } }); const tagRex = new RegExp('^' + tag + '$'); if (evt.tags && typeof evt.tags === 'string') { evt.tags = [evt.tags]; } if ((evt.tags || []).some(t => tagRex.test(t)) && new RegExp(type).test(evt.type || '')) { switch (evt.type) { case 'data': parsed.data = lodash.GTSLib.getData(evt.value); parsed.hasEvent = true; break; case 'style': // map css selector -> content parsed.style = evt.value; parsed.hasEvent = true; break; case 'xpath': parsed.xpath = { selector: evt.selector, value: evt.value }; parsed.hasEvent = true; break; case 'popup': parsed.popup = evt.value; parsed.hasEvent = true; break; case 'variable': parsed.vars = evt.value; parsed.hasEvent = true; break; case 'audio': parsed.audio = evt.value; parsed.hasEvent = true; break; case 'zoom': parsed.zoom = evt.value; if (evt.selector) { const v = {}; v[evt.selector] = evt.value; parsed.vars = v; } parsed.hasEvent = true; break; case 'focus': parsed.focus = evt.value; if (evt.selector) { const v = {}; v[evt.selector] = evt.value; parsed.vars = v; } parsed.hasEvent = true; break; case 'margin': parsed.margin = evt.value; parsed.hasEvent = true; break; case 'bounds': parsed.bounds = evt.value; if (evt.selector) { const v = {}; v[evt.selector] = evt.value; parsed.vars = v; } parsed.hasEvent = true; break; case 'title': parsed.title = evt.value; parsed.hasEvent = true; break; case 'description': parsed.description = evt.value; parsed.hasEvent = true; break; case 'link': parsed.link = typeof evt.value === 'string' ? { link: evt.value, target: 'self' } : Object.assign({}, evt.value); parsed.hasEvent = true; break; case 'selected': parsed.selected = parsed.selected || {}; parsed.selected[evt.selector] = evt.value; parsed.hasEvent = true; break; case 'poi': parsed.poi = evt.value; if (evt.selector) { const v = {}; v[evt.selector] = evt.value; parsed.vars = v; } parsed.hasEvent = true; break; // nothing } } } return parsed; } static parseXML(xmlString, contentType) { const parser = new DOMParser(); // Parse a simple Invalid XML source to get namespace of <parsererror>: const docError = parser.parseFromString('INVALID', contentType); const parsererrorNS = docError.getElementsByTagName('parsererror')[0].namespaceURI; // Parse xmlString: // (XMLDocument object) const doc = parser.parseFromString(xmlString, contentType); if (doc.getElementsByTagNameNS(parsererrorNS, 'parsererror').length > 0) { throw new Error('Error parsing XML'); } return doc; } /** * Compute the backend url if it is a relative one * * @param url */ static getUrl(url) { if (!url.toLowerCase().startsWith('http') && !url.toLowerCase().startsWith('ws')) { const { host, pathname, port, protocol, search } = window.location; let urlComputed = protocol + '//' + host + (port !== '' ? ':' + port : ''); urlComputed += url.startsWith('/') ? url : pathname + (pathname.endsWith('/') ? '' : '/') + url; return urlComputed + search; } else { return url; } } } exports.Param = Param; exports.Utils = Utils; //# sourceMappingURL=utils-0e400704.js.map