-
Notifications
You must be signed in to change notification settings - Fork 10
/
wc_main.js
168 lines (150 loc) · 5.12 KB
/
wc_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
/*
Autor: Raphael Hoeser
Datum: 11.07.2017
Version: 1.1
WordClock Main JS
*/
// Settings
var wc_settings = {
lang: 'DE',
updateInterval: 1000,
round: false,
showMinutePoints: true,
stencilMode: false,
fuzzyTime: "both", // Can be none, before (for nearly...), after (just was...) or both
};
// ClassNames of HTML Elements
var wc_wcClassName = 'wc';
var wc_minutesClassName = wc_wcClassName + '__minutes';
var wc_minuteDotClassName = wc_wcClassName + '__minute';
var wc_rowClassName = wc_wcClassName + '__row';
var wc_letterClassName = wc_rowClassName + '__letter';
var wc_stencilLetterClassName = wc_rowClassName + '__stencil';
var wc_brightLetterClassName = wc_letterClassName + '--bright';
// Vars for the Wordclock (not to be changed)
var wc_updateInterval;
// Developer Options
var wc_debugMode = 0; // Debug Mode defines which notes should be logged by wc_debugLog(note, priority) (0 = all Massages default)
// Language Packs
var wc_languagePacks = [];
// adds a LanguagePack to the database
function wc_addLanguagePack(pack) {
wc_languagePacks[wc_languagePacks.length] = pack;
}
// logging Function, which only logs notes with priotity > wc_debugMode
function wc_debugLog(note, priority) {
if (priority - wc_debugMode > 0) console.log(note);
}
// Init to place Letters in clock and set Interval
function wc_init() {
wc_setLetters(wc_settings.lang);
wc_updateInterval = window.setInterval(wc_update, wc_settings.updateInterval);
wc_update();
}
// Set Load Listener to fire init();
window.addEventListener('load', wc_init);
// Updates Time
function wc_update() {
var html_wcs = document.getElementsByClassName(wc_wcClassName);
var date = new Date();
var pack = wc_getLanguagePack(wc_settings.lang);
wc_setMinuteDots(date.getMinutes() % 5, wc_settings);
var timestring = pack.timeString(
date.getHours(),
date.getMinutes(),
wc_settings
);
var timewords = timestring.split(' ');
var displayChars = '';
var rows = pack.letterSet;
var allRows = [];
for (var i = 0; i < rows.length; i++) {
allRows = allRows.concat(rows[i]);
}
displayChars = allRows.join('');
for (var i = 0; i < html_wcs.length; i++) {
var dcToWork = displayChars;
var letters = html_wcs[i].getElementsByClassName(wc_letterClassName);
var offset = 0;
wc_setMode(letters, 0, dcToWork.length, false);
for (var j = 0; j < timewords.length; j++) {
var start = dcToWork.search(timewords[j]);
wc_setMode(
letters,
start + offset,
start + offset + timewords[j].length,
true
);
offset += start + timewords[j].length;
dcToWork = dcToWork.substr(start + timewords[j].length);
}
}
}
// Sets all letters from index start to index end-1 to bright if mode == true, else dark
function wc_setMode(domElems, start, end, mode) {
for (var i = start; i < end; i++) {
if (mode) domElems[i].classList.add(wc_brightLetterClassName);
else domElems[i].classList.remove(wc_brightLetterClassName);
}
}
// Sets lettes by Language Code to all Clocks
function wc_setLetters(langCode) {
var html_wcs = document.getElementsByClassName(wc_wcClassName);
var currentLangPack = wc_getLanguagePack(langCode);
for (var i = 0; i < html_wcs.length; i++)
wc_setHTMLLetters(html_wcs[i], currentLangPack);
}
// Sets HTMl letters of given pack to given clock
function wc_setHTMLLetters(clock, langPack) {
var letters = langPack.letterSet;
for (var i = 0; i < letters.length; i++) {
var html_row = document.createElement('div');
html_row.classList.add(wc_rowClassName);
for (var j = 0; j < letters[i].length; j++) {
var html_letter = document.createElement('span');
html_letter.classList.add(wc_letterClassName);
if (wc_settings.stencilMode) {
html_letter.classList.add(wc_stencilLetterClassName);
}
html_letter.innerText = letters[i][j];
html_row.appendChild(html_letter);
}
clock.appendChild(html_row);
}
}
// returns Language Pack by LangCode
function wc_getLanguagePack(langCode) {
for (var i = 0; i < wc_languagePacks.length; i++) {
if (wc_languagePacks[i].langCode == langCode) return wc_languagePacks[i];
}
return false;
}
function wc_setMinuteDots(dots, settings) {
for (var clock of document.getElementsByClassName(wc_minutesClassName)) {
if (settings.showMinutePoints) {
clock.classList.remove('wc--hidden');
for (var i = 1; i <= dots; i++) {
clock
.getElementsByClassName(wc_minuteDotClassName + '__' + i)[0]
.classList.add('wc__minute--bright');
if (wc_settings.stencilMode) {
clock
.getElementsByClassName(wc_minuteDotClassName + '__' + i)[0]
.classList.add('wc__minute--stencil');
}
}
for (var i = dots + 1; i <= 4; i++) {
clock
.getElementsByClassName(wc_minuteDotClassName + '__' + i)[0]
.classList.remove('wc__minute--bright');
if (wc_settings.stencilMode) {
clock
.getElementsByClassName(wc_minuteDotClassName + '__' + i)[0]
.classList.add('wc__minute--stencil');
}
}
} else {
clock.classList.add('wc--hidden');
}
}
}