Upgrade to ExtJS 4.0.2 - Released 06/09/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 True to render a close tool button into the tooltip header (defaults to false).
29      */
30     /**
31      * @cfg {Number} width
32      * Width in pixels of the tip (defaults to auto).  Width will be ignored if it exceeds the bounds of
33      * {@link #minWidth} or {@link #maxWidth}.  The maximum supported value is 500.
34      */
35     /**
36      * @cfg {Number} minWidth The minimum width of the tip in pixels (defaults to 40).
37      */
38     minWidth : 40,
39     /**
40      * @cfg {Number} maxWidth The maximum width of the tip in pixels (defaults to 300).  The maximum supported value is 500.
41      */
42     maxWidth : 300,
43     /**
44      * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop"
45      * for bottom-right shadow (defaults to "sides").
46      */
47     shadow : "sides",
48
49     /**
50      * @cfg {String} defaultAlign <b>Experimental</b>. The default {@link Ext.core.Element#alignTo} anchor position value
51      * for this tip relative to its element of origin (defaults to "tl-bl?").
52      */
53     defaultAlign : "tl-bl?",
54     /**
55      * @cfg {Boolean} constrainPosition If true, then the tooltip will be automatically constrained to stay within
56      * the browser viewport. Defaults to false.
57      */
58     constrainPosition : true,
59
60     /**
61      * @inherited
62      */
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         this.callParent(arguments);
100
101         // Or in the deprecated config. Floating.doConstrain only constrains if the constrain property is truthy.
102         this.constrain = this.constrain || this.constrainPosition;
103     },
104
105     /**
106      * Shows this tip at the specified XY position.  Example usage:
107      * <pre><code>
108 // Show the tip at x:50 and y:100
109 tip.showAt([50,100]);
110 </code></pre>
111      * @param {Array} xy An array containing the x and y coordinates
112      */
113     showAt : function(xy){
114         var me = this;
115         this.callParent();
116         // Show may have been vetoed.
117         if (me.isVisible()) {
118             me.setPagePosition(xy[0], xy[1]);
119             if (me.constrainPosition || me.constrain) {
120                 me.doConstrain();
121             }
122             me.toFront(true);
123         }
124     },
125
126     /**
127      * <b>Experimental</b>. Shows this tip at a position relative to another element using a standard {@link Ext.core.Element#alignTo}
128      * anchor position value.  Example usage:
129      * <pre><code>
130 // Show the tip at the default position ('tl-br?')
131 tip.showBy('my-el');
132
133 // Show the tip's top-left corner anchored to the element's top-right corner
134 tip.showBy('my-el', 'tl-tr');
135 </code></pre>
136      * @param {Mixed} el An HTMLElement, Ext.core.Element or string id of the target element to align to
137      * @param {String} position (optional) A valid {@link Ext.core.Element#alignTo} anchor position (defaults to 'tl-br?' or
138      * {@link #defaultAlign} if specified).
139      */
140     showBy : function(el, pos) {
141         this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign));
142     },
143
144     /**
145      * @private
146      * @override
147      * Set Tip draggable using base Component's draggability
148      */
149     initDraggable : function(){
150         var me = this;
151         me.draggable = {
152             el: me.getDragEl(),
153             delegate: me.header.el,
154             constrain: me,
155             constrainTo: me.el.dom.parentNode
156         };
157         // Important: Bypass Panel's initDraggable. Call direct to Component's implementation.
158         Ext.Component.prototype.initDraggable.call(me);
159     },
160
161     // Tip does not ghost. Drag is "live"
162     ghost: undefined,
163     unghost: undefined
164 });
165