nightTab/js/clock.js

166 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-12-26 08:45:53 +01:00
var clock = (function() {
var bind = function() {
window.setInterval(function() {
clear();
render();
}, 1000);
};
2018-12-26 08:45:53 +01:00
var clear = function() {
var clock = helper.e(".clock");
2018-12-26 08:45:53 +01:00
while (clock.lastChild) {
clock.removeChild(clock.lastChild);
};
};
var _makeTimeObject = function() {
2018-12-26 08:45:53 +01:00
var time = helper.getDateTime();
time.meridiem = "AM";
if (state.get().header.clock.hour24.show) {
2019-01-05 21:57:21 +01: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 08:45:53 +01: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 timeObject = _makeTimeObject();
var action = {
hours: {
word: function(value) {
if (state.get().header.clock.hour24.show && value < 10) {
return "Zero " + helper.toWords(value);
} else {
return helper.toWords(value);
};
},
number: function(value) {
return value;
}
},
minutes: {
word: function(value) {
if (value < 10) {
return "Zero " + helper.toWords(value);
} else {
return helper.toWords(value);
};
},
number: function(value) {
return value;
}
},
seconds: {
word: function(value) {
return helper.toWords(value);
},
number: function(value) {
return value;
}
}
};
timeObject.hours = action.hours[state.get().header.clock.hours.display](timeObject.hours);
timeObject.minutes = action.minutes[state.get().header.clock.minutes.display](timeObject.minutes);
timeObject.seconds = action.seconds[state.get().header.clock.seconds.display](timeObject.seconds);
var elementHours = helper.makeNode({
tag: "span",
text: timeObject.hours,
attr: [{
key: "class",
value: "clock-item clock-hours"
}]
});
var elementMinutes = helper.makeNode({
tag: "span",
text: timeObject.minutes,
attr: [{
key: "class",
value: "clock-item clock-minutes"
}]
});
var elementSeconds = helper.makeNode({
tag: "span",
text: timeObject.seconds,
attr: [{
key: "class",
value: "clock-item clock-seconds"
}]
});
var elementMeridiem = helper.makeNode({
tag: "span",
text: timeObject.meridiem,
attr: [{
key: "class",
value: "clock-item clock-meridiem"
}]
});
if (state.get().header.clock.hours.show) {
clock.appendChild(elementHours);
2019-01-05 21:57:21 +01:00
};
if (state.get().header.clock.minutes.show) {
clock.appendChild(elementMinutes);
};
if (state.get().header.clock.seconds.show) {
clock.appendChild(elementSeconds);
};
if (!state.get().header.clock.hour24.show && state.get().header.clock.meridiem.show) {
clock.appendChild(elementMeridiem);
};
if (state.get().header.clock.separator.show) {
var separatorCharacter = ":";
2019-01-05 21:57:21 +01: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 23:35:50 +01:00
var separator = helper.makeNode({
2019-01-05 21:57:21 +01:00
tag: "span",
text: separatorCharacter,
2019-01-05 21:57:21 +01:00
attr: [{
key: "class",
value: "clock-item clock-separator"
2019-01-05 21:57:21 +01:00
}]
});
2019-01-05 23:35:50 +01:00
clock.insertBefore(separator, arrayItem);
2019-01-05 21:57:21 +01:00
};
});
};
};
};
if (state.get().header.clock.seconds.show || state.get().header.clock.minutes.show || state.get().header.clock.hours.show) {
_clock();
};
2018-12-26 08:45:53 +01:00
};
var init = function() {
render();
bind();
2018-12-26 08:45:53 +01:00
};
// exposed methods
return {
init: init,
bind: bind,
2018-12-26 08:45:53 +01:00
render: render,
clear: clear
};
})();