import { HTMLWidget, Meta, Widget } from "@hpcc-js/common";
import * as marked from "marked";
export class PublishedProperties extends HTMLWidget {
constructor() {
super();
}
infostring(): string {
return this.data()[0][0];
}
text(): string {
return this.data()[0][1];
}
parseClassID(classID: string): [string, string] {
const [moduleName, className] = classID.split("_");
return [`@hpcc-js/${moduleName}`, className];
}
extends(w: Widget): [string, string] {
const classParts = w.class().split(" ");
classParts.pop();
return this.parseClassID(classParts.pop());
}
update(domNode, element) {
super.update(domNode, element);
const [module, widget] = this.infostring().split(":");
import(module).then(mod => {
const md: string[] = [];
const w = new mod[widget]();
const derivedFrom = this.extends(w);
md.push(`Derived from: ${derivedFrom[1]} (${derivedFrom[0]})\n`);
md.push("Published properties act as both getter and setter methods. If a value is passed as an argument the property will be set and the widgets' context will be returned allowing these properties to be chained as seen in the above code.\n");
const pp: Meta[] = w.publishedProperties(false, true);
md.push("|Property|Type|Optional?|Default|Description|");
md.push("|--------|----|---------|-------|-----------|");
pp.forEach(meta => {
let setStr = "";
if (meta.type === "set") {
try {
const set = typeof meta.set === "function" ? meta.set() : meta.set;
setStr = `
_**Potential values include:**_ "${set.join('", "')}"`;
} catch (e) {
setStr = "
_**Potential values include:**_ generated by a runtime function";
}
}
let defaultValue = meta.defaultValue;
if (typeof defaultValue === "object" ) {
try {
defaultValue = JSON.stringify(defaultValue);
} catch (e) {
// console.log(e);
}
}
md.push(`|${meta.id}|${meta.type}|${!!meta.ext.optional}|${defaultValue}|${meta.description}${setStr}|`);
});
element.html(marked(md.join("\n")));
});
}
}
PublishedProperties.prototype._class = "";