-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathframework-classes-time-plugin.js
More file actions
49 lines (44 loc) · 1.64 KB
/
framework-classes-time-plugin.js
File metadata and controls
49 lines (44 loc) · 1.64 KB
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
/**
* This class is used for a DataFormsJS Plugins.
*
* DataFormsJS plugins are used to commonly define features that can be shared
* between different pages. A good usage of plugins is to create them to handle
* custom attributes so that shared features can be quickly added to a page
* through HTML markup. In this demo elements on the page with [data-time="{lang}"]
* will the display in the current time.
*
* Compared to components (React, Vue, Web Components, etc) DataFormsJS plugins
* are created once and called over an over. They can be defined as either a
* simple function, and object, or as a class.
*/
class Time {
constructor() {
this.timers = new Map();
}
showTime(el) {
let lang = el.getAttribute('data-time')
lang = (lang ? lang : navigator.language);
const options = { hour: 'numeric', minute: 'numeric', second: 'numeric' };
el.textContent = Intl.DateTimeFormat(lang, options).format(new Date());
}
onRendered() {
const elements = document.querySelectorAll('[data-time]');
for (const el of elements) {
if (!this.timers.has(el)) {
this.showTime(el);
const interval = window.setInterval(() => {
this.showTime(el);
}, 1000);
this.timers.set(el, interval);
}
}
}
onRouteUnload() {
for (const [el, interval] of this.timers) {
window.clearInterval(interval);
this.timers.delete(el);
}
}
}
// From DevTools this plugin will show under `app.plugins.time` based on the name.
app.addPlugin('time', Time);