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