_base.js 6.1 KB

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