forked from fufar/simple-clock-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-clock-card-jb.js
64 lines (55 loc) · 2.23 KB
/
simple-clock-card-jb.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
class SimpleClockCard extends HTMLElement {
set hass(hass) {
if (!this.content) {
var config = this.config;
const card = document.createElement('HA-card');
this.content = document.createElement('div');
this.content.style.paddingLeft = this.config.paddingLeft_size ? this.config.paddingLeft_size : '0px';
this.content.style.paddingRight = this.config.paddingRight_size ? this.config.paddingRight_size : '0px';
this.content.style.paddingTop = this.config.paddingTop_size ? this.config.paddingTop_size : '60px';
this.content.style.paddingBottom = this.config.paddingBottom_size ? this.config.paddingBottom_size : '60px';
this.content.style.fontSize = this.config.font_size ? this.config.font_size : '4rem' ;
this.content.style.fontFamily = this.config.font_family ? this.config.font_family : 'Roboto, serif' ;
this.content.style.backgroundColor = this.config.background_color ? this.config.background_color : 'transparent' ;
this.style.textAlign = 'center';
this.content.style.display = 'inline-block';
card.appendChild(this.content);
this.appendChild(card);
var content = this.content;
startTime();
setInterval(startTime, 1000);
function addZero(i){
if (i < 10){
i = "0" + i;
}
return i;
}
function startTime() {
var today = new Date(),
h = today.getHours(),
m = today.getMinutes(),
s = today.getSeconds(),
p = ( h < 12 ) ? "AM" : "PM";
m = addZero(m);
s = addZero(s);
let use_military = config.use_military !== undefined ? config.use_military : true;
let hide_seconds = config.hide_seconds !== undefined ? config.hide_seconds : false;
let lead_zero = config.lead_zero !== undefined ? config.lead_zero : false;
let time_str = (use_military ? (lead_zero ? addZero(h) : h) : ((h + 11) % 12) + 1 ) +
":" +
m +
(hide_seconds ? "" : ":" + s ) +
(use_military ? " " : " " + p );
content.innerHTML = time_str;
content.parentElement.style.backgroundColor = this.config.background_color
}
}
}
setConfig(config) {
this.config = config;
}
getCardSize() {
return 1;
}
}
customElements.define('simple-clock-card-jb', SimpleClockCard);