-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (68 loc) · 2.05 KB
/
index.js
File metadata and controls
90 lines (68 loc) · 2.05 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
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
import './index.styl';
import log from '@sled/log';
class Core {
constructor($slider, ...modules) {
this.$ = typeof $slider == 'object' ? $slider
: document.querySelector($slider);
this.modules = {};
this.domModules = {};
this.id = this.$.id || 'core';
log({ id: this.id }, `created`);
this.loadDomModules(...this.$.children);
this.loadModules(...modules);
}
module(name) {
return this.modules[name];
}
load(name, cb) {
let module = this.module(name);
let err;
if (!module)
err = new Error('missing module', name);
cb && cb(err, module);
return new Promise((res, rej) => module ? res(module) : rej(err));
}
bootstrapModule(Module) {
let name = Module.name;
let $ = this.domModules[name];
let module = new Module.class(this);
if ($) {
module.$ = $;
if ($.children.length) {
module.$$ = $.children;
this.log('module', name, '$$');
}
this.log('module', name, 'inject dom-module');
} else if (Module.peer === '$') {
//TODO #3
throw new Error(`missing dom-module ${name}`);
}
this.modules[name] = module;
this.log('module', name, 'loaded');
return module;
}
loadModules(...modules) {
this.detect('module', modules);
let init = modules
.map(::this.bootstrapModule)
.filter(module => module.init);
init.forEach(module => module.init(this));
log({ id:this.id, name: 'module' }, `${init.length} module${init.length > 1 ? 's' : ''} initiated`);
return new Promise(res => res(this));
}
loadDomModules(...modules) {
this.detect('dom-module', modules);
modules.forEach(domModule => {
let name = domModule.classList[0];
this.domModules[name] = domModule;
this.log('dom-module', name, 'loaded');
});
}
detect(type, modules) {
log({ id:this.id, name: type }, `${modules.length} module${modules.length > 1 ? 's' : ''} detected`);
}
log(type, name, msg) {
log({ id: this.id, name: `${type}] [${name}` }, msg);
}
};
export default Core;