-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.js
84 lines (74 loc) · 2.32 KB
/
classes.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
$(function() {
$("#linkClasses").click(function(event) {
$("#classList").removeClass("hidden");
$(".classItem > .className").first().focus();
});
$(".linkNewClass").click(function(event) {
var classItem = {id:getNewClassId(), name:""};
if (!data.classes) data.classes = [];
data.classes.push(classItem);
addClassItem(classItem);
$(".classItem:not(.persistant) > input").last().focus();
});
$("#classListCancel > a, #classList").click(function(event) {
if ($(event.target).is(
"#classListCancel > a, #classList")) {
goBack();
}
});
if (data.classes) {
buildClassTable();
}
$(document).keyup(function(event) {
if (event.keyCode === 27) {// esc
goBack();
}
});
});
function buildClassTable() {
$(".classItem:not(.persistant)").remove();
for (var i = 0; i < data.classes.length; i++) {
var classItem = data.classes[i];
addClassItem(classItem);
}
}
function addClassItem(classItem) {
var classElem = $("#templateClassItem").get(0).cloneNode(true);
classElem.id = "c" + classItem.id;
var $classElem = $(classElem);
$classElem.removeClass("persistant");
$classElem.find(".className").val(classItem.name);
$classElem.find(".linkDeleteClass").click(deleteClass);
$classElem.find(".className").change(changeClass);
$("#templateClassItem").before($classElem);
}
function deleteClass(event) {
var classItem = event.target.parentElement;
var id = parseInt(classItem.id.substr(1));
var ind = getObjIndForKeyValue(data.classes, "id", id);
if (!ind) return;
data.classes.splice(ind, 1);
$(classItem).remove();
saveData();
}
function changeClass(event) {
var classItem = event.target.parentElement;
var id = parseInt(classItem.id.substr(1));
var ind = getObjIndForKeyValue(data.classes, "id", id);
if (ind == null) return;
data.classes[ind].name = event.target.value;
saveData();
}
function getNewClassId() {
var maxId = 0;
if (data.classes) {
for (var i = 0; i < data.classes.length; i++) {
maxId = Math.max(data.classes[i].id, maxId);
}
}
return maxId + 1;
}
function goBack() {
$("#linkClasses").focus();
$("#classList").addClass("hidden");
}