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