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