-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
177 lines (159 loc) · 4.69 KB
/
script.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
/*
Author: Sirvan Almasi
Imperial College London
git: https://github.com/sirvan3tr/RemNote-Pomodoro
Date created: 14 Aug 2020
Version: 1.0.0
*/
// global variables
let t = 25, // time limit is 25min
pomConsole = document.getElementById("pomodoro_console"),
logName = "Pomodoro 10,000hr log",
locStorage = window.localStorage,
toLog = true,
locStorageTag = "remnote_pomodoro_tag";
/*
Parameters:
logName (default=Pomodoro 10,000hr log)
t (default=25min)
toLog (whether to create a page to log the hours) (true or false)
*/
let urlParams = new URLSearchParams(window.location.search);
if(urlParams.has("t")) {
try {
let tempT = parseInt(urlParams.get("t"));
t = tempT;
} catch(e) {
console.log("error" + e);
}
}
if(urlParams.has("logName")) logName = urlParams.get("logName");
if(urlParams.has("log")) {
let tempToLog = urlParams.get("log");
if (tempToLog === "true" || tempToLog == "false") toLog = urlParams.get("log");
}
class Pomodoro {
constructor(tLimit, dispDiv, logName, pomConsole, toLog) {
this.tLimit = tLimit;
this.dispDiv = dispDiv;
this.locStorage = window.localStorage;
this.locKey = "remonote_pomodoro2";
this.interval = 0;
this.logName = logName;
this.pomConsole = pomConsole;
this.logBool = false;
// Create a log file
if(toLog==="true" || toLog==true) {
this.createLogFile();
this.logBool = true;
}
}
// functions
async start() {
var countDownDate = this.getState();
if(!countDownDate) return;
// Update the count down every 1 second
this.interval = setInterval(
async function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
//var days = Math.floor(distance / (1000 * 60 * 60 * 24));
//var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById(this.dispDiv).innerHTML =
minutes + "m " + seconds + "s ";
if (distance < 0) this.stop();
}.bind(this),
1000
);
}
getState() {
var countDownDate = new Date();
if (this.locStorage[this.locKey]) {
let item = this.locStorage.getItem(this.locKey);
if(item==="stop") return false;
countDownDate = new Date(item);
} else {
// Set the date we're counting down to
countDownDate.setMinutes(countDownDate.getMinutes() + this.tLimit);
this.locStorage.setItem(this.locKey, countDownDate);
}
return countDownDate;
}
restart() {
this.clear();
this.start();
}
clear() {
clearInterval(this.interval);
document.getElementById(this.dispDiv).innerHTML =
"Good job! Time for a break.";
this.locStorage.removeItem(this.locKey);
}
stop() {
this.clear();
this.locStorage.setItem(this.locKey, "stop");
if(this.logBool) this.logEntry();
}
async createLogFile() {
try {
const pomLog = await RemNoteAPI.v0.get_by_name(this.logName);
if (!pomLog.found) {
this.createDocRem(this.logName, null, true);
}
} catch (e) {
console.log(e);
}
}
async logEntry() {
try {
const pomLog = await RemNoteAPI.v0.get_by_name(this.logName);
let dateNow = new Date();
let fomattedDate =
dateNow.getFullYear() +
"-" +
(dateNow.getMonth() + 1) +
"-" +
dateNow.getDate() +
" " +
dateNow.getHours() +
":" +
dateNow.getMinutes();
let tags = this.pomConsole.value;
let txt = fomattedDate.toString() + " - " + tags;
await RemNoteAPI.v0.create(txt, pomLog._id);
} catch (e) {
console.log(e);
}
}
async createDocRem(name, parent, isDoc) {
await RemNoteAPI.v0.create(name, parent, 0, { isDocument: isDoc });
return "done";
}
} // end of class
function getTag() {
let tag = locStorage[locStorageTag];
if (tag === undefined) {
console.log("no tag found");
} else {
pomConsole.value = locStorage[locStorageTag];
}
}
document.addEventListener("keyup", logTag);
function logTag(e) {
locStorage.setItem(locStorageTag, pomConsole.value);
}
const pomodoro = new Pomodoro(t, "pomodoro_container", logName, pomConsole, toLog);
pomodoro.start();
document.getElementById("pomodoro_restart").onclick = () => {
pomodoro.restart();
};
document.getElementById("pomodoro_stop").onclick = () => {
pomodoro.stop();
};
getTag();