-
Notifications
You must be signed in to change notification settings - Fork 0
/
modulo.js
77 lines (62 loc) · 2.01 KB
/
modulo.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
var app = app || {};
(function () {
/* Hacks
IE7 and IE8
*/
if (!Array.prototype.some) {
Array.prototype.some = (function () {
return function (fn) {
var result = false;
for (var i = 0; i < this.length; i++) {
if (fn(this[i], i, this)) {
result = true;
break;
}
}
return result;
};
}());
}
var core = {};
var libs = {};
core.extensions = {};
function registerLib(id, obj) {
libs[id] = obj;
}
function registerExtension(id, fn) {
if (core.extensions[id]) console.error('there is an extension with name: "' + id + '"');
core.extensions[id] = fn(libs);
}
var Sandbox = function (moduleId, element) {
var sandbox = {};
sandbox.element = element;
sandbox.subscribe = function (channel, fn) {
return core.extensions.pubsub.subscribe(channel, fn);
};
sandbox.publish = function (channel, msg) {
core.extensions.pubsub.publish(channel, msg);
};
sandbox.unsubscribe = function (token) {
return core.extensions.pubsub.unsubscribe(token);
};
sandbox.extensions = (function () { return core.extensions; }());
return sandbox;
};
core.registerModule = function (id, htmlFile, constructor) {
var moduleElement = document.getElementById(id);
function fn(html) {
if(moduleElement)
moduleElement.innerHTML = html;
constructor(new Sandbox(id, moduleElement));
}
if (typeof htmlFile !== 'function')
core.extensions.http.get(htmlFile, fn);
else {
constructor = constructor || htmlFile;
fn('');
}
};
app.registerModule = core.registerModule;
app.registerLib = registerLib;
app.registerExtension = registerExtension;
}());