Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / src / widgets / tips / QuickTips.js
1 /*!
2  * Ext JS Library 3.1.1
3  * Copyright(c) 2006-2010 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.QuickTips\r
9  * <p>Provides attractive and customizable tooltips for any element. The QuickTips\r
10  * singleton is used to configure and manage tooltips globally for multiple elements\r
11  * in a generic manner.  To create individual tooltips with maximum customizability,\r
12  * you should consider either {@link Ext.Tip} or {@link Ext.ToolTip}.</p>\r
13  * <p>Quicktips can be configured via tag attributes directly in markup, or by\r
14  * registering quick tips programmatically via the {@link #register} method.</p>\r
15  * <p>The singleton's instance of {@link Ext.QuickTip} is available via\r
16  * {@link #getQuickTip}, and supports all the methods, and all the all the\r
17  * configuration properties of Ext.QuickTip. These settings will apply to all\r
18  * tooltips shown by the singleton.</p>\r
19  * <p>Below is the summary of the configuration properties which can be used.\r
20  * For detailed descriptions see {@link #getQuickTip}</p>\r
21  * <p><b>QuickTips singleton configs (all are optional)</b></p>\r
22  * <div class="mdetail-params"><ul><li>dismissDelay</li>\r
23  * <li>hideDelay</li>\r
24  * <li>maxWidth</li>\r
25  * <li>minWidth</li>\r
26  * <li>showDelay</li>\r
27  * <li>trackMouse</li></ul></div>\r
28  * <p><b>Target element configs (optional unless otherwise noted)</b></p>\r
29  * <div class="mdetail-params"><ul><li>autoHide</li>\r
30  * <li>cls</li>\r
31  * <li>dismissDelay (overrides singleton value)</li>\r
32  * <li>target (required)</li>\r
33  * <li>text (required)</li>\r
34  * <li>title</li>\r
35  * <li>width</li></ul></div>\r
36  * <p>Here is an example showing how some of these config options could be used:</p>\r
37  * <pre><code>\r
38 // Init the singleton.  Any tag-based quick tips will start working.\r
39 Ext.QuickTips.init();\r
40 \r
41 // Apply a set of config properties to the singleton\r
42 Ext.apply(Ext.QuickTips.getQuickTip(), {\r
43     maxWidth: 200,\r
44     minWidth: 100,\r
45     showDelay: 50,\r
46     trackMouse: true\r
47 });\r
48 \r
49 // Manually register a quick tip for a specific element\r
50 Ext.QuickTips.register({\r
51     target: 'my-div',\r
52     title: 'My Tooltip',\r
53     text: 'This tooltip was added in code',\r
54     width: 100,\r
55     dismissDelay: 20\r
56 });\r
57 </code></pre>\r
58  * <p>To register a quick tip in markup, you simply add one or more of the valid QuickTip attributes prefixed with\r
59  * the <b>ext:</b> namespace.  The HTML element itself is automatically set as the quick tip target. Here is the summary\r
60  * of supported attributes (optional unless otherwise noted):</p>\r
61  * <ul><li><b>hide</b>: Specifying "user" is equivalent to setting autoHide = false.  Any other value will be the\r
62  * same as autoHide = true.</li>\r
63  * <li><b>qclass</b>: A CSS class to be applied to the quick tip (equivalent to the 'cls' target element config).</li>\r
64  * <li><b>qtip (required)</b>: The quick tip text (equivalent to the 'text' target element config).</li>\r
65  * <li><b>qtitle</b>: The quick tip title (equivalent to the 'title' target element config).</li>\r
66  * <li><b>qwidth</b>: The quick tip width (equivalent to the 'width' target element config).</li></ul>\r
67  * <p>Here is an example of configuring an HTML element to display a tooltip from markup:</p>\r
68  * <pre><code>\r
69 // Add a quick tip to an HTML button\r
70 &lt;input type="button" value="OK" ext:qtitle="OK Button" ext:qwidth="100"\r
71      ext:qtip="This is a quick tip from markup!">&lt;/input>\r
72 </code></pre>\r
73  * @singleton\r
74  */\r
75 Ext.QuickTips = function(){\r
76     var tip, locks = [];\r
77     return {\r
78         /**\r
79          * Initialize the global QuickTips instance and prepare any quick tips.\r
80          * @param {Boolean} autoRender True to render the QuickTips container immediately to preload images. (Defaults to true) \r
81          */\r
82         init : function(autoRender){\r
83             if(!tip){\r
84                 if(!Ext.isReady){\r
85                     Ext.onReady(function(){\r
86                         Ext.QuickTips.init(autoRender);\r
87                     });\r
88                     return;\r
89                 }\r
90                 tip = new Ext.QuickTip({elements:'header,body'});\r
91                 if(autoRender !== false){\r
92                     tip.render(Ext.getBody());\r
93                 }\r
94             }\r
95         },\r
96 \r
97         /**\r
98          * Enable quick tips globally.\r
99          */\r
100         enable : function(){\r
101             if(tip){\r
102                 locks.pop();\r
103                 if(locks.length < 1){\r
104                     tip.enable();\r
105                 }\r
106             }\r
107         },\r
108 \r
109         /**\r
110          * Disable quick tips globally.\r
111          */\r
112         disable : function(){\r
113             if(tip){\r
114                 tip.disable();\r
115             }\r
116             locks.push(1);\r
117         },\r
118 \r
119         /**\r
120          * Returns true if quick tips are enabled, else false.\r
121          * @return {Boolean}\r
122          */\r
123         isEnabled : function(){\r
124             return tip !== undefined && !tip.disabled;\r
125         },\r
126 \r
127         /**\r
128          * Gets the global QuickTips instance.\r
129          */\r
130         getQuickTip : function(){\r
131             return tip;\r
132         },\r
133 \r
134         /**\r
135          * Configures a new quick tip instance and assigns it to a target element.  See\r
136          * {@link Ext.QuickTip#register} for details.\r
137          * @param {Object} config The config object\r
138          */\r
139         register : function(){\r
140             tip.register.apply(tip, arguments);\r
141         },\r
142 \r
143         /**\r
144          * Removes any registered quick tip from the target element and destroys it.\r
145          * @param {String/HTMLElement/Element} el The element from which the quick tip is to be removed.\r
146          */\r
147         unregister : function(){\r
148             tip.unregister.apply(tip, arguments);\r
149         },\r
150 \r
151         /**\r
152          * Alias of {@link #register}.\r
153          * @param {Object} config The config object\r
154          */\r
155         tips :function(){\r
156             tip.register.apply(tip, arguments);\r
157         }\r
158     }\r
159 }();