OpenAjax.js 5.4 KB

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