@senx/discovery-plugin-marauder
Version:
Discovery plugin - Marauder's map
304 lines (299 loc) • 14.9 kB
JavaScript
import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { P as Param, l as lodash, U as Utils, L as Logger, G as GTSLib } from './utils.js';
import { d as domtoimage } from './dom-to-image.js';
/*
* Copyright 2022 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 LangUtils {
static prepare(ws, vars = {}, skipedVars, type, lang = 'warpscript') {
switch (lang) {
case 'flows':
return LangUtils.generateFlows(ws, vars, skipedVars, type);
case 'warpscript':
return LangUtils.generateWarpscript(ws, vars, skipedVars, type);
}
}
static generateFlowsVars(key, value) {
if (typeof value === 'string') {
return `${key} = "${value}"`;
}
else if (typeof value === 'number') {
return `${key} = "${value}"`;
}
else {
if (value.hasOwnProperty('type') && value.hasOwnProperty('value')) {
if (value.type === 'string') {
return `${key} = "${value.value}"`;
}
else {
return `${key} = ${value.value}`;
}
}
else {
return `${key} = JSON->('${encodeURIComponent(JSON.stringify(value))}')`;
}
}
}
static generateWarpscriptVars(key, value) {
if (typeof value === 'string') {
return `"${value}" "${key}" STORE`;
}
else if (typeof value === 'number') {
return `${value} "${key}" STORE`;
}
else {
if (value.hasOwnProperty('type') && value.hasOwnProperty('value')) {
if (value.type === 'string') {
return `"${value.value}" "${key}" STORE`;
}
else {
return `${value.value} "${key}" STORE`;
}
}
else {
return `
<'
${JSON.stringify(value)}
'>
JSON-> "${key}" STORE`;
}
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
static generateFlows(ws, vars, skipedVars, type) {
const varsStr = Object.keys(vars || {})
.filter(k => !(skipedVars || []).includes(k))
.map(k => LangUtils.generateFlowsVars(k, vars[k])).join('\n') + '\n';
return `<'
${varsStr}
${ws}
'>
FLOWS`;
}
// noinspection JSUnusedLocalSymbols
// eslint-disable-next-line @typescript-eslint/no-unused-vars
static generateWarpscript(ws, vars, skipedVars, type) {
const varsStr = Object.keys(vars || {})
.filter(k => !(skipedVars || []).includes(k))
.map(k => LangUtils.generateWarpscriptVars(k, vars[k])).join('\n') + '\n';
return `
${varsStr}
${ws}
`;
}
}
const discoveryButtonCss = "/*!\n * Copyright 2022 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%}:host .button-wrapper{width:100%;text-align:center;display:-ms-flexbox;display:flex;-ms-flex-pack:center;justify-content:center;-ms-flex-align:center;align-items:center;height:100%}:host .button-wrapper button.discovery-btn{margin:0.25rem 0.125rem;display:inline-block;font-weight:400;line-height:1.5;text-align:center;text-decoration:none;vertical-align:middle;cursor:pointer;font-size:var(--warp-view-button-font-size, 1rem);border:1px solid var(--warp-view-button-border-color, #004eff);padding:var(--warp-view-button-padding, 0.375rem 0.75rem);color:var(--warp-view-button-label-color, #ffffff);background-color:var(--warp-view-button-bg-color, #004eff);min-width:var(--warp-view-button-width, auto);border-radius:var(--warp-view-button-border-radius, 0.25rem)}:host .discovery-btn-group button.discovery-btn{margin:0.25rem 0;color:var(--warp-view-button-inactive-label-color, #004eff);background-color:var(--warp-view-button-inactive-bg-color, #ffffff)}:host .discovery-btn-group button.discovery-btn.active{color:var(--warp-view-button-label-color, #ffffff);background-color:var(--warp-view-button-bg-color, #004eff)}:host .discovery-btn-group button.discovery-btn:hover{color:var(--warp-view-button-label-color, #ffffff);background-color:var(--warp-view-button-bg-color, #004eff)}:host .discovery-btn-group button.discovery-btn:not(:last-child){border-top-right-radius:0;border-bottom-right-radius:0}:host .discovery-btn-group button.discovery-btn:not(:first-of-type){border-top-left-radius:0;border-bottom-left-radius:0}";
const DiscoveryButtonStyle0 = discoveryButtonCss;
const DiscoveryButtonComponent = /*@__PURE__*/ proxyCustomElement(class DiscoveryButtonComponent extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.draw = createEvent(this, "draw", 7);
this.execResult = createEvent(this, "execResult", 7);
this.statusError = createEvent(this, "statusError", 7);
this.discoveryEvent = createEvent(this, "discoveryEvent", 7);
this.defOptions = new Param();
this.innerVars = {};
this.result = undefined;
this.type = undefined;
this.options = new Param();
this.width = undefined;
this.height = undefined;
this.debug = false;
this.url = undefined;
this.language = 'warpscript';
this.vars = '{}';
this.parsing = false;
this.rendering = false;
this.label = 'Ok';
this.innerStyle = undefined;
this.active = undefined;
}
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;
this.parseResult();
}
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['varsUpdate'], { vars: this.vars, newValue, oldValue });
}
updateRes(newValue, oldValue) {
if (JSON.stringify(newValue) !== JSON.stringify(oldValue)) {
this.parseResult();
}
}
discoveryEventHandler(event) {
const res = Utils.parseEventData(event.detail, this.options.eventHandler, this.el.id);
if (res.style) {
this.innerStyle = Object.assign(Object.assign({}, this.innerStyle), res.style);
}
if (res.vars) {
this.innerVars = Object.assign(Object.assign({}, this.innerVars), res.vars);
}
}
async resize() {
// empty
}
// noinspection JSUnusedLocalSymbols
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async export(type = 'png') {
let bgColor = Utils.getCSSColor(this.el, '--warp-view-bg-color', 'transparent');
bgColor = (this.options || { bgColor }).bgColor || bgColor;
const dm = ((this.innerResult || {
globalParams: { bgColor },
}).globalParams || { bgColor });
bgColor = dm.bgColor || bgColor;
return await domtoimage.toPng(this.root, { height: this.height, width: this.width, bgcolor: bgColor });
}
componentWillLoad() {
var _a;
this.LOG = new Logger(DiscoveryButtonComponent, this.debug);
this.parsing = true;
if (typeof this.options === 'string') {
this.options = JSON.parse(this.options);
}
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['componentWillLoad'], {
type: this.type,
options: this.options,
});
this.parseResult();
this.draw.emit();
}
parseResult() {
this.innerResult = GTSLib.getData(this.result);
const btnLabel = (this.options.button || { label: 'Ok' }).label;
const dm = (this.innerResult || {
globalParams: {
button: { label: btnLabel },
},
}).globalParams || { button: { label: btnLabel } };
this.label = dm.button.label;
let options = Utils.mergeDeep(this.defOptions, this.options || {});
options = Utils.mergeDeep(options || {}, this.innerResult.globalParams);
this.options = Object.assign({}, options);
if (!!this.vars && typeof this.vars === 'string') {
this.innerVars = JSON.parse(this.vars);
}
else if (!!this.vars) {
this.innerVars = this.vars;
}
if (this.options.customStyles) {
this.innerStyle = Object.assign(Object.assign({}, this.innerStyle), this.options.customStyles || {});
}
setTimeout(() => { var _a, _b; return this.active = (_b = (((_a = this.innerResult) === null || _a === void 0 ? void 0 : _a.data) || []).find(v => v.active)) === null || _b === void 0 ? void 0 : _b.value; });
}
handleClick() {
var _a;
const ws = LangUtils.prepare(`${this.innerResult.data} EVAL`, this.innerVars || {}, ((_a = this.options) === null || _a === void 0 ? void 0 : _a.skippedVars) || [], this.type, this.language);
Utils.httpPost(this.url, ws, this.options.httpHeaders)
.then(res => {
var _a, _b;
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['handleClick', 'res.data'], res.data);
const result = GTSLib.getData(res.data);
(_b = this.LOG) === null || _b === void 0 ? void 0 : _b.debug(['handleClick', 'getData'], result);
if (!!result) {
(result.events || []).forEach(e => {
var _a;
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['handleClick', 'emit'], { discoveryEvent: e });
if (typeof e.value !== 'object' && GTSLib.isArray(e.value)) {
e.value = [e.value];
}
this.discoveryEvent.emit(Object.assign(Object.assign({}, e), { source: this.el.id }));
});
}
this.execResult.emit(res.data);
})
.catch(e => {
var _a;
this.statusError.emit(e);
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.error(['exec'], e);
});
}
toggle(value) {
this.active = value;
(this.innerResult.events || []).forEach(e => {
var _a;
(_a = this.LOG) === null || _a === void 0 ? void 0 : _a.debug(['handleClick', 'emit'], { discoveryEvent: e });
if (!e.value) {
e.value = {};
}
e.value[e.selector] = value;
this.discoveryEvent.emit(Object.assign(Object.assign({}, e), { source: this.el.id }));
});
}
generateStyle(innerStyle) {
return Object.keys(innerStyle || {}).map(k => k + ' { ' + innerStyle[k] + ' }').join('\n');
}
render() {
var _a, _b;
return [
h("style", { key: '1a76acc321cf715d56752d6761e4e63cf202546e' }, this.generateStyle(this.innerStyle)),
h("div", { key: 'c5fe4be86b662c8fa1676513cc1b2564ade91509', ref: el => this.root = el, class: "button-wrapper" }, this.type === 'button'
? h("button", { type: "button", class: "discovery-btn", innerHTML: this.label, onClick: () => this.handleClick() })
: '', this.type === 'button:radio'
? h("div", { class: "discovery-btn-group" }, GTSLib.isArray((_a = this.innerResult) === null || _a === void 0 ? void 0 : _a.data)
? (((_b = this.innerResult) === null || _b === void 0 ? void 0 : _b.data) || []).map(v => h("button", { type: "button", class: {
'discovery-btn': true,
'active': v.value === this.active,
}, innerHTML: v.label, onClick: () => this.toggle(v.value) })) : '') : ''),
];
}
get el() { return this; }
static get watchers() { return {
"vars": ["varsUpdate"],
"result": ["updateRes"]
}; }
static get style() { return DiscoveryButtonStyle0; }
}, [1, "discovery-button", {
"result": [1025],
"type": [1],
"options": [1025],
"width": [2],
"height": [2],
"debug": [4],
"url": [1],
"language": [1],
"vars": [1],
"parsing": [32],
"rendering": [32],
"label": [32],
"innerStyle": [32],
"active": [32],
"resize": [64],
"export": [64]
}, [[8, "discoveryEvent", "discoveryEventHandler"]], {
"vars": ["varsUpdate"],
"result": ["updateRes"]
}]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["discovery-button"];
components.forEach(tagName => { switch (tagName) {
case "discovery-button":
if (!customElements.get(tagName)) {
customElements.define(tagName, DiscoveryButtonComponent);
}
break;
} });
}
export { DiscoveryButtonComponent as D, LangUtils as L, defineCustomElement as d };
//# sourceMappingURL=discovery-button2.js.map