-
Notifications
You must be signed in to change notification settings - Fork 38
/
clockwork-card.js
192 lines (163 loc) · 6.42 KB
/
clockwork-card.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Clockwork Card
// https://github.com/robmarkoski/ha-clockwork-card
class ClockWorkCard extends HTMLElement {
constructor() {
super();
this.attachShadow({
mode: 'open'
});
}
/* This is called every time sensor is updated */
set hass(hass) {
const config = this.config;
const locale = config.locale;
const _locale = locale ? locale : undefined;
var _other_timezones = config.other_time;
const entityId = config.entity;
// Need to check for safari as safari dates are parsed as being UTC when not specified.
// Therefore all dates are adjusted
//var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
if (entityId) {
const state = hass.states[entityId];
const stateStr = state ? state.state : "Unavailable";
if (stateStr == "Unavailable") {
throw new Error("Sensor State Unavailable");
}
// if (isSafari) {
// var _stateStr_utc = new Date(stateStr).toLocaleString(locale, {timeZone: "Etc/UTC"});
// var _date_time = new Date(_stateStr_utc);
// } else {
// var _date_time = new Date(stateStr);
// }
var _date_time = new Date(stateStr);
} else {
var _date_time = new Date();
}
if (_date_time == "Invalid Date") {
throw new Error("Invalid date. Ensure its a ISO Date")
}
//Format the Time
var _time = _date_time.toLocaleTimeString(_locale, {
hour: 'numeric',
minute: 'numeric'
});
//Format the Date
var _date = _date_time.toLocaleDateString(_locale, {
weekday : 'long',
day : 'numeric',
month : 'long'
});
//Build List of Other Timezones
//
var otherclocks = `
<div class = "other_clocks">
`;
var i;
var j = _other_timezones.length; //TODO: Recommend max 3.
for (i= 0; i < j; i++) {
//Format other timezones.
var _tztime = _date_time.toLocaleTimeString(_locale, {
hour: 'numeric',
minute: 'numeric',
timeZone: _other_timezones[i],
weekday: 'short'
});
// List other Timezones.
otherclocks = otherclocks + `
<div class="tz_locale">${_other_timezones[i]} </div>
<div class="otime"> ${_tztime} </div>
`;
//console.log(_tztime);
};
otherclocks = otherclocks + `
</div>
`;
/*console.log(otherclocks);*/
// Build Current Local Time
var local_time = `
<div class="clock">
<div class="time" id="time">${_time}</div>
<div class="date" id="date">${_date}</div>
</div>
`;
var clock_contents = local_time + otherclocks;
/* console.log("Clock Contents: " + clock_contents);*/
this.shadowRoot.getElementById('container').innerHTML = clock_contents;
}
/* This is called only when config is updated */
setConfig(config) {
// TODO: Add some more error checking.
// if (!config.entity) {
// throw new Error('You must define an entity')
// }
/*console.log(_other_timezones);*/
const root = this.shadowRoot;
if (root.lastChild) root.removeChild(root.lastChild);
this.config = config;
const card = document.createElement('ha-card');
const content = document.createElement('div');
const style = document.createElement('style')
style.textContent = `
.container {
padding: 5px 5px 5px;
display:flex;
flex-flow: row wrap;
justify-content: space-around;
align-items: flex-start;
}
.clock {
padding: 5px 5px 5px 0px;
margin: auto;
}
.other_clocks {
float: right;
margin: auto;
}
.otime {
padding: 0px 5px 2px;
font-size: 14px;
font-family: var(--paper-font-headline_-_font-family);
letter-spacing: var(--paper-font-headline_-_letter-spacing);
text-rendering: var(--paper-font-common-expensive-kerning_-_text-rendering);
}
.tz_locale {
padding: 0px 5px 1px;
color: var(--secondary-text-color);
font-size: 11px;
font-family: var(--paper-font-headline_-_font-family);
letter-spacing: var(--paper-font-headline_-_letter-spacing);
text-rendering: var(--paper-font-common-expensive-kerning_-_text-rendering);
}
.time {
padding:
font-family: var(--paper-font-headline_-_font-family);
-webkit-font-smoothing: var(--paper-font-headline_-_-webkit-font-smoothing);
font-size: 56px;
font-weight: var(--paper-font-headline_-_font-weight);
letter-spacing: var(--paper-font-headline_-_letter-spacing);
line-height: 1em;
text-rendering: var(--paper-font-common-expensive-kerning_-_text-rendering);
}
.date {
font-family: var(--paper-font-headline_-_font-family);
-webkit-font-smoothing: var(--paper-font-headline_-_-webkit-font-smoothing);
font-size: 24px;
font-weight: var(--paper-font-headline_-_font-weight);
letter-spacing: var(--paper-font-headline_-_letter-spacing);
line-height: var(--paper-font-headline_-_line-height);
text-rendering: var(--paper-font-common-expensive-kerning_-_text-rendering);
}
`;
content.id = "container";
content.className = "container";
card.header = config.title;
card.appendChild(style);
card.appendChild(content);
root.appendChild(card);
}
// The height of the card.
getCardSize() {
return 3;
}
}
customElements.define('clockwork-card', ClockWorkCard);