source: Dev/trunk/src/client/dojo/OpenAjax.js @ 527

Last change on this file since 527 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 5.4 KB
Line 
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
22if(!window["OpenAjax"]){
23        OpenAjax = new function(){
24                // summary:
25                //              the OpenAjax hub
26                // description:
27                //              see http://www.openajax.org/member/wiki/OpenAjax_Hub_Specification
28
29                var libs = {};
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 = {};
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,
45                                extraData: extra
46                        };
47                        this.publish(ooh+"registerLibrary", libs[prefix]);
48                };
49                h.unregisterLibrary = function(prefix){
50                        this.publish(ooh+"unregisterLibrary", libs[prefix]);
51                        delete libs[prefix];
52                };
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;
68                };
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)){
76                                for(var i = 0; i < this._cleanup.length; i++){
77                                        this.unsubscribe(this._cleanup[i].hdl);
78                                }
79                                delete(this._cleanup);
80                                this._cleanup = [];
81                        }
82                };
83
84                h.unsubscribe = function(sub){
85                        var path = sub.split(".");
86                        var sid = path.pop();
87                        this._unsubscribe(this._subscriptions, path, 0, sid);
88                };
89               
90                h._subscribe = function(tree, path, index, sub){
91                        var token = path[index];
92                        if(index == path.length){
93                                tree.s.push(sub);
94                        }else{
95                                if(typeof tree.c == "undefined"){
96                                        tree.c = {};
97                                }
98                                if(typeof tree.c[token] == "undefined"){
99                                        tree.c[token] = { c: {}, s: [] };
100                                }
101                                this._subscribe(tree.c[token], path, index + 1, sub);
102                        }
103                };
104
105                h._publish = function(tree, path, index, name, msg){
106                        if(typeof tree != "undefined"){
107                                var node;
108                                if(index == path.length){
109                                        node = tree;
110                                }else{
111                                        this._publish(tree.c[path[index]], path, index + 1, name, msg);
112                                        this._publish(tree.c["*"], path, index + 1, name, msg);
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                                                        }
132                                                        if((!fcb) ||
133                                                                (fcb.call(sc, name, msg, d))){
134                                                                cb.call(sc, name, msg, d);
135                                                        }
136                                                }
137                                        }
138                                }
139                        }
140                };
141                       
142                h._unsubscribe = function(tree, path, index, sid){
143                        if(typeof tree != "undefined"){
144                                if(index < path.length){
145                                        var childNode = tree.c[path[index]];
146                                        this._unsubscribe(childNode, path, index + 1, sid);
147                                        if(childNode.s.length == 0){
148                                                for(var x in childNode.c)
149                                                        return;
150                                                delete tree.c[path[index]];
151                                        }
152                                        return;
153                                }
154                                else{
155                                        var callbacks = tree.s;
156                                        var max = callbacks.length;
157                                        for(var i = 0; i < max; i++){
158                                                if(sid == callbacks[i].sid){
159                                                        if(this._pubDepth > 0){
160                                                                callbacks[i].cb = null;
161                                                                this._cleanup.push(callbacks[i]);
162                                                        }
163                                                        else
164                                                                callbacks.splice(i, 1);
165                                                        return;
166                                                }
167                                        }
168                                }
169                        }
170                };
171
172                // The following function is provided for automatic testing purposes.
173                // It is not expected to be deployed in run-time OpenAjax Hub implementations.
174                h.reinit = function(){
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
189        // Register the OpenAjax Hub itself as a library.
190        OpenAjax.hub.registerLibrary("OpenAjax", "http://openajax.org/hub", "0.6", {});
191
192}
Note: See TracBrowser for help on using the repository browser.