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