4 A Utility Class which executes functions one after another, with each function firing after completion of the previous.
5 Its methods can be implemented with [Class:implement][] into any [Class][], and it is currently implemented in [Fx][] and [Request][].
6 In [Fx][], for example, it is used to create custom, complex animations.
10 Chain Method: constructor {#Chain:constructor}
11 ----------------------------------------------
18 var MyClass = new Class({ Implements: Chain });
20 #### For existing classes:
22 MyClass.implement(Chain);
26 var myChain = new Chain;
30 var Todo = new Class({
32 initialize: function(){
33 this.chain.apply(this, arguments);
37 var myTodoList = new Todo(
38 function(){ alert('get groceries'); },
39 function(){ alert('go workout'); },
40 function(){ alert('code mootools documentation until eyes close involuntarily'); },
41 function(){ alert('sleep'); }
50 Chain Method: chain {#Chain:chain}
51 ----------------------------------
53 Adds functions to the end of the call stack of the Chain instance.
57 myClass.chain(fn[, fn2[, fn3[, ...]]]);
61 1. fn - (*function* or *array*) The function (or array of functions) to add to the chain call stack. Will accept and number of functions or arrays of functions.
65 * (*object*) The current Class instance. Calls to chain can also be chained.
68 //Fx.Tween has already implemented the Chain class because of inheritance of the Fx class.
69 var myFx = new Fx.Tween('myElement', {property: 'opacity'});
70 myFx.start(1,0).chain(
71 //Notice that "this" refers to the calling object (in this case, the myFx object).
72 function(){ this.start(0,1); },
73 function(){ this.start(1,0); },
74 function(){ this.start(0,1); }
75 ); //Will fade the Element out and in twice.
80 - [Fx][], [Fx.Tween][]
84 Chain Method: callChain {#Chain:callChain}
85 ------------------------------------------
87 Removes the first function of the Chain instance stack and executes it. The next function will then become first in the array.
91 myClass.callChain([any arguments]);
95 1. Any arguments passed in will be passed to the "next" function.
99 * (*mixed*) The return value of the "next" function or false when the chain was empty.
103 var myChain = new Chain();
105 function(){ alert('do dishes'); },
106 function(){ alert('put away clean dishes'); }
108 myChain.callChain(); // alerts 'do dishes'.
109 myChain.callChain(); // alerts 'put away clean dishes'.
113 Chain Method: clearChain {#Chain:clearChain}
114 --------------------------------------------
116 Clears the stack of a Chain instance.
120 myClass.clearChain();
124 * (*object*) The current Class instance.
128 var myFx = Fx.Tween('myElement', 'color'); // Fx.Tween inherited Fx's implementation of Chain.
129 myFx.chain(function(){ while(true) alert("D'oh!"); }); // chains an infinite loop of alerts.
130 myFx.clearChain(); // cancels the infinite loop of alerts before allowing it to begin.
134 - [Fx][], [Fx.Tween][]
138 Class: Events {#Events}
139 =======================
141 A Utility Class. Its methods can be implemented with [Class:implement][] into any [Class][].
142 In [Fx][], for example, this Class is used to allow any number of functions to be added to the Fx events, like 'complete', 'start', and 'cancel'.
143 Events in a Class that implements [Events][] must be either added as an option or with addEvent, not directly through .options.onEventName.
147 #### For new classes:
149 var MyClass = new Class({ Implements: Events });
151 #### For existing classes:
153 MyClass.implement(Events);
157 - This class can be implemented into other classes to add its functionality to them.
158 - Events has been designed to work well with the [Options][] class. When the option property begins with 'on' and is followed by a capital letter it will be added as an event (e.g. 'onComplete' will add as 'complete' event).
162 var Widget = new Class({
164 initialize: function(element){
167 complete: function(){
168 this.fireEvent('complete');
172 var myWidget = new Widget();
173 myWidget.addEvent('complete', myFunction);
177 - Events starting with 'on' are still supported in all methods and are converted to their representation without 'on' (e.g. 'onComplete' becomes 'complete').
182 - [Class][], [Options][]
186 Events Method: addEvent {#Events:addEvent}
187 ------------------------------------------
189 Adds an event to the Class instance's event stack.
193 myClass.addEvent(type, fn[, internal]);
197 1. type - (*string*) The type of event (e.g. 'complete').
198 2. fn - (*function*) The function to execute.
199 3. internal - (*boolean*, optional) Sets the function property: internal to true. Internal property is used to prevent removal.
203 * (*object*) This Class instance.
207 var myFx = new Fx.Tween('element', 'opacity');
208 myFx.addEvent('start', myStartFunction);
211 Events Method: addEvents {#Events:addEvents}
212 ------------------------------------------
214 The same as [addEvent][], but accepts an object to add multiple events at once.
218 myClass.addEvents(events);
222 1. events - (*object*) An object with key/value representing: key the event name (e.g. 'start'), and value the function that is called when the Event occurs.
226 * (*object*) This Class instance.
230 var myFx = new Fx.Tween('element', 'opacity');
232 start: myStartFunction,
233 complete: function() {
240 Events Method: fireEvent {#Events:fireEvent}
241 --------------------------------------------
243 Fires all events of the specified type in the Class instance.
247 myClass.fireEvent(type[, args[, delay]]);
251 1. type - (*string*) The type of event (e.g. 'complete').
252 2. args - (*mixed*, optional) The argument(s) to pass to the function. To pass more than one argument, the arguments must be in an array.
253 3. delay - (*number*, optional) Delay in milliseconds to wait before executing the event (defaults to 0).
257 * (*object*) This Class instance.
261 var Widget = new Class({
263 initialize: function(arg1, arg2){
265 this.fireEvent('initialize', [arg1, arg2], 50);
271 Events Method: removeEvent {#Events:removeEvent}
272 ------------------------------------------------
274 Removes an event from the stack of events of the Class instance.
278 myClass.removeEvent(type, fn);
282 1. type - (*string*) The type of event (e.g. 'complete').
283 2. fn - (*function*) The function to remove.
287 * (*object*) This Class instance.
291 - If the function has the property internal and is set to true, then the event will not be removed.
294 Events Method: removeEvents {#Events:removeEvents}
295 --------------------------------------------------
297 Removes all events of the given type from the stack of events of a Class instance. If no type is specified, removes all events of all types.
301 myClass.removeEvents([events]);
305 1. events - (optional) If not passed removes all events of all types.
306 - (*string*) The event name (e.g. 'success'). Removes all events of that type.
307 - (*object*) An object of type function pairs. Like the one passed to [addEvents][].
311 * (*object*) The current Class instance.
315 var myFx = new Fx.Tween('myElement', 'opacity');
316 myFx.removeEvents('complete');
321 - removeEvents will not remove internal events. See [Events:removeEvent][].
325 Class: Options {#Options}
326 =========================
328 A Utility Class. Its methods can be implemented with [Class:implement][] into any [Class][].
329 Used to automate the setting of a Class instance's options.
330 Will also add Class [Events][] when the option property begins with 'on' and is followed by a capital letter (e.g. 'onComplete' adds a 'complete' event). You will need to call this.setOptions() for this to have an effect, however.
334 #### For new classes:
336 var MyClass = new Class({Implements: Options});
338 #### For existing classes:
340 MyClass.implement(Options);
344 Options Method: setOptions {#Options:setOptions}
345 ------------------------------------------------
347 Merges the default options of the Class with the options passed in. Every value passed in to this method will be deep copied. Therefore other class instances or objects that are not intended for copying must be passed to a class in other ways.
351 myClass.setOptions([options]);
355 1. options - (*object*, optional) The user defined options to merge with the defaults.
359 * (*object*) The current Class instance.
363 var Widget = new Class({
372 initialize: function(options){
373 this.setOptions(options);
377 var myWidget = new Widget({
384 //myWidget.options is now: {color: #f00, size: {width: 200, height: 100}}
392 var myWidget = new Widget({
396 (mySize == myWidget.options.size) // false! mySize was copied in the setOptions call.
400 - Relies on the default options of a Class defined in its options property.
403 Options in combination with Events
404 -----------------------------------
406 If a Class has [Events][] as well as [Options][] implemented, every option beginning with 'on' and followed by a capital letter (e.g. 'onComplete') becomes a Class instance event, assuming the value of the option is a function.
410 var Widget = new Class({
411 Implements: [Options, Events],
419 initialize: function(options){
420 this.setOptions(options);
423 // Do some cool stuff
425 this.fireEvent('show');
430 var myWidget = new Widget({
436 alert('Lets show it!');
440 myWidget.show(); // fires the event and alerts 'Lets show it!'
443 [Class]: /core/Class/Class
444 [Class:implement]: /core/Class/Class/#Class:implement
446 [Fx.Tween]: /core/Fx/Fx.Tween
447 [Request]: /core/Request/Request
448 [Request.HTML]: /core/Request/Request.HTML
449 [Events:removeEvent]: /core/Element/Element.Event/#Element:removeEvent
452 [addEvent]: #Events:addEvent
453 [addEvents]: #Events:addEvents