Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / tip / QuickTipManager.js
1 /**
2  * @class Ext.tip.QuickTipManager
3  * <p>Provides attractive and customizable tooltips for any element. The QuickTips
4  * singleton is used to configure and manage tooltips globally for multiple elements
5  * in a generic manner.  To create individual tooltips with maximum customizability,
6  * you should consider either {@link Ext.tip.Tip} or {@link Ext.tip.ToolTip}.</p>
7  * <p>Quicktips can be configured via tag attributes directly in markup, or by
8  * registering quick tips programmatically via the {@link #register} method.</p>
9  * <p>The singleton's instance of {@link Ext.tip.QuickTip} is available via
10  * {@link #getQuickTip}, and supports all the methods, and all the all the
11  * configuration properties of Ext.tip.QuickTip. These settings will apply to all
12  * tooltips shown by the singleton.</p>
13  * <p>Below is the summary of the configuration properties which can be used.
14  * For detailed descriptions see the config options for the {@link Ext.tip.QuickTip QuickTip} class</p>
15  * <p><b>QuickTips singleton configs (all are optional)</b></p>
16  * <div class="mdetail-params"><ul><li>dismissDelay</li>
17  * <li>hideDelay</li>
18  * <li>maxWidth</li>
19  * <li>minWidth</li>
20  * <li>showDelay</li>
21  * <li>trackMouse</li></ul></div>
22  * <p><b>Target element configs (optional unless otherwise noted)</b></p>
23  * <div class="mdetail-params"><ul><li>autoHide</li>
24  * <li>cls</li>
25  * <li>dismissDelay (overrides singleton value)</li>
26  * <li>target (required)</li>
27  * <li>text (required)</li>
28  * <li>title</li>
29  * <li>width</li></ul></div>
30  * <p>Here is an example showing how some of these config options could be used:</p>
31  *
32  * {@img Ext.tip.QuickTipManager/Ext.tip.QuickTipManager.png Ext.tip.QuickTipManager component}
33  *
34  * ## Code
35  *    // Init the singleton.  Any tag-based quick tips will start working.
36  *    Ext.tip.QuickTipManager.init();
37  *    
38  *    // Apply a set of config properties to the singleton
39  *    Ext.apply(Ext.tip.QuickTipManager.getQuickTip(), {
40  *        maxWidth: 200,
41  *        minWidth: 100,
42  *        showDelay: 50      // Show 50ms after entering target
43  *    });
44  *    
45  *    // Create a small panel to add a quick tip to
46  *    Ext.create('Ext.container.Container', {
47  *        id: 'quickTipContainer',
48  *        width: 200,
49  *        height: 150,
50  *        style: {
51  *            backgroundColor:'#000000'
52  *        },
53  *        renderTo: Ext.getBody()
54  *    });
55  *
56  *    
57  *    // Manually register a quick tip for a specific element
58  *    Ext.tip.QuickTipManager.register({
59  *        target: 'quickTipContainer',
60  *        title: 'My Tooltip',
61  *        text: 'This tooltip was added in code',
62  *        width: 100,
63  *        dismissDelay: 10000 // Hide after 10 seconds hover
64  *    });
65 </code></pre>
66  * <p>To register a quick tip in markup, you simply add one or more of the valid QuickTip attributes prefixed with
67  * the <b>ext:</b> namespace.  The HTML element itself is automatically set as the quick tip target. Here is the summary
68  * of supported attributes (optional unless otherwise noted):</p>
69  * <ul><li><b>hide</b>: Specifying "user" is equivalent to setting autoHide = false.  Any other value will be the
70  * same as autoHide = true.</li>
71  * <li><b>qclass</b>: A CSS class to be applied to the quick tip (equivalent to the 'cls' target element config).</li>
72  * <li><b>qtip (required)</b>: The quick tip text (equivalent to the 'text' target element config).</li>
73  * <li><b>qtitle</b>: The quick tip title (equivalent to the 'title' target element config).</li>
74  * <li><b>qwidth</b>: The quick tip width (equivalent to the 'width' target element config).</li></ul>
75  * <p>Here is an example of configuring an HTML element to display a tooltip from markup:</p>
76  * <pre><code>
77 // Add a quick tip to an HTML button
78 &lt;input type="button" value="OK" ext:qtitle="OK Button" ext:qwidth="100"
79      data-qtip="This is a quick tip from markup!">&lt;/input>
80 </code></pre>
81  * @singleton
82  */
83 Ext.define('Ext.tip.QuickTipManager', function() {
84     var tip,
85         disabled = false;
86
87     return {
88         requires: ['Ext.tip.QuickTip'],
89         singleton: true,
90         alternateClassName: 'Ext.QuickTips',
91         /**
92          * Initialize the global QuickTips instance and prepare any quick tips.
93          * @param {Boolean} autoRender True to render the QuickTips container immediately to preload images. (Defaults to true) 
94          */
95         init : function(autoRender){
96             if (!tip) {
97                 if (!Ext.isReady) {
98                     Ext.onReady(function(){
99                         Ext.tip.QuickTipManager.init(autoRender);
100                     });
101                     return;
102                 }
103                 tip = Ext.create('Ext.tip.QuickTip', {
104                     disabled: disabled,
105                     renderTo: autoRender !== false ? document.body : undefined
106                 });
107             }
108         },
109
110         /**
111          * Destroy the QuickTips instance.
112          */
113         destroy: function() {
114             if (tip) {
115                 var undef;
116                 tip.destroy();
117                 tip = undef;
118             }
119         },
120
121         // Protected method called by the dd classes
122         ddDisable : function(){
123             // don't disable it if we don't need to
124             if(tip && !disabled){
125                 tip.disable();
126             }
127         },
128
129         // Protected method called by the dd classes
130         ddEnable : function(){
131             // only enable it if it hasn't been disabled
132             if(tip && !disabled){
133                 tip.enable();
134             }
135         },
136
137         /**
138          * Enable quick tips globally.
139          */
140         enable : function(){
141             if(tip){
142                 tip.enable();
143             }
144             disabled = false;
145         },
146
147         /**
148          * Disable quick tips globally.
149          */
150         disable : function(){
151             if(tip){
152                 tip.disable();
153             }
154             disabled = true;
155         },
156
157         /**
158          * Returns true if quick tips are enabled, else false.
159          * @return {Boolean}
160          */
161         isEnabled : function(){
162             return tip !== undefined && !tip.disabled;
163         },
164
165         /**
166          * Gets the single {@link Ext.tip.QuickTip QuickTip} instance used to show tips from all registered elements.
167          * @return {Ext.tip.QuickTip}
168          */
169         getQuickTip : function(){
170             return tip;
171         },
172
173         /**
174          * Configures a new quick tip instance and assigns it to a target element.  See
175          * {@link Ext.tip.QuickTip#register} for details.
176          * @param {Object} config The config object
177          */
178         register : function(){
179             tip.register.apply(tip, arguments);
180         },
181
182         /**
183          * Removes any registered quick tip from the target element and destroys it.
184          * @param {String/HTMLElement/Element} el The element from which the quick tip is to be removed.
185          */
186         unregister : function(){
187             tip.unregister.apply(tip, arguments);
188         },
189
190         /**
191          * Alias of {@link #register}.
192          * @param {Object} config The config object
193          */
194         tips : function(){
195             tip.register.apply(tip, arguments);
196         }
197     };
198 }());