f9bbd5b04dd385ac3c104f03aad86a048f0edd68.svn-base 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. if (!dojo._hasResource["dojox.wire.ml.Action"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.wire.ml.Action"] = true;
  6. dojo.provide("dojox.wire.ml.Action");
  7. dojo.provide("dojox.wire.ml.ActionFilter");
  8. dojo.require("dijit._Widget");
  9. dojo.require("dijit._Container");
  10. dojo.require("dojox.wire.Wire");
  11. dojo.require("dojox.wire.ml.util");
  12. dojo.declare("dojox.wire.ml.Action", [dijit._Widget, dijit._Container], {
  13. // summary:
  14. // A base widget to "run" a task on an event or a topic
  15. // description:
  16. // This widget represents a controller task to be run when an
  17. // event
  18. // (a function) or a topic is issued.
  19. // Sub-classes must implement _run() method to implement their
  20. // tasks.
  21. // 'trigger' specifies an event scope, an ID of a widget or an
  22. // DOM
  23. // element, or its property with the optional dotted notation.
  24. // If this widget has child ActionFilter widgets, their filter()
  25. // methods are called with the arguments to the event or the
  26. // topic.
  27. // If one of filter() methods returns false, run() won't be
  28. // invoked.
  29. // This widget also can serve as a composite task to run child
  30. // Actions on an event or a topic specified to this widget.
  31. // trigger:
  32. // An event scope
  33. // triggerEvent:
  34. // An event (function) name
  35. // triggerTopic:
  36. // A topic name
  37. trigger : "",
  38. triggerEvent : "",
  39. triggerTopic : "",
  40. postCreate : function() {
  41. // summary:
  42. // Call _connect()
  43. // description:
  44. // See _connect().
  45. this._connect();
  46. },
  47. _connect : function() {
  48. // summary:
  49. // Connect run() method to an event or a topic
  50. // description:
  51. // If 'triggerEvent' and 'trigger' are specified, connect()
  52. // is
  53. // used to set up run() to be called on the event.
  54. // If 'triggerTopic' is specified, subscribe() is used to
  55. // set up
  56. // run() to be called on the topic.
  57. if (this.triggerEvent) {
  58. if (this.trigger) {
  59. var scope = dojox.wire.ml._getValue(this.trigger);
  60. if (scope) {
  61. if (!scope[this.triggerEvent]) {
  62. // set a dummy function for an anonymous
  63. // object
  64. scope[this.triggerEvent] = function() {
  65. };
  66. }
  67. this._triggerHandle = dojo.connect(scope,
  68. this.triggerEvent, this, "run");
  69. }
  70. } else {
  71. var event = this.triggerEvent.toLowerCase();
  72. if (event == "onload") {
  73. var self = this;
  74. dojo.addOnLoad(function() {
  75. self._run.apply(self, arguments);
  76. });
  77. }
  78. }
  79. } else if (this.triggerTopic) {
  80. this._triggerHandle = dojo.subscribe(this.triggerTopic,
  81. this, "run");
  82. }
  83. },
  84. _disconnect : function() {
  85. // summary:
  86. // Disconnect run() method from an event or a topic
  87. // description:
  88. // If 'triggerEvent' and 'trigger' are specified,
  89. // disconnect() is
  90. // used to set up run() not to be called on the event.
  91. // If 'triggerTopic' is specified, unsubscribe() is used to
  92. // set up
  93. // run() not to be called on the topic.
  94. if (this._triggerHandle) {
  95. if (this.triggerTopic) {
  96. dojo.unsubscribe(this.triggerTopic,
  97. this._triggerHandle);
  98. } else {
  99. dojo.disconnect(this._triggerHandle);
  100. }
  101. }
  102. },
  103. run : function() {
  104. // summary:
  105. // Run a task
  106. // description:
  107. // This method calls filter() method of child ActionFilter
  108. // widgets.
  109. // If one of them returns false, this method returns.
  110. // Otherwise, _run() method is called.
  111. var children = this.getChildren();
  112. for (var i in children) {
  113. var child = children[i];
  114. if (child instanceof dojox.wire.ml.ActionFilter) {
  115. if (!child.filter.apply(child, arguments)) {
  116. return;
  117. }
  118. }
  119. }
  120. this._run.apply(this, arguments);
  121. },
  122. _run : function() {
  123. // summary:
  124. // Call run() methods of child Action widgets
  125. // description:
  126. // If this widget has child Action widgets, their run()
  127. // methods
  128. // are called.
  129. var children = this.getChildren();
  130. for (var i in children) {
  131. var child = children[i];
  132. if (child instanceof dojox.wire.ml.Action) {
  133. child.run.apply(child, arguments);
  134. }
  135. }
  136. },
  137. uninitialize : function() {
  138. // summary:
  139. // Over-ride of base widget unitialize function to do some
  140. // connection cleanup.
  141. this._disconnect();
  142. return true;
  143. }
  144. });
  145. dojo.declare("dojox.wire.ml.ActionFilter", dijit._Widget, {
  146. // summary:
  147. // A widget to define a filter for the parent Action to run
  148. // description:
  149. // This base class checks a required property specified with
  150. // 'required' attribute.
  151. // If 'message' is specified, the message is set to a property
  152. // specified with 'error'.
  153. // Subclasses may implement their own filter() method.
  154. // required:
  155. // A property required
  156. // requiredValue:
  157. // Optional. A specific value the property is required to have.
  158. // If this isn't provided
  159. // than any non-false/non-null value of the required propery
  160. // will cause this filter
  161. // to pass.
  162. // type:
  163. // Optional. A specific type to compare the values as (if
  164. // requiredValue is set)
  165. // Valid values for type are boolean, int, string. Default is
  166. // string.
  167. // message:
  168. // An error message to emit if the filter doesn't execute due to
  169. // property mismatch.
  170. // error:
  171. // A property to store an error due to property mismatch.
  172. required : "",
  173. requiredValue : "",
  174. type : "",
  175. message : "",
  176. error : "",
  177. filter : function() {
  178. // summary:
  179. // Check if a required property is specified. Also, if
  180. // provided, check to see
  181. // if the required property contains a specific value.
  182. // description:
  183. // If a value is undefined for a property, specified with
  184. // 'required', this method returns false.
  185. // If the value for a property is defined, but there isn't a
  186. // requiredValue for it
  187. // then any non-false value will cause the method to return
  188. // true.
  189. // if requiredValue is set, then filter compares that value
  190. // with the value from
  191. // the required property and returns true if and only if
  192. // they match.
  193. // The type option just allows for a way to convert the
  194. // required property values
  195. // into a proper form for comparison (boolean, number, etc).
  196. // If 'message' is specified, it is set to a proeprty
  197. // specified
  198. // with 'error' or shown with alert().
  199. // If 'required' starts with "arguments", a property of
  200. // the method arguments are checked.
  201. // returns:
  202. // True if a required property is specified (and if
  203. // requiredValue is specified,
  204. // that they match), otherwise false
  205. if (this.required === "") {
  206. return true; // Boolean
  207. } else {
  208. var value = dojox.wire.ml._getValue(this.required,
  209. arguments);
  210. if (this.requiredValue === "") {
  211. // Just see if there's a value, nothing to compare
  212. // it to.
  213. if (value) {
  214. return true; // Boolean
  215. }
  216. } else {
  217. // See if we need to type convert.
  218. var reqValue = this.requiredValue;
  219. if (this.type !== "") {
  220. var lType = this.type.toLowerCase();
  221. if (lType === "boolean") {
  222. if (reqValue.toLowerCase() === "false") {
  223. reqValue = false;
  224. } else {
  225. reqValue = true;
  226. }
  227. } else if (lType === "number") {
  228. reqValue = parseInt(reqValue, 10);
  229. }
  230. }
  231. if (value === reqValue) {
  232. return true; // boolean
  233. }
  234. }
  235. }
  236. if (this.message) {
  237. if (this.error) {
  238. dojox.wire.ml._setValue(this.error, this.message);
  239. } else {
  240. alert(this.message);
  241. }
  242. }
  243. return false; // Boolean
  244. }
  245. });
  246. }