2 * @class Ext.util.ClickRepeater
3 * @extends Ext.util.Observable
5 * A wrapper class which can be applied to any element. Fires a "click" event while the
6 * mouse is pressed. The interval between firings may be specified in the config but
7 * defaults to 20 milliseconds.
9 * Optionally, a CSS class may be applied to the element during the time it is pressed.
12 * @param {Mixed} el The element to listen on
13 * @param {Object} config
16 Ext.define('Ext.util.ClickRepeater', {
17 extend: 'Ext.util.Observable',
19 constructor : function(el, config){
20 this.el = Ext.get(el);
21 this.el.unselectable();
23 Ext.apply(this, config);
28 * Fires when the mouse button is depressed.
29 * @param {Ext.util.ClickRepeater} this
30 * @param {Ext.EventObject} e
35 * Fires on a specified interval during the time the element is pressed.
36 * @param {Ext.util.ClickRepeater} this
37 * @param {Ext.EventObject} e
42 * Fires when the mouse key is released.
43 * @param {Ext.util.ClickRepeater} this
44 * @param {Ext.EventObject} e
54 // allow inline handler
56 this.on("click", this.handler, this.scope || this);
63 * @cfg {Mixed} el The element to act as a button.
67 * @cfg {String} pressedCls A CSS class name to be applied to the element while pressed.
71 * @cfg {Boolean} accelerate True if autorepeating should start slowly and accelerate.
72 * "interval" and "delay" are ignored.
76 * @cfg {Number} interval The interval between firings of the "click" event. Default 20 ms.
81 * @cfg {Number} delay The initial delay before the repeating event begins firing.
82 * Similar to an autorepeat key delay.
87 * @cfg {Boolean} preventDefault True to prevent the default click event
89 preventDefault : true,
91 * @cfg {Boolean} stopDefault True to stop the default click event
98 * Enables the repeater and allows events to fire.
102 this.el.on('mousedown', this.handleMouseDown, this);
104 this.el.on('dblclick', this.handleDblClick, this);
106 if(this.preventDefault || this.stopDefault){
107 this.el.on('click', this.eventOptions, this);
110 this.disabled = false;
114 * Disables the repeater and stops events from firing.
116 disable: function(/* private */ force){
117 if(force || !this.disabled){
118 clearTimeout(this.timer);
120 this.el.removeCls(this.pressedCls);
122 Ext.getDoc().un('mouseup', this.handleMouseUp, this);
123 this.el.removeAllListeners();
125 this.disabled = true;
129 * Convenience function for setting disabled/enabled by boolean.
130 * @param {Boolean} disabled
132 setDisabled: function(disabled){
133 this[disabled ? 'disable' : 'enable']();
136 eventOptions: function(e){
137 if(this.preventDefault){
140 if(this.stopDefault){
146 destroy : function() {
148 Ext.destroy(this.el);
149 this.clearListeners();
152 handleDblClick : function(e){
153 clearTimeout(this.timer);
156 this.fireEvent("mousedown", this, e);
157 this.fireEvent("click", this, e);
161 handleMouseDown : function(e){
162 clearTimeout(this.timer);
165 this.el.addCls(this.pressedCls);
167 this.mousedownTime = new Date();
169 Ext.getDoc().on("mouseup", this.handleMouseUp, this);
170 this.el.on("mouseout", this.handleMouseOut, this);
172 this.fireEvent("mousedown", this, e);
173 this.fireEvent("click", this, e);
175 // Do not honor delay or interval if acceleration wanted.
176 if (this.accelerate) {
180 // Re-wrap the event object in a non-shared object, so it doesn't lose its context if
181 // the global shared EventObject gets a new Event put into it before the timer fires.
182 e = new Ext.EventObjectImpl(e);
184 this.timer = Ext.defer(this.click, this.delay || this.interval, this, [e]);
189 this.fireEvent("click", this, e);
190 this.timer = Ext.defer(this.click, this.accelerate ?
191 this.easeOutExpo(Ext.Date.getElapsed(this.mousedownTime),
195 this.interval, this, [e]);
198 easeOutExpo : function (t, b, c, d) {
199 return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
203 handleMouseOut : function(){
204 clearTimeout(this.timer);
206 this.el.removeCls(this.pressedCls);
208 this.el.on("mouseover", this.handleMouseReturn, this);
212 handleMouseReturn : function(){
213 this.el.un("mouseover", this.handleMouseReturn, this);
215 this.el.addCls(this.pressedCls);
221 handleMouseUp : function(e){
222 clearTimeout(this.timer);
223 this.el.un("mouseover", this.handleMouseReturn, this);
224 this.el.un("mouseout", this.handleMouseOut, this);
225 Ext.getDoc().un("mouseup", this.handleMouseUp, this);
227 this.el.removeCls(this.pressedCls);
229 this.fireEvent("mouseup", this, e);