commit extjs-2.2.1
[extjs.git] / source / widgets / tips / Tip.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.Tip\r
11  * @extends Ext.Panel\r
12  * This is the base class for {@link Ext.QuickTip} and {@link Ext.Tooltip} that provides the basic layout and\r
13  * positioning that all tip-based classes require. This class can be used directly for simple, statically-positioned\r
14  * tips that are displayed programmatically, or it can be extended to provide custom tip implementations.\r
15  * @constructor\r
16  * Create a new Tip\r
17  * @param {Object} config The configuration options\r
18  */\r
19 Ext.Tip = Ext.extend(Ext.Panel, {\r
20     /**\r
21      * @cfg {Boolean} closable True to render a close tool button into the tooltip header (defaults to false).\r
22      */\r
23     /**\r
24      * @cfg {Number} width\r
25      * Width in pixels of the tip (defaults to auto).  Width will be ignored if it exceeds the bounds of\r
26      * {@link #minWidth} or {@link #maxWidth}.  The maximum supported value is 500.\r
27      */\r
28     /**\r
29      * @cfg {Number} minWidth The minimum width of the tip in pixels (defaults to 40).\r
30      */\r
31     minWidth : 40,\r
32     /**\r
33      * @cfg {Number} maxWidth The maximum width of the tip in pixels (defaults to 300).  The maximum supported value is 500.\r
34      */\r
35     maxWidth : 300,\r
36     /**\r
37      * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop"\r
38      * for bottom-right shadow (defaults to "sides").\r
39      */\r
40     shadow : "sides",\r
41     /**\r
42      * @cfg {String} defaultAlign <b>Experimental</b>. The default {@link Ext.Element#alignTo} anchor position value\r
43      * for this tip relative to its element of origin (defaults to "tl-bl?").\r
44      */\r
45     defaultAlign : "tl-bl?",\r
46     autoRender: true,\r
47     quickShowInterval : 250,\r
48 \r
49     // private panel overrides\r
50     frame:true,\r
51     hidden:true,\r
52     baseCls: 'x-tip',\r
53     floating:{shadow:true,shim:true,useDisplay:true,constrain:false},\r
54     autoHeight:true,\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.hide,\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 });