-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
267 lines (235 loc) · 7.71 KB
/
index.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
var Module = (function () {
'use strict';
/**
* Is obj an Element via duck-typing
* @param {{}} obj
* @returns {boolean}
*/
function isElement(obj) {
return !!(obj && obj.nodeType === 1);
}
/**
* Get the first item in arguments that is a DOM/jQuery element via duck-typing
* @param args
* @returns {Element}
* @throws Error if no element is found
*/
function getFirstElementArgument(args) {
var $el, i;
for (i = 0; i < args.length; i++) {
//assume first element in arguments is root element
if (!$el && isElement(args[i])) {
return args[i]; //do not cast to jq, because we can't assume that it exists
}
}
throw new Error('Must have element to bind controller');
}
/**
* Iterates through all event definitions in object
* @param {{}} events
* @param {Element} el
* @param {function} controller
*/
function addEvents(events, el, controller) {
var i, event, eventName, indexOfLastSpace, elList;
// loop through the defined events
for (event in events) {
if (events.hasOwnProperty(event)) {
indexOfLastSpace = event.lastIndexOf(' ');
if (indexOfLastSpace === -1) {
// event is defined on the component el, e.g. 'click'
el.addEventListener(event, controller[events[event]].bind(controller));
} else {
// event is defined on a child el, e.g. 'div a click'
eventName = event.substring(indexOfLastSpace + 1);
elList = el.querySelectorAll(event.substring(0, indexOfLastSpace)); // get all child els that match
// loop through child els (could be just one!) and add event listeners
for (i = 0; i < elList.length; i++) {
elList[i].addEventListener(eventName, controller[events[event]].bind(controller));
}
}
}
}
}
/**
* Create a Service
*
* Basically any kind of stand-alone singleton.
*
* @param definition
* @param dependencies
* @constructor
*/
function DSService(definition, dependencies) {
//jshint -W058
//the 'new' keyword resets the service's context
var service = new (Function.prototype.bind.apply(definition, [null].concat(dependencies)));
definition.module.context[definition.refName] = service;
return service;
}
/**
* Create a Controller
*
* Mimics both AngularJS controllers and directives, since we don't need double-binding or scopes
*
* @param definition
* @param dependencies
* @param instanceArguments
* @constructor
*/
function DSController(definition, dependencies, instanceArguments) {
//jshint -W058
var el = getFirstElementArgument(instanceArguments),
constructor = definition.apply(null, dependencies),
controller = new (Function.prototype.bind.apply(constructor, [null].concat(instanceArguments)));
//we handle event registration, ala Marionette
// so event attachment does not need to be unit tested
if (controller.events) {
addEvents(controller.events, el, controller);
}
return controller;
}
/**
* Define a thing that can be instantiated using a provider strategy
*
* NOTE: keep private because it's a reclusive iterator
*
* @param {Module} module
* @param {function} providerStrategy
* @param {string} name
* @param {[] || function} definition
* @returns {function}
* @private
*/
function define(module, providerStrategy, name, definition) {
var dependencies;
if (typeof name !== 'string') {
throw new Error('Name must be a string');
}
if (typeof definition === 'function') {
dependencies = [];
} else {
dependencies = definition.slice(0, definition.length - 1);
definition = definition[definition.length - 1];
}
if (typeof definition !== 'function') {
throw new Error('Must define function as last argument or last element of definition array');
}
//everything needed to create this thing on demand
definition.refName = name;
definition.dependencies = dependencies;
definition.module = module;
definition.providerStrategy = providerStrategy;
return definition;
}
/**
* Create a new thing based solely on definition
*
* NOTE: Visitor pattern. Can jump from module to module, do not reference 'this'.
* @param definition
* @returns {*}
*/
function instantiate(definition) {
var i,
constructorArgs = [],
module = definition.module,
dependencies = definition.dependencies;
//get dependencies
for (i = 0; i < dependencies.length; i++) {
if (module.context[dependencies[i]]) {
constructorArgs[i] = module.context[dependencies[i]];
} else if (module.definitions[dependencies[i]]) {
constructorArgs[i] = instantiate(module.definitions[dependencies[i]]);
} else {
throw new Error(dependencies[i] + ' not defined');
}
}
return definition.providerStrategy(definition, constructorArgs, Array.prototype.slice.call(arguments, 1));
}
var constructor = function () {
this.definitions = {};
this.context = {};
};
constructor.prototype = {
providers: {
controller: DSController,
service: DSService
},
/**
* Create a new thing based solely on definition
*
* NOTE: Is safe, because instantiate does not and should not reference 'this'.
* @param definition
* @returns {*}
*/
instantiate: instantiate,
/**
* Gets or instantiates thing
* @param name
* @returns {*}
*/
get: function (name) {
if (this.context[name]) {
return this.context[name];
}
if (this.definitions[name] && typeof this.definitions[name] === 'function') {
return instantiate.apply(this, [this.definitions[name]].concat(Array.prototype.slice.call(arguments, 1)));
} else if (typeof require !== 'undefined') {
return instantiate.apply(this, [define(this, DSController, name, require(name))].concat(Array.prototype.slice.call(arguments, 1)));
// note: this will throw an error if `name` doesn't exist anywhere
} else {
// note: this can only be hit when not using browserify
throw new Error(name + ' is not defined');
}
},
/**
* Only create if exists in DOM. Has distinct graphical attachment. Scope represents container for thing on the
* page. Remembers container element.
* @param name
* @param definition
* @returns {Module}
*/
controller: function (name, definition) {
this.definitions[name] = define(this, DSController, name, definition);
return this;
},
/**
* Singleton helper classes and abstractions
* @param {string} name
* @param {[] || function} definition
*/
service: function (name, definition) {
//definition always becomes a function when defined
this.definitions[name] = define(this, DSService, name, definition);
return this;
},
/**
* @param name
* @param value
* @returns {Module}
*/
value: function (name, value) {
this.context[name] = value;
return this;
}
};
return constructor;
})();
//at least always one default module
var DS = new Module();
var attach = this;
if (this === undefined) {
// support for es2015 module strictness (this will be undefined, so explicitly add it to window)
attach = window;
} // older browsers, commonjs modules, etc will simply use `this`
// internally defined components should start with $, ala Angular convention
DS.value('$window', attach);
DS.value('$document', attach.document);
//explicitly global
attach.DS = DS;
//reliable self reference
DS.value('$module', DS);
// export it for node and browserify
if (typeof exports !== 'undefined') {
module.exports = DS;
}