-
Notifications
You must be signed in to change notification settings - Fork 27
/
Core.Controls.js
71 lines (68 loc) · 2.55 KB
/
Core.Controls.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
/*globals Core, require*/
(function() {
var coreControls = function() {
var controls = { },
slice = [].slice,
sendMessage = function(controlId, message) {
var control = controls[controlId];
if (control === undefined) {
throw new Error("Attempt to send messages to a control that has not been registered: " + controlId);
}
else if (control.instance === null) {
throw new Error("Attempt to send messages to a control that has not been started: " + controlId);
}
else {
if (Object.prototype.toString.call(message.parameters) === '[object Array]') {
control.instance[message.name].apply(null, message.parameters);
}
else {
control.instance[message.name].call(null, message.parameters);
}
}
},
sendMessages = function(controlId, messages) {
messages.forEach(function(message) {
sendMessage(controlId, message);
});
};
return {
register: function(controlId, creationFunction) {
controls[controlId] = {
creator: creationFunction,
args: slice.call(arguments, 2),
instance: null
};
},
activate: function(controlId) {
if (controls[controlId] === undefined) {
throw new Error("Attempt to start a control that has not been registered: " + controlId);
}
else if (controls[controlId].instance === null) {
controls[controlId].instance = controls[controlId].creator.call();
controls[controlId].instance.activate();
}
},
destroy: function(controlId) {
if (controls[controlId] === undefined) {
throw new Error("Attempt to destroy a control that has not been registered: " + controlId);
}
else if (controls[controlId].instance !== null) {
controls[controlId].instance.destroy();
controls[controlId].instance = null;
}
},
sendMessage: sendMessage,
sendMessages: sendMessages
};
};
//manage require module loading scenario
if (typeof define === "function" && define.amd) {
define("Core.Controls", ["Core"], function (core) {
core.Controls = coreControls();
return core.Controls;
});
}
else {
Core.Controls = coreControls();
}
})();