ryutai
Version:
A next generation window manager.
95 lines (87 loc) • 2.73 kB
JavaScript
var Clock = function(config) {
if(!config) {
config = {};
}
if(config.interval) {
this.adjust = config.adjust;
} else {
this.adjust = 0;
}
if(config.format) {
this.format = config.format;
} else {
this.format = "HOURS:MINUTES TT";
}
if(config.refreshRate) {
this.refreshRate = config.refreshRate
} else {
this.refreshRate = "SECONDS";
}
var acc = this.refreshRate;
var fm = this.format;
this.mesh = {
dom: "_obj.clock",
type: "SPAN",
text: "...",
init: function(obj) {
var time = new Date();
var text = fm;
if(this.AmPmH(time.getHours(), fm) > 0) {
text = text.replace("HOURS", this.AmPmH(time.getHours(), fm));
} else {
text = text.replace("HOURS", 12);
}
text = text.replace("MINUTES", this.fix(time.getMinutes()));
text = text.replace("SECONDS", this.fix(time.getSeconds()));
text = text.replace("MILLISECONDS", this.fixM(time.getMilliseconds()));
text = text.replace("TT", this.AmPmT(time.getHours()));
obj.innerHTML = text;
this.text = text;
if(acc == "HOURS") {
setTimeout(() => {
this.init(obj);
}, (((59 - time.getMinutes()) * 60000) + ((59 - time.getSeconds()) * 1000)));
} else if(acc == "MINUTES") {
setTimeout(() => {
this.init(obj);
}, (((59 - time.getSeconds()) * 1000) + (999 - time.getMilliseconds())));
} else if(acc == "SECONDS") {
setTimeout(() => {
this.init(obj);
}, (999 - time.getMilliseconds()));
} else {
throw "No refreshRate set!";
}
},
fix: function(time) {
if(time < 10) {
return "0" + time;
} else {
return time;
}
},
fixM: function(time) {
time = time + "";
for(var x = 0; x < (5 - time.length); x++) {
var fork = time;
time = "0" + fork;
}
return time;
},
AmPmH: function(time, format) {
if(format.indexOf("TT") > -1) {
if(time > 12) {
return (time - 12);
}
}
return time;
},
AmPmT: function(time) {
if(time < 12) {
return "AM";
} else {
return "PM";
}
}
};
};