-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
executable file
·194 lines (167 loc) · 5 KB
/
main.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
window.onload = function() {
this.initBody()
}
searchBarDivId = "search-bar"
searchBarId = "search-bar-input"
messageDivId = "message"
dateDivId = "date"
dateId = "date-text"
messageId = "message-text"
otherContentId = "other-content"
userName = "Deepjyoti"
bgClassContainer = [
"social",
"media",
"git",
"contact",
"funky",
"purple",
"upvoty",
"indigo",
"foxxy"
]
dateMap = {
0: "Jan",
1: "Feb",
2: "Mar",
3: "Apr",
4: "May",
5: "Jun",
6: "Jul",
7: "Aug",
8: "Sep",
9: "Oct",
10: "Nov",
11: "Dec"
}
function initBody() {
/**
* Function called when the body is loaded.
*
* Do everything like adding an event listener to
* other things.
*/
// Read the json file
json = readJSON("config.json")
}
function initSearchBar() {
// Clear the search bar on load, just in case
document.getElementById(searchBarId).value = ""
document.getElementById(searchBarId).focus()
document.getElementById(searchBarId).addEventListener("keypress", (event) => {
if (event.key != 'Enter') return
// Open kagi with the search results.
googleSearchUrl = "https://www.kagi.com/search?q="
query = document.getElementById(searchBarId).value.replace(/\ /g, "+")
document.location = googleSearchUrl + query
})
}
function buildMsg() {
/**
* Build a nice message for the user.
*
* Following is how the message would be decided.
* 0 - 5:59 : It's too late, take some sleep
* 6 - 8:59 : You're up early
* 9 - 11:59 : Have a good day ahead
* 12 - 16:59 : Good Afternoon
* 17 - 19:59 : Good Evening
* 20 - 23:59 : It's time to wrap up for the day
*/
date = new Date()
currentHour = date.getHours()
currentMinute = date.getMinutes()
currentTime = currentHour + (0.01 * currentMinute)
if (inRange(currentTime, 0, 5.59))
return "It's too late, take some sleep"
if (inRange(currentTime, 6, 8.59))
return "You're up early"
if (inRange(currentTime, 9, 11.59))
return "Have a good day ahead"
if (inRange(currentTime, 12, 16.59))
return "Good Afternoon"
if (inRange(currentTime, 17, 19.59))
return "Good Evening"
if (inRange(currentTime, 20, 24))
return "It's time to wrap up for the day"
else
return ""
}
function updateTime() {
/**
* Get the current time and date and return it.
*/
document.getElementById(dateId).textContent = new Date().toLocaleTimeString("en-GB")
}
function updateTimeHook() {
updateTime()
interval = setInterval(() => {
updateTime()
}, 10)
}
function inRange(number, min, max) {
return (number >= min && number <= max)
}
function readJSON(fileName) {
// Load the data of the passed file.
fetch(fileName)
.then(response => {return response.json()})
.then(jsonData => {
parseAndCreate(jsonData)
})
}
function parseAndCreate(jsonData) {
/**
* Parse the passed jsonData and create div's accordingly.
*/
this.userName = jsonData["user"]
// Build a message for the user
builtMsg = buildMsg()
builtMsg == "" ?
builtMsg = `Hello ${this.userName}` : builtMsg = `Hey ${this.userName}, ${builtMsg}!`
document.getElementById(messageId).textContent = builtMsg
// Check if welcome message is supposed to be disabled
if (jsonData["disableMessage"])
document.getElementById(messageDivId).style.display = "none"
if (jsonData["disableDate"])
document.getElementById(dateDivId).style.display = "none"
else
updateTimeHook()
if (jsonData["disableSearchBar"])
document.getElementById(searchBarDivId).style.display = "none"
else
initSearchBar()
sqrs = jsonData["squares"]
sqrs.forEach((element, index) => {
sqr = createSqr(element, index)
document.getElementById(otherContentId).appendChild(sqr)
})
}
function createSqr(sqrData, index) {
// Create a new square division with the passed element
name = sqrData["name"]
links = sqrData["links"]
div = document.createElement("div")
cls = document.createAttribute("class")
div.setAttributeNode(cls)
div.classList.add("sqr")
if (index > bgClassContainer.length - 1)
customClass = "media"
else
customClass = bgClassContainer[index]
div.classList.add(customClass)
h4 = document.createElement("h4")
h4.textContent = name
div.appendChild(h4)
links.forEach(element => {
aName = element["name"]
aHref = element["url"]
a = document.createElement("a")
attrHref = document.createAttribute("href")
attrHref.value = aHref
a.setAttributeNode(attrHref)
a.textContent = aName
div.appendChild(a)
})
return div
}