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