Action.js 6.9 KB

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