-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathform-client.js
270 lines (248 loc) · 7.61 KB
/
form-client.js
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
$(function () {
const event = new Event("form_loaded");
// global vars
var u = window.location.href;
var pos = u.substr(u.lastIndexOf("/") + 1);
// config vars
var timeoutID;
var timerHandle;
var wasDisconnected = false;
var usercanceled = false;
window.onbeforeunload=()=>{
if(activesocket && usercanceled === false)
triggerCancel()
}
// watch out in case the libraries don't load
if (pos == "config.html") {
if (typeof JSONForm !== "object") {
$("#outMsg").html(
"Unable to load Required Libraries <br> Please try again in a few moments"
);
showElm("#out", 1);
return false;
}
}
function triggerCancel(){
activesocket.emit("cancel");
}
function findGetParameter(parameterName) {
var result = null,
tmp = [];
var items = location.search.substr(1).split("&");
for (var index = 0; index < items.length; index++) {
tmp = items[index].split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
}
return result;
}
function parseData(data) {
return JSON.parse(
data,
function (key, value) {
if (typeof value === "string") {
// only handle specified fields in jsonform
switch (key) {
case "onChange":
case "onClick":
case "onKeyUp":
case "onInput":
// get the function from the string
// parens mean don't EXECUTE the function, just get its value
value = eval("(" + value + ")");
break;
// default:
// if ( (value.startsWith("(") || value.startsWith("function(")) && value.endsWith("}"))
// value = eval("(" + value + ")");
}
}
return value;
}
);
}
// socket
const activesocket = io("/mConfig" // server + ":" + port
, {
reconnectionDelayMax: 10000
});
activesocket.on("close", function(){
window.close()
})
// global socket events
activesocket.on("connected", function () {
switch (pos) {
case "config.html":
config_init();
break;
default:
index_init();
}
});
activesocket.on("disconnect", function () {
// don't know what to do on disconnect
$("#outmessage").html("<p><strong>MagicMirror is not running</strong></p>");
$("#result").html('<form id="result-form" class="form-vertical"></form>');
//hideElm('#submit_button')
clearTimeout(timerHandle)
window.close()
wasDisconnected = true;
if (timerHandle) clearTimeout(timerHandle);
});
// config socket events
activesocket.on("json", function (incoming_json) {
usercanceled= false
//data.configJSON = data
let data = parseData(incoming_json.slice(1, -1));
// free the memory
incoming_json = null;
let pairs = data.pairs;
let arrays = data.arrays;
let objects = data.objects;
let mangled_names = data.mangled_names;
let convertedObjects = data.convertedObjects;
let scriptConverted = data.scriptConvertedObjects
let substituted_variables=null
if(data.substituted_variables)
substituted_variables = data.substituted_variables
$("#outmessage").text("");
try {
data.onSubmitValid = function (values) {
// restore the fixup data from the incoming
values["pairs"] = pairs;
values["arrays"] = arrays;
values["objects"] = objects;
values["mangled_names"] = mangled_names;
values["convertedObjects"] = convertedObjects;
values["scriptConvertedObjects"]=scriptConverted
if(substituted_variables)
values["substituted_variables"] = substituted_variables
activesocket.emit("saveConfig", values);
$("#outmessage").html(
"<p><strong>Your Configuration has been submitted.</strong></p>"
);
};
data.onSubmit = function (errors, values) {
if (errors) {
console.log("Validation errors 1", errors, values);
let buildInner = "";
errors.forEach(function (errItem) {
let errSchemaUri = errItem.schemaUri
.replace(/.+\/properties\//, "")
.replace("/", " >> ");
buildInner +=
`<p><strong style="font-color:red">Error: ` +
errItem.message +
"</strong></br>Location: " +
errSchemaUri +
"</p>";
});
$("#outMsg").html(buildInner);
console.log("Validation errors 2", values);
return false;
}
return true;
};
// replace any form from last connection
$("#result").html('<form id="result-form" class="form-vertical"></form>');
// insert the new form
$("#result-form").jsonForm(data);
// trigger the custom event for any extension that needs to manipulate its part of the form
document.dispatchEvent(event)
// delete entry
$(
"fieldset.module_entry > div > div > div > div > ul ~ span > ._jsonform-array-deletelast "
).click((event) => {
let m = $(event.target)
.closest(".module_entry")
.children("legend")
.text();
$("[value='" + m + "']")
.closest(".possibly-hidden-tab")
.find("._jsonform-array-deleteitem")
.trigger("click");
});
// add entry
$(
"fieldset.module_entry > div > div > div > div > ul ~ span > ._jsonform-array-addmore "
).click((event) => {
let m = $(event.target)
.closest(".module_entry")
.children("legend")
.text();
$("[value='" + m + "']")
.closest(".possibly-hidden-tab")
.find("._jsonform-array-addmore")
.trigger("click");
});
} catch (e) {
$("#result").html(
"<pre>Entered content is not yet a valid" +
" JSON Form object.\n\nThe JSON Form library returned:\n" +
e.stack +
"</pre>"
);
console.error("error stack", e.stack);
return;
}
});
activesocket.on("saved", function (msg) {
if (!msg) {
msg =
"I Could not save your configuration. Don't give me that look, I'm just as sad about it as you are.";
}
if (msg.includes("successfully")) {
timer_handle = setTimeout(() => {
config_init();
timerHandle = null;
}, 10000);
}
$("#outmessage").html("<p><strong>" + msg + "</strong></p>");
});
/*
______ _ _
| ____| | | (_)
| |__ _ _ _ __ ___| |_ _ ___ _ __ ___
| __| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
| | | |_| | | | | (__| |_| | (_) | | | \__ \
|_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
*/
// global functions
function isIosDevice() {
var iosDeviceList = [
"iPhone",
"iPod",
"iPad",
"iPhone Simulator",
"iPod Simulator",
"iPad Simulator",
"Pike v7.6 release 92",
"Pike v7.8 release 517"
];
return iosDeviceList.some(function (device) {
return device == navigator.platform;
});
}
function index_init() {
if (isIosDevice()) {
$speak.addClass("hidden");
$nospeak.removeClass("hidden");
}
}
function config_init() {
if (wasDisconnected) window.location.reload(true);
else {
activesocket.emit("getForm", true);
$("#outmessage").html("<div></div>");
}
}
// config functions
function hideElm(element) {
$(element).fadeOut("fast");
}
function showElm(element, timeOutMins = 1) {
var timeOutMillis = timeOutMins * 60000;
$(element).fadeIn();
timeoutID = setTimeout(function () {
hideElm(element);
}, timeOutMillis);
}
});