Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / util / ClickRepeater.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  @class Ext.util.ClickRepeater
9  @extends Ext.util.Observable
10
11  A wrapper class which can be applied to any element. Fires a "click" event while the
12  mouse is pressed. The interval between firings may be specified in the config but
13  defaults to 20 milliseconds.
14
15  Optionally, a CSS class may be applied to the element during the time it is pressed.
16
17  @cfg {Mixed} el The element to act as a button.
18  @cfg {Number} delay The initial delay before the repeating event begins firing.
19  Similar to an autorepeat key delay.
20  @cfg {Number} interval The interval between firings of the "click" event. Default 20 ms.
21  @cfg {String} pressClass A CSS class name to be applied to the element while pressed.
22  @cfg {Boolean} accelerate True if autorepeating should start slowly and accelerate.
23            "interval" and "delay" are ignored.
24  @cfg {Boolean} preventDefault True to prevent the default click event
25  @cfg {Boolean} stopDefault True to stop the default click event
26
27  @history
28     2007-02-02 jvs Original code contributed by Nige "Animal" White
29     2007-02-02 jvs Renamed to ClickRepeater
30     2007-02-03 jvs Modifications for FF Mac and Safari
31
32  @constructor
33  @param {Mixed} el The element to listen on
34  @param {Object} config
35  */
36 Ext.util.ClickRepeater = function(el, config)
37 {
38     this.el = Ext.get(el);
39     this.el.unselectable();
40
41     Ext.apply(this, config);
42
43     this.addEvents(
44     /**
45      * @event mousedown
46      * Fires when the mouse button is depressed.
47      * @param {Ext.util.ClickRepeater} this
48      */
49         "mousedown",
50     /**
51      * @event click
52      * Fires on a specified interval during the time the element is pressed.
53      * @param {Ext.util.ClickRepeater} this
54      */
55         "click",
56     /**
57      * @event mouseup
58      * Fires when the mouse key is released.
59      * @param {Ext.util.ClickRepeater} this
60      */
61         "mouseup"
62     );
63
64     if(!this.disabled){
65         this.disabled = true;
66         this.enable();
67     }
68
69     // allow inline handler
70     if(this.handler){
71         this.on("click", this.handler,  this.scope || this);
72     }
73
74     Ext.util.ClickRepeater.superclass.constructor.call(this);
75 };
76
77 Ext.extend(Ext.util.ClickRepeater, Ext.util.Observable, {
78     interval : 20,
79     delay: 250,
80     preventDefault : true,
81     stopDefault : false,
82     timer : 0,
83
84     /**
85      * Enables the repeater and allows events to fire.
86      */
87     enable: function(){
88         if(this.disabled){
89             this.el.on('mousedown', this.handleMouseDown, this);
90             if(this.preventDefault || this.stopDefault){
91                 this.el.on('click', this.eventOptions, this);
92             }
93         }
94         this.disabled = false;
95     },
96     
97     /**
98      * Disables the repeater and stops events from firing.
99      */
100     disable: function(/* private */ force){
101         if(force || !this.disabled){
102             clearTimeout(this.timer);
103             if(this.pressClass){
104                 this.el.removeClass(this.pressClass);
105             }
106             Ext.getDoc().un('mouseup', this.handleMouseUp, this);
107             this.el.removeAllListeners();
108         }
109         this.disabled = true;
110     },
111     
112     /**
113      * Convenience function for setting disabled/enabled by boolean.
114      * @param {Boolean} disabled
115      */
116     setDisabled: function(disabled){
117         this[disabled ? 'disable' : 'enable']();    
118     },
119     
120     eventOptions: function(e){
121         if(this.preventDefault){
122             e.preventDefault();
123         }
124         if(this.stopDefault){
125             e.stopEvent();
126         }       
127     },
128     
129     // private
130     destroy : function() {
131         this.disable(true);
132         Ext.destroy(this.el);
133         this.purgeListeners();
134     },
135     
136     // private
137     handleMouseDown : function(){
138         clearTimeout(this.timer);
139         this.el.blur();
140         if(this.pressClass){
141             this.el.addClass(this.pressClass);
142         }
143         this.mousedownTime = new Date();
144
145         Ext.getDoc().on("mouseup", this.handleMouseUp, this);
146         this.el.on("mouseout", this.handleMouseOut, this);
147
148         this.fireEvent("mousedown", this);
149         this.fireEvent("click", this);
150
151 //      Do not honor delay or interval if acceleration wanted.
152         if (this.accelerate) {
153             this.delay = 400;
154             }
155         this.timer = this.click.defer(this.delay || this.interval, this);
156     },
157
158     // private
159     click : function(){
160         this.fireEvent("click", this);
161         this.timer = this.click.defer(this.accelerate ?
162             this.easeOutExpo(this.mousedownTime.getElapsed(),
163                 400,
164                 -390,
165                 12000) :
166             this.interval, this);
167     },
168
169     easeOutExpo : function (t, b, c, d) {
170         return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
171     },
172
173     // private
174     handleMouseOut : function(){
175         clearTimeout(this.timer);
176         if(this.pressClass){
177             this.el.removeClass(this.pressClass);
178         }
179         this.el.on("mouseover", this.handleMouseReturn, this);
180     },
181
182     // private
183     handleMouseReturn : function(){
184         this.el.un("mouseover", this.handleMouseReturn, this);
185         if(this.pressClass){
186             this.el.addClass(this.pressClass);
187         }
188         this.click();
189     },
190
191     // private
192     handleMouseUp : function(){
193         clearTimeout(this.timer);
194         this.el.un("mouseover", this.handleMouseReturn, this);
195         this.el.un("mouseout", this.handleMouseOut, this);
196         Ext.getDoc().un("mouseup", this.handleMouseUp, this);
197         this.el.removeClass(this.pressClass);
198         this.fireEvent("mouseup", this);
199     }
200 });