forked from genieacs/genieacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui-config-component.ts
148 lines (130 loc) · 4.33 KB
/
ui-config-component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Copyright 2013-2019 GenieACS Inc.
*
* This file is part of GenieACS.
*
* GenieACS is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* GenieACS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
import { ClosureComponent, Component } from "mithril";
import { m } from "./components";
import * as store from "./store";
import { yaml } from "./dynamic-loader";
import * as configFunctions from "./config-functions";
import codeEditorComponent from "./code-editor-component";
import { parse } from "../lib/common/expression-parser";
function putActionHandler(prefix: string[], dataYaml: string): Promise<any> {
return new Promise((resolve, reject) => {
try {
let updated = yaml.parse(dataYaml, { schema: "failsafe" });
if (updated) {
const config = {};
let ref = config;
prefix.forEach((seg, index) => {
if (index < prefix.length - 1) {
ref[seg] = {};
ref = ref[seg];
} else {
ref[seg] = updated;
}
});
updated = configFunctions.flattenConfig(config);
} else {
updated = {};
}
// Try parse to ensure valid expressions
for (const v of Object.values(updated)) parse(v as string);
store
.queryConfig(`${prefix.join(".")}.%`)
.then((res) => {
const current = {};
for (const f of res) current[f._id] = f.value;
const diff = configFunctions.diffConfig(current, updated);
if (!diff.add.length && !diff.remove.length) return void resolve();
const promises = [];
for (const obj of diff.add) {
promises.push(
store.putResource(
"config",
obj._id,
(obj as unknown) as Record<string, unknown>
)
);
}
for (const id of diff.remove)
promises.push(store.deleteResource("config", id));
Promise.all(promises)
.then(() => {
resolve();
})
.catch(reject);
})
.catch(reject);
} catch (error) {
resolve({ config: error.message });
}
});
}
const component: ClosureComponent = (): Component => {
return {
view: (vnode) => {
const prefix = vnode.attrs["prefix"].split(".");
const name = vnode.attrs["name"];
const data = vnode.attrs["data"];
if (prefix[prefix.length - 1] === "") prefix.pop();
let config;
if (data.length) {
config = configFunctions.structureConfig(data);
for (const seg of prefix) config = config[seg];
}
const yamlString =
config && Object.values(config).length
? yaml.stringify(config, { schema: "failsafe" })
: "";
const attrs = {
id: `${name}-ui-config`,
value: yamlString,
mode: "yaml",
focus: true,
onSubmit: (dom) => {
dom.form.querySelector("button[type=submit]").click();
},
onChange: (value) => {
vnode.state["updatedYaml"] = value;
vnode.state["modified"] = true;
},
};
const code = m(codeEditorComponent, attrs);
const submit = m("button.primary", { type: "submit" }, "Save");
return m("div.put-form", [
m("h1", `Editing ${name}`),
m(
"form",
{
onsubmit: (e) => {
e.redraw = false;
e.preventDefault();
if (vnode.state["updatedYaml"] == null)
vnode.state["updatedYaml"] = yamlString;
putActionHandler(prefix, vnode.state["updatedYaml"])
.then(vnode.attrs["onUpdate"])
.catch(vnode.attrs["onError"]);
},
},
[code, m(".actions-bar", [submit])]
),
]);
},
};
};
export default component;