/
ComponentProvider.js
79 lines (79 loc) · 3.23 KB
/
ComponentProvider.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
"use strict";
/// <reference path="../contracts/ComponentProvider.ts" />
Object.defineProperty(exports, "__esModule", { value: true });
const najs_binding_1 = require("najs-binding");
const najs_facade_1 = require("najs-facade");
const constants_1 = require("../constants");
const functions_1 = require("../util/functions");
class ComponentProvider extends najs_facade_1.Facade {
constructor() {
super(...arguments);
this.components = {};
this.binding = {};
this.extended = {};
}
getClassName() {
return constants_1.NajsEloquent.Provider.ComponentProvider;
}
extend(model, driver) {
const prototype = Object.getPrototypeOf(model);
const components = this.resolveComponents(model, driver);
for (const component of components) {
const className = najs_binding_1.getClassName(model);
if (typeof this.extended[className] === 'undefined') {
this.extended[className] = [];
}
if (this.extended[className].indexOf(component.getClassName()) !== -1) {
continue;
}
this.extended[className].push(component.getClassName());
component.extend(prototype, functions_1.find_base_prototypes(prototype, Object.prototype), driver);
}
}
resolveComponents(model, driver) {
const modelComponents = this.getComponents(najs_binding_1.getClassName(model));
const driverComponents = driver.getModelComponentName();
const combinedComponents = modelComponents.concat(driverComponents ? [driverComponents] : []);
return driver.getModelComponentOrder(combinedComponents).map((name) => {
return this.resolve(name);
});
}
getComponents(model) {
const defaultComponents = Object.keys(this.components).filter((name) => {
return this.components[name].isDefault;
});
const components = model ? defaultComponents.concat(this.binding[model] || []) : defaultComponents;
return components.sort((a, b) => {
return this.components[a].index - this.components[b].index;
});
}
resolve(component) {
if (typeof this.components[component] === 'undefined') {
throw new ReferenceError('Component "' + component + '" is not found.');
}
return najs_binding_1.make(this.components[component].className);
}
register(component, name, isDefault = false) {
if (typeof component === 'function') {
najs_binding_1.register(component);
}
const count = Object.keys(this.components).length;
this.components[name] = {
className: najs_binding_1.getClassName(component),
isDefault: isDefault,
index: count
};
return this;
}
bind(model, component) {
if (typeof this.binding[model] === 'undefined') {
this.binding[model] = [];
}
this.binding[model].push(component);
this.binding[model] = functions_1.array_unique(this.binding[model]);
return this;
}
}
ComponentProvider.className = constants_1.NajsEloquent.Provider.ComponentProvider;
exports.ComponentProvider = ComponentProvider;
najs_binding_1.register(ComponentProvider);