OpenAjax.js 5.5 KB

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