Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
/*******************************************************************************
2
 * OpenAjax.js
3
 *
4
 * Reference implementation of the OpenAjax Hub, as specified by OpenAjax Alliance.
5
 * Specification is under development at:
6
 *
7
 *   http://www.openajax.org/member/wiki/OpenAjax_Hub_Specification
8
 *
9
 * Copyright 2006-2007 OpenAjax Alliance
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
12
 * use this file except in compliance with the License. You may obtain a copy
13
 * of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless
14
 * required by applicable law or agreed to in writing, software distributed
15
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
16
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
17
 * specific language governing permissions and limitations under the License.
18
 *
19
 ******************************************************************************/
20
 
21
// prevent re-definition of the OpenAjax object
22
if(!window["OpenAjax"]){
23
	OpenAjax = new function(){
24
		var t = true;
25
		var f = false;
26
		var g = window;
27
		var libs;
28
		var ooh = "org.openajax.hub.";
29
 
30
		var h = {};
31
		this.hub = h;
32
		h.implementer = "http://openajax.org";
33
		h.implVersion = "0.6";
34
		h.specVersion = "0.6";
35
		h.implExtraData = {};
36
		var libs = {};
37
		h.libraries = libs;
38
 
39
		h.registerLibrary = function(prefix, nsURL, version, extra){
40
			libs[prefix] = {
41
				prefix: prefix,
42
				namespaceURI: nsURL,
43
				version: version,
44
				extraData: extra
45
			};
46
			this.publish(ooh+"registerLibrary", libs[prefix]);
47
		}
48
		h.unregisterLibrary = function(prefix){
49
			this.publish(ooh+"unregisterLibrary", libs[prefix]);
50
			delete libs[prefix];
51
		}
52
 
53
		h._subscriptions = { c:{}, s:[] };
54
		h._cleanup = [];
55
		h._subIndex = 0;
56
		h._pubDepth = 0;
57
 
58
		h.subscribe = function(name, callback, scope, subscriberData, filter){
59
			if(!scope){
60
				scope = window;
61
			}
62
			var handle = name + "." + this._subIndex;
63
			var sub = { scope: scope, cb: callback, fcb: filter, data: subscriberData, sid: this._subIndex++, hdl: handle };
64
			var path = name.split(".");
65
	 		this._subscribe(this._subscriptions, path, 0, sub);
66
			return handle;
67
		}
68
 
69
		h.publish = function(name, message){
70
			var path = name.split(".");
71
			this._pubDepth++;
72
			this._publish(this._subscriptions, path, 0, name, message);
73
			this._pubDepth--;
74
			if((this._cleanup.length > 0) && (this._pubDepth == 0)){
75
				for(var i = 0; i < this._cleanup.length; i++){
76
					this.unsubscribe(this._cleanup[i].hdl);
77
				}
78
				delete(this._cleanup);
79
				this._cleanup = [];
80
			}
81
		}
82
 
83
		h.unsubscribe = function(sub){
84
			var path = sub.split(".");
85
			var sid = path.pop();
86
			this._unsubscribe(this._subscriptions, path, 0, sid);
87
		}
88
 
89
		h._subscribe = function(tree, path, index, sub){
90
			var token = path[index];
91
			if(index == path.length){
92
				tree.s.push(sub);
93
			}else{
94
				if(typeof tree.c == "undefined"){
95
					 tree.c = {};
96
				}
97
				if(typeof tree.c[token] == "undefined"){
98
					tree.c[token] = { c: {}, s: [] };
99
					this._subscribe(tree.c[token], path, index + 1, sub);
100
				}else{
101
					this._subscribe( tree.c[token], path, index + 1, sub);
102
				}
103
			}
104
		}
105
 
106
		h._publish = function(tree, path, index, name, msg){
107
			if(typeof tree != "undefined"){
108
				var node;
109
				if(index == path.length) {
110
					node = tree;
111
				}else{
112
					this._publish(tree.c[path[index]], path, index + 1, name, msg);
113
					this._publish(tree.c["*"], path, index + 1, name, msg);
114
					node = tree.c["**"];
115
				}
116
				if(typeof node != "undefined"){
117
					var callbacks = node.s;
118
					var max = callbacks.length;
119
					for(var i = 0; i < max; i++){
120
						if(callbacks[i].cb){
121
							var sc = callbacks[i].scope;
122
							var cb = callbacks[i].cb;
123
							var fcb = callbacks[i].fcb;
124
							var d = callbacks[i].data;
125
							if(typeof cb == "string"){
126
								// get a function object
127
								cb = sc[cb];
128
							}
129
							if(typeof fcb == "string"){
130
								// get a function object
131
								fcb = sc[fcb];
132
							}
133
							if((!fcb) ||
134
							   (fcb.call(sc, name, msg, d))) {
135
								cb.call(sc, name, msg, d);
136
							}
137
						}
138
					}
139
				}
140
			}
141
		}
142
 
143
		h._unsubscribe = function(tree, path, index, sid) {
144
			if(typeof tree != "undefined") {
145
				if(index < path.length) {
146
					var childNode = tree.c[path[index]];
147
					this._unsubscribe(childNode, path, index + 1, sid);
148
					if(childNode.s.length == 0) {
149
						for(var x in childNode.c)
150
					 		return;
151
						delete tree.c[path[index]];
152
					}
153
					return;
154
				}
155
				else {
156
					var callbacks = tree.s;
157
					var max = callbacks.length;
158
					for(var i = 0; i < max; i++)
159
						if(sid == callbacks[i].sid) {
160
							if(this._pubDepth > 0) {
161
								callbacks[i].cb = null;
162
								this._cleanup.push(callbacks[i]);
163
							}
164
							else
165
								callbacks.splice(i, 1);
166
							return;
167
						}
168
				}
169
			}
170
		}
171
		// The following function is provided for automatic testing purposes.
172
		// It is not expected to be deployed in run-time OpenAjax Hub implementations.
173
		h.reinit = function()
174
		{
175
			for (var lib in OpenAjax.hub.libraries) {
176
				delete OpenAjax.hub.libraries[lib];
177
			}
178
			OpenAjax.hub.registerLibrary("OpenAjax", "http://openajax.org/hub", "0.6", {});
179
 
180
			delete OpenAjax._subscriptions;
181
			OpenAjax._subscriptions = {c:{},s:[]};
182
			delete OpenAjax._cleanup;
183
			OpenAjax._cleanup = [];
184
			OpenAjax._subIndex = 0;
185
			OpenAjax._pubDepth = 0;
186
		}
187
	};
188
	// Register the OpenAjax Hub itself as a library.
189
	OpenAjax.hub.registerLibrary("OpenAjax", "http://openajax.org/hub", "0.6", {});
190
 
191
}