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