-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
158 lines (133 loc) · 4.64 KB
/
script.js
File metadata and controls
158 lines (133 loc) · 4.64 KB
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
const dateInput = document.getElementById("date-input");
const taskInput = document.getElementById("task-input");
const durationInput = document.getElementById("duration-input");
const addBtn = document.getElementById("add-btn");
const taskList = document.getElementById("task-list");
const outputArea = document.getElementById("output");
let tasks = JSON.parse(localStorage.getItem("tasks")) || [];
function addTask() {
const dateValue = dateInput.value
? formatDate(new Date(dateInput.value))
: formatDate(new Date());
const task = taskInput.value.trim();
const duration = durationInput.value.trim();
if (task && duration) {
const formattedDuration = formatDuration(duration);
const newTask = { date: dateValue, task, duration: formattedDuration };
tasks.push(newTask);
saveTasksToLocalStorage();
renderTasks();
dateInput.value = "";
taskInput.value = "";
durationInput.value = "";
}
}
function removeTask(index) {
tasks.splice(index, 1);
saveTasksToLocalStorage();
renderTasks();
}
function renderTasks() {
taskList.innerHTML = "";
outputArea.value = "";
tasks.forEach((task, index) => {
const listItem = document.createElement("li");
listItem.textContent = `${task.date} - ${task.task} - ${task.duration}`;
const removeBtn = document.createElement("button");
removeBtn.classList.add("rembtn");
removeBtn.textContent = "Remove";
removeBtn.addEventListener("click", () => removeTask(index));
listItem.appendChild(removeBtn);
taskList.appendChild(listItem);
const formattedTask = `${task.date}\n\nDuration: ${task.duration}\nTask: ${task.task}\n\n---\n\n`;
outputArea.value += formattedTask;
});
}
function saveTasksToLocalStorage() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
function formatDuration(duration) {
const [hours, minutes, seconds] = duration.split(":").map(Number);
const totalSeconds = hours * 3600 + minutes * 60 + seconds;
const formattedHours = Math.floor(totalSeconds / 3600);
const formattedMinutes = Math.floor((totalSeconds % 3600) / 60);
const formattedSeconds = totalSeconds % 60;
return `${formatTime(formattedHours)} hours, ${formatTime(formattedMinutes)} minutes, ${formatTime(formattedSeconds)} seconds.`
}
function formatTime(time) {
return time.toString().padStart(2, "0");
}
function formatDate(date) {
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
return date.toLocaleDateString("en-US", options);
}
function copyToClipboard() {
const taskList = document.getElementById("output");
taskList.select();
document.execCommand("copy");
taskList.blur();
document.querySelector(".tip").innerText = "Copied! Have a nice day.";
setTimeout(() => {
let t = "Tip: Click taskarea to copy it to clipboard";
document.querySelector(".tip").innerText = t;
}, 3000);
}
addBtn.addEventListener("click", addTask);
renderTasks();
document.querySelector("#output").addEventListener("click", (e) => {
copyToClipboard();
});
function setBgEnabled(st) {
localStorage.setItem("bg_enabled", st ? "1" : "0");
}
function getBgEnabled() {
let s = localStorage.getItem("bg_enabled");
let d = true;
if (s) {
d = s === "1" ? true : false;
}
return d;
}
window.onload = (e) => {
if (getBgEnabled()) {
const img = new Image();
img.src =
"https://source.unsplash.com/random/2560x1080/?work,office,asthetic";
img.onload = () => {
document.querySelector(
"body"
).style.background = `linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)), url("${img.src}")`;
};
document.querySelector(".dbg").innerText = "Disable background";
} else {
document.querySelector(".dbg").innerText = "Enable background";
}
document.querySelector(".dbg").addEventListener("click", (e) => {
setBgEnabled(!getBgEnabled());
if (getBgEnabled()) {
document.querySelector(".dbg").innerText = "Disable background";
const img = new Image();
img.src =
"https://source.unsplash.com/random/1366x768/?work,office,asthetic";
img.onload = () => {
document.querySelector(
"body"
).style.background = `linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)), url("${img.src}")`;
};
} else {
document.body.style.background = "";
document.querySelector(".dbg").innerText = "Enable background";
}
});
};
document.querySelector(".gh").addEventListener("click", (e) => {
window.location.href = `https://github.com/ashusharmasigdev/worktimesheet`;
});
document.querySelector(".ct").addEventListener("click", (e) => {
window.location.href = `mailto:ashusharma.sigmadev@gmail.com`;
});