3 * Copyright(c) 2006-2009 Ext JS, LLC
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);
90 if(this.preventDefault || this.stopDefault){
91 this.el.on('click', this.eventOptions, this);
94 this.disabled = false;
98 * Disables the repeater and stops events from firing.
100 disable: function(/* private */ force){
101 if(force || !this.disabled){
102 clearTimeout(this.timer);
104 this.el.removeClass(this.pressClass);
106 Ext.getDoc().un('mouseup', this.handleMouseUp, this);
107 this.el.removeAllListeners();
109 this.disabled = true;
113 * Convenience function for setting disabled/enabled by boolean.
114 * @param {Boolean} disabled
116 setDisabled: function(disabled){
117 this[disabled ? 'disable' : 'enable']();
120 eventOptions: function(e){
121 if(this.preventDefault){
124 if(this.stopDefault){
130 destroy : function() {
132 Ext.destroy(this.el);
133 this.purgeListeners();
137 handleMouseDown : function(){
138 clearTimeout(this.timer);
141 this.el.addClass(this.pressClass);
143 this.mousedownTime = new Date();
145 Ext.getDoc().on("mouseup", this.handleMouseUp, this);
146 this.el.on("mouseout", this.handleMouseOut, this);
148 this.fireEvent("mousedown", this);
149 this.fireEvent("click", this);
151 // Do not honor delay or interval if acceleration wanted.
152 if (this.accelerate) {
155 this.timer = this.click.defer(this.delay || this.interval, this);
160 this.fireEvent("click", this);
161 this.timer = this.click.defer(this.accelerate ?
162 this.easeOutExpo(this.mousedownTime.getElapsed(),
166 this.interval, this);
169 easeOutExpo : function (t, b, c, d) {
170 return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
174 handleMouseOut : function(){
175 clearTimeout(this.timer);
177 this.el.removeClass(this.pressClass);
179 this.el.on("mouseover", this.handleMouseReturn, this);
183 handleMouseReturn : function(){
184 this.el.un("mouseover", this.handleMouseReturn, this);
186 this.el.addClass(this.pressClass);
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);