-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallat.js
443 lines (414 loc) · 14 KB
/
allat.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
var root = (function() {return this; }());
if (root.Allat) {
throw new Error("Allat.js is already initialized.");
}
root.Allat = {};
(function () {
Object.getKeys = Object.getKeys || function (obj) {
var keys = [], key;
for (key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
keys.push(key);
}
}
return keys;
};
Array.prototype.forEach = Array.prototype.forEach || function (fn, scope) {
'use strict';
var i, len = this.length;
for (i = 0; i < len; ++i) {
if (!!this[i]) {
fn.call(scope, this[i], i, this);
}
}
};
}());
/*global Allat */
(function (Allat) {
/**
* @namespace
*/
var ModuleSupport = {};
function noop() { return undefined; }
/**
* Base module for module support
* @constructor
* @param name The name of the module
*/
function DefaultModule(name) {
this.name = name;
}
DefaultModule.prototype = {
services: [],
version: "0.1",
name: "DefaultModule",
init: noop,
/**
* Utility method on module to register other modules or services (singleton modules)
* @param {String} service the name of the module or service
* @param {DefaultModule} impl the module instance
*/
setup: function (service, impl) {
this.services[service] = impl;
},
getName: function () {
return this.name;
},
clear: noop
};
/**
* Map to hold name of the modules and their factory methods
* @type Object.<String,Object>
* @private
*/
ModuleSupport._modules = {};
/**
* Map to hold singleton instances of singleton services to reuse
* @type Object.<String,DefaultModule>
* @private
*/
ModuleSupport._singletons = {};
/**
* Default factory implementation for Modules
* @returns {DefaultModule}
*/
function defaultModuleFactory(name) {
return new DefaultModule(name);
}
ModuleSupport.defaultModuleFactory = defaultModuleFactory;
/**
* @param name Module to be created
*/
function instantiate(name) {
var module = ModuleSupport._modules[name],
instance = ModuleSupport._singletons[name];
if (!instance) {
if (module && module.build) {
instance = (module && module.build)();
instance.init();
if (module && module.singleton) {
ModuleSupport._singletons[name] = instance;
}
} else {
throw new Error("Unable to load module " + name);
}
}
return instance;
}
ModuleSupport.instantiate = instantiate;
/**
* DI utility method to inject dependencies for a module
* @param {DefaultModule} module module where the dependencies should be injected in
* @param {Array} dependencies list of module names to be injected
* @returns {DefaultModule} original module where dependencies already injected
*/
function inject(module, dependencies) {
var dependency, instance, i;
if (dependencies) {
for (i = 0; i < dependencies.length; i++) {
dependency = dependencies[i];
instance = instantiate(dependency);
module.setup(dependency, instance);
}
}
return module;
}
ModuleSupport.inject = inject;
/**
* @param {string} name Module's name
* @param {{factory:function, dependencies:Array, singleton:Boolean}} options object
*/
function define(name, options) {
var factory, dependencies, singleton;
factory = options.factory || function (obj) {return obj; };
dependencies = options.dependencies || [];
singleton = options.singleton || false;
if (!ModuleSupport._modules[name]) {
ModuleSupport._modules[name] = {
build: function () {
var module = factory(defaultModuleFactory(name));
return inject(module, dependencies);
},
singleton: singleton
};
}
}
ModuleSupport.define = define;
Allat.module = ModuleSupport;
}(Allat));
(function (Allat) {
/**
* @namespace
*/
var util = {};
/**
* Proxy utility to bind a function to a scope
* @param {function} func
* @param {Object} scope
* @returns {function}
*/
function proxy(func, scope) {
return function() {
return func.apply(scope || this, arguments);
};
}
util.proxy = proxy;
/**
* Utility method to convert anything array like to valid array.
* @param obj
* @returns {Array}
*/
function makeArray(obj) {
var arr = [], i, ref;
for (i = 0, ref = arr.length = obj.length; i < ref; i++) {
arr[i] = obj[i];
}
return arr;
}
util.makeArray = makeArray;
/**
* Utility function to escape characters which are special in html, like .,',",<,> and /
* @param {string} str
* @returns {string}
*/
function escape(str) {
str = str.replace(/[\.'"<>\/]/g, "");
return str;
}
util.escape = escape;
Allat.util = util;
}(Allat));
/*global Allat */
(function (Allat) {
Allat.module.define("EventBus", {
factory: function (EventBus) {
EventBus.init = function () {
this._subscribers = this.services.MapSupport.createMap();
};
/**
* @param {String} event
* @param {function} handler
*/
EventBus.subscribe = function (event, handler) {
this._subscribers.add(event, handler);
};
EventBus.unSubscribe = function (event, handler) {
this._subscribers.remove(event, handler);
};
EventBus.fireEvent = function (event, data) {
var handlers = this._subscribers.getValues(event), i;
if (!handlers || !handlers.length) {
return;
}
for (i = 0; i < handlers.length; i++) {
if (handlers[i](event, data)) {
break;
}
}
};
return EventBus;
},
dependencies: ["MapSupport"],
singleton: true
});
}(Allat));
/*global Allat */
(function (Allat) {
Allat.module.define("MapSupport", {
factory: function (MapSupport) {
var Map = function () {
this._map = {};
};
Map.prototype.add = function (key, value) {
var values = this._map[key] || [];
values.push(value);
this._map[key] = values;
};
Map.prototype.remove = function (key, value) {
var values = this._map[key], i;
for (i = 0; i < values.length; i++) {
if (value === values[i]) {
this._map[key] = values.splice(i, 1);
break;
}
}
return value;
};
Map.prototype.contains = function (key, value) {
var values = this._map[key], i;
for (i = 0; i < values.length; i++) {
if (value === values[i]) {
return true;
}
}
return false;
};
Map.prototype.getValues = function (key) {
return this._map[key];
};
Map.prototype.getKeys = function () {
return Object.getKeys(this._map);
};
MapSupport.createMap = function () {
return new Map();
};
return MapSupport;
},
singleton: true
});
}(Allat));
/*global Allat */
(function (Allat) {
Allat.module.define("PropertyBindSupport", {
factory: function (propertyBindSupport) {
propertyBindSupport.init = function() {
var p = Allat.util.proxy;
this.dictionary = {};
this.parsePage();
/*jslint unparam: true*/
this.services.EventBus.subscribe("property.set", p(function (event, data) {
if (this.resolve(data.key)) {
this.update(data.key, data.value);
}
}, this));
/*jslint unparam: false*/
};
propertyBindSupport.parsePage = function () {
var template, i, boundTo, key, p = Allat.util.proxy,
createChangeHandler = p(function(key, template) {
return p(function (e) {
var value = Allat.util.escape(e.target.value);
this.dictionary[key] = {
template: template,
boundTo: e.target,
value: value
};
template.innerHTML = value;
this.services.EventBus.fireEvent("property.updated", {
key: key,
value: value
});
}, this);
}, this);
this.templates = document.getElementsByClassName("bind");
for (i = 0; i < this.templates.length; i++) {
template = this.templates[i];
if (template.attributes["data-bind"]) {
key = template.attributes["data-bind"].value;
boundTo = document.getElementById(key);
boundTo.onchange = createChangeHandler(key, template);
boundTo.onkeyup = boundTo.onchange;
boundTo.onchange({
target: boundTo
});
}
}
};
propertyBindSupport.resolve = function (key) {
return this.dictionary[key].value;
};
propertyBindSupport.update = function (key, value) {
this.dictionary[key].value = value;
this.dictionary[key].boundTo.value = value;
this.dictionary[key].boundTo.onchange({
target: this.dictionary[key].boundTo
});
return value;
};
return propertyBindSupport;
},
dependencies: ["EventBus"],
singleton: true
});
}(Allat));
/*global root,Allat */
(function (Allat) {
Allat.module.define("TemplateSupport", {
factory: function (TemplateSupport) {
function addSimpleTemplate() {
var SimpleTemplate = {
render: function (template, data) {
var keys = Object.getKeys(data), l = keys.length, i, regex;
for (i = 0; i < l; i++) {
regex = new RegExp("{" + keys[i] + "}", "g");
template = template.replace(regex, data[keys[i]]);
}
return template;
}
};
TemplateSupport.addEngine("SimpleTemplate", {
engine: SimpleTemplate,
renderFn: function (template, data) {
return SimpleTemplate.render(template, data);
}
});
}
function detectTemplateSystems() {
var engines = {
Mustache: function (template, data) {
return root.Mustache.render(template, data);
}
}, keys = Object.getKeys(engines), l = keys.length, i, engine, renderFn;
for (i = 0; i < l; i++) {
engine = keys[i];
renderFn = engines[engine];
if (Object.prototype.hasOwnProperty.call(root, engine)) {
TemplateSupport.addEngine(engine, {
engine: root[engine],
renderFn: renderFn
});
}
}
}
TemplateSupport.init = function() {
addSimpleTemplate();
detectTemplateSystems();
};
TemplateSupport.addEngine = function(name, engineDef) {
TemplateSupport.ENGINES[name] = engineDef;
this.setEngine(name);
};
TemplateSupport.setEngine = function(name) {
this.engine = TemplateSupport.ENGINES[name];
};
TemplateSupport.render = function (template, data) {
return this.engine.renderFn(template, data);
};
TemplateSupport.ENGINES = {};
return TemplateSupport;
},
singleton: true
});
}(Allat));
/*global root,Allat */
(function (Allat) {
var $, prevHandler;
if (root.jQuery) {
$ = root.jQuery;
} else {
$ = function(callback) {
prevHandler = root.onload;
root.onload = function (e) {
if (prevHandler) {
prevHandler(e);
}
if (callback) {
callback(e);
}
};
};
}
Allat.module.define("BootModule", {
factory: function (BootModule) {
BootModule.init = function() {
$(Allat.util.proxy(function () {
this.services.EventBus.fireEvent("load");
}, this));
};
return BootModule;
},
dependencies: ["EventBus"],
singleton: true
});
Allat.module.instantiate("BootModule");
}(Allat));