Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / tip / 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.tip.Tip
17  * @extends Ext.panel.Panel
18  * This is the base class for {@link Ext.tip.QuickTip} and {@link Ext.tip.ToolTip} that provides the basic layout and
19  * positioning that all tip-based classes require. This class can be used directly for simple, statically-positioned
20  * tips that are displayed programmatically, or it can be extended to provide custom tip implementations.
21  * @xtype tip
22  */
23 Ext.define('Ext.tip.Tip', {
24     extend: 'Ext.panel.Panel',
25     requires: [ 'Ext.layout.component.Tip' ],
26     alternateClassName: 'Ext.Tip',
27     /**
28      * @cfg {Boolean} [closable=false]
29      * True to render a close tool button into the tooltip header.
30      */
31     /**
32      * @cfg {Number} width
33      * Width in pixels of the tip (defaults to auto).  Width will be ignored if it exceeds the bounds of
34      * {@link #minWidth} or {@link #maxWidth}.  The maximum supported value is 500.
35      */
36     /**
37      * @cfg {Number} minWidth The minimum width of the tip in pixels.
38      */
39     minWidth : 40,
40     /**
41      * @cfg {Number} maxWidth The maximum width of the tip in pixels.  The maximum supported value is 500.
42      */
43     maxWidth : 300,
44     /**
45      * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop"
46      * for bottom-right shadow.
47      */
48     shadow : "sides",
49
50     /**
51      * @cfg {String} defaultAlign
52      * <b>Experimental</b>. The default {@link Ext.Element#alignTo} anchor position value for this tip relative
53      * to its element of origin.
54      */
55     defaultAlign : "tl-bl?",
56     /**
57      * @cfg {Boolean} constrainPosition
58      * If true, then the tooltip will be automatically constrained to stay within the browser viewport.
59      */
60     constrainPosition : true,
61
62     // @inherited
63     frame: false,
64
65     // private panel overrides
66     autoRender: true,
67     hidden: true,
68     baseCls: Ext.baseCSSPrefix + 'tip',
69     floating: {
70         shadow: true,
71         shim: true,
72         constrain: true
73     },
74     focusOnToFront: false,
75     componentLayout: 'tip',
76
77     /**
78      * @cfg {String} closeAction
79      * <p>The action to take when the close header tool is clicked:
80      * <div class="mdetail-params"><ul>
81      * <li><b><code>'{@link #destroy}'</code></b> : <div class="sub-desc">
82      * {@link #destroy remove} the window from the DOM and {@link Ext.Component#destroy destroy}
83      * it and all descendant Components. The window will <b>not</b> be available to be
84      * redisplayed via the {@link #show} method.
85      * </div></li>
86      * <li><b><code>'{@link #hide}'</code></b> : <b>Default</b><div class="sub-desc">
87      * {@link #hide} the window by setting visibility to hidden and applying negative offsets.
88      * The window will be available to be redisplayed via the {@link #show} method.
89      * </div></li>
90      * </ul></div>
91      * <p><b>Note:</b> This behavior has changed! setting *does* affect the {@link #close} method
92      * which will invoke the approriate closeAction.
93      */
94     closeAction: 'hide',
95
96     ariaRole: 'tooltip',
97
98     initComponent: function() {
99         var me = this;
100
101         me.floating = Ext.apply({}, {shadow: me.shadow}, me.self.prototype.floating);
102         me.callParent(arguments);
103
104         // Or in the deprecated config. Floating.doConstrain only constrains if the constrain property is truthy.
105         me.constrain = me.constrain || me.constrainPosition;
106     },
107
108     /**
109      * Shows this tip at the specified XY position.  Example usage:
110      * <pre><code>
111 // Show the tip at x:50 and y:100
112 tip.showAt([50,100]);
113 </code></pre>
114      * @param {Number[]} xy An array containing the x and y coordinates
115      */
116     showAt : function(xy){
117         var me = this;
118         this.callParent(arguments);
119         // Show may have been vetoed.
120         if (me.isVisible()) {
121             me.setPagePosition(xy[0], xy[1]);
122             if (me.constrainPosition || me.constrain) {
123                 me.doConstrain();
124             }
125             me.toFront(true);
126         }
127     },
128
129     /**
130      * <b>Experimental</b>. Shows this tip at a position relative to another element using a standard {@link Ext.Element#alignTo}
131      * anchor position value.  Example usage:
132      * <pre><code>
133 // Show the tip at the default position ('tl-br?')
134 tip.showBy('my-el');
135
136 // Show the tip's top-left corner anchored to the element's top-right corner
137 tip.showBy('my-el', 'tl-tr');
138 </code></pre>
139      * @param {String/HTMLElement/Ext.Element} el An HTMLElement, Ext.Element or string id of the target element to align to
140      * @param {String} [position] A valid {@link Ext.Element#alignTo} anchor position (defaults to 'tl-br?' or
141      * {@link #defaultAlign} if specified).
142      */
143     showBy : function(el, pos) {
144         this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign));
145     },
146
147     /**
148      * @private
149      * @override
150      * Set Tip draggable using base Component's draggability
151      */
152     initDraggable : function(){
153         var me = this;
154         me.draggable = {
155             el: me.getDragEl(),
156             delegate: me.header.el,
157             constrain: me,
158             constrainTo: me.el.getScopeParent()
159         };
160         // Important: Bypass Panel's initDraggable. Call direct to Component's implementation.
161         Ext.Component.prototype.initDraggable.call(me);
162     },
163
164     // Tip does not ghost. Drag is "live"
165     ghost: undefined,
166     unghost: undefined
167 });
168