summaryrefslogtreecommitdiff
path: root/frontend/beta/js/Clipperz/NotificationCenter.js
Unidiff
Diffstat (limited to 'frontend/beta/js/Clipperz/NotificationCenter.js') (more/less context) (show whitespace changes)
-rw-r--r--frontend/beta/js/Clipperz/NotificationCenter.js325
1 files changed, 325 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/NotificationCenter.js b/frontend/beta/js/Clipperz/NotificationCenter.js
new file mode 100644
index 0000000..815d4fd
--- a/dev/null
+++ b/frontend/beta/js/Clipperz/NotificationCenter.js
@@ -0,0 +1,325 @@
1/*
2
3Copyright 2008-2011 Clipperz Srl
4
5This file is part of Clipperz's Javascript Crypto Library.
6Javascript Crypto Library provides web developers with an extensive
7and efficient set of cryptographic functions. The library aims to
8obtain maximum execution speed while preserving modularity and
9reusability.
10For further information about its features and functionalities please
11refer to http://www.clipperz.com
12
13* Javascript Crypto Library is free software: you can redistribute
14 it and/or modify it under the terms of the GNU Affero General Public
15 License as published by the Free Software Foundation, either version
16 3 of the License, or (at your option) any later version.
17
18* Javascript Crypto Library is distributed in the hope that it will
19 be useful, but WITHOUT ANY WARRANTY; without even the implied
20 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU Affero General Public License for more details.
22
23* You should have received a copy of the GNU Affero General Public
24 License along with Javascript Crypto Library. If not, see
25 <http://www.gnu.org/licenses/>.
26
27*/
28
29if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
30if (typeof(Clipperz.NotificationCenter) == 'undefined') { Clipperz.NotificationCenter = {}; }
31
32
33//#############################################################################
34
35Clipperz.NotificationCenterEvent = function(args) {
36 args = args || {};
37 //MochiKit.Base.bindMethods(this);
38
39 this._source = args.source || null;
40 this._event = args.event || null;
41 this._parameters = args.parameters || null;
42 this._isSynchronous = args.isSynchronous || false;
43
44 return this;
45}
46
47//=============================================================================
48
49Clipperz.NotificationCenterEvent.prototype = MochiKit.Base.update(null, {
50
51 //-------------------------------------------------------------------------
52
53 'toString': function() {
54 return "Clipperz.NotificationCenterEvent";
55 //return "Clipperz.NotificationCenterEvent {source: " + this.source() + ", event: " + this.event() + ", parameters: " + this.parameters() + "}";
56 },
57
58 //-------------------------------------------------------------------------
59
60 'source': function() {
61 return this._source;
62 },
63
64 'setSource': function(aValue) {
65 this._source = aValue;
66 },
67
68 //-------------------------------------------------------------------------
69
70 'event': function() {
71 return this._event;
72 },
73
74 'setEvent': function(aValue) {
75 this._event = aValue;
76 },
77
78 //-------------------------------------------------------------------------
79
80 'parameters': function() {
81 return this._parameters;
82 },
83
84 'setParameters': function(aValue) {
85 this._parameters = aValue;
86 },
87
88 //-------------------------------------------------------------------------
89
90 'isSynchronous': function() {
91 return this._isSynchronous;
92 },
93
94 //-------------------------------------------------------------------------
95 __syntaxFix__: "syntax fix"
96});
97
98
99//#############################################################################
100//#############################################################################
101
102Clipperz.NotificationCenter = function(args) {
103 args = args || {};
104 //MochiKit.Base.bindMethods(this);
105
106 this._listeners = {};
107 this._useSynchronousListenerInvocation = args.useSynchronousListenerInvocation || false;
108 this._timeoutDelay = args.timeoutDelay || 0.1;
109
110 return this;
111}
112
113//=============================================================================
114
115Clipperz.NotificationCenter.prototype = MochiKit.Base.update(null, {
116
117 //-------------------------------------------------------------------------
118
119 'toString': function() {
120 return "Clipperz.NotificationCenter";
121 },
122
123 //-------------------------------------------------------------------------
124
125 'useSynchronousListenerInvocation': function() {
126 return this._useSynchronousListenerInvocation;
127 },
128
129 'setUseSynchronousListenerInvocation': function(aValue) {
130 this._useSynchronousListenerInvocation = aValue;
131 },
132
133 //-------------------------------------------------------------------------
134
135 'timeoutDelay': function() {
136 return this._timeoutDelay;
137 },
138
139 'setTimeoutDelay': function(aValue) {
140 this._timeoutDelay = aValue;
141 },
142
143 //-------------------------------------------------------------------------
144
145 'listeners': function() {
146 return this._listeners;
147 },
148
149 //-------------------------------------------------------------------------
150
151 'register': function(aSource, anEvent, aListener, aMethod) {
152 vareventListeners;
153 varlistenerInfo;
154 vareventKey;
155
156 if (anEvent != null) {
157 eventKey = anEvent;
158 } else {
159 eventKey = '_notificationCenter_matchAnyEvent_key_';
160 }
161
162 eventListeners = this.listeners()[eventKey];
163
164 if (eventListeners == null) {
165 eventListeners = [];
166 this.listeners()[eventKey] = eventListeners;
167 }
168
169 listenerInfo = {};
170 if (aSource != null) {
171 listenerInfo['source'] = aSource;
172 } else {
173 listenerInfo['source'] = 'any';
174 }
175
176 listenerInfo['listener'] = aListener;
177 listenerInfo['method'] = aMethod;
178
179 eventListeners.push(listenerInfo);
180
181 return listenerInfo;
182 },
183
184 //-------------------------------------------------------------------------
185
186 'removeListenerInfoFromListeners': function(aListener, someListeners) {
187 varlistenerIndex;
188 vari,c;
189
190 if (someListeners != null) {
191 listenerIndex = -1;
192 c = someListeners.length;
193 for (i=0; i<c; i++) {
194 varlistenerInfo;
195
196 listenerInfo = someListeners[i];
197 if (listenerInfo['listener'] === aListener) {
198 listenerIndex = i;
199 }
200 }
201
202 if (listenerIndex != -1) {
203 Clipperz.Base.removeObjectAtIndexFromArray(listenerIndex, someListeners);
204 }
205 }
206 },
207
208 //-------------------------------------------------------------------------
209
210 'unregister': function(aListener, anEvent) {
211 if (anEvent == null) {
212 varallListenerList;
213 vari, c;
214
215 // allListenerList = Clipperz.Base.values(this.listeners());
216 allListenerList = MochiKit.Base.values(this.listeners());
217 c = allListenerList.length;
218 for (i=0; i<c; i++) {
219 this.removeListenerInfoFromListeners(aListener, allListenerList[i]);
220 }
221 } else {
222 this.removeListenerInfoFromListeners(aListener, this.listeners()[anEvent]);
223 }
224 },
225
226 //-------------------------------------------------------------------------
227
228 'asysnchronousListenerNotification': function(anEventInfo, aMethod, aListener) {
229 MochiKit.Async.callLater(this.timeoutDelay(), MochiKit.Base.partial(MochiKit.Base.methodcaller(aMethod, anEventInfo), aListener));
230 // setTimeout(MochiKit.Base.partial(MochiKit.Base.methodcaller(aMethod, anEventInfo), aListener), this.timeoutDelay());
231 },
232
233 //-------------------------------------------------------------------------
234
235 'processListenerInfo': function(anEventInfo, aListenerInfo) {
236 varshouldInvokeListenerMethod;
237
238 if (aListenerInfo['source'] == 'any') {
239 shouldInvokeListenerMethod = true;
240 } else {
241 if (aListenerInfo['source'] === anEventInfo.source()) {
242 shouldInvokeListenerMethod = true;
243 } else {
244 shouldInvokeListenerMethod = false;
245 }
246 }
247
248 if (shouldInvokeListenerMethod) {
249 if (this.useSynchronousListenerInvocation() || anEventInfo.isSynchronous()) {
250//MochiKit.Logging.logDebug("syncrhronous listener invocation");
251 try {
252 // MochiKit.Base.map(MochiKit.Base.methodcaller(aListenerInfo['method'], anEventInfo), [aListenerInfo['listener']]);
253//console.log("notification: ", aListenerInfo['listener'], aListenerInfo['method'], anEventInfo);
254 MochiKit.Base.method(aListenerInfo['listener'], aListenerInfo['method'], anEventInfo)();
255 } catch(exception) {
256 MochiKit.Logging.logError('NotificationCenter ERROR: unable to invoke method \'' + aListenerInfo['method'] + '\' on object ' + aListenerInfo['listener']);
257 }
258 } else {
259 var asyncMethod;
260
261//MochiKit.Logging.logDebug("asyncrhronous listener invocation");
262 asyncMethod = MochiKit.Base.bind(this.asysnchronousListenerNotification, this)
263 MochiKit.Base.map(MochiKit.Base.partial(asyncMethod, anEventInfo, aListenerInfo['method']), [aListenerInfo['listener']]);
264 }
265 }
266 },
267
268 //-------------------------------------------------------------------------
269
270 'notify': function(aSource, anEvent, someEventParameters, isSynchronous) {
271 vareventInfo;
272 var processInfoMethod;
273
274//MochiKit.Logging.logDebug(">>> NotificationCenter.notify");
275 eventInfo = new Clipperz.NotificationCenterEvent({source:aSource, event:anEvent, parameters:someEventParameters, isSynchronous:isSynchronous});
276//MochiKit.Logging.logDebug("--- NotificationCenter.notify - 1");
277 processInfoMethod = MochiKit.Base.bind(this.processListenerInfo, this);
278//MochiKit.Logging.logDebug("--- NotificationCenter.notify - 2");
279
280 MochiKit.Base.map(MochiKit.Base.partial(processInfoMethod, eventInfo), this.listeners()[anEvent] || []);
281//MochiKit.Logging.logDebug("--- NotificationCenter.notify - 3");
282 MochiKit.Base.map(MochiKit.Base.partial(processInfoMethod, eventInfo), this.listeners()['_notificationCenter_matchAnyEvent_key_'] || []);
283//MochiKit.Logging.logDebug("<<< NotificationCenter.notify");
284 },
285
286 //-------------------------------------------------------------------------
287
288 'deferredNotification': function(aSource, anEvent, someEventParameters, aDeferredResult) {
289
290 this.notify(aSource, anEvent, someEventParameters, true);
291
292 return aDeferredResult;
293 // return MochiKit.Async.wait(1, aDeferredResult);
294 },
295
296 //-------------------------------------------------------------------------
297
298 'resetStatus': function() {
299 this._listeners = {};
300 },
301
302 //-------------------------------------------------------------------------
303 __syntaxFix__: "syntax fix"
304
305});
306
307//#############################################################################
308
309Clipperz.NotificationCenter.defaultNotificationCenter = new Clipperz.NotificationCenter();
310
311Clipperz.NotificationCenter.notify = MochiKit.Base.method(Clipperz.NotificationCenter.defaultNotificationCenter, 'notify');
312Clipperz.NotificationCenter.register = MochiKit.Base.method(Clipperz.NotificationCenter.defaultNotificationCenter, 'register');
313Clipperz.NotificationCenter.unregister = MochiKit.Base.method(Clipperz.NotificationCenter.defaultNotificationCenter, 'unregister');
314Clipperz.NotificationCenter.deferredNotification = MochiKit.Base.method(Clipperz.NotificationCenter.defaultNotificationCenter, 'deferredNotification');
315/*
316_clipperz_notificationCenter_defaultNotificationCenter = null;
317
318Clipperz.NotificationCenter.defaultNotificationCenter = function() {
319 if (_clipperz_notificationCenter_defaultNotificationCenter == null) {
320 _clipperz_notificationCenter_defaultNotificationCenter = new Clipperz.NotificationCenter();
321 }
322
323 return _clipperz_notificationCenter_defaultNotificationCenter;
324};
325*/