Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

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