OpenAjax.js 5.7 KB

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