-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticket.js
437 lines (367 loc) · 14.7 KB
/
ticket.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
426
427
428
429
430
431
432
433
434
435
436
437
(function(){
const STATE_NEW = "n";
const STATE_IN_PROGRESS = "p";
const STATE_DONE = "d";
const STATE_CANCELED = "c";
const STATE2STRING = {
[STATE_NEW]: "new",
[STATE_IN_PROGRESS]: "in progress",
[STATE_DONE]: "done",
[STATE_CANCELED]: "canceled"
};
let currentSorting = {
"name": "", // e.g. subject
"asc": false,
};
function createFlashManager() {
const flash = document.getElementById("flash");
flash.hidden = true;
return {
message: function(m) {
flash.innerText = m;
flash.hidden = false;
setTimeout(function() {
flash.hidden = true;
}, 1000);
}
}
}
function createTicketManager() {
const STORAGE_KEY_TICKETS = "t:tickets";
const STORAGE_KEY_TOGGLE = "t:toggle";
let tickets = {}; // uid => valueObj
function loadTickets() {
tickets = JSON.parse(localStorage.getItem(STORAGE_KEY_TICKETS)) || {};
}
// save to localStorage
function saveTickets() {
localStorage.setItem(STORAGE_KEY_TICKETS, JSON.stringify(tickets))
}
// ticket is always an object generated with newTicket()
return {
add: function(ticket) {
loadTickets();
tickets[ticket.uid] = ticket;
saveTickets();
},
update: function(ticket) {
loadTickets();
ticket.modified = Date.now();
tickets[ticket.uid] = ticket;
saveTickets();
},
remove: function(uid) {
loadTickets();
delete tickets[uid];
saveTickets();
},
all: function() {
loadTickets();
return tickets;
},
newTicket: function(values) {
loadTickets();
let uid;
do {
uid = generateUID()
} while(tickets[uid]);
let t = {};
t.uid = uid;
t.subject = values.subject;
t.text = values.text;
t.created = Date.now();
t.modified = Date.now();
t.state = STATE_NEW;
t.priority = false;
return t;
},
setAll: function(newTickets) {
tickets = newTickets;
saveTickets();
},
getToggle: function() {
return JSON.parse(localStorage.getItem(STORAGE_KEY_TOGGLE));
},
setToggle: function(toggle) {
localStorage.setItem(STORAGE_KEY_TOGGLE, JSON.stringify(toggle));
}
}
}
// create a renderer object
function createTicketRenderer(ticketManager, flashManager) {
const DETAIL_SUFFIX = "_detail";
const EXPANDED_CLASS = "expanded";
const table = document.getElementById("tickets");
const ticketTemplate = document.getElementById("ticketTemplate");
// sorty by a specific key and returns an uid list
function sortBy(key, asc = true) {
const tickets = ticketManager.all();
let sortable = [];
for (let uid in tickets) {
sortable.push([uid, tickets[uid][key]]);
}
sortable.sort(function(a, b) {
if (a[1] < b[1]) return -1;
if (a[1] > b[1]) return 1;
return 0;
});
let sortedArray = sortable.map(function(e) {
return e[0];
});
if (!asc) sortedArray.reverse();
return sortedArray;
}
function toggleTicket(uid) {
const ticketRow = document.getElementById(uid);
const detailRow = document.getElementById(uid + DETAIL_SUFFIX);
if (!ticketRow || !detailRow) return;
if (detailRow.hidden) {
detailRow.hidden = false;
ticketRow.classList.add(EXPANDED_CLASS);
location.hash = uid;
}
else {
detailRow.hidden = true;
ticketRow.classList.remove(EXPANDED_CLASS);
location.hash = "";
}
}
function fillTicketRow(ticket, ticketRow, i) {
ticketRow.id = ticket.uid;
ticketRow.classList.add(
(i % 2 == 0) ? "e" : "o"
);
let modified = ticketRow.querySelector(".modified");
let modifiedDate = date2str(new Date(ticket.modified));
let createdDate = date2str(new Date(ticket.created));
ticketRow.querySelector(".uid").innerText = ticket.uid;
ticketRow.querySelector(".priority").hidden = !ticket.priority;
ticketRow.querySelector(".subject").innerText = ticket.subject;
modified.innerText = modifiedDate;
modified.title = "Created: " + createdDate + "\nModified: " + modifiedDate;
ticketRow.querySelector(".state").innerText = STATE2STRING[ticket.state];
ticketRow.querySelector(".state").classList.add(ticket.state);
ticketRow.addEventListener("click", function() {
toggleTicket(ticket.uid);
registerTextareas();
});
}
function fillDetailRow(ticket, detailRow) {
detailRow.id = ticket.uid + DETAIL_SUFFIX;
detailRow.hidden = true;
let saveButton = detailRow.querySelector(".save");
let deleteButton = detailRow.querySelector(".delete");
let subjectInput = detailRow.querySelector(".subject");
subjectInput.value = ticket.subject;
subjectInput.addEventListener("keyup", function(e) {
if (e.keyCode === 13) {
saveButton.click();
}
});
let textInput = detailRow.querySelector(".text");
textInput.value = ticket.text;
let priorityInput = detailRow.querySelector(".priority");
priorityInput.checked = (ticket.priority == true);
let stateRadios = detailRow.querySelectorAll(".state");
for (let i = 0; i < stateRadios.length; i++) {
stateRadios[i].name = ticket.uid + "_state";
if (stateRadios[i].value == ticket.state) {
stateRadios[i].checked = true;
}
}
saveButton.addEventListener("click", function() {
ticket.subject = subjectInput.value;
ticket.text = textInput.value;
ticket.priority = priorityInput.checked;
if (ticket.subject.length === 0) {
alert("No subject given");
return;
}
for (let i = 0; i < stateRadios.length; i++) {
if (stateRadios[i].checked) {
ticket.state = stateRadios[i].value;
break;
}
}
ticketManager.update(ticket);
render();
flashManager.message("Ticket updated");
});
deleteButton.addEventListener("click", function() {
if (confirm("Are you sure?")) {
ticketManager.remove(ticket.uid);
render();
flashManager.message("Ticket removed");
}
});
}
function registerTextareas() {
const textareas = document.querySelectorAll("textarea.text");
function autoResize(ta) {
ta.style.height = ta.scrollHeight + "px";
}
for (let i = 0; i < textareas.length; i++) {
textareas[i].addEventListener("input", function() {
autoResize(textareas[i]);
});
autoResize(textareas[i]);
}
}
function render() {
const tickets = ticketManager.all();
let sortedUids = null;
if (currentSorting.name !== "") {
sortedUids = sortBy(currentSorting.name, currentSorting.asc);
}
const uids = sortedUids || Object.keys(tickets);
let newBody = document.createElement("tbody");
for (let i = 0; i < uids.length; i++) {
const ticket = tickets[uids[i]];
let html = ticketTemplate.content.cloneNode(true);
fillTicketRow(ticket, html.querySelector(".ticket-row"), i);
fillDetailRow(ticket, html.querySelector(".detail-row"));
newBody.appendChild(html);
}
table.replaceChild(newBody, table.querySelector("tbody"));
registerTextareas();
}
return {
render: function() {
render();
}
}
}
function registerTableSort(ticketManager, ticketRenderer) {
const sortIndicators = {
"subject": {
"elm": document.getElementById("sort-subject-indicator"),
"asc": true
},
"modified": {
"elm": document.getElementById("sort-modified-indicator"),
"asc": true
},
"state": {
"elm": document.getElementById("sort-state-indicator"),
"asc": true
}
};
// set others hidden, set class at activeSorting
function updateIndicatorsAndGetAsc(activeSorting) {
let isAsc = true;
let keys = Object.keys(sortIndicators);
for (let i = 0; i < keys.length; i++) {
let sortObj = sortIndicators[keys[i]]
if (keys[i] === activeSorting) {
isAsc = sortObj.asc;
sortObj.elm.hidden = false;
sortObj.elm.className = isAsc ? "asc" : "desc";
sortIndicators[keys[i]].asc = !sortObj.asc;
}
else {
sortObj.elm.hidden = true;
}
}
return isAsc;
}
// set indicators and global sorting setting
function setCurrentSorting(name) {
const isAsc = updateIndicatorsAndGetAsc(name);
currentSorting.name = name;
currentSorting.asc = isAsc;
}
// table sorting TODO: symbol
document.getElementById("sort-subject").addEventListener("click", function() {
setCurrentSorting("subject");
ticketRenderer.render();
});
document.getElementById("sort-modified").addEventListener("click", function() {
setCurrentSorting("modified");
ticketRenderer.render();
});
document.getElementById("sort-state").addEventListener("click", function() {
setCurrentSorting("state");
ticketRenderer.render();
});
document.getElementById("sort-reset").addEventListener("click", function() {
setCurrentSorting("");
ticketRenderer.render();
});
}
// registers listeners for the "new ticket" form and import / export / clear
function registerForms(ticketManager, ticketRenderer, flashManager) {
const new_subject = document.getElementById("new-subject");
const new_text = document.getElementById("new-text");
const bottom_container = document.getElementById("bottom-container");
// is bottom form visible or not
if (ticketManager.getToggle()) bottom_container.hidden = true;
document.querySelector("#bottom #toggler").addEventListener("click", function() {
bottom_container.hidden = !bottom_container.hidden;
ticketManager.setToggle(bottom_container.hidden);
});
document.getElementById("new-form").addEventListener("submit", function(e) {
e.preventDefault();
if (validateNew()) {
const ticket = ticketManager.newTicket({
"subject": new_subject.value,
"text": new_text.value
});
ticketManager.add(ticket);
clearForm();
ticketRenderer.render();
flashManager.message("Ticket added");
}
function validateNew() {
if (new_subject.value === "") {
flashManager.message("No subject given");
return false;
}
return true;
}
function clearForm() {
new_subject.value = "";
new_text.value = "";
}
});
document.querySelector("#import").addEventListener("click", function() {
importJson = prompt("Insert an exported JSON string.\n\nATTENTION: Current data will be lost!");
ticketManager.setAll(JSON.parse(importJson).tickets);
ticketRenderer.render();
flashManager.message("Tickets imported");
});
document.querySelector("#export").addEventListener("click", function() {
exportObj = {
"tickets": ticketManager.all()
};
prompt("Save this JSON string for saving your data.", JSON.stringify(exportObj));
flashManager.message("Tickets exported");
});
document.querySelector("#clear").addEventListener("click", function() {
if (confirm("Really delete all your data?")) {
ticketManager.setAll({});
ticketRenderer.render();
}
flashManager.message("Tickets cleared");
});
}
// thanks Stackoverflow!
function generateUID() {
var firstPart = (Math.random() * 46656) | 0;
var secondPart = (Math.random() * 46656) | 0;
firstPart = ("000" + firstPart.toString(36)).slice(-3);
secondPart = ("000" + secondPart.toString(36)).slice(-3);
return firstPart + secondPart;
}
function date2str(d) {
return d.toLocaleDateString() + " " + d.toLocaleTimeString();
}
// Starting point
window.onload = function() {
const flashManager = createFlashManager();
const ticketManager = createTicketManager();
const ticketRenderer = createTicketRenderer(ticketManager, flashManager);
registerForms(ticketManager, ticketRenderer, flashManager);
registerTableSort(ticketManager, ticketRenderer);
ticketRenderer.render();
}
}());