Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / tip / Tip.js
1 /**
2  * @class Ext.tip.Tip
3  * @extends Ext.panel.Panel
4  * This is the base class for {@link Ext.tip.QuickTip} and {@link Ext.tip.ToolTip} that provides the basic layout and
5  * positioning that all tip-based classes require. This class can be used directly for simple, statically-positioned
6  * tips that are displayed programmatically, or it can be extended to provide custom tip implementations.
7  * @constructor
8  * Create a new Tip
9  * @param {Object} config The configuration options
10  * @xtype tip
11  */
12 Ext.define('Ext.tip.Tip', {
13     extend: 'Ext.panel.Panel',
14     requires: [ 'Ext.layout.component.Tip' ],
15     alternateClassName: 'Ext.Tip',
16     /**
17      * @cfg {Boolean} closable True to render a close tool button into the tooltip header (defaults to false).
18      */
19     /**
20      * @cfg {Number} width
21      * Width in pixels of the tip (defaults to auto).  Width will be ignored if it exceeds the bounds of
22      * {@link #minWidth} or {@link #maxWidth}.  The maximum supported value is 500.
23      */
24     /**
25      * @cfg {Number} minWidth The minimum width of the tip in pixels (defaults to 40).
26      */
27     minWidth : 40,
28     /**
29      * @cfg {Number} maxWidth The maximum width of the tip in pixels (defaults to 300).  The maximum supported value is 500.
30      */
31     maxWidth : 300,
32     /**
33      * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop"
34      * for bottom-right shadow (defaults to "sides").
35      */
36     shadow : "sides",
37
38     /**
39      * @cfg {String} defaultAlign <b>Experimental</b>. The default {@link Ext.core.Element#alignTo} anchor position value
40      * for this tip relative to its element of origin (defaults to "tl-bl?").
41      */
42     defaultAlign : "tl-bl?",
43     /**
44      * @cfg {Boolean} constrainPosition If true, then the tooltip will be automatically constrained to stay within
45      * the browser viewport. Defaults to false.
46      */
47     constrainPosition : true,
48
49     /**
50      * @inherited
51      */
52     frame: false,
53
54     // private panel overrides
55     autoRender: true,
56     hidden: true,
57     baseCls: Ext.baseCSSPrefix + 'tip',
58     floating: {
59         shadow: true,
60         shim: true,
61         constrain: true
62     },
63     focusOnToFront: false,
64     componentLayout: 'tip',
65
66     closeAction: 'hide',
67
68     ariaRole: 'tooltip',
69
70     initComponent: function() {
71         this.callParent(arguments);
72
73         // Or in the deprecated config. Floating.doConstrain only constrains if the constrain property is truthy.
74         this.constrain = this.constrain || this.constrainPosition;
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         var me = this;
87         this.callParent();
88         // Show may have been vetoed.
89         if (me.isVisible()) {
90             me.setPagePosition(xy[0], xy[1]);
91             if (me.constrainPosition || me.constrain) {
92                 me.doConstrain();
93             }
94             me.toFront(true);
95         }
96     },
97
98     /**
99      * <b>Experimental</b>. Shows this tip at a position relative to another element using a standard {@link Ext.core.Element#alignTo}
100      * anchor position value.  Example usage:
101      * <pre><code>
102 // Show the tip at the default position ('tl-br?')
103 tip.showBy('my-el');
104
105 // Show the tip's top-left corner anchored to the element's top-right corner
106 tip.showBy('my-el', 'tl-tr');
107 </code></pre>
108      * @param {Mixed} el An HTMLElement, Ext.core.Element or string id of the target element to align to
109      * @param {String} position (optional) A valid {@link Ext.core.Element#alignTo} anchor position (defaults to 'tl-br?' or
110      * {@link #defaultAlign} if specified).
111      */
112     showBy : function(el, pos) {
113         this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign));
114     },
115
116     /**
117      * @private
118      * @override
119      * Set Tip draggable using base Component's draggability
120      */
121     initDraggable : function(){
122         var me = this;
123         me.draggable = {
124             el: me.getDragEl(),
125             delegate: me.header.el,
126             constrain: me,
127             constrainTo: me.el.dom.parentNode
128         };
129         // Important: Bypass Panel's initDraggable. Call direct to Component's implementation.
130         Ext.Component.prototype.initDraggable.call(me);
131     },
132
133     // Tip does not ghost. Drag is "live"
134     ghost: undefined,
135     unghost: undefined
136 });