-
Notifications
You must be signed in to change notification settings - Fork 0
/
kav.js
125 lines (114 loc) · 2.83 KB
/
kav.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
var group = {list: [],
ids: []};
var indexOfPerson = function (id) {
var i = 0;
for (i = 0; i < group.list.length; i = i + 1) {
if (group.list[i].id === id) {
return i;
}
}
return -1;
};
var sortShiftByTime = function (person) {
person.shift.sort(function (a, b) {
return a.end - b.end;
}).reverse;
};
var simpleTimeCheck = function (period) {
var startDate;
var endDate;
startDate = Date.parse(period.start);
if (isNaN(startDate)) {
return {name: 'errorParseStartTime',
message: 'start time parse error '+ period.start};
}
endDate = Date.parse(period.end);
if (isNaN(endDate)) {
return {name: 'errorParseEndTime',
message: 'end time parse error ' + period.end};
}
if ((endDate - startDate) < 0) {
return {name: 'errorEndBeforeStart',
message: 'start time ' + period.start +
' cannot be later then end time ' +
period.end};
};
return 0;
};
exports.getGroup = function () {
return group;
};
exports.addShift = function (id, shift) {
var ret = simpleTimeCheck(shift);
if (ret !== 0) {
return ret;
}
index = indexOfPerson(id);
if (index !== -1) {
if (!group.list[index].shift) {
group.list[index].shift = [];
}
group.list[index].shift.unshift(shift);
return 0;
} else {
return {name: 'errorNoId',
message: 'could not find the index for id ' + id}
}
};
exports.groupSortById = function () {
group.list.sort(function (a, b) {
return a.id - b.id;
});
};
exports.groupSortByTime = function () {
group.list.sort(function (a, b) {
return Date.parse(a.shift[0].end) - Date.parse(b.shift[0].end);
}).reverse;
};
exports.reset = function () {
group = {list: [],
ids: []}
};
exports.addPerson = function (person) {
if (!person.id) {
return {name: 'errorMissingId',
message: 'person missing id'};
}
if (group.ids.indexOf(person.id) !== -1) {
return {name: 'errorIdNotUnique',
message: 'id ' + person.id + ' not unique'};
}
group.list.push(person);
group.ids.push(person.id);
return 0;
};
exports.removePerson = function (id) {
var index = indexOfPerson(id);
if (index !== -1) {
group.list.splice(index, 1);
index = group.ids.indexOf(id);
if (index !== -1) {
group.ids.splice(index, 1);
}
} else {
return {name: 'errorNoId',
message: 'could not find the index for id ' + id}
}
};
exports.addTour = function (id, period) {
var ret = simpleTimeCheck(period);
if (ret !== 0) {
return ret;
}
index = indexOfPerson(id);
if (index !== -1) {
if (!group.list[index].period) {
group.list[index].period = [];
}
group.list[index].period.unshift(period);
return 0;
} else {
return {name: 'errorNoId',
message: 'could not find the index for id ' + id}
}
};