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