_base.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. if(!dojo._hasResource["dojox.wire._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.wire._base"] = true;
  8. dojo.provide("dojox.wire._base");
  9. dojox.wire._defaultWireClass = "dojox.wire.Wire";
  10. dojox.wire._wireClasses = {
  11. "attribute": "dojox.wire.DataWire",
  12. "path": "dojox.wire.XmlWire",
  13. "children": "dojox.wire.CompositeWire",
  14. "columns": "dojox.wire.TableAdapter",
  15. "nodes": "dojox.wire.TreeAdapter",
  16. "segments": "dojox.wire.TextAdapter"
  17. };
  18. dojox.wire.register = function(/*Function||String*/wireClass, /*String*/key){
  19. // summary:
  20. // Register a Wire class
  21. // desription:
  22. // The specified Wire class or a class name is registered with
  23. // a key property of arguments to create a Wire
  24. // wireClass:
  25. // A class or full qualified class name
  26. // key:
  27. // A key property of arguments to create a Wire
  28. if(!wireClass || !key){
  29. return; //undefined
  30. }
  31. if(dojox.wire._wireClasses[key]){ // key already in use
  32. return; //undefined
  33. }
  34. dojox.wire._wireClasses[key] = wireClass;
  35. };
  36. dojox.wire._getClass = function(/*String*/name){
  37. // summary:
  38. // Returns a class
  39. // description:
  40. // The class is loaded by dojo.require() and returned
  41. // by dojo.getObject().
  42. // name:
  43. // A class name
  44. // returns:
  45. // A class
  46. dojo["require"](name); // use dojo["require"] instead of dojo.require to avoid a build problem
  47. return dojo.getObject(name); //Function
  48. };
  49. dojox.wire.create = function(/*Object*/args){
  50. // summary:
  51. // Create a Wire from arguments
  52. // description:
  53. // If 'args' specifies 'wireClass', it is used as a class or full
  54. // qualified class name to create a Wire with 'args' as arguments.
  55. // Otherwise, a Wire class is determined by other proeprties of 'args'
  56. // checking if 'args' specifies a key property for a Wire class.
  57. // If no key property found, the default Wire class is used.
  58. // args:
  59. // Arguments to create a Wire
  60. // returns:
  61. // A Wire
  62. if(!args){
  63. args = {};
  64. }
  65. var wireClass = args.wireClass;
  66. if(wireClass){
  67. if(dojo.isString(wireClass)){
  68. wireClass = dojox.wire._getClass(wireClass);
  69. }
  70. }else{
  71. for(var key in args){
  72. if(!args[key]){
  73. continue;
  74. }
  75. wireClass = dojox.wire._wireClasses[key];
  76. if(wireClass){
  77. if(dojo.isString(wireClass)){
  78. wireClass = dojox.wire._getClass(wireClass);
  79. dojox.wire._wireClasses[key] = wireClass;
  80. }
  81. break;
  82. }
  83. }
  84. }
  85. if(!wireClass){
  86. if(dojo.isString(dojox.wire._defaultWireClass)){
  87. dojox.wire._defaultWireClass = dojox.wire._getClass(dojox.wire._defaultWireClass);
  88. }
  89. wireClass = dojox.wire._defaultWireClass;
  90. }
  91. return new wireClass(args); //Object
  92. };
  93. dojox.wire.isWire = function(/*Object*/wire){
  94. // summary:
  95. // Check if an object is a Wire
  96. // description:
  97. // If the specified object is a Wire, true is returned.
  98. // Otherwise, false is returned.
  99. // wire:
  100. // An object to check
  101. // returns:
  102. // True if the object is a Wire, otherwise false
  103. return (wire && wire._wireClass); //Boolean
  104. };
  105. dojox.wire.transfer = function(/*Wire||Object*/source, /*Wire||Object*/target, /*Object?*/defaultObject, /*Object?*/defaultTargetObject){
  106. // summary:
  107. // Transfer a source value to a target value
  108. // description:
  109. // If 'source' and/or 'target' are not Wires, Wires are created with
  110. // them as arguments.
  111. // A value is got through the source Wire and set through the target
  112. // Wire.
  113. // 'defaultObject' is passed to Wires as a default root object.
  114. // If 'defaultTargetObject' is specified, it is passed to the target
  115. // Wire as a default root object, instead of 'defaultObject'.
  116. // source:
  117. // A Wire or arguments to create a Wire for a source value
  118. // target:
  119. // A Wire or arguments to create a Wire for a target value
  120. // defaultObject:
  121. // defaultTargetObject;
  122. // Optional default root objects passed to Wires
  123. if(!source || !target){
  124. return; //undefined
  125. }
  126. if(!dojox.wire.isWire(source)){
  127. source = dojox.wire.create(source);
  128. }
  129. if(!dojox.wire.isWire(target)){
  130. target = dojox.wire.create(target);
  131. }
  132. var value = source.getValue(defaultObject);
  133. target.setValue(value, (defaultTargetObject || defaultObject));
  134. };
  135. dojox.wire.connect = function(/*Object*/trigger, /*Wire||Object*/source, /*Wire||Object*/target){
  136. // summary:
  137. // Transfer a source value to a target value on a trigger event or
  138. // topic
  139. // description:
  140. // If 'trigger' specifies 'topic', the topic is subscribed to transer
  141. // a value on the topic.
  142. // Otherwise, the event specified to 'event' of 'trigger' is listened
  143. // to transfer a value.
  144. // On the specified event or topic, transfer() is called with
  145. // 'source', 'target' and the arguments of the event or topic (as
  146. // default root objects).
  147. // trigger:
  148. // An event or topic to trigger a transfer
  149. // source:
  150. // A Wire or arguments to create a Wire for a source value
  151. // target:
  152. // A Wire or arguments to create a Wire for a target value
  153. // returns:
  154. // A connection handle for disconnect()
  155. if(!trigger || !source || !target){
  156. return; //undefined
  157. }
  158. var connection = {topic: trigger.topic};
  159. if(trigger.topic){
  160. connection.handle = dojo.subscribe(trigger.topic, function(){
  161. dojox.wire.transfer(source, target, arguments);
  162. });
  163. }else if(trigger.event){
  164. connection.handle = dojo.connect(trigger.scope, trigger.event, function(){
  165. dojox.wire.transfer(source, target, arguments);
  166. });
  167. }
  168. return connection; //Object
  169. };
  170. dojox.wire.disconnect = function(/*Object*/connection){
  171. // summary:
  172. // Remove a connection or subscription for transfer
  173. // description:
  174. // If 'handle' has 'topic', the topic is unsubscribed.
  175. // Otherwise, the listener to an event is removed.
  176. // connection:
  177. // A connection handle returned by connect()
  178. if(!connection || !connection.handle){
  179. return; //undefined
  180. }
  181. if(connection.topic){
  182. dojo.unsubscribe(connection.handle);
  183. }else{
  184. dojo.disconnect(connection.handle);
  185. }
  186. };
  187. }