-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCore.js
166 lines (155 loc) · 5.88 KB
/
Core.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
/*globals Sandbox*/
(function () {
var core = function () {
// Private Variable
var modules = {},
slice = [].slice,
//the sandbox factory allows the default sandbox behaviour to be replaced at runtime allowing for easier unit testing
//the default sandbox factory allows dynamic addition of functions to the sandbox at runtime
sandboxFactory = (function () {
var sandboxFunctions = {};
return {
getNewSandbox: function (core, moduleId) {
var newSandbox = new Sandbox(core, moduleId);
for (var property in sandboxFunctions) {
if (sandboxFunctions.hasOwnProperty(property)) {
var sandboxFunction = (function () {
var returnValue,
newFunctionDefinition = sandboxFunctions[property];
if (newFunctionDefinition.passModuleId) {
returnValue = function () {
var functionArguments = slice.call(arguments, 0);
functionArguments.push(moduleId);
return newFunctionDefinition.functionPointer.apply(newFunctionDefinition.context, functionArguments);
};
}
else {
returnValue = function () {
return newFunctionDefinition.functionPointer.apply(newFunctionDefinition.context, slice.call(arguments, 0));
};
}
return returnValue;
})();
newSandbox[property] = sandboxFunction;
}
}
return newSandbox;
},
addSandboxFunction: function (name, func, context, passModuleId) {
sandboxFunctions[name] = { functionPointer: func, context: context, passModuleId: passModuleId };
}
};
})(),
createInstance = function (moduleId, sandbox) {
var module = modules[moduleId],
i,
arrayLength,
args = module.args,
newArguments = [],
instance;
newArguments.push(sandbox);
for (i = 0, arrayLength = args.length; i < arrayLength; i++) {
newArguments.push(args[i]);
}
instance = modules[moduleId].creator.apply(null, newArguments);
//if (Core.Error !== undefined) {
// Core.Error.sanitise(instance);
// Core.Error.sanitise(sandbox);
//}
return instance;
};
return {
setSandboxFactory: function (newFactory) {
sandboxFactory = newFactory;
},
getSandboxFactory: function () {
return sandboxFactory;
},
register: function (moduleId, Creator) {
modules[moduleId] = {
creator: Creator,
instance: null,
args: slice.call(arguments, 2)
};
},
registerArguments: function (moduleId) {
var module = modules[moduleId];
if (module === null || module === undefined) { }
else {
module.args = slice.call(arguments, 1);
}
},
start: function (moduleId) {
if (modules[moduleId] === undefined) {
throw new Error("Attempt to start a module that has not been registered: " + moduleId);
}
//we don't want to start the module twice
else if (modules[moduleId].instance === null) {
modules[moduleId].instance = createInstance(moduleId, sandboxFactory.getNewSandbox(this, moduleId));
modules[moduleId].instance.activate();
}
},
stop: function (moduleId) {
if (modules[moduleId] === undefined) {
throw new Error("Attempt to stop a module that has not been registered: " + moduleId);
}
var data = modules[moduleId];
if (data.instance) {
data.instance.destroy();
data.instance = null;
}
},
startAll: function () {
var moduleId;
for (moduleId in modules) {
if (modules.hasOwnProperty(moduleId)) {
this.start(moduleId);
}
}
},
stopAll: function () {
var moduleId;
for (moduleId in modules) {
if (modules.hasOwnProperty(moduleId)) {
this.stop(moduleId);
}
}
},
moduleIsActive: function (moduleId) {
var returnValue;
if (modules[moduleId] === undefined) {
returnValue = false;
}
else if (modules[moduleId].instance === null) {
returnValue = false;
}
else {
returnValue = true;
}
return returnValue;
},
getModule: function (moduleId) {
var returnValue;
if (modules[moduleId] === undefined) {
returnValue = null;
}
else if (modules[moduleId].instance === null) {
returnValue = null;
}
else {
returnValue = modules[moduleId].instance;
}
return returnValue;
}
};
};
// Expose Core as an AMD module
if (typeof define === "function" && define.amd) {
define("Core", [], function () {
return core();
});
}
else {
window['Core'] = core();
}
})();