-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluton.js
43 lines (37 loc) · 1.18 KB
/
pluton.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
export default class Pluton {
constructor() {
this.classes = this.importAll();
this.instances = {};
this.setup();
}
clear() {
this.instances = {};
}
setup(root) {
for (var className in this.classes) {
this.setupComponent(className, this.classes[className], root);
}
}
setupComponent(className, component, root) {
if (!component.selector) return;
[].forEach.call((root||document).querySelectorAll(component.selector), (el) => {
if (!this.instances[className]) this.instances[className] = [];
this.instances[className].push(new component(el));
});
}
call(className, fn, parameters) {
if (!this.instances[className]) return;
for (var i = this.instances[className].length - 1; i >= 0; i--) {
this.instances[className][i][fn](parameters);
}
}
importAll() {
var context = require.context(PLUTON_PATH, true, /\.js$/);
var obj = {};
context.keys().forEach(key => {
let code = context(key);
obj[code.default.selector] = code.default;
});
return obj;
}
}