-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.js
314 lines (314 loc) · 11.1 KB
/
ui.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
function openMenu() {
getCategoryList();
document.getElementById('menu').style.display = '';
document.getElementById('menu_bar').style.display = 'none';
}
function clearSelect(element) {
//document.getElementById(element).options.length = 0;//IE doesn't like it
document.getElementById(element).innerHTML = '';
var new_optgroup = document.createElement('optgroup');
new_optgroup.label = 'Pick One Or Many';
document.getElementById(element).appendChild(new_optgroup);
}
function hashHandler() {
var layer = location.hash.substring(1);
switch (layer) {
case 'active':
list_filter = 'active';
getTaskList();
openLayer('task_list');
break;
case 'over-deadline':
list_filter = 'over-deadline';
getTaskList();
openLayer('task_list');
break;
case 'deleted':
list_filter = 'deleted';
getTaskList();
openLayer('task_list');
break;
case 'sort-title':
list_sort = 'title';
getTaskList();
openLayer('task_list');
break;
case 'sort-deadline':
list_sort = 'deadline';
getTaskList();
openLayer('task_list');
break;
case 'sort-priority':
list_sort = 'priority';
getTaskList();
openLayer('task_list');
break;
case 'sort-added':
list_sort = 'added';
getTaskList();
openLayer('task_list');
break;
case 'sort-order-asc':
list_sort_order = 'asc';
getTaskList();
openLayer('task_list');
break;
case 'sort-order-desc':
list_sort_order = 'desc';
getTaskList();
openLayer('task_list');
break;
case 'task_list':
getTaskList();
openLayer('task_list');
break;
case 'task':
document.getElementById('task_id').value = '';
document.getElementById('task_title').value = '';
document.getElementById('task_description').value = '';
document.getElementById('task_deadline').value = '';
document.getElementById('priority_normal').checked = true;
document.getElementById('task_status_0').checked = true;
getCategoryList();
openLayer('task');
break;
case 'category':
document.getElementById('category_id').value = '';
document.getElementById('category_name').value = '';
document.getElementById('category_status_0').checked = true;
openLayer('category');
break;
case 'category_list':
getCategoryList();
openLayer('category_list');
break;
default:
if (layer.substring(0, 5) == 'task=') {
var layer_parts = layer.split('=');
loadTask(layer_parts[1]);
}
else if (layer.substring(0, 9) == 'category=') {
var layer_parts = layer.split('=');
loadCategory(layer_parts[1]);
}
else {
window.location.replace('#task_list');
}
break;
}
}
function openLayer(layer) {
document.getElementById('task').style.display = 'none';
document.getElementById('category').style.display = 'none';
document.getElementById('task_list').style.display = 'none';
document.getElementById('category_list').style.display = 'none';
document.getElementById(layer).style.display = '';
}
function jsonParse(data) {
return (new Function('return ' + data))();
}
function filterCatergories() {
list_categories = '';
var select = document.getElementById('filter_categories');
for (i=0; i<select.options.length; i++) {
if (select.options[i].selected) {
list_categories += '&category[]='+ select.options[i].value;
}
}
getTaskList();
}
function postTask() {
document.getElementById('save_task').disabled = true;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
//console.log(jsonParse(xmlhttp.responseText));
location.hash = 'task_list';
document.getElementById('save_task').disabled = false;
}
}
}
var selected = '';
var select = document.getElementById('task_categories');
for (i=0; i<select.options.length; i++) {
if (select.options[i].selected) {
selected += '&category[]='+ select.options[i].value;
}
}
var status = 0;
if (document.getElementById('task_status_1').checked) {
status = 1;
}
var priority = 'normal';
if (document.getElementById('priority_high').checked) {
priority = 'high';
}
else if (document.getElementById('priority_low').checked) {
priority = 'low';
}
var params = 'task_id='+ document.getElementById('task_id').value + '&task_title='+ document.getElementById('task_title').value +'&task_description='+ document.getElementById('task_description').value +'&task_deadline='+ document.getElementById('task_deadline').value +'&task_status='+ status +'&task_priority='+ priority + selected;
xmlhttp.open('POST', base_url +'api.php?action=task', true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
xmlhttp.send(params);
}
function postCategory() {
document.getElementById('save_category').disabled = true;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
//console.log(jsonParse(xmlhttp.responseText));
getCategoryList();
location.hash = 'task_list';
document.getElementById('save_category').disabled = false;
}
}
}
var status = 0;
if (document.getElementById('category_status_1').checked) {
status = 1;
}
var params = 'category_id='+ document.getElementById('category_id').value + '&category_name='+ document.getElementById('category_name').value + '&category_status='+ status;
xmlhttp.open('POST', base_url +'api.php?action=category', true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
xmlhttp.send(params);
}
function loadCategory(id) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
//console.log(jsonParse(xmlhttp.responseText));
var data = jsonParse(xmlhttp.responseText).result;
document.getElementById('category_id').value = data.category_id;
document.getElementById('category_name').value = data.category_name;
document.getElementById('category_status_'+data.category_status).checked = true;
openLayer('category');
}
}
}
var url = base_url +'api.php?action=category&id='+ id;
xmlhttp.open('GET', url, true);
xmlhttp.setRequestHeader('Content-Type', 'text/plain;charset=utf-8');
xmlhttp.setRequestHeader('Cache-Control', 'no-cache, must-revalidate');
xmlhttp.send();
}
function loadTask(id) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
//console.log(jsonParse(xmlhttp.responseText));
var data = jsonParse(xmlhttp.responseText).result;
document.getElementById('task_id').value = data.task.task_id;
document.getElementById('task_title').value = data.task.task_title;
document.getElementById('task_description').value = data.task.task_description;
document.getElementById('task_deadline').value = data.task.task_deadline;
document.getElementById('priority_'+data.task.task_priority).checked = true;
document.getElementById('task_status_'+data.task.task_status).checked = true;
clearSelect('task_categories');
var selected = [];
for (category in data.task.task_categories) {
selected[data.task.task_categories[category].category_id] = true;
}
for(category in data.categories) {
var new_option = document.createElement('option');
new_option.text = data.categories[category].category_name;
new_option.value = data.categories[category].category_id;
if (selected[data.categories[category].category_id]) {
new_option.selected = true;
}
try {
document.getElementById('task_categories').add(new_option, null);
}
catch(ex) {
document.getElementById('task_categories').add(new_option);
}
}
openLayer('task');
}
}
}
var url = base_url +'api.php?action=task&id='+ id;
xmlhttp.open('GET', url, true);
xmlhttp.setRequestHeader('Content-Type', 'text/plain;charset=utf-8');
xmlhttp.setRequestHeader('Cache-Control', 'no-cache, must-revalidate');
xmlhttp.send();
}
function getTaskList() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
//console.log(jsonParse(xmlhttp.responseText));
tasks = jsonParse(xmlhttp.responseText).result;
document.getElementById('task_list_items').innerHTML = '';
for(task in tasks) {
var new_task = document.createElement('div');
new_task.className = 'list_item '+ tasks[task].task_priority;
new_task.id = '#task='+ tasks[task].task_id;
new_task.onclick = function(){window.location = this.id};
var new_title = document.createElement('div');
new_title.className = 'list_title';
new_title.innerHTML = tasks[task].task_title;
new_task.appendChild(new_title);
var new_deadline = document.createElement('div');
new_deadline.className = 'list_deadline';
new_deadline.innerHTML = tasks[task].task_deadline;
new_task.appendChild(new_deadline);
document.getElementById('task_list_items').appendChild(new_task);
}
}
}
}
var url = base_url +'api.php?action=task_list&list_filter='+ list_filter +'&list_sort='+ list_sort +'&list_sort_order='+ list_sort_order + list_categories;
xmlhttp.open('GET', url, true);
xmlhttp.setRequestHeader('Content-Type', 'text/plain;charset=utf-8');
xmlhttp.setRequestHeader('Cache-Control', 'no-cache, must-revalidate');
xmlhttp.send();
document.getElementById('task_list_items').innerHTML = 'Loading...';
}
function getCategoryList() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
//console.log(jsonParse(xmlhttp.responseText));
var categories = jsonParse(xmlhttp.responseText).result;
clearSelect('filter_categories');
list_categories = '';
clearSelect('task_categories');
document.getElementById('category_list_items').innerHTML = '';
for(category in categories) {
var new_option = document.createElement('option');
new_option.text = categories[category].category_name;
new_option.value = categories[category].category_id;
var new_option2 = document.createElement('option');
new_option2.text = categories[category].category_name;
new_option2.value = categories[category].category_id;
try {
document.getElementById('filter_categories').add(new_option, null);
document.getElementById('task_categories').add(new_option2, null);
}
catch(ex) {
document.getElementById('filter_categories').add(new_option);
document.getElementById('task_categories').add(new_option2);
}
var new_list_item = document.createElement('div');
new_list_item.className = 'list_item';
new_list_item.id = '#category='+ categories[category].category_id;
new_list_item.onclick = function(){window.location = this.id};
new_list_item.innerHTML = categories[category].category_name;
document.getElementById('category_list_items').appendChild(new_list_item);
}
}
}
}
var url = base_url +'api.php?action=category_list';
xmlhttp.open('GET', url, true);
xmlhttp.setRequestHeader('Content-Type', 'text/plain;charset=utf-8');
xmlhttp.setRequestHeader('Cache-Control', 'no-cache, must-revalidate');
xmlhttp.send();
document.getElementById('category_list_items').innerHTML = 'Loading...';
}