]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/_base/connect.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojo / _base / connect.js
1 if(!dojo._hasResource["dojo._base.connect"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo._base.connect"] = true;
3 dojo.provide("dojo._base.connect");
4 dojo.require("dojo._base.lang");
5
6 // this file courtesy of the TurboAjax Group, licensed under a Dojo CLA
7
8 // low-level delegation machinery
9 dojo._listener = {
10         // create a dispatcher function
11         getDispatcher: function(){
12                 // following comments pulled out-of-line to prevent cloning them 
13                 // in the returned function.
14                 // - indices (i) that are really in the array of listeners (ls) will 
15                 //   not be in Array.prototype. This is the 'sparse array' trick
16                 //   that keeps us safe from libs that take liberties with built-in 
17                 //   objects
18                 // - listener is invoked with current scope (this)
19                 return function(){
20                         var ap=Array.prototype, c=arguments.callee, ls=c._listeners, t=c.target;
21                         // return value comes from original target function
22                         var r=t && t.apply(this, arguments);
23                         // invoke listeners after target function
24                         for(var i in ls){
25                                 if(!(i in ap)){
26                                         ls[i].apply(this, arguments);
27                                 }
28                         }
29                         // return value comes from original target function
30                         return r;
31                 }
32         },
33         // add a listener to an object
34         add: function(/*Object*/ source, /*String*/ method, /*Function*/ listener){
35                 // Whenever 'method' is invoked, 'listener' will have the same scope.
36                 // Trying to supporting a context object for the listener led to 
37                 // complexity. 
38                 // Non trivial to provide 'once' functionality here
39                 // because listener could be the result of a dojo.hitch call,
40                 // in which case two references to the same hitch target would not
41                 // be equivalent. 
42                 source = source || dojo.global;
43                 // The source method is either null, a dispatcher, or some other function
44                 var f = source[method];
45                 // Ensure a dispatcher
46                 if(!f||!f._listeners){
47                         var d = dojo._listener.getDispatcher();
48                         // original target function is special
49                         d.target = f;
50                         // dispatcher holds a list of listeners
51                         d._listeners = []; 
52                         // redirect source to dispatcher
53                         f = source[method] = d;
54                 }
55                 // The contract is that a handle is returned that can 
56                 // identify this listener for disconnect. 
57                 //
58                 // The type of the handle is private. Here is it implemented as Integer. 
59                 // DOM event code has this same contract but handle is Function 
60                 // in non-IE browsers.
61                 //
62                 // We could have separate lists of before and after listeners.
63                 return f._listeners.push(listener) ; /*Handle*/
64         },
65         // remove a listener from an object
66         remove: function(/*Object*/ source, /*String*/ method, /*Handle*/ handle){
67                 var f = (source||dojo.global)[method];
68                 // remember that handle is the index+1 (0 is not a valid handle)
69                 if(f && f._listeners && handle--){
70                         delete f._listeners[handle]; 
71                 }
72         }
73 };
74
75 // Multiple delegation for arbitrary methods.
76
77 // This unit knows nothing about DOM, 
78 // but we include DOM aware 
79 // documentation and dontFix
80 // argument here to help the autodocs.
81 // Actual DOM aware code is in event.js.
82
83 dojo.connect = function(/*Object|null*/ obj, 
84                                                 /*String*/ event, 
85                                                 /*Object|null*/ context, 
86                                                 /*String|Function*/ method,
87                                                 /*Boolean*/ dontFix){
88         // summary:
89         //              Create a link that calls one function when another executes. 
90         //
91         // description:
92         //              Connects method to event, so that after event fires, method
93         //              does too. All connected functions are passed the same arguments as
94         //              the event function was initially called with. You may connect as
95         //              many methods to event as needed.
96         //
97         //              event must be a string. If obj is null, dojo.global is used.
98         //
99         //              null arguments may simply be omitted.
100         //
101         //              obj[event] can resolve to a function or undefined (null). 
102         //              If obj[event] is null, it is assigned a function.
103         //
104         //              The return value is a handle that is needed to 
105         //              remove this connection with dojo.disconnect.
106         //
107         // obj: 
108         //              The source object for the event function. 
109         //              Defaults to dojo.global if null.
110         //              If obj is a DOM node, the connection is delegated 
111         //              to the DOM event manager (unless dontFix is true).
112         //
113         // event:
114         //              String name of the event function in obj. 
115         //              I.e. identifies a property obj[event].
116         //
117         // context: 
118         //              The object that method will receive as "this".
119         //
120         //              If context is null and method is a function, then method
121         //              inherits the context of event.
122         //      
123         //              If method is a string then context must be the source 
124         //              object object for method (context[method]). If context is null,
125         //              dojo.global is used.
126         //
127         // method:
128         //              A function reference, or name of a function in context. 
129         //              The function identified by method fires after event does. 
130         //              method receives the same arguments as the event.
131         //              See context argument comments for information on method's scope.
132         //
133         // dontFix:
134         //              If obj is a DOM node, set dontFix to true to prevent delegation 
135         //              of this connection to the DOM event manager. 
136         //
137         // example:
138         //              When obj.onchange(), do ui.update():
139         //      |       dojo.connect(obj, "onchange", ui, "update");
140         //      |       dojo.connect(obj, "onchange", ui, ui.update); // same
141         //
142         // example:
143         //              Using return value for disconnect:
144         //      |       var link = dojo.connect(obj, "onchange", ui, "update");
145         //      |       ...
146         //      |       dojo.disconnect(link);
147         //
148         // example:
149         //              When onglobalevent executes, watcher.handler is invoked:
150         //      |       dojo.connect(null, "onglobalevent", watcher, "handler");
151         //
152         // example:
153         //              When ob.onCustomEvent executes, customEventHandler is invoked:
154         //      |       dojo.connect(ob, "onCustomEvent", null, "customEventHandler");
155         //      |       dojo.connect(ob, "onCustomEvent", "customEventHandler"); // same
156         //
157         // example:
158         //              When ob.onCustomEvent executes, customEventHandler is invoked
159         //              with the same scope (this):
160         //      |       dojo.connect(ob, "onCustomEvent", null, customEventHandler);
161         //      |       dojo.connect(ob, "onCustomEvent", customEventHandler); // same
162         //
163         // example:
164         //              When globalEvent executes, globalHandler is invoked
165         //              with the same scope (this):
166         //      |       dojo.connect(null, "globalEvent", null, globalHandler);
167         //      |       dojo.connect("globalEvent", globalHandler); // same
168
169         // normalize arguments
170         var a=arguments, args=[], i=0;
171         // if a[0] is a String, obj was ommited
172         args.push(dojo.isString(a[0]) ? null : a[i++], a[i++]);
173         // if the arg-after-next is a String or Function, context was NOT omitted
174         var a1 = a[i+1];
175         args.push(dojo.isString(a1)||dojo.isFunction(a1) ? a[i++] : null, a[i++]);
176         // absorb any additional arguments
177         for(var l=a.length; i<l; i++){  args.push(a[i]); }
178         // do the actual work
179         return dojo._connect.apply(this, args); /*Handle*/
180 }
181
182 // used by non-browser hostenvs. always overriden by event.js
183 dojo._connect = function(obj, event, context, method){
184         var l=dojo._listener, h=l.add(obj, event, dojo.hitch(context, method)); 
185         return [obj, event, h, l]; // Handle
186 }
187
188 dojo.disconnect = function(/*Handle*/ handle){
189         // summary:
190         //              Remove a link created by dojo.connect.
191         // description:
192         //              Removes the connection between event and the method referenced by handle.
193         // handle:
194         //              the return value of the dojo.connect call that created the connection.
195         if(handle && handle[0] !== undefined){
196                 dojo._disconnect.apply(this, handle);
197                 // let's not keep this reference
198                 delete handle[0];
199         }
200 }
201
202 dojo._disconnect = function(obj, event, handle, listener){
203         listener.remove(obj, event, handle);
204 }
205
206 // topic publish/subscribe
207
208 dojo._topics = {};
209
210 dojo.subscribe = function(/*String*/ topic, /*Object|null*/ context, /*String|Function*/ method){
211         //      summary:
212         //              Attach a listener to a named topic. The listener function is invoked whenever the
213         //              named topic is published (see: dojo.publish).
214         //              Returns a handle which is needed to unsubscribe this listener.
215         //      context:
216         //              Scope in which method will be invoked, or null for default scope.
217         //      method:
218         //              The name of a function in context, or a function reference. This is the function that
219         //              is invoked when topic is published.
220         //      example:
221         //      |       dojo.subscribe("alerts", null, function(caption, message){ alert(caption + "\n" + message); };
222         //      |       dojo.publish("alerts", [ "read this", "hello world" ]);                                                                                                                                 
223
224         // support for 2 argument invocation (omitting context) depends on hitch
225         return [topic, dojo._listener.add(dojo._topics, topic, dojo.hitch(context, method))]; /*Handle*/
226 }
227
228 dojo.unsubscribe = function(/*Handle*/ handle){
229         //      summary:
230         //              Remove a topic listener. 
231         //      handle:
232         //              The handle returned from a call to subscribe.
233         //      example:
234         //      |       var alerter = dojo.subscribe("alerts", null, function(caption, message){ alert(caption + "\n" + message); };
235         //      |       ...
236         //      |       dojo.unsubscribe(alerter);
237         if(handle){
238                 dojo._listener.remove(dojo._topics, handle[0], handle[1]);
239         }
240 }
241
242 dojo.publish = function(/*String*/ topic, /*Array*/ args){
243         //      summary:
244         //              Invoke all listener method subscribed to topic.
245         //      topic:
246         //              The name of the topic to publish.
247         //      args:
248         //              An array of arguments. The arguments will be applied 
249         //              to each topic subscriber (as first class parameters, via apply).
250         //      example:
251         //      |       dojo.subscribe("alerts", null, function(caption, message){ alert(caption + "\n" + message); };
252         //      |       dojo.publish("alerts", [ "read this", "hello world" ]); 
253
254         // Note that args is an array, which is more efficient vs variable length
255         // argument list.  Ideally, var args would be implemented via Array
256         // throughout the APIs.
257         var f = dojo._topics[topic];
258         if(f){
259                 f.apply(this, args||[]);
260         }
261 }
262
263 dojo.connectPublisher = function(       /*String*/ topic, 
264                                                                         /*Object|null*/ obj, 
265                                                                         /*String*/ event){
266         //      summary:
267         //              Ensure that everytime obj.event() is called, a message is published
268         //              on the topic. Returns a handle which can be passed to
269         //              dojo.disconnect() to disable subsequent automatic publication on
270         //              the topic.
271         //      topic:
272         //              The name of the topic to publish.
273         //      obj: 
274         //              The source object for the event function. Defaults to dojo.global
275         //              if null.
276         //      event:
277         //              The name of the event function in obj. 
278         //              I.e. identifies a property obj[event].
279         //      example:
280         //      |       dojo.connectPublisher("/ajax/start", dojo, "xhrGet"};
281         var pf = function(){ dojo.publish(topic, arguments); }
282         return (event) ? dojo.connect(obj, event, pf) : dojo.connect(obj, pf); //Handle
283 };
284
285 }