-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.js
More file actions
136 lines (102 loc) · 3.54 KB
/
Module.js
File metadata and controls
136 lines (102 loc) · 3.54 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import * as h from './helpers';
import error from './error';
import services from './services';
import directives from './directives';
var modules = {};
export default function Module(name, deps){
this._name = name;
this._components = { directives: {}, services: {}, controllers: {} };
this._deps = deps;
modules[name] = this;
deps = h.extend([], deps);
for(var i = 0, length = deps.length; i < length; i++){
var module = modules[deps[i]];
h.extend(this._components, module._components);
}
this.error = error('module');
this._defineDefaultServices()
._defineDefaultDirectives();
}
Module.prototype = {
controller: function (){
return this.component('controllers', arguments[0], arguments[1]);
},
checkController: function(name){
return this.checkComponent('controllers', name);
},
directive: function (){
return this.component('directives', arguments[0], arguments[1]);
},
checkDirective: function(name){
return this.checkComponent('directives', name);
},
service: function(){
return this.component('services', arguments[0], arguments[1]);
},
checkService: function(name){
return this.checkComponent('services', name);
},
invoke: function(fn, locals, chain) {
var deps = fn.$inject instanceof Array ? fn.$inject.slice() : [];
locals = locals || {};
locals.$app = this;
chain = chain instanceof Array ? chain.slice() : [];
chain.push(name);
for(var i = 0, length = deps.length; i < length; i++){
var depName = deps[i];
var local = locals[depName];
if(chain.indexOf(depName) !== -1){
throw this.error('01', 'Service "{0}" fixated in "{1}"', depName, name);
}
deps[i] = local ? local : this._getComponent('services', depName, chain);
if(!deps[i]){
throw this.error('02', 'Unknown service: {0}', depName);
}
}
return new function Constructor(){
return fn.apply(this, deps);
};
},
component: function(type, components){
if(arguments[2]) {
this._setComponent(type, arguments[1], arguments[2]);
} else if(h.isObject(components)) {
for(var name in components){
var body = components[name];
this._setComponent(type, name, body);
}
} else {
return this._getComponent(type, arguments[1]);
}
},
_setComponent: function(type, name, body){
body = body || [];
this._components[type][name] = {
fn: body[body.length - 1],
deps: body.splice(0, body.length - 1)
};
},
checkComponent: function(type, name) {
return !!this._components[type][name];
},
_getComponent: function(type, name, chain) {
if(!this.checkComponent(type, name)) {
this.error('03', 'undefined component {0} "{1}"', type, name);
return;
}
var component = this._components[type][name];
if(!component.getFn) {
component.fn.$inject = component.deps;
component.getFn = (type === 'controllers') ? component.fn : this.invoke(component.fn, null, chain);
}
return component.getFn;
},
_defineDefaultServices: function () {
this.service(services);
return this;
},
_defineDefaultDirectives: function () {
this.directive(directives);
return this;
}
};