-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathhistory.js
274 lines (228 loc) · 8.39 KB
/
history.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
// Fetching the records from the Local Storage of the Browser
/*
{Records} is an/a Object/Dictionary of record keyed by their ID's.
Structure of record is to be as follows:
id : Unique id of the record such as the time in ms from Jan 1 1970 (NUM to a STRING) (KEY OF THE OBJECT)
name : Name of the record which is to be taken form user while saving a record (STRING)
time_recorded : Time at which the user confirms the record action (INTEGER)
time_devoted : Time devoted for the action (INTEGER)
time_actual : Actual time used (INTEGER)
time_wasted : Time Wasted by the user in that record (INTEGER)
List/Array of Todos:
Where a Todo is of the structure below:
name : Name of the todo (STRING)
completed : Whether that todo is accomplished or not (BOOLEAN)
*/
// Following is a sample information which will later be connected to real data
// End of Sample Data
const toTime = (millis = 0) => {
let hours = '00', minutes = '00',
miliseconds = '00', seconds = '00',
time = '';
// milliseconds
if (millis > 10) {
miliseconds = Math.floor((millis % 1000) / 10);
if (miliseconds < 10) {
miliseconds = '0' + String(miliseconds);
}
}
// seconds
if (millis > 1000) {
seconds = Math.floor(millis / 1000);
if (seconds >= 60) {
seconds = seconds % 60;
}
if (seconds < 10) {
seconds = '0' + String(seconds);
}
}
// minutes
if (millis > 60000) {
minutes = Math.floor(millis / 60000);
if (minutes >= 60) {
minutes = minutes % 60;
}
if (minutes < 10) {
minutes = '0' + String(minutes);
}
}
// hours
if (millis > 3600000) {
hours = Math.floor(millis / 3600000);
// if (hours > 24) {
// hours = hours % 24;
// }
if (hours < 10) {
hours = '0' + String(hours);
}
}
time = hours + ':';
time += minutes + ':';
time += seconds + ':';
time += miliseconds;
return(time);
}
records = JSON.parse(localStorage.getItem("records"));
let count = 0;
for (const record_id in records) {
let record_text = $("#recordTemplate")
.html()
.replace(/@record_id@/g, records[record_id].id)
.replace(/@unique_key@/g, records[record_id].id)
.replace(/@name@/g, records[record_id].name)
.replace(/@first_save_time@/g, records[record_id].first_save_time)
.replace(/@last_save_time@/g, records[record_id].last_save_time)
.replace(/@devoted_time@/g, toTime(records[record_id].time_devoted) + ' Hrs')
.replace(/@used_time@/g, toTime(records[record_id].time_actual) + ' Hrs')
.replace(/@wasted_time@/g, toTime(records[record_id].time_wasted) + ' Hrs')
.replace(/@prob_count@/g, records[record_id].cnt)
.replace(/@avg_prob_time@/g, toTime(Math.floor(records[record_id].avg)) + ' Hrs')
if (count == 0) $("#records").html(record_text);
else $("#records").append(record_text);
let inside_count = 0;
for (const task of records[record_id].todos) {
let task_text = $("#taskTemplate")
.html()
.replace(/@record_id@/g, records[record_id].id)
.replace(/@task_enum@/g, inside_count + 1)
.replace(/@task_name@/g, task.name)
.replace(/@completed@/g, task.completed ? "checked" : "")
if (inside_count == 0) $("#record" + records[record_id].id + " .tasks").html(task_text);
else $("#record" + records[record_id].id + " .tasks").append(task_text);
inside_count++;
}
count++;
}
// Following is JQuery Code
// Initialisation of looks
let inSelectionMode = false;
$(".tasksBlock").hide();
$(".selectionCtrls").hide();
$(".recordTickLabel").hide();
$("#backDrop").hide();
$("#confirmation").hide();
$(".clickable").hide();
$(".selected").hide();
// Event Handlers
$(".toggler").click(function () {
$(this).parent().siblings(".tasksBlock").slideToggle(100);
if ($(this).attr("pos") == 0)
$(this).css({ 'transform': 'rotate(' + 180 + 'deg)' });
else
$(this).css({ 'transform': 'rotate(' + 0 + 'deg)' });
pos = $(this).attr("pos")
$(this).attr("pos", 1 - pos)
})
// Entering Selection mode by clicking Select button
$("#selection>button").click(function () {
inSelectionMode = true;
$(".selectionCtrls").show();
$("#selection").hide();
$(".recordTickLabel").show();
$(".clickable").show();
})
// Closing selection mode by clicking the close button
$(".selectionCtrls>.closeSelect").click(function () {
inSelectionMode = false;
$(".selectionCtrls").hide();
$("#selection").show();
$(".recordTickLabel").hide();
$(this).siblings("input.selectAll").prop("checked", false);
$(".recordTickBox").prop("checked", false);
$(".clickable").hide();
$(".selected").hide();
})
// Select all checkbox Click event
$(".selectionCtrls>input.selectAll").click(function () {
$(".recordTickBox").prop("checked", $(this).prop("checked"));
$(".selected").show();
})
// Select all button click event
$(".selectionCtrls>button.selectAll").click(function () {
$(this).siblings("input.selectAll").prop("checked", true);
$(".recordTickBox").prop("checked", true);
$(".selected").show();
})
// delete event
$(".selectionCtrls>#delete").click(function () {
// Initialising the Confirmation Box
$("#confirmMatter").html("Are you sure want to delete?");
$("#confirmYes").html("Yes, Delete");
$("#confirmNo").html("No, Cancel");
$("#backDrop").show();
$("#confirmation").show();
// Removes any delegated click event
$("#confirmYes").unbind("click")
// Registers a new click event
$("#confirmYes").click(function () {
$.each($(".recordTickBox"), function (i, value) {
let checkStatus = $(value).prop("checked")
if (checkStatus) {
let key = $(value).parents(".record").attr("unique_key")
$("#record" + key).remove();
delete records[key]
}
})
inSelectionMode = false;
$(".selectionCtrls").toggle();
$("#selection").toggle();
$(".recordTickLabel").toggle();
$(this).siblings("input.select_all").prop("checked", false);
$(".recordTickBox").prop("checked", false);
localStorage.setItem("records", JSON.stringify(records));
if (JSON.stringify(records) === "{}")
$("#records").text("\
No Records Found.\
Please make sure that you have used this app in this device before tracking for records.");
$(".clickable").hide();
$("#backDrop").hide();
$("#confirmation").hide();
})
$("#confirmNo").click(function () {
$("#backDrop").hide();
$("#confirmation").hide();
})
})
$("#backDrop").click(function () {
$("#backDrop").hide();
$("#confirmation").hide();
})
$(".clickable").click(function () {
if (inSelectionMode) {
$(this).parentsUntil("#records").parent().siblings("#selectBar").find(".selectAll").prop("checked", false);
$(this).children(".selected").toggle();
let status = $(this).siblings(".recordDetails").find(".recordTickBox").prop("checked");
$(this).siblings(".recordDetails").find(".recordTickBox").prop("checked", !status);
}
})
// Long press detection
// Set the duration of the long press and declare a couple variables
var longpress = 500;
var start;
var divMouseDown;
$(".record").on('mousedown', function (e) {
// Get the time it was clicked
start = new Date().getTime();
element = this
// See if mouse is still being held down after the longpress duration
divMouseDown = setTimeout(function () {
// What we want to happen when mouse is clicked and held for 1 second
inSelectionMode = true;
$(".selectionCtrls").show();
$("#selection").hide();
$("#delete").show();
$(".recordTickLabel").show();
$(element).find(".selected").show();
$(".clickable").show();
$(element).find(".recordTickBox").prop("checked", true);
}, longpress);
// If the mouse leaves the element or is released before the long touch event,
// reset variables and stop the function from triggering
$(".record").on('mouseup mouseleave', function () {
if (divMouseDown) {
clearTimeout(divMouseDown);
}
start = 0;
e.stopPropagation();
});
});