2017-11-30 18:05:55 +01:00
|
|
|
import { withComponent, props } from 'skatejs';
|
|
|
|
import dateformat from 'dateformat';
|
|
|
|
|
|
|
|
const Component = withComponent();
|
|
|
|
class Time extends Component {
|
|
|
|
static props = {
|
|
|
|
format: props.string,
|
|
|
|
time: props.number,
|
|
|
|
interval: props.number,
|
|
|
|
};
|
|
|
|
|
|
|
|
connected() {
|
|
|
|
if (!this.time) {
|
|
|
|
this.time = Date.now();
|
|
|
|
}
|
|
|
|
if (!this.interval) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.runInterval = window.setInterval(() => {
|
|
|
|
this.time = Date.now();
|
|
|
|
}, this.interval * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnecting() {
|
|
|
|
if (this.runInterval) {
|
|
|
|
window.clearInterval(this.runInterval);
|
|
|
|
delete this.runInterval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer(renderRoot, render) {
|
|
|
|
const root = renderRoot;
|
|
|
|
root.innerHTML = render();
|
|
|
|
}
|
|
|
|
|
|
|
|
render({ time, format }) {
|
2017-11-30 18:17:46 +01:00
|
|
|
const useFormat = format || 'ddd ddS HH:MM:ss';
|
2017-11-30 18:05:55 +01:00
|
|
|
return dateformat(new Date(time), useFormat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define('cbase-time', Time);
|