1 | dojo.provide("dojox.wire._base"); |
---|
2 | |
---|
3 | dojox.wire._defaultWireClass = "dojox.wire.Wire"; |
---|
4 | |
---|
5 | dojox.wire._wireClasses = { |
---|
6 | "attribute": "dojox.wire.DataWire", |
---|
7 | "path": "dojox.wire.XmlWire", |
---|
8 | "children": "dojox.wire.CompositeWire", |
---|
9 | "columns": "dojox.wire.TableAdapter", |
---|
10 | "nodes": "dojox.wire.TreeAdapter", |
---|
11 | "segments": "dojox.wire.TextAdapter" |
---|
12 | }; |
---|
13 | |
---|
14 | dojox.wire.register = function(/*Function||String*/wireClass, /*String*/key){ |
---|
15 | // summary: |
---|
16 | // Register a Wire class |
---|
17 | // description: |
---|
18 | // The specified Wire class or a class name is registered with |
---|
19 | // a key property of arguments to create a Wire |
---|
20 | // wireClass: |
---|
21 | // A class or full qualified class name |
---|
22 | // key: |
---|
23 | // A key property of arguments to create a Wire |
---|
24 | if(!wireClass || !key){ |
---|
25 | return; //undefined |
---|
26 | } |
---|
27 | if(dojox.wire._wireClasses[key]){ // key already in use |
---|
28 | return; //undefined |
---|
29 | } |
---|
30 | dojox.wire._wireClasses[key] = wireClass; |
---|
31 | }; |
---|
32 | |
---|
33 | dojox.wire._getClass = function(/*String*/name){ |
---|
34 | // summary: |
---|
35 | // Returns a class |
---|
36 | // description: |
---|
37 | // The class is loaded by dojo.require() and returned |
---|
38 | // by dojo.getObject(). |
---|
39 | // name: |
---|
40 | // A class name |
---|
41 | // returns: |
---|
42 | // A class |
---|
43 | dojo["require"](name); // use dojo["require"] instead of dojo.require to avoid a build problem |
---|
44 | return dojo.getObject(name); //Function |
---|
45 | }; |
---|
46 | |
---|
47 | dojox.wire.create = function(/*Object*/args){ |
---|
48 | // summary: |
---|
49 | // Create a Wire from arguments |
---|
50 | // description: |
---|
51 | // If 'args' specifies 'wireClass', it is used as a class or full |
---|
52 | // qualified class name to create a Wire with 'args' as arguments. |
---|
53 | // Otherwise, a Wire class is determined by other proeprties of 'args' |
---|
54 | // checking if 'args' specifies a key property for a Wire class. |
---|
55 | // If no key property found, the default Wire class is used. |
---|
56 | // args: |
---|
57 | // Arguments to create a Wire |
---|
58 | // returns: |
---|
59 | // A Wire |
---|
60 | if(!args){ |
---|
61 | args = {}; |
---|
62 | } |
---|
63 | var wireClass = args.wireClass; |
---|
64 | if(wireClass){ |
---|
65 | if(dojo.isString(wireClass)){ |
---|
66 | wireClass = dojox.wire._getClass(wireClass); |
---|
67 | } |
---|
68 | }else{ |
---|
69 | for(var key in args){ |
---|
70 | if(!args[key]){ |
---|
71 | continue; |
---|
72 | } |
---|
73 | wireClass = dojox.wire._wireClasses[key]; |
---|
74 | if(wireClass){ |
---|
75 | if(dojo.isString(wireClass)){ |
---|
76 | wireClass = dojox.wire._getClass(wireClass); |
---|
77 | dojox.wire._wireClasses[key] = wireClass; |
---|
78 | } |
---|
79 | break; |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | if(!wireClass){ |
---|
84 | if(dojo.isString(dojox.wire._defaultWireClass)){ |
---|
85 | dojox.wire._defaultWireClass = dojox.wire._getClass(dojox.wire._defaultWireClass); |
---|
86 | } |
---|
87 | wireClass = dojox.wire._defaultWireClass; |
---|
88 | } |
---|
89 | return new wireClass(args); //Object |
---|
90 | }; |
---|
91 | |
---|
92 | dojox.wire.isWire = function(/*Object*/wire){ |
---|
93 | // summary: |
---|
94 | // Check if an object is a Wire |
---|
95 | // description: |
---|
96 | // If the specified object is a Wire, true is returned. |
---|
97 | // Otherwise, false is returned. |
---|
98 | // wire: |
---|
99 | // An object to check |
---|
100 | // returns: |
---|
101 | // True if the object is a Wire, otherwise false |
---|
102 | return (wire && wire._wireClass); //Boolean |
---|
103 | }; |
---|
104 | |
---|
105 | dojox.wire.transfer = function(/*Wire||Object*/source, /*Wire||Object*/target, /*Object?*/defaultObject, /*Object?*/defaultTargetObject){ |
---|
106 | // summary: |
---|
107 | // Transfer a source value to a target value |
---|
108 | // description: |
---|
109 | // If 'source' and/or 'target' are not Wires, Wires are created with |
---|
110 | // them as arguments. |
---|
111 | // A value is got through the source Wire and set through the target |
---|
112 | // Wire. |
---|
113 | // 'defaultObject' is passed to Wires as a default root object. |
---|
114 | // If 'defaultTargetObject' is specified, it is passed to the target |
---|
115 | // Wire as a default root object, instead of 'defaultObject'. |
---|
116 | // source: |
---|
117 | // A Wire or arguments to create a Wire for a source value |
---|
118 | // target: |
---|
119 | // A Wire or arguments to create a Wire for a target value |
---|
120 | // defaultObject: |
---|
121 | // Optional default root object passed to Wires |
---|
122 | // defaultTargetObject: |
---|
123 | // Optional default root object passed to Wires |
---|
124 | if(!source || !target){ |
---|
125 | return; //undefined |
---|
126 | } |
---|
127 | if(!dojox.wire.isWire(source)){ |
---|
128 | source = dojox.wire.create(source); |
---|
129 | } |
---|
130 | if(!dojox.wire.isWire(target)){ |
---|
131 | target = dojox.wire.create(target); |
---|
132 | } |
---|
133 | |
---|
134 | var value = source.getValue(defaultObject); |
---|
135 | target.setValue(value, (defaultTargetObject || defaultObject)); |
---|
136 | }; |
---|
137 | |
---|
138 | dojox.wire.connect = function(/*Object*/trigger, /*Wire||Object*/source, /*Wire||Object*/target){ |
---|
139 | // summary: |
---|
140 | // Transfer a source value to a target value on a trigger event or |
---|
141 | // topic |
---|
142 | // description: |
---|
143 | // If 'trigger' specifies 'topic', the topic is subscribed to transer |
---|
144 | // a value on the topic. |
---|
145 | // Otherwise, the event specified to 'event' of 'trigger' is listened |
---|
146 | // to transfer a value. |
---|
147 | // On the specified event or topic, transfer() is called with |
---|
148 | // 'source', 'target' and the arguments of the event or topic (as |
---|
149 | // default root objects). |
---|
150 | // trigger: |
---|
151 | // An event or topic to trigger a transfer |
---|
152 | // source: |
---|
153 | // A Wire or arguments to create a Wire for a source value |
---|
154 | // target: |
---|
155 | // A Wire or arguments to create a Wire for a target value |
---|
156 | // returns: |
---|
157 | // A connection handle for disconnect() |
---|
158 | if(!trigger || !source || !target){ |
---|
159 | return; //undefined |
---|
160 | } |
---|
161 | |
---|
162 | var connection = {topic: trigger.topic}; |
---|
163 | if(trigger.topic){ |
---|
164 | connection.handle = dojo.subscribe(trigger.topic, function(){ |
---|
165 | dojox.wire.transfer(source, target, arguments); |
---|
166 | }); |
---|
167 | }else if(trigger.event){ |
---|
168 | connection.handle = dojo.connect(trigger.scope, trigger.event, function(){ |
---|
169 | dojox.wire.transfer(source, target, arguments); |
---|
170 | }); |
---|
171 | } |
---|
172 | return connection; //Object |
---|
173 | }; |
---|
174 | |
---|
175 | dojox.wire.disconnect = function(/*Object*/connection){ |
---|
176 | // summary: |
---|
177 | // Remove a connection or subscription for transfer |
---|
178 | // description: |
---|
179 | // If 'handle' has 'topic', the topic is unsubscribed. |
---|
180 | // Otherwise, the listener to an event is removed. |
---|
181 | // connection: |
---|
182 | // A connection handle returned by connect() |
---|
183 | if(!connection || !connection.handle){ |
---|
184 | return; //undefined |
---|
185 | } |
---|
186 | |
---|
187 | if(connection.topic){ |
---|
188 | dojo.unsubscribe(connection.handle); |
---|
189 | }else{ |
---|
190 | dojo.disconnect(connection.handle); |
---|
191 | } |
---|
192 | }; |
---|