forked from brainfoolong/rwa-timedcommands
-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend.js
102 lines (93 loc) · 4.08 KB
/
frontend.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
"use strict";
Widget.register("rwa-timedcommands", function (widget) {
var form = widget.template(".form");
var dataTable = widget.template(".data-table");
var updateScripts = function () {
widget.backend("get", null, function (commands) {
dataTable.find("tbody").html('');
for (var commandsIndex in commands) {
if (commands.hasOwnProperty(commandsIndex)) {
var row = commands[commandsIndex];
var tr = $('<tr>');
tr.attr("data-name", row.name);
tr.append($('<td>').text(row.name));
tr.append($('<td>').text(t(row.active)));
tr.append($('<td>').html(
'<span class="btn btn-info edit">' + widget.t("edit") + '</span> ' +
'<span class="btn btn-danger delete">' + widget.t("delete") + '</span>'
));
dataTable.find("tbody").append(tr);
}
}
});
};
/**
* On initialization
*/
widget.onInit = function () {
widget.content.append(form);
widget.content.append(dataTable);
var i = 0;
var select = widget.content.find("select[name='minutes']");
for (i = 0; i <= 59; i++) {
select.append($('<option>').attr("value", i).text(i));
}
select = widget.content.find("select[name='hours']");
for (i = 0; i <= 23; i++) {
select.append($('<option>').attr("value", i).text(i));
}
select = widget.content.find("select[name='weekdays']");
for (i = 0; i <= 6; i++) {
select.append($('<option>').attr("value", i).text(widget.t("weekday." + i)));
}
widget.content.find(".selectpicker").selectpicker();
updateScripts();
collapsable(widget.content);
widget.content.on("click", ".save", function () {
var f = $(this).closest("form");
var name = f.attr("name");
if (f[0].checkValidity()) {
var data = {};
f.find(":input").filter("[name]").each(function () {
if ($(this).closest(".dropdown-toggle, .dropdown-menu").length) return true;
data[$(this).attr("name")] = $(this).val();
});
if (data.name && data.name.length) {
widget.backend("save", data, function (messageData) {
note(widget.t("saved"), "success");
updateScripts();
});
}
} else {
// on validation error trigger a fake submit button to enable validation UI popup
$(this).after('<input type="submit">');
$(this).next().trigger("click").remove();
}
}).on("click", ".edit", function () {
var e = $(this);
widget.backend("get", null, function (commands) {
widget.content.find("h2.collapsed.collapsable-trigger[data-collapsable-target='timedcommands.form']").trigger("click");
window.scrollTo(0, form.offset().top);
var name = e.closest("tr").attr("data-name");
if (typeof commands[name] != "undefined") {
populateForm(form.find("form"), commands[name]);
widget.content.find(".selectpicker").selectpicker("refresh");
}
});
}).on("click", ".delete", function () {
var e = $(this);
widget.backend("get", null, function (commands) {
var name = e.closest("tr").attr("data-name");
if (typeof commands[name] != "undefined") {
Modal.confirm(widget.t("sure"), function (success) {
if (success) {
widget.backend("delete", name);
note(widget.t("deleted"), "success");
updateScripts();
}
})
}
});
});
};
});