forked from brunschgi/terrificjs
/
Tc.Connector.js
executable file
·105 lines (93 loc) · 3.06 KB
/
Tc.Connector.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
(function($) {
"use strict";
/**
* Base class for the different connectors.
*
* @author Remo Brunschwiler
* @namespace Tc
* @class Connector
*/
Tc.Connector = Class.extend({
/**
* Initializes the Connector.
*
* @method init
* @return {void}
* @constructor
* @param {String} connectorId
* The unique connector ID
*/
init : function(connectorId) {
this.connectorId = connectorId;
this.components = [];
},
/**
* Registers a component.
*
* @method registerComponent
* @param {Module} component
* The module to register
* @param {String} role
* The role of the module (e.g. master, slave etc.)
* @return {void}
*/
registerComponent: function(component, role) {
role = role || 'standard';
this.components.push({
'component': component,
'role': role
});
},
/**
* Unregisters a component.
*
* @method unregisterComponent
* @param {Module} component
* The module to unregister
* @return {void}
*/
unregisterComponent: function(component) {
var components = this.components;
for (var i = 0, len = components.length; i < len; i++) {
if(components[i] && components[i].component === component) {
delete components[i];
}
}
},
/**
* Notifies all registered components about a state change
* This can be be overriden in the specific connectors.
*
* @method notify
* @param {Module} origin
* The module that sends the state change
* @param {String} state
* The component's state
* @param {Object} data
* Contains the state relevant data (if any)
* @param {Function} callback
* The callback function, it can be executed after an asynchronous
* action.
* @return {boolean}
* Indicates whether the default action should be excuted or not
*/
notify: function(origin, state, data, callback) {
/**
* Gives the components the ability to prevent the default- and
* afteraction from the events by returning false in the
* on {Event}-Handler.
*/
var proceed = true,
components = this.components;
for (var i = 0, len = components.length; i < len; i++) {
var component = components[i].component;
if (component && component !== origin && component[state]) {
if (component[state](data, callback) === false) {
proceed = false;
}
}
}
return proceed;
}
});
})(Tc.$);