Transfer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.ml.Transfer"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.wire.ml.Transfer"] = true;
  8. dojo.provide("dojox.wire.ml.Transfer");
  9. dojo.provide("dojox.wire.ml.ChildWire");
  10. dojo.provide("dojox.wire.ml.ColumnWire");
  11. dojo.provide("dojox.wire.ml.NodeWire");
  12. dojo.provide("dojox.wire.ml.SegmentWire");
  13. dojo.require("dijit._Widget");
  14. dojo.require("dijit._Container");
  15. dojo.require("dojox.wire._base");
  16. dojo.require("dojox.wire.ml.Action");
  17. dojo.declare("dojox.wire.ml.Transfer", dojox.wire.ml.Action, {
  18. // summary:
  19. // A widget to transfer values through source and target Wires
  20. // description:
  21. // This widget represents a controller task to transfer a value from
  22. // a source to a target, through a source and a target Wires, when
  23. // an event (a function) or a topic is issued.
  24. // If this widget has child ChildWire widgets, their _addWire()
  25. // methods are called to add Wire arguments to a source or a target
  26. // Wire.
  27. // source:
  28. // A source object and/or property
  29. // sourceStore:
  30. // A data store for a source data item
  31. // sourceAttribute:
  32. // An attribute of a source data item
  33. // sourcePath:
  34. // A simplified XPath to a source property of an XML element
  35. // type:
  36. // A type of the value to be transferred
  37. // converter:
  38. // A class name of a converter for the value to be transferred
  39. // target:
  40. // A target object and/or property
  41. // targetStore:
  42. // A data store for a target data item
  43. // targetAttribute:
  44. // An attribute of a target data item
  45. // targetPath:
  46. // A simplified XPath to a target property of an XML element
  47. source: "",
  48. sourceStore: "",
  49. sourceAttribute: "",
  50. sourcePath: "",
  51. type: "",
  52. converter: "",
  53. delimiter: "",
  54. target: "",
  55. targetStore: "",
  56. targetAttribute: "",
  57. targetPath: "",
  58. _run: function(){
  59. // summary:
  60. // Transfer a value from a source to a target
  61. // description:
  62. // First, Wires for a source and a target are created from attributes.
  63. // Then, a value is obtained by getValue() of the source Wire is set
  64. // by setValue() of the target Wire.
  65. // The arguments to this method is passed to getValue() and setValue()
  66. // of Wires, so that they can be used to identify the root objects off
  67. // the arguments.
  68. var sourceWire = this._getWire("source");
  69. var targetWire = this._getWire("target");
  70. dojox.wire.transfer(sourceWire, targetWire, arguments);
  71. },
  72. _getWire: function(/*String*/which){
  73. // summary:
  74. // Build Wire arguments from attributes
  75. // description:
  76. // Arguments object for a source or a target Wire, specified by
  77. // 'which' argument, are build from corresponding attributes,
  78. // including '*Store' (for 'dataStore'), '*Attribute'
  79. // (for 'attribute), '*Path' (for 'path'), 'type' and 'converter'.
  80. // 'source' or 'target' attribute is parsed as:
  81. // "object_id.property_name[.sub_property_name...]"
  82. // If 'source' or 'target' starts with "arguments", 'object'
  83. // argument for a Wire is set to null, so that the root object is
  84. // given as an event or topic arguments.
  85. // If this widget has child ChildWire widgets with a corresponding
  86. // 'which' attribute, their _addWire() methods are called to add
  87. // additional Wire arguments and nested Wire is created,
  88. // specifying the Wire defined by this widget to 'object' argument.
  89. // which:
  90. // Which Wire arguments to build, "source" or "target"
  91. // returns:
  92. // Wire arguments object
  93. var args = undefined;
  94. if(which == "source"){
  95. args = {
  96. object: this.source,
  97. dataStore: this.sourceStore,
  98. attribute: this.sourceAttribute,
  99. path: this.sourcePath,
  100. type: this.type,
  101. converter: this.converter
  102. };
  103. }else{ // "target"
  104. args = {
  105. object: this.target,
  106. dataStore: this.targetStore,
  107. attribute: this.targetAttribute,
  108. path: this.targetPath
  109. };
  110. }
  111. if(args.object){
  112. if(args.object.length >= 9 && args.object.substring(0, 9) == "arguments"){
  113. args.property = args.object.substring(9);
  114. args.object = null;
  115. }else{
  116. var i = args.object.indexOf('.');
  117. if(i < 0){
  118. args.object = dojox.wire.ml._getValue(args.object);
  119. }else{
  120. args.property = args.object.substring(i + 1);
  121. args.object = dojox.wire.ml._getValue(args.object.substring(0, i));
  122. }
  123. }
  124. }
  125. if(args.dataStore){
  126. args.dataStore = dojox.wire.ml._getValue(args.dataStore);
  127. }
  128. var childArgs = undefined;
  129. var children = this.getChildren();
  130. for(var i in children){
  131. var child = children[i];
  132. if(child instanceof dojox.wire.ml.ChildWire && child.which == which){
  133. if(!childArgs){
  134. childArgs = {};
  135. }
  136. child._addWire(this, childArgs);
  137. }
  138. }
  139. if(childArgs){ // make nested Wires
  140. childArgs.object = dojox.wire.create(args);
  141. childArgs.dataStore = args.dataStore;
  142. args = childArgs;
  143. }
  144. return args; //Object
  145. }
  146. });
  147. dojo.declare("dojox.wire.ml.ChildWire", dijit._Widget, {
  148. // summary:
  149. // A widget to add a child wire
  150. // description:
  151. // Attributes of this widget are used to add a child Wire to
  152. // a composite Wire of the parent Transfer widget.
  153. // which:
  154. // Which Wire to add a child Wire, "source" or "target", default to
  155. // "source"
  156. // object:
  157. // A root object for the value
  158. // property:
  159. // A property for the value
  160. // type:
  161. // A type of the value
  162. // converter:
  163. // A class name of a converter for the value
  164. // attribute:
  165. // A data item attribute for the value
  166. // path:
  167. // A simplified XPath for the value
  168. // name:
  169. // A composite property name
  170. which: "source",
  171. object: "",
  172. property: "",
  173. type: "",
  174. converter: "",
  175. attribute: "",
  176. path: "",
  177. name: "",
  178. _addWire: function(/*Transfer*/parent, /*Object*/args){
  179. // summary:
  180. // Add a child Wire to Wire arguments
  181. // description:
  182. // If 'name' attribute is specified, a child Wire is added as
  183. // the named property of 'children' object of 'args'.
  184. // Otherwise, a child Wire is added to 'children' array of 'args'.
  185. // parent:
  186. // A parent Transfer widget
  187. // args:
  188. // Wire arguments
  189. if(this.name){ // object
  190. if(!args.children){
  191. args.children = {};
  192. }
  193. args.children[this.name] = this._getWire(parent);
  194. }else{ // array
  195. if(!args.children){
  196. args.children = [];
  197. }
  198. args.children.push(this._getWire(parent));
  199. }
  200. },
  201. _getWire: function(/*Transfer*/parent){
  202. // summary:
  203. // Build child Wire arguments from attributes
  204. // description:
  205. // Arguments object for a child Wire are build from attributes,
  206. // including 'object', 'property', 'type', 'converter',
  207. // 'attribute' and 'path'.
  208. // parent:
  209. // A parent Transfer widget
  210. // returns:
  211. // Wire arguments object
  212. return {
  213. object: (this.object ? dojox.wire.ml._getValue(this.object) : undefined),
  214. property: this.property,
  215. type: this.type,
  216. converter: this.converter,
  217. attribute: this.attribute,
  218. path: this.path
  219. }; //Object
  220. }
  221. });
  222. dojo.declare("dojox.wire.ml.ColumnWire", dojox.wire.ml.ChildWire, {
  223. // summary:
  224. // A widget to add a column wire
  225. // description:
  226. // Attributes of this widget are used to add a column Wire to
  227. // a TableAdapter of the parent Transfer widget.
  228. // column:
  229. // A column name
  230. column: "",
  231. _addWire: function(/*Transfer*/parent, /*Object*/args){
  232. // summary:
  233. // Add a column Wire to Wire arguments
  234. // description:
  235. // If 'column' attribute is specified, a column Wire is added as
  236. // the named property of 'columns' object of 'args'.
  237. // Otherwise, a column Wire is added to 'columns' array of 'args'.
  238. // parent:
  239. // A parent Transfer widget
  240. // args:
  241. // Wire arguments
  242. if(this.column){ // object
  243. if(!args.columns){
  244. args.columns = {};
  245. }
  246. args.columns[this.column] = this._getWire(parent);
  247. }else{ // array
  248. if(!args.columns){
  249. args.columns = [];
  250. }
  251. args.columns.push(this._getWire(parent));
  252. }
  253. }
  254. });
  255. dojo.declare("dojox.wire.ml.NodeWire", [dojox.wire.ml.ChildWire, dijit._Container], {
  256. // summary:
  257. // A widget to add node wires
  258. // description:
  259. // Attributes of this widget are used to add node Wires to
  260. // a TreeAdapter of the parent Transfer widget.
  261. // titleProperty:
  262. // A property for the node title
  263. // titleAttribute:
  264. // A data item attribute for the node title
  265. // titlePath:
  266. // A simplified XPath for the node title
  267. titleProperty: "",
  268. titleAttribute: "",
  269. titlePath: "",
  270. _addWire: function(/*Transfer*/parent, /*Object*/args){
  271. // summary:
  272. // Add node Wires to Wire arguments
  273. // description:
  274. // Node Wires are added to 'nodes' array of 'args'.
  275. // parent:
  276. // A parent Transfer widget
  277. // args:
  278. // Wire arguments
  279. if(!args.nodes){
  280. args.nodes = [];
  281. }
  282. args.nodes.push(this._getWires(parent));
  283. },
  284. _getWires: function(/*Transfer*/parent){
  285. // summary:
  286. // Build node Wires arguments from attributes
  287. // description:
  288. // Arguments object for 'node' Wire are build from attributes,
  289. // including 'object', 'property', 'type', 'converter',
  290. // 'attribute' and 'path'.
  291. // Arguments object for 'title' Wire are build from another set of
  292. // attributes, 'titleProperty', 'titleAttribute' and 'titlePath'.
  293. // If this widget has child NodeWire widgets, their _getWires()
  294. // methods are called recursively to build 'children' array of
  295. // 'args'.
  296. // parent:
  297. // A parent Transfer widget
  298. // returns:
  299. // Wire arguments object
  300. var args = {
  301. node: this._getWire(parent),
  302. title: {
  303. type: "string",
  304. property: this.titleProperty,
  305. attribute: this.titleAttribute,
  306. path: this.titlePath
  307. }
  308. };
  309. var childArgs = [];
  310. var children = this.getChildren();
  311. for(var i in children){
  312. var child = children[i];
  313. if(child instanceof dojox.wire.ml.NodeWire){
  314. childArgs.push(child._getWires(parent));
  315. }
  316. }
  317. if(childArgs.length > 0){
  318. args.children = childArgs;
  319. }
  320. return args; //Object
  321. }
  322. });
  323. dojo.declare("dojox.wire.ml.SegmentWire", dojox.wire.ml.ChildWire, {
  324. // summary:
  325. // A widget to add a segment wire
  326. // description:
  327. // Attributes of this widget are used to add a segment Wire to
  328. // a TextAdapter of the parent Transfer widget.
  329. _addWire: function(/*Transfer*/parent, /*Object*/args){
  330. // summary:
  331. // Add a segument Wire to Wire arguments
  332. // description:
  333. // A segment Wire is added to 'segments' array of 'args'.
  334. // If 'parent' has 'delimiter' attribute, it is used for
  335. // 'delimiter' property of 'args'.
  336. // parent:
  337. // A parent Transfer widget
  338. // args:
  339. // Wire arguments
  340. if(!args.segments){
  341. args.segments = [];
  342. }
  343. args.segments.push(this._getWire(parent));
  344. if(parent.delimiter && !args.delimiter){
  345. args.delimiter = parent.delimiter;
  346. }
  347. }
  348. });
  349. }