-
-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathimportHelper.js
More file actions
425 lines (408 loc) · 15.8 KB
/
Copy pathimportHelper.js
File metadata and controls
425 lines (408 loc) · 15.8 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
importHelper.js - ESP3D WebUI helper file
Copyright (c) 2021 Alexandre Aussourd. All rights reserved.
Modified by Luc LEBOSSE 2021
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Formats an item data object.
*
* @param {Object} itemData - The item data object to format.
* @param {number} [index=-1] - The index of the item.
* @param {string} [origineId="extrapanels"] - The origin ID of the item.
* @returns {Object} The formatted item object.
*/
function formatItem(itemData, index = -1, origineId = "extrapanels") {
const itemFormated = {}
itemFormated.id = itemData.id
//to track any change in order
itemFormated.index = index
itemFormated.value = []
Object.keys(itemData).forEach((key) => {
if (key != "id") {
const newItem = {}
newItem.id = itemData.id + "-" + key
newItem.name = key
newItem.value = itemData[key]
newItem.initial = itemData[key]
if (index == -1) newItem.newItem = true
switch (key) {
case "cmds":
newItem.type = "text"
newItem.label = "S115"
newItem.help = "S97"
break
case "key":
newItem.type = "text"
newItem.label = "key"
newItem.shortkey = true
break
case "name":
newItem.type = "text"
newItem.label = "S129"
break
case "icon":
newItem.type = "icon"
newItem.label = "S132"
break
case "refreshtime":
newItem.type = "number"
newItem.min = 0
newItem.minsecondary = 100
newItem.step = 100
newItem.append = "S114"
newItem.label = "S113"
break
case "type":
newItem.type = "select"
newItem.label = "S135"
if (origineId == "verbosefilters") {
newItem.options = [
{ label: "S229", value: "startswith" },
{ label: "S230", value: "endswith" },
{ label: "S231", value: "contain" },
{ label: "S232", value: "regex" },
]
} else if (origineId == "macros") {
newItem.options = [
{
label: "S137",
value: "FS",
depend: [
{
id: "flashfs",
value: true,
},
{
connection_id: "FlashFileSystem",
value: "!='none'",
},
{
connection_id: "FWTargetID",
value: "!='30'",
},
],
},
{
label: "S138",
value: "SD",
depend: [
{
ids: [
{
id: "sd",
value: true,
},
{
id: "directsd",
value: true,
},
{
id: "ext",
value: true,
},
{
id: "directsdext",
value: true,
},
{
id: "tftsd",
value: true,
},
],
},
{
connection_id: "SDConnection",
value: "!='none'",
},
],
},
{ label: "S139", value: "URI" },
{ label: "S140", value: "CMD" },
]
} else {
newItem.options = [
{ label: "S160", value: "image" },
{ label: "S161", value: "content" },
{ label: "S121", value: "extension" },
{ label: "S162", value: "camera" },
]
}
break
case "target":
newItem.type = "select"
newItem.label = "S136"
newItem.options = [
{ label: "S158", value: "page" },
{ label: "S157", value: "panel" },
]
break
case "openonstart":
newItem.type = "boolean"
newItem.label = "S93"
break
case "source":
newItem.type = "text"
newItem.label = "S139"
newItem.min = "2"
break
case "action":
newItem.type = "text"
newItem.label = "S159"
newItem.min = "1"
// The action field is G-code for CMD macros; keep all macro actions editable
// as a textarea so switching the macro type does not require a reload.
if (origineId == "macros") {
newItem.multiline = true
newItem.rows = 8
}
break
case "value":
newItem.type = "text"
newItem.label = origineId == "verbosefilters" ? "S233" : key
break
default:
newItem.type = "text"
newItem.label = key
}
itemFormated.value.push(newItem)
}
})
// For extracontents items saved before openonstart was introduced, inject the default
if (origineId === "extracontents" && !("openonstart" in itemData)) {
itemFormated.value.push({
id: itemData.id + "-openonstart",
name: "openonstart",
value: true,
initial: true,
type: "boolean",
label: "S93",
})
}
return itemFormated
}
/**
* If the list item is in "formatted" form (value is array of { name, value }), flatten it
* so formatItem always receives flat shape { id, type, value, ... }. Avoids double-nesting
* and "[object Object]" when preferences are reloaded after save.
*/
function normalizeListItem(item) {
if (!item || typeof item !== "object") return item
const val = item.value
if (!Array.isArray(val) || val.length === 0) return item
const first = val[0]
if (typeof first !== "object" || first.name === undefined) return item
const flat = { id: item.id }
if (item.index !== undefined) flat.index = item.index
val.forEach((s) => {
flat[s.name] = s.value
})
return flat
}
/**
* Formats the items list.
*
* @param {Array} itemsList - The list of items to be formatted.
* @param {string} origineId - The origin ID.
* @returns {Array} - The formatted items list.
*/
function formatItemsList(itemsList, origineId) {
const formatedItems = []
itemsList.forEach((element, index) => {
let el = normalizeListItem(element)
if (origineId === "verbosefilters" && (el.id == null || el.id === "")) {
el = { ...el, id: (el.type != null ? String(el.type) : "filter") + "-" + index }
}
formatedItems.push(formatItem(el, index, origineId))
})
return formatedItems
}
/**
* Formats the preferences section to add inital value when missing
*
* @param {Object} section - The preferences section to format.
* @returns {Object} - The formatted settings.
*/
function formatPreferences(section) {
function formatSection(arr) {
if (!Array.isArray(arr)) return
for (let index = 0; index < arr.length; index++) {
const el = arr[index]
if (el.type == "group") {
if (Array.isArray(el.value)) {
el.value.forEach((element) => {
element.initial = element.value
})
// Also format list items inside the group
el.value.forEach((element) => {
if (element.type == "list" && Array.isArray(element.value)) {
element.nb = element.value.length
element.value = formatItemsList(
[...element.value],
element.id
)
}
})
}
} else if (el.type == "list") {
el.nb = el.value.length
el.value = formatItemsList([...el.value], el.id)
} else {
el.initial = el.value
}
}
}
for (let key in section) {
if (Array.isArray(section[key])) {
formatSection(section[key])
}
}
return section
}
/**
* Imports preferences into the current preferences data.
* @param {Object} currentPreferencesData - The current preferences data section.
* @param {Object} importedPreferences - The preferences to be imported section.
* @returns {Array} An array containing a copy of preferences data section with imported data and a flag indicating if there were any errors during the import.
*/
function importPreferencesSection(currentPreferencesData, importedPreferences) {
let hasErrors = false;
const currentPreferences = JSON.parse(JSON.stringify(currentPreferencesData));
function updateElement(id, value) {
function traverse(obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (obj[key] && typeof obj[key] === 'object') {
if (obj[key].id === id) {
obj[key].value = value;
return true;
}
if (traverse(obj[key])) {
return true;
}
}
}
}
return false;
}
return traverse(currentPreferences);
}
if (importedPreferences) {
for (let key in importedPreferences) {
if (!updateElement(key, importedPreferences[key])) {
hasErrors = true;
console.log("Error with ", key);
}
}
}
return [currentPreferences, hasErrors];
}
/**
* Remove from panelsorder any entry whose name value is extracontents_<id>
* when <id> is not in the current extracontents list (panel target).
* Mutates settings in place.
* @param {Object} settings - preferences settings object (keyed by section, values are arrays of elements)
*/
function prunePanelsOrderOrphans(settings) {
if (!settings || typeof settings !== "object") return
let panelsOrderEl = null
let extraContentsEl = null
for (const key of Object.keys(settings)) {
const arr = settings[key]
if (!Array.isArray(arr)) continue
for (let i = 0; i < arr.length; i++) {
if (arr[i].id === "panelsorder") panelsOrderEl = arr[i]
if (arr[i].id === "extracontents") extraContentsEl = arr[i]
}
if (panelsOrderEl && extraContentsEl) break
}
if (!panelsOrderEl || !Array.isArray(panelsOrderEl.value)) return
const panelIds =
extraContentsEl && Array.isArray(extraContentsEl.value)
? extraContentsEl.value
.filter((item) => {
const targetField =
item.value &&
item.value.find((s) => s && s.name === "target")
return (
targetField &&
(targetField.value === "panel" ||
targetField.initial === "panel")
)
})
.map((item) => item.id)
: []
let arr = panelsOrderEl.value
const filtered = arr.filter((item) => {
const nameVal =
item.value && item.value.find((s) => s && s.name === "name")?.value
if (!nameVal || !String(nameVal).startsWith("extracontents_"))
return true
const rootId = String(nameVal).replace("extracontents_", "")
return panelIds.includes(rootId)
})
if (filtered.length < arr.length) {
panelsOrderEl.value = filtered
panelsOrderEl.nb = filtered.length
arr = filtered
arr.forEach((item, i) => {
item.index = i
if (item.value) {
const idxField = item.value.find(
(s) => s && s.name === "index"
)
if (idxField) idxField.value = i
}
})
}
const existingExtraIds = new Set(
arr
.map((item) => {
const n =
item.value &&
item.value.find((s) => s && s.name === "name")?.value
if (!n || !String(n).startsWith("extracontents_")) return null
return String(n).replace("extracontents_", "")
})
.filter(Boolean)
)
const missingIds = panelIds.filter((id) => !existingExtraIds.has(id))
if (missingIds.length > 0) {
const newItems = missingIds.map((id, i) => ({
id: "extracontents_" + id,
value: [
{ name: "name", value: "extracontents_" + id },
{ name: "index", value: arr.length + i },
],
index: arr.length + i,
}))
const newArray = [...arr, ...newItems]
newArray.forEach((item, i) => {
item.index = i
if (item.value) {
const idxField = item.value.find(
(s) => s && s.name === "index"
)
if (idxField) idxField.value = i
}
})
panelsOrderEl.value = newArray
panelsOrderEl.nb = newArray.length
}
}
export {
importPreferencesSection,
formatPreferences,
formatItem,
prunePanelsOrderOrphans,
}