Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / src / widgets / tips / Tip.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.Tip
9  * @extends Ext.Panel
10  * @xtype tip
11  * This is the base class for {@link Ext.QuickTip} and {@link Ext.Tooltip} that provides the basic layout and
12  * positioning that all tip-based classes require. This class can be used directly for simple, statically-positioned
13  * tips that are displayed programmatically, or it can be extended to provide custom tip implementations.
14  * @constructor
15  * Create a new Tip
16  * @param {Object} config The configuration options
17  */
18 Ext.Tip = Ext.extend(Ext.Panel, {
19     /**
20      * @cfg {Boolean} closable True to render a close tool button into the tooltip header (defaults to false).
21      */
22     /**
23      * @cfg {Number} width
24      * Width in pixels of the tip (defaults to auto).  Width will be ignored if it exceeds the bounds of
25      * {@link #minWidth} or {@link #maxWidth}.  The maximum supported value is 500.
26      */
27     /**
28      * @cfg {Number} minWidth The minimum width of the tip in pixels (defaults to 40).
29      */
30     minWidth : 40,
31     /**
32      * @cfg {Number} maxWidth The maximum width of the tip in pixels (defaults to 300).  The maximum supported value is 500.
33      */
34     maxWidth : 300,
35     /**
36      * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop"
37      * for bottom-right shadow (defaults to "sides").
38      */
39     shadow : "sides",
40     /**
41      * @cfg {String} defaultAlign <b>Experimental</b>. The default {@link Ext.Element#alignTo} anchor position value
42      * for this tip relative to its element of origin (defaults to "tl-bl?").
43      */
44     defaultAlign : "tl-bl?",
45     autoRender: true,
46     quickShowInterval : 250,
47
48     // private panel overrides
49     frame:true,
50     hidden:true,
51     baseCls: 'x-tip',
52     floating:{shadow:true,shim:true,useDisplay:true,constrain:false},
53     autoHeight:true,
54
55     closeAction: 'hide',
56
57     // private
58     initComponent : function(){
59         Ext.Tip.superclass.initComponent.call(this);
60         if(this.closable && !this.title){
61             this.elements += ',header';
62         }
63     },
64
65     // private
66     afterRender : function(){
67         Ext.Tip.superclass.afterRender.call(this);
68         if(this.closable){
69             this.addTool({
70                 id: 'close',
71                 handler: this[this.closeAction],
72                 scope: this
73             });
74         }
75     },
76
77     /**
78      * Shows this tip at the specified XY position.  Example usage:
79      * <pre><code>
80 // Show the tip at x:50 and y:100
81 tip.showAt([50,100]);
82 </code></pre>
83      * @param {Array} xy An array containing the x and y coordinates
84      */
85     showAt : function(xy){
86         Ext.Tip.superclass.show.call(this);
87         if(this.measureWidth !== false && (!this.initialConfig || typeof this.initialConfig.width != 'number')){
88             this.doAutoWidth();
89         }
90         if(this.constrainPosition){
91             xy = this.el.adjustForConstraints(xy);
92         }
93         this.setPagePosition(xy[0], xy[1]);
94     },
95
96     // protected
97     doAutoWidth : function(adjust){
98         adjust = adjust || 0;
99         var bw = this.body.getTextWidth();
100         if(this.title){
101             bw = Math.max(bw, this.header.child('span').getTextWidth(this.title));
102         }
103         bw += this.getFrameWidth() + (this.closable ? 20 : 0) + this.body.getPadding("lr") + adjust;
104         this.setWidth(bw.constrain(this.minWidth, this.maxWidth));
105         
106         // IE7 repaint bug on initial show
107         if(Ext.isIE7 && !this.repainted){
108             this.el.repaint();
109             this.repainted = true;
110         }
111     },
112
113     /**
114      * <b>Experimental</b>. Shows this tip at a position relative to another element using a standard {@link Ext.Element#alignTo}
115      * anchor position value.  Example usage:
116      * <pre><code>
117 // Show the tip at the default position ('tl-br?')
118 tip.showBy('my-el');
119
120 // Show the tip's top-left corner anchored to the element's top-right corner
121 tip.showBy('my-el', 'tl-tr');
122 </code></pre>
123      * @param {Mixed} el An HTMLElement, Ext.Element or string id of the target element to align to
124      * @param {String} position (optional) A valid {@link Ext.Element#alignTo} anchor position (defaults to 'tl-br?' or
125      * {@link #defaultAlign} if specified).
126      */
127     showBy : function(el, pos){
128         if(!this.rendered){
129             this.render(Ext.getBody());
130         }
131         this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign));
132     },
133
134     initDraggable : function(){
135         this.dd = new Ext.Tip.DD(this, typeof this.draggable == 'boolean' ? null : this.draggable);
136         this.header.addClass('x-tip-draggable');
137     }
138 });
139
140 Ext.reg('tip', Ext.Tip);
141
142 // private - custom Tip DD implementation
143 Ext.Tip.DD = function(tip, config){
144     Ext.apply(this, config);
145     this.tip = tip;
146     Ext.Tip.DD.superclass.constructor.call(this, tip.el.id, 'WindowDD-'+tip.id);
147     this.setHandleElId(tip.header.id);
148     this.scroll = false;
149 };
150
151 Ext.extend(Ext.Tip.DD, Ext.dd.DD, {
152     moveOnly:true,
153     scroll:false,
154     headerOffsets:[100, 25],
155     startDrag : function(){
156         this.tip.el.disableShadow();
157     },
158     endDrag : function(e){
159         this.tip.el.enableShadow(true);
160     }
161 });