web: fix saving for CodeMirror not returning an object

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-03-30 18:20:48 +02:00
parent 58a7d67922
commit 9c65fd814b
4 changed files with 36 additions and 12 deletions

View file

@ -116,9 +116,7 @@ class PropertyMappingViewSet(
if not users.exists(): if not users.exists():
raise PermissionDenied() raise PermissionDenied()
response_data = { response_data = {"successful": True}
"successful": True
}
try: try:
result = mapping.evaluate( result = mapping.evaluate(
users.first(), users.first(),

View file

@ -10,6 +10,7 @@ import "codemirror/mode/python/python.js";
import CodeMirrorStyle from "codemirror/lib/codemirror.css"; import CodeMirrorStyle from "codemirror/lib/codemirror.css";
import CodeMirrorTheme from "codemirror/theme/monokai.css"; import CodeMirrorTheme from "codemirror/theme/monokai.css";
import { ifDefined } from "lit-html/directives/if-defined"; import { ifDefined } from "lit-html/directives/if-defined";
import YAML from "yaml";
@customElement("ak-codemirror") @customElement("ak-codemirror")
export class CodeMirrorTextarea extends LitElement { export class CodeMirrorTextarea extends LitElement {
@ -22,11 +23,37 @@ export class CodeMirrorTextarea extends LitElement {
@property() @property()
name?: string; name?: string;
@property()
value?: string;
editor?: CodeMirror.EditorFromTextArea; editor?: CodeMirror.EditorFromTextArea;
private _value?: string;
@property()
set value(v: string) {
this._value = v.toString();
}
get value(): string {
switch (this.mode.toLowerCase()) {
case "yaml":
return YAML.parse(this.getInnerValue());
case "javascript":
return JSON.parse(this.getInnerValue());
default:
return this.getInnerValue();
}
}
private getInnerValue(): string {
if (!this.shadowRoot) {
return "";
}
const ta = this.shadowRoot?.querySelector("textarea");
if (!ta) {
return "";
}
return ta.value;
}
static get styles(): CSSResult[] { static get styles(): CSSResult[] {
return [PFForm, CodeMirrorStyle, CodeMirrorTheme]; return [PFForm, CodeMirrorStyle, CodeMirrorTheme];
} }
@ -49,6 +76,6 @@ export class CodeMirrorTextarea extends LitElement {
} }
render(): TemplateResult { render(): TemplateResult {
return html`<textarea class="pf-c-form-control" name=${ifDefined(this.name)}>${this.value || ""}</textarea>`; return html`<textarea class="pf-c-form-control" name=${ifDefined(this.name)}>${this._value || ""}</textarea>`;
} }
} }

View file

@ -78,10 +78,9 @@ export class Form<T> extends LitElement {
} }
serializeForm(form: IronFormElement): T { serializeForm(form: IronFormElement): T {
const elements = form._getSubmittableElements(); const elements: HTMLInputElement[] = form._getSubmittableElements();
const json: { [key: string]: unknown } = {}; const json: { [key: string]: unknown } = {};
for (let i = 0; i < elements.length; i++) { elements.forEach(element => {
const element = elements[i] as HTMLInputElement;
const values = form._serializeElementValues(element); const values = form._serializeElementValues(element);
if (element.tagName.toLowerCase() === "select" && "multiple" in element.attributes) { if (element.tagName.toLowerCase() === "select" && "multiple" in element.attributes) {
json[element.name] = values; json[element.name] = values;
@ -90,7 +89,7 @@ export class Form<T> extends LitElement {
form._addSerializedElement(json, element.name, values[v]); form._addSerializedElement(json, element.name, values[v]);
} }
} }
} });
return json as unknown as T; return json as unknown as T;
} }

View file

@ -34,7 +34,7 @@ export class PolicyTestForm extends Form<PolicyTest> {
return html`<ak-form-element-horizontal return html`<ak-form-element-horizontal
label=${gettext("Result")}> label=${gettext("Result")}>
${this.result?.successful ? ${this.result?.successful ?
html`<ak-codemirror mode="javascript" ?readOnly=${true} value="${this.result?.result}"> html`<ak-codemirror mode="javascript" ?readOnly=${true} value="${ifDefined(this.result?.result)}">
</ak-codemirror>`: </ak-codemirror>`:
html` html`
<div class="pf-c-form__group-label"> <div class="pf-c-form__group-label">