nightTab/js/clock.js

129 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-12-26 07:45:53 +00:00
var clock = (function() {
var bind = function() {
window.setInterval(function() {
clear();
render();
}, 1000);
};
2018-12-26 07:45:53 +00:00
var clear = function() {
var clock = helper.e(".clock");
2018-12-26 07:45:53 +00:00
while (clock.lastChild) {
clock.removeChild(clock.lastChild);
};
};
var _makeTimeObject = function() {
2018-12-26 07:45:53 +00:00
var time = helper.getDateTime();
time.meridiem = "AM";
if (state.get().header.clock.hour24) {
2019-01-05 20:57:21 +00:00
if (time.hours < 10) {
time.hours = "0" + time.hours;
};
} else {
if (time.hours > 12) {
time.meridiem = "PM";
time.hours = time.hours - 12;
};
if (time.hours == 0) {
time.hours = 12;
};
};
2018-12-26 07:45:53 +00:00
if (time.minutes < 10) {
time.minutes = "0" + time.minutes;
};
if (time.seconds < 10) {
time.seconds = "0" + time.seconds;
};
return time;
};
var render = function() {
var _clock = function() {
var clock = helper.e(".clock");
var time = _makeTimeObject();
2019-01-05 20:57:21 +00:00
var sepCha = ":";
var hours = helper.makeNode({
tag: "span",
text: time.hours,
attr: [{
key: "class",
value: "clock-item clock-hours"
}]
});
var minutes = helper.makeNode({
tag: "span",
text: time.minutes,
attr: [{
key: "class",
value: "clock-item clock-minutes"
}]
});
var seconds = helper.makeNode({
tag: "span",
text: time.seconds,
attr: [{
key: "class",
value: "clock-item clock-seconds"
}]
});
var meridiem = helper.makeNode({
tag: "span",
text: time.meridiem,
attr: [{
key: "class",
value: "clock-item clock-meridiem"
}]
});
2019-01-05 20:57:21 +00:00
if (state.get().header.clock.show.hours) {
clock.appendChild(hours);
};
if (state.get().header.clock.show.minutes) {
clock.appendChild(minutes);
};
if (state.get().header.clock.show.seconds) {
clock.appendChild(seconds);
};
if (!state.get().header.clock.hour24 && state.get().header.clock.show.meridiem) {
clock.appendChild(meridiem);
};
2019-01-05 22:35:50 +00:00
if (state.get().header.clock.show.separator) {
2019-01-05 20:57:21 +00:00
var parts = clock.querySelectorAll("span");
if (parts.length > 1) {
parts.forEach(function(arrayItem, index) {
if (index > 0 && !arrayItem.classList.contains("clock-meridiem")) {
2019-01-05 22:35:50 +00:00
var separator = helper.makeNode({
2019-01-05 20:57:21 +00:00
tag: "span",
text: sepCha,
attr: [{
key: "class",
2019-01-05 22:35:50 +00:00
value: "clock-separator"
2019-01-05 20:57:21 +00:00
}]
});
2019-01-05 22:35:50 +00:00
clock.insertBefore(separator, arrayItem);
2019-01-05 20:57:21 +00:00
};
});
};
};
};
2019-01-05 20:57:21 +00:00
if (state.get().header.clock.show.seconds || state.get().header.clock.show.minutes || state.get().header.clock.show.hours) {
_clock();
};
2018-12-26 07:45:53 +00:00
};
var init = function() {
render();
bind();
2018-12-26 07:45:53 +00:00
};
// exposed methods
return {
init: init,
bind: bind,
2018-12-26 07:45:53 +00:00
render: render,
clear: clear
};
})();