]> git.wh0rd.org - tt-rss.git/blob - lib/dojo/OpenAjax.js
update dojo to 1.7.3
[tt-rss.git] / lib / dojo / OpenAjax.js
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 // 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,
47 extraData: extra
48 };
49 this.publish(ooh+"registerLibrary", libs[prefix]);
50 };
51 h.unregisterLibrary = function(prefix){
52 this.publish(ooh+"unregisterLibrary", libs[prefix]);
53 delete libs[prefix];
54 };
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;
70 };
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)){
78 for(var i = 0; i < this._cleanup.length; i++){
79 this.unsubscribe(this._cleanup[i].hdl);
80 }
81 delete(this._cleanup);
82 this._cleanup = [];
83 }
84 };
85
86 h.unsubscribe = function(sub){
87 var path = sub.split(".");
88 var sid = path.pop();
89 this._unsubscribe(this._subscriptions, path, 0, sid);
90 };
91
92 h._subscribe = function(tree, path, index, sub){
93 var token = path[index];
94 if(index == path.length){
95 tree.s.push(sub);
96 }else{
97 if(typeof tree.c == "undefined"){
98 tree.c = {};
99 }
100 if(typeof tree.c[token] == "undefined"){
101 tree.c[token] = { c: {}, s: [] };
102 this._subscribe(tree.c[token], path, index + 1, sub);
103 }else{
104 this._subscribe(tree.c[token], path, index + 1, sub);
105 }
106 }
107 };
108
109 h._publish = function(tree, path, index, name, msg){
110 if(typeof tree != "undefined"){
111 var node;
112 if(index == path.length){
113 node = tree;
114 }else{
115 this._publish(tree.c[path[index]], path, index + 1, name, msg);
116 this._publish(tree.c["*"], path, index + 1, name, msg);
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 }
136 if((!fcb) ||
137 (fcb.call(sc, name, msg, d))){
138 cb.call(sc, name, msg, d);
139 }
140 }
141 }
142 }
143 }
144 };
145
146 h._unsubscribe = function(tree, path, index, sid){
147 if(typeof tree != "undefined"){
148 if(index < path.length){
149 var childNode = tree.c[path[index]];
150 this._unsubscribe(childNode, path, index + 1, sid);
151 if(childNode.s.length == 0){
152 for(var x in childNode.c)
153 return;
154 delete tree.c[path[index]];
155 }
156 return;
157 }
158 else{
159 var callbacks = tree.s;
160 var max = callbacks.length;
161 for(var i = 0; i < max; i++)
162 if(sid == callbacks[i].sid){
163 if(this._pubDepth > 0){
164 callbacks[i].cb = null;
165 this._cleanup.push(callbacks[i]);
166 }
167 else
168 callbacks.splice(i, 1);
169 return;
170 }
171 }
172 }
173 };
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
194 }