Rename Array.from to Array.convert, keep as is in compat layer
[mootools.git] / Source / Class / Class.Extras.js
blob4e6de14caad5461d91d0364c56f50d70d6ca8112
1 /*
2 ---
4 name: Class.Extras
6 description: Contains Utility Classes that can be implemented into your own Classes to ease the execution of many common tasks.
8 license: MIT-style license.
10 requires: Class
12 provides: [Class.Extras, Chain, Events, Options]
14 ...
17 (function(){
19 this.Chain = new Class({
21         $chain: [],
23         chain: function(){
24                 this.$chain.append(Array.flatten(arguments));
25                 return this;
26         },
28         callChain: function(){
29                 return (this.$chain.length) ? this.$chain.shift().apply(this, arguments) : false;
30         },
32         clearChain: function(){
33                 this.$chain.empty();
34                 return this;
35         }
37 });
39 var removeOn = function(string){
40         return string.replace(/^on([A-Z])/, function(full, first){
41                 return first.toLowerCase();
42         });
45 this.Events = new Class({
47         $events: {},
49         addEvent: function(type, fn, internal){
50                 type = removeOn(type);
52                 /*<1.2compat>*/
53                 if (fn == $empty) return this;
54                 /*</1.2compat>*/
56                 this.$events[type] = (this.$events[type] || []).include(fn);
57                 if (internal) fn.internal = true;
58                 return this;
59         },
61         addEvents: function(events){
62                 for (var type in events) this.addEvent(type, events[type]);
63                 return this;
64         },
66         fireEvent: function(type, args, delay){
67                 type = removeOn(type);
68                 var events = this.$events[type];
69                 if (!events) return this;
70                 args = Array.convert(args);
71                 events.each(function(fn){
72                         if (delay) fn.delay(delay, this, args);
73                         else fn.apply(this, args);
74                 }, this);
75                 return this;
76         },
78         removeEvent: function(type, fn){
79                 type = removeOn(type);
80                 var events = this.$events[type];
81                 if (events && !fn.internal){
82                         var index = events.indexOf(fn);
83                         if (index != -1) delete events[index];
84                 }
85                 return this;
86         },
88         removeEvents: function(events){
89                 var type;
90                 if (typeOf(events) == 'object'){
91                         for (type in events) this.removeEvent(type, events[type]);
92                         return this;
93                 }
94                 if (events) events = removeOn(events);
95                 for (type in this.$events){
96                         if (events && events != type) continue;
97                         var fns = this.$events[type];
98                         for (var i = fns.length; i--;) if (i in fns){
99                                 this.removeEvent(type, fns[i]);
100                         }
101                 }
102                 return this;
103         }
107 this.Options = new Class({
109         setOptions: function(){
110                 var options = this.options = Object.merge.apply(null, [{}, this.options].append(arguments));
111                 if (this.addEvent) for (var option in options){
112                         if (typeOf(options[option]) != 'function' || !(/^on[A-Z]/).test(option)) continue;
113                         this.addEvent(option, options[option]);
114                         delete options[option];
115                 }
116                 return this;
117         }
121 })();