Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / slider / Tip.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * Simple plugin for using an Ext.tip.Tip with a slider to show the slider value. In general this class is not created
17  * directly, instead pass the {@link Ext.slider.Multi#useTips} and {@link Ext.slider.Multi#tipText} configuration
18  * options to the slider directly.
19  *
20  *     @example
21  *     Ext.create('Ext.slider.Single', {
22  *         width: 214,
23  *         minValue: 0,
24  *         maxValue: 100,
25  *         useTips: true,
26  *         renderTo: Ext.getBody()
27  *     });
28  *
29  * Optionally provide your own tip text by passing tipText:
30  *
31  *     @example
32  *     Ext.create('Ext.slider.Single', {
33  *         width: 214,
34  *         minValue: 0,
35  *         maxValue: 100,
36  *         useTips: true,
37  *         tipText: function(thumb){
38  *             return Ext.String.format('**{0}% complete**', thumb.value);
39  *         },
40  *         renderTo: Ext.getBody()
41  *     });
42  */
43 Ext.define('Ext.slider.Tip', {
44     extend: 'Ext.tip.Tip',
45     minWidth: 10,
46     alias: 'widget.slidertip',
47     offsets : [0, -10],
48
49     isSliderTip: true,
50
51     init: function(slider) {
52         var me = this;
53
54         slider.on({
55             scope    : me,
56             dragstart: me.onSlide,
57             drag     : me.onSlide,
58             dragend  : me.hide,
59             destroy  : me.destroy
60         });
61     },
62     /**
63      * @private
64      * Called whenever a dragstart or drag event is received on the associated Thumb.
65      * Aligns the Tip with the Thumb's new position.
66      * @param {Ext.slider.MultiSlider} slider The slider
67      * @param {Ext.EventObject} e The Event object
68      * @param {Ext.slider.Thumb} thumb The thumb that the Tip is attached to
69      */
70     onSlide : function(slider, e, thumb) {
71         var me = this;
72         me.show();
73         me.update(me.getText(thumb));
74         me.doComponentLayout();
75         me.el.alignTo(thumb.el, 'b-t?', me.offsets);
76     },
77
78     /**
79      * Used to create the text that appears in the Tip's body. By default this just returns the value of the Slider
80      * Thumb that the Tip is attached to. Override to customize.
81      * @param {Ext.slider.Thumb} thumb The Thumb that the Tip is attached to
82      * @return {String} The text to display in the tip
83      */
84     getText : function(thumb) {
85         return String(thumb.value);
86     }
87 });