-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathconfig.js
425 lines (367 loc) · 17 KB
/
config.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
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
var config = {
initialized: false,
name: "",
db: {},
app: {},
feeds: {},
feedsbyid: {},
feedsbyname: {},
init: function() {
// console.log("CONFIG: Init");
for (var z in config.feeds) config.feedsbyname[config.feeds[z].name] = config.feeds[z];
for (var z in config.feeds) config.feedsbyid[config.feeds[z].id] = config.feeds[z];
// Check that the config is complete first otherwise show config interface
if (!config.check()) {
config.showConfig(); // Show setup block
config.UI(); // Populate setup UI options
$(".ajax-loader").hide(); // Hide AJAX loader
} else {
$("#app-block").show(); // Show app block
$(".ajax-loader").show(); // Show AJAX loader
config.load(); // Merge db config into app config
config.initapp();
config.initialized = true; // Init app
config.showapp();
}
$("body").on("click", ".openconfig", function(event){
config.showConfig();
config.UI();
});
// don't save and just show app
$("body").on("click",".close-config", function(event){
config.closeConfig();
});
// save and show app
$("body").on("click", ".launchapp", function(){
$(".ajax-loader").show();
config.closeConfig();
config.load();
if (!config.initialized) {
config.initapp();
config.initialized = true;
}
config.showapp();
});
$("body").on("click",".deleteapp",function(){
console.log("delete: "+config.name);
$.ajax({
url: path+"app/remove",
data: "name="+config.name,
dataType: 'text',
async: false,
success: function(result){
try {
result = JSON.parse(result);
if (result.success!=undefined && !result.success) app_log("ERROR",result.message);
window.location = path+"app/view";
} catch (e) {
app.log("ERROR","Could not parse /setconfig reply, error: "+e);
}
}
});
});
},
/**
* hide the app config window and show the app.
* enable the buttons in the app header
*/
closeConfig: function () {
$("#app-block").toggleClass('hide', false).show();
$("#app-setup").toggleClass('hide', true);
$('.openconfig').toggleClass('hide', false);
$('.close-config').toggleClass('hide', true);
$('#buttons #tabs .btn').attr('disabled',false).css('opacity',1);
// allow app to react to closing config window
$('body').trigger('config.closed')
},
/**
* hide the app window and show the config window.
* disable the buttons in the app header
*/
showConfig: function () {
$("#app-block").toggleClass('hide', true);
$("#app-setup").toggleClass('hide', false).show();
$('.openconfig').toggleClass('hide', true);
$('.close-config').toggleClass('hide', false);
$('#buttons #tabs .btn').attr('disabled',true).css('opacity',.2);
},
UI: function() {
$(".app-config").html("");
$("body").css('background-color','#222');
$("#footer").css('background-color','#181818');
$("#footer").css('color','#999');
// Remove old config items that are no longer used/described in new config definition
if (config.db.constructor===Array) config.db = {};
for (var z in config.db) {
if (config.db[z]==undefined) delete config.db[z];
}
// Draw the config interface from the config object:
var out = "";
for (var z in config.app) {
out += "<div class='appconfig-box' key='"+z+"'>";
if (config.app[z].type=="feed") {
out += "<i class='status icon-ok-sign icon-white'></i> <b class='feedname' key='"+z+"'>"+config.app[z].autoname+" <span style='color:#ccc; font-size:12px'>[AUTO]</span></b><i class='appconfig-edit icon-pencil icon-white' style='float:right; cursor:pointer'></i>";
out += "<br><span class='appconfig-info'></span>";
out += "<div class='feed-select-div input-append'><select class='feed-select'></select><button class='btn feed-select-ok'>ok</button></div>";
} else if (config.app[z].type=="value") {
out += "<i class='status icon-ok-sign icon-white'></i> <b>"+config.app[z].name+"</b>";
out += "<br><span class='appconfig-info'></span>";
out += "<input class='appconfig-value' type='text' key='"+z+"' value='"+config.app[z].default+"' / >";
} else if (config.app[z].type=="checkbox") {
out += "<i class='status icon-ok-sign icon-white'></i> <b>"+config.app[z].name+"</b>";
out += "<br><span class='appconfig-info'></span>";
var checked = ""; if (config.app[z].default) checked = "checked";
out += " <input class='appconfig-value' type='checkbox' key='"+z+"' "+checked+" / >";
} else if (config.app[z].type=="select") {
out += "<i class='status icon-ok-sign icon-white'></i> <b>"+config.app[z].name+"</b>";
out += "<select class='appconfig-value' key='"+z+"' style='margin-top:5px; width:100%'>";
for (var o in config.app[z].options) {
out += "<option>"+config.app[z].options[o]+"</option>";
}
out += "</select>";
}
out += "</div>";
}
out += "<br><div style='text-align:center;'><button class='btn launchapp' style='padding:10px; display:none'>Launch App</button><button class='btn btn-danger deleteapp' style='padding:10px; margin-left:20px'><i class='icon-trash icon-white'></i> Delete</button></div>";
$(".app-config").html(out);
for (var z in config.app) {
var configItem = $(".appconfig-box[key="+z+"]");
if (config.app[z].type=="feed") {
// Create list of feeds that satisfy engine requirement
var out = "<option value=0>Select "+z+" feed:</option>" +
"<option value=auto>AUTO SELECT</option>";
var feedsbygroup = [];
for (var f in config.feedsbyid) {
if (config.engine_check(config.feedsbyid[f], config.app[z])) {
var group = (config.feedsbyid[f].tag === null ? "NoGroup" : config.feedsbyid[f].tag);
if (group != "Deleted") {
if (!feedsbygroup[group]) feedsbygroup[group] = []
feedsbygroup[group].push(config.feedsbyid[f]);
}
}
}
for (group in feedsbygroup) {
out += "<optgroup label='"+group+"'>";
for (f in feedsbygroup[group]) {
out += "<option value="+feedsbygroup[group][f].id+">"+feedsbygroup[group][f].name+"</option>";
}
out += "</optgroup>";
}
configItem.find(".feed-select").html(out);
var feedvalid = false;
// Check for existing configuration
if (config.db[z]!=undefined) {
var feedid = config.db[z];
if (config.feedsbyid[feedid]!=undefined && config.engine_check(config.feedsbyid[feedid],config.app[z])) {
var keyappend = ""; if (z!=config.feedsbyid[feedid].name) keyappend = z+": ";
configItem.find(".feedname").html(keyappend+config.feedsbyid[feedid].name);
configItem.find(".feed-select").val(feedid);
configItem.find(".feed-select-div").hide();
feedvalid = true;
} else {
// Invalid feedid remove from configuration
delete config.db[z];
}
}
// Important that this is not an else here as an invalid feedid
// in the section above will call delete making the item undefined again
if (config.db[z]==undefined) {
// Auto match feeds that follow the naming convention
for (var n in config.feedsbyid) {
if (config.feedsbyid[n].name==config.app[z].autoname && config.engine_check(config.feedsbyid[n],config.app[z])) {
configItem.find(".feed-select").val("auto");
configItem.find(".feed-select-div").hide();
feedvalid = true;
}
}
}
// Indicator icon to show if setup correctly or not
if (!feedvalid) {
configItem.find(".status").removeClass("icon-ok-sign");
configItem.find(".status").addClass("icon-remove-circle");
}
}
if (config.app[z].type=="value") {
if (config.db[z]!=undefined) configItem.find(".appconfig-value").val(config.db[z]);
}
if (config.app[z].type=="checkbox") {
if (config.db[z]!=undefined) configItem.find(".appconfig-value")[0].checked = config.db[z];
}
if (config.app[z].type=="select") {
if (config.db[z]!=undefined) configItem.find(".appconfig-value").val(config.db[z]);
}
// Set description
configItem.find(".appconfig-info").html(config.app[z].description);
}
if (config.check()) {
$(".launchapp").show();
}
// Brings up the feed selector if the pencil item is clicked
$(".appconfig-edit").unbind("click");
$(".appconfig-edit").click(function(){
var key = $(this).parent().attr("key");
var configItem = $(".appconfig-box[key="+key+"]");
if (config.app[key].type=="feed") {
configItem.find(".feed-select-div").show();
}
});
$(".feed-select-ok").unbind("click");
$(".feed-select-ok").click(function(){
var key = $(this).parent().parent().attr("key");
var configItem = $(".appconfig-box[key="+key+"]");
var feedid = $(this).parent().find(".feed-select").val();
if (feedid!="auto" && feedid!=0) {
config.db[key] = feedid;
var keyappend = ""; if (key!=config.feedsbyid[feedid].name) keyappend = key+": ";
configItem.find(".feedname").html(keyappend+config.feedsbyid[feedid].name);
configItem.find(".status").addClass("icon-ok-sign");
configItem.find(".status").removeClass("icon-remove-circle");
// Save config
}
if (feedid=="auto") {
delete config.db[key];
configItem.find(".feedname").html(config.app[key].autoname+" <span style='color:#ccc; font-size:12px'>[AUTO]</span>");
}
if (feedid!=0 ) {
configItem.find(".feed-select-div").hide();
config.set();
if (config.check()) {
$(".launchapp").show();
} else {
$(".launchapp").hide();
}
}
});
$(".appconfig-value").unbind("click");
$(".appconfig-value").change(function(){
var value = false;
var key = $(this).parent().attr("key");
var configItem = $(".appconfig-box[key="+key+"]");
if (config.app[key].type=="value") {
value = $(this).val();
} else if (config.app[key].type=="checkbox") {
value = $(this)[0].checked;
} else if (config.app[key].type=="select") {
value = $(this).val();
}
config.db[key] = value;
config.set();
});
},
check: function()
{
var valid = {};
for (var key in config.app) {
if (config.app[key].type=="feed") {
if (config.app[key].optional!=undefined && config.app[key].optional) {
} else {
valid[key] = false;
if (config.db[key]==undefined) {
// Check if feeds match naming convention and engine
var autoname = config.app[key].autoname;
if (config.feedsbyname[autoname]!=undefined) {
if (config.engine_check(config.feedsbyname[autoname],config.app[key])) valid[key] = true;
}
} else {
// Overwrite with any user set feeds if applicable
for (var z in config.feedsbyid) {
// Check that the feed exists
// config will be shown if a previous valid feed has been deleted
var feedid = config.feedsbyid[z].id;
if (config.db[key] == feedid) {
if (config.engine_check(config.feedsbyid[z],config.app[key])) valid[key] = true;
break;
}
}
}
}
}
}
for (var key in valid) {
if (valid[key]==false) return false;
}
return true;
},
load: function()
{
for (var key in config.app) {
if (config.app[key].type=="feed") {
config.app[key].value = false;
// Check if feeds match naming convention
var autoname = config.app[key].autoname;
if (config.feedsbyname[autoname]!=undefined) {
config.app[key].value = config.feedsbyname[autoname].id;
}
// Overwrite with any user set feeds if applicable
if (config.db[key]!=undefined) {
config.app[key].value = config.db[key];
}
}
if (config.app[key].type=="value") {
if (config.db[key]!=undefined) {
config.app[key].value = config.db[key];
} else {
config.app[key].value = config.app[key].default;
}
}
if (config.app[key].type=="checkbox") {
if (config.db[key]!=undefined) {
config.app[key].value = config.db[key];
} else {
config.app[key].value = config.app[key].default;
}
}
if (config.app[key].type=="select") {
if (config.db[key]!=undefined) {
config.app[key].value = config.db[key];
} else {
config.app[key].value = config.app[key].default;
}
}
}
return config.app;
},
engine_check: function(feed,conf)
{
if (typeof conf.engine === 'undefined') {
return true;
}
if (isNaN(conf.engine)) {
var engines = conf.engine.split(",");
if (engines.length>0) {
for (var z in engines) {
if (feed.engine==engines[z]) return true;
}
}
} else {
if (feed.engine*1==conf.engine*1) return true;
}
return false;
},
set: function()
{
$.ajax({
url: path+"app/setconfig",
data: "name="+config.name+"&config="+JSON.stringify(config.db),
dataType: 'text',
async: false,
success: function(result){
try {
result = JSON.parse(result);
if (result.success!=undefined && !result.success) app_log("ERROR",result.message);
} catch (e) {
try {
app.log("ERROR","Could not parse /setconfig reply, error: "+e);
} catch (e2) {
console.log(e,e2);
}
}
}
});
},
sortByLongname: function(a, b){
var aName = a.longname.toLowerCase();
var bName = b.longname.toLowerCase();
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}
}