3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 @class Ext.util.ClickRepeater
9 @extends Ext.util.Observable
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.
15 Optionally, a CSS class may be applied to the element during the time it is pressed.
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
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
33 @param {Mixed} el The element to listen on
34 @param {Object} config
36 Ext.util.ClickRepeater = function(el, config)
38 this.el = Ext.get(el);
39 this.el.unselectable();
41 Ext.apply(this, config);
46 * Fires when the mouse button is depressed.
47 * @param {Ext.util.ClickRepeater} this
52 * Fires on a specified interval during the time the element is pressed.
53 * @param {Ext.util.ClickRepeater} this
58 * Fires when the mouse key is released.
59 * @param {Ext.util.ClickRepeater} this
69 // allow inline handler
71 this.on("click", this.handler, this.scope || this);
74 Ext.util.ClickRepeater.superclass.constructor.call(this);
77 Ext.extend(Ext.util.ClickRepeater, Ext.util.Observable, {
80 preventDefault : true,
85 * Enables the repeater and allows events to fire.
89 this.el.on('mousedown', this.handleMouseDown, this);
91 this.el.on('dblclick', this.handleDblClick, this);
93 if(this.preventDefault || this.stopDefault){
94 this.el.on('click', this.eventOptions, this);
97 this.disabled = false;
101 * Disables the repeater and stops events from firing.
103 disable: function(/* private */ force){
104 if(force || !this.disabled){
105 clearTimeout(this.timer);
107 this.el.removeClass(this.pressClass);
109 Ext.getDoc().un('mouseup', this.handleMouseUp, this);
110 this.el.removeAllListeners();
112 this.disabled = true;
116 * Convenience function for setting disabled/enabled by boolean.
117 * @param {Boolean} disabled
119 setDisabled: function(disabled){
120 this[disabled ? 'disable' : 'enable']();
123 eventOptions: function(e){
124 if(this.preventDefault){
127 if(this.stopDefault){
133 destroy : function() {
135 Ext.destroy(this.el);
136 this.purgeListeners();
139 handleDblClick : function(){
140 clearTimeout(this.timer);
143 this.fireEvent("mousedown", this);
144 this.fireEvent("click", this);
148 handleMouseDown : function(){
149 clearTimeout(this.timer);
152 this.el.addClass(this.pressClass);
154 this.mousedownTime = new Date();
156 Ext.getDoc().on("mouseup", this.handleMouseUp, this);
157 this.el.on("mouseout", this.handleMouseOut, this);
159 this.fireEvent("mousedown", this);
160 this.fireEvent("click", this);
162 // Do not honor delay or interval if acceleration wanted.
163 if (this.accelerate) {
166 this.timer = this.click.defer(this.delay || this.interval, this);
171 this.fireEvent("click", this);
172 this.timer = this.click.defer(this.accelerate ?
173 this.easeOutExpo(this.mousedownTime.getElapsed(),
177 this.interval, this);
180 easeOutExpo : function (t, b, c, d) {
181 return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
185 handleMouseOut : function(){
186 clearTimeout(this.timer);
188 this.el.removeClass(this.pressClass);
190 this.el.on("mouseover", this.handleMouseReturn, this);
194 handleMouseReturn : function(){
195 this.el.un("mouseover", this.handleMouseReturn, this);
197 this.el.addClass(this.pressClass);
203 handleMouseUp : function(){
204 clearTimeout(this.timer);
205 this.el.un("mouseover", this.handleMouseReturn, this);
206 this.el.un("mouseout", this.handleMouseOut, this);
207 Ext.getDoc().un("mouseup", this.handleMouseUp, this);
208 this.el.removeClass(this.pressClass);
209 this.fireEvent("mouseup", this);