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