Upgrade to ExtJS 4.0.2 - Released 06/09/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  * @class Ext.slider.Tip
17  * @extends Ext.tip.Tip
18  * Simple plugin for using an Ext.tip.Tip with a slider to show the slider value. In general this
19  * class is not created directly, instead pass the {@link Ext.slider.Multi#useTips} and 
20  * {@link Ext.slider.Multi#tipText} configuration options to the slider directly.
21  * {@img Ext.slider.Tip/Ext.slider.Tip1.png Ext.slider.Tip component}
22  * Example usage:
23 <pre>
24     Ext.create('Ext.slider.Single', {
25         width: 214,
26         minValue: 0,
27         maxValue: 100,
28         useTips: true,
29         renderTo: Ext.getBody()
30     });   
31 </pre>
32  * Optionally provide your own tip text by passing tipText:
33  <pre>
34  new Ext.slider.Single({
35      width: 214,
36      minValue: 0,
37      maxValue: 100,
38      useTips: true,
39      tipText: function(thumb){
40          return Ext.String.format('<b>{0}% complete</b>', thumb.value);
41      }
42  });
43  </pre>
44  */
45 Ext.define('Ext.slider.Tip', {
46     extend: 'Ext.tip.Tip',
47     minWidth: 10,
48     alias: 'widget.slidertip',
49     offsets : [0, -10],
50     
51     isSliderTip: true,
52
53     init: function(slider) {
54         var me = this;
55         
56         slider.on({
57             scope    : me,
58             dragstart: me.onSlide,
59             drag     : me.onSlide,
60             dragend  : me.hide,
61             destroy  : me.destroy
62         });
63     },
64     /**
65      * @private
66      * Called whenever a dragstart or drag event is received on the associated Thumb. 
67      * Aligns the Tip with the Thumb's new position.
68      * @param {Ext.slider.MultiSlider} slider The slider
69      * @param {Ext.EventObject} e The Event object
70      * @param {Ext.slider.Thumb} thumb The thumb that the Tip is attached to
71      */
72     onSlide : function(slider, e, thumb) {
73         var me = this;
74         me.show();
75         me.update(me.getText(thumb));
76         me.doComponentLayout();
77         me.el.alignTo(thumb.el, 'b-t?', me.offsets);
78     },
79
80     /**
81      * Used to create the text that appears in the Tip's body. By default this just returns
82      * the value of the Slider Thumb that the Tip is attached to. Override to customize.
83      * @param {Ext.slider.Thumb} thumb The Thumb that the Tip is attached to
84      * @return {String} The text to display in the tip
85      */
86     getText : function(thumb) {
87         return String(thumb.value);
88     }
89 });