summaryrefslogtreecommitdiff
path: root/includes/js/dojox/wire/_base.js
blob: cc632c165eed47bb3122160e0dfa490a8f6e7cb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
if(!dojo._hasResource["dojox.wire._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.wire._base"] = true;
dojo.provide("dojox.wire._base");

dojox.wire._defaultWireClass = "dojox.wire.Wire";

dojox.wire._wireClasses = {
	"attribute": "dojox.wire.DataWire",
	"path": "dojox.wire.XmlWire",
	"children": "dojox.wire.CompositeWire",
	"columns": "dojox.wire.TableAdapter",
	"nodes": "dojox.wire.TreeAdapter",
	"segments": "dojox.wire.TextAdapter"
};

dojox.wire.register = function(/*Function||String*/wireClass, /*String*/key){
	//	summary:
	//		Register a Wire class
	//	desription:
	//		The specified Wire class or a class name is registered with
	//		a key property of arguments to create a Wire
	//	wireClass:
	//		A class or full qualified class name
	//	key:
	//		A key property of arguments to create a Wire
	if(!wireClass || !key){
		return; //undefined
	}
	if(dojox.wire._wireClasses[key]){ // key already in use
		return; //undefined
	}
	dojox.wire._wireClasses[key] = wireClass;
};

dojox.wire._getClass = function(/*String*/name){
	//	summary:
	//		Returns a class 
	//	description:
	//		The class is loaded by dojo.require() and returned
	//		by dojo.getObject().
	//	name:
	//		A class name
	//	returns:
	//		A class
	dojo["require"](name); // use dojo["require"] instead of dojo.require to avoid a build problem
	return dojo.getObject(name); //Function
};

dojox.wire.create = function(/*Object*/args){
	//	summary:
	//		Create a Wire from arguments
	//	description:
	//		If 'args' specifies 'wireClass', it is used as a class or full
	//		qualified class name to create a Wire with 'args' as arguments.
	//		Otherwise, a Wire class is determined by other proeprties of 'args'
	//		checking if 'args' specifies a key property for a Wire class.
	//		If no key property found, the default Wire class is used.
	//	args:
	//		Arguments to create a Wire
	//	returns:
	//		A Wire
	if(!args){
		args = {};
	}
	var wireClass = args.wireClass;
	if(wireClass){
		if(dojo.isString(wireClass)){
			wireClass = dojox.wire._getClass(wireClass);
		}
	}else{
		for(var key in args){
			if(!args[key]){
				continue;
			}
			wireClass = dojox.wire._wireClasses[key];
			if(wireClass){
				if(dojo.isString(wireClass)){
					wireClass = dojox.wire._getClass(wireClass);
					dojox.wire._wireClasses[key] = wireClass;
				}
				break;
			}
		}
	}
	if(!wireClass){
		if(dojo.isString(dojox.wire._defaultWireClass)){
			dojox.wire._defaultWireClass = dojox.wire._getClass(dojox.wire._defaultWireClass);
		}
		wireClass = dojox.wire._defaultWireClass;
	}
	return new wireClass(args); //Object
};

dojox.wire.isWire = function(/*Object*/wire){
	//	summary:
	//		Check if an object is a Wire
	//	description:
	//		If the specified object is a Wire, true is returned.
	//		Otherwise, false is returned.
	//	wire:
	//		An object to check
	//	returns:
	//		True if the object is a Wire, otherwise false
	return (wire && wire._wireClass); //Boolean
};

dojox.wire.transfer = function(/*Wire||Object*/source, /*Wire||Object*/target, /*Object?*/defaultObject, /*Object?*/defaultTargetObject){
	//	summary:
	//		Transfer a source value to a target value
	//	description:
	//		If 'source' and/or 'target' are not Wires, Wires are created with
	//		them as arguments.
	//		A value is got through the source Wire and set through the target
	//		Wire.
	//		'defaultObject' is passed to Wires as a default root object.
	//		If 'defaultTargetObject' is specified, it is passed to the target
	//		Wire as a default root object, instead of 'defaultObject'.
	//	source:
	//		A Wire or arguments to create a Wire for a source value
	//	target:
	//		A Wire or arguments to create a Wire for a target value
	//	defaultObject:
	//	defaultTargetObject;
	//		Optional default root objects passed to Wires
	if(!source || !target){
		return; //undefined
	}
	if(!dojox.wire.isWire(source)){
		source = dojox.wire.create(source);
	}
	if(!dojox.wire.isWire(target)){
		target = dojox.wire.create(target);
	}

	var value = source.getValue(defaultObject);
	target.setValue(value, (defaultTargetObject || defaultObject));
};

dojox.wire.connect = function(/*Object*/trigger, /*Wire||Object*/source, /*Wire||Object*/target){
	//	summary:
	//		Transfer a source value to a target value on a trigger event or
	//		topic
	//	description:
	//		If 'trigger' specifies 'topic', the topic is subscribed to transer
	//		a value on the topic.
	//		Otherwise, the event specified to 'event' of 'trigger' is listened
	//		to transfer a value.
	//		On the specified event or topic, transfer() is called with
	//		'source', 'target' and the arguments of the event or topic (as
	//		default root objects).
	//	trigger:
	//		An event or topic to trigger a transfer
	//	source:
	//		A Wire or arguments to create a Wire for a source value
	//	target:
	//		A Wire or arguments to create a Wire for a target value
	//	returns:
	//		A connection handle for disconnect()
	if(!trigger || !source || !target){
		return; //undefined
	}

	var connection = {topic: trigger.topic};
	if(trigger.topic){
		connection.handle = dojo.subscribe(trigger.topic, function(){
			dojox.wire.transfer(source, target, arguments);
		});
	}else if(trigger.event){
		connection.handle = dojo.connect(trigger.scope, trigger.event, function(){
			dojox.wire.transfer(source, target, arguments);
		});
	}
	return connection; //Object
};

dojox.wire.disconnect = function(/*Object*/connection){
	//	summary:
	//		Remove a connection or subscription for transfer
	//	description:
	//		If 'handle' has 'topic', the topic is unsubscribed.
	//		Otherwise, the listener to an event is removed.
	//	connection:
	//		A connection handle returned by connect()
	if(!connection || !connection.handle){
		return; //undefined
	}

	if(connection.topic){
		dojo.unsubscribe(connection.handle);
	}else{
		dojo.disconnect(connection.handle);
	}
};

}