-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
192 lines (187 loc) · 6.24 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
window.onload = function () {
// Select tasks
$(".task h2").on("click", null, null, function (e) {
$(this).parent().toggleClass("selected");
});
$("h1").on("click", null, null, function (e) {
var oldtext = $("#new-event-input").val();
$("#new-event-input").val(oldtext + $(this).parent().attr("data-date"));
$("#new-event-input").focus();
});
// Keyboard shortcuts
$("body").on("keypress", null, null, function (e) {
var tag = e.target.tagName.toLowerCase();
if(tag === "input" || tag === "textarea" || ($("#new-event-input")[0].value.trim() !== "")) {
// react to enter here
// call another function that can also be triggered by clicking on a day heading
// when clicking on a day heading, append date of that day to the input field and send TODO
if(e.which === 13) {
processTask(e);
}
return;
}
// Which tasks operate on?
task_ids = [];
commands = [];
additional = null;
selected_days = $(".selected");
selected_days.each(function () {
task_ids.push(this.id.substring(5));
});
switch (e.which) {
// Note that the order might be important
case 84:
case 68:
// D for done and move to today, also for uppercase T
commands = ["today", "done"];
break;
case 100:
// d for done
commands = ["done"];
break;
case 127:
// del
commands = ["delete"];
break;
// Durations
case 49:
// 1 for set duration to 1x 15min
case 50:
// 2 for set duration to 2x 15min = 30min etc
case 51:
case 52:
case 53:
case 54:
case 55:
case 56: // 2 hours
commands = ["duration"];
additional = (e.which - 48) * 15 * 60;
break;
// Move
// tricky code: use switch fallthrough but avoid overriding variable
// for uppercase letters, add "done"
// Q W E R for the first row
case 81:
case 113:
day_id = 1;
case 87:
case 119:
if(typeof day_id === "undefined") day_id = 2;
case 69:
case 101:
if(typeof day_id === "undefined") day_id = 3;
case 82:
case 114:
if(typeof day_id === "undefined") day_id = 4;
// Z U I O for the second row
case 90:
case 122:
if(typeof day_id === "undefined") day_id = 5;
case 85:
case 117:
if(typeof day_id === "undefined") day_id = 6;
case 73:
case 105:
if(typeof day_id === "undefined") day_id = 7;
case 79:
case 111:
if(typeof day_id === "undefined") day_id = 8;
additional = $("#day-" + day_id).attr("data-date");
if(e.shiftKey) {
commands = ["done", "move"]
}
else {
commands = ["move"];
}
break;
// Today
case 116:
commands = ["today"];
break;
case 32:
// space for abort all
selected_days.removeClass("selected");
break;
default:
return;
}
if (typeof commands !== "undefined") {
$.ajax({
url: "/planer/_api.php",
data: {
task_ids: task_ids.join(","),
commands: commands.join(","),
additional_data: additional
},
success: function (result) {
window.location.reload(true);
}
});
}
});
};
function processTask(e) {
var data = {
commands: "add",
task_string: $("#new-event-input")[0].value.trim()
};
// check for day shortcuts
skip_shift_date = false;
const regex = /(?<= )([QWERTZUIOYqwertzuioy])([+-][0-9]+)?$/g;
const shortcut_result = regex.exec(data.task_string);
if(shortcut_result !== null) {
switch(shortcut_result[1].toUpperCase()) {
case "T":
date = $("#today").attr("data-date");
break;
case "Y":
date = $("#today").attr("data-yesterday");
break;
case "Q":
date = $("#day-1").attr("data-date");
break;
case "W":
date = $("#day-2").attr("data-date");
break;
case "E":
date = $("#day-3").attr("data-date");
break;
case "R":
date = $("#day-4").attr("data-date");
break;
case "Z":
date = $("#day-5").attr("data-date");
break;
case "U":
date = $("#day-6").attr("data-date");
break;
case "I":
date = $("#day-7").attr("data-date");
break;
case "O":
date = $("#day-8").attr("data-date");
break;
}
// replace the letter with the date
data.task_string = data.task_string.substring(0, data.task_string.length - shortcut_result[0].length) + date;
if(shortcut_result[2] !== undefined) {
data.task_string += shortcut_result[2];
}
skip_shift_date = true;
}
// process shift key
// this appends "today". Because only the first appearance of a date is processed, a manually added date will prevail
if(e.shiftKey) {
data.done = true;
if(!skip_shift_date) {
data.task_string += " " + $("#today").attr("data-date");
}
}
$.ajax({
url: "/planer/_api.php",
data: data,
success: function (result) {
window.location.reload(true);
}
});
}