Roo/MessageBox.js
[roojs1] / Roo / MessageBox.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.MessageBox
14  * Utility class for generating different styles of message boxes.  The alias Roo.Msg can also be used.
15  * Example usage:
16  *<pre><code>
17 // Basic alert:
18 Roo.Msg.alert('Status', 'Changes saved successfully.');
19
20 // Prompt for user data:
21 Roo.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
22     if (btn == 'ok'){
23         // process text value...
24     }
25 });
26
27 // Show a dialog using config options:
28 Roo.Msg.show({
29    title:'Save Changes?',
30    msg: 'Your are closing a tab that has unsaved changes. Would you like to save your changes?',
31    buttons: Roo.Msg.YESNOCANCEL,
32    fn: processResult,
33    animEl: 'elId'
34 });
35 </code></pre>
36  * @singleton
37  */
38 Roo.MessageBox = function(){
39     var dlg, opt, mask, waitTimer;
40     var bodyEl, msgEl, textboxEl, textareaEl, progressEl, pp;
41     var buttons, activeTextEl, bwidth;
42
43     // private
44     var handleButton = function(button){
45         dlg.hide();
46         Roo.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value], 1);
47     };
48
49     // private
50     var handleHide = function(){
51         if(opt && opt.cls){
52             dlg.el.removeClass(opt.cls);
53         }
54         if(waitTimer){
55             Roo.TaskMgr.stop(waitTimer);
56             waitTimer = null;
57         }
58     };
59
60     // private
61     var updateButtons = function(b){
62         var width = 0;
63         if(!b){
64             buttons["ok"].hide();
65             buttons["cancel"].hide();
66             buttons["yes"].hide();
67             buttons["no"].hide();
68             dlg.footer.dom.style.display = 'none';
69             return width;
70         }
71         dlg.footer.dom.style.display = '';
72         for(var k in buttons){
73             if(typeof buttons[k] != "function"){
74                 if(b[k]){
75                     buttons[k].show();
76                     buttons[k].setText(typeof b[k] == "string" ? b[k] : Roo.MessageBox.buttonText[k]);
77                     width += buttons[k].el.getWidth()+15;
78                 }else{
79                     buttons[k].hide();
80                 }
81             }
82         }
83         return width;
84     };
85
86     // private
87     var handleEsc = function(d, k, e){
88         if(opt && opt.closable !== false){
89             dlg.hide();
90         }
91         if(e){
92             e.stopEvent();
93         }
94     };
95
96     return {
97         /**
98          * Returns a reference to the underlying {@link Roo.BasicDialog} element
99          * @return {Roo.BasicDialog} The BasicDialog element
100          */
101         getDialog : function(){
102            if(!dlg){
103                 dlg = new Roo.BasicDialog("x-msg-box", {
104                     autoCreate : true,
105                     shadow: true,
106                     draggable: true,
107                     resizable:false,
108                     constraintoviewport:false,
109                     fixedcenter:true,
110                     collapsible : false,
111                     shim:true,
112                     modal: true,
113                     width:400, height:100,
114                     buttonAlign:"center",
115                     closeClick : function(){
116                         if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
117                             handleButton("no");
118                         }else{
119                             handleButton("cancel");
120                         }
121                     }
122                 });
123                 dlg.on("hide", handleHide);
124                 mask = dlg.mask;
125                 dlg.addKeyListener(27, handleEsc);
126                 buttons = {};
127                 var bt = this.buttonText;
128                 buttons["ok"] = dlg.addButton(bt["ok"], handleButton.createCallback("ok"));
129                 buttons["yes"] = dlg.addButton(bt["yes"], handleButton.createCallback("yes"));
130                 buttons["no"] = dlg.addButton(bt["no"], handleButton.createCallback("no"));
131                 buttons["cancel"] = dlg.addButton(bt["cancel"], handleButton.createCallback("cancel"));
132                 bodyEl = dlg.body.createChild({
133
134                     html:'<span class="roo-mb-text"></span><br /><input type="text" class="roo-mb-input" /><textarea class="roo-mb-textarea"></textarea><div class="roo-mb-progress-wrap"><div class="roo-mb-progress"><div class="roo-mb-progress-bar">&#160;</div></div></div>'
135                 });
136                 msgEl = bodyEl.dom.firstChild;
137                 textboxEl = Roo.get(bodyEl.dom.childNodes[2]);
138                 textboxEl.enableDisplayMode();
139                 textboxEl.addKeyListener([10,13], function(){
140                     if(dlg.isVisible() && opt && opt.buttons){
141                         if(opt.buttons.ok){
142                             handleButton("ok");
143                         }else if(opt.buttons.yes){
144                             handleButton("yes");
145                         }
146                     }
147                 });
148                 textareaEl = Roo.get(bodyEl.dom.childNodes[3]);
149                 textareaEl.enableDisplayMode();
150                 progressEl = Roo.get(bodyEl.dom.childNodes[4]);
151                 progressEl.enableDisplayMode();
152                 var pf = progressEl.dom.firstChild;
153                 if (pf) {
154                     pp = Roo.get(pf.firstChild);
155                     pp.setHeight(pf.offsetHeight);
156                 }
157                 
158             }
159             return dlg;
160         },
161
162         /**
163          * Updates the message box body text
164          * @param {String} text (optional) Replaces the message box element's innerHTML with the specified string (defaults to
165          * the XHTML-compliant non-breaking space character '&amp;#160;')
166          * @return {Roo.MessageBox} This message box
167          */
168         updateText : function(text){
169             if(!dlg.isVisible() && !opt.width){
170                 dlg.resizeTo(this.maxWidth, 100); // resize first so content is never clipped from previous shows
171             }
172             msgEl.innerHTML = text || '&#160;';
173             var w = Math.max(Math.min(opt.width || msgEl.offsetWidth, this.maxWidth), 
174                         Math.max(opt.minWidth || this.minWidth, bwidth));
175             if(opt.prompt){
176                 activeTextEl.setWidth(w);
177             }
178             if(dlg.isVisible()){
179                 dlg.fixedcenter = false;
180             }
181             // to big, make it scoll.
182             if ( bodyEl.getHeight() > (Roo.lib.Dom.getViewHeight() - 100)) {
183                 bodyEl.setHeight ( Roo.lib.Dom.getViewHeight() - 100 );
184                 bodyEl.el.dom.style.overflow = '';
185             } else {
186                 bodyEl.el.dom.style.height = '';
187                 bodyEl.el.dom.style.overflowY = 'auto';
188             }
189             
190             dlg.setContentSize(w, bodyEl.getHeight());
191             if(dlg.isVisible()){
192                 dlg.fixedcenter = true;
193             }
194             return this;
195         },
196
197         /**
198          * Updates a progress-style message box's text and progress bar.  Only relevant on message boxes
199          * initiated via {@link Roo.MessageBox#progress} or by calling {@link Roo.MessageBox#show} with progress: true.
200          * @param {Number} value Any number between 0 and 1 (e.g., .5)
201          * @param {String} text (optional) If defined, the message box's body text is replaced with the specified string (defaults to undefined)
202          * @return {Roo.MessageBox} This message box
203          */
204         updateProgress : function(value, text){
205             if(text){
206                 this.updateText(text);
207             }
208             if (pp) { // weird bug on my firefox - for some reason this is not defined
209                 pp.setWidth(Math.floor(value*progressEl.dom.firstChild.offsetWidth));
210             }
211             return this;
212         },        
213
214         /**
215          * Returns true if the message box is currently displayed
216          * @return {Boolean} True if the message box is visible, else false
217          */
218         isVisible : function(){
219             return dlg && dlg.isVisible();  
220         },
221
222         /**
223          * Hides the message box if it is displayed
224          */
225         hide : function(){
226             if(this.isVisible()){
227                 dlg.hide();
228             }  
229         },
230
231         /**
232          * Displays a new message box, or reinitializes an existing message box, based on the config options
233          * passed in. All functions (e.g. prompt, alert, etc) on MessageBox call this function internally.
234          * The following config object properties are supported:
235          * <pre>
236 Property    Type             Description
237 ----------  ---------------  ------------------------------------------------------------------------------------
238 animEl            String/Element   An id or Element from which the message box should animate as it opens and
239                                    closes (defaults to undefined)
240 buttons           Object/Boolean   A button config object (e.g., Roo.MessageBox.OKCANCEL or {ok:'Foo',
241                                    cancel:'Bar'}), or false to not show any buttons (defaults to false)
242 closable          Boolean          False to hide the top-right close button (defaults to true).  Note that
243                                    progress and wait dialogs will ignore this property and always hide the
244                                    close button as they can only be closed programmatically.
245 cls               String           A custom CSS class to apply to the message box element
246 defaultTextHeight Number           The default height in pixels of the message box's multiline textarea if
247                                    displayed (defaults to 75)
248 fn                Function         A callback function to execute after closing the dialog.  The arguments to the
249                                    function will be btn (the name of the button that was clicked, if applicable,
250                                    e.g. "ok"), and text (the value of the active text field, if applicable).
251                                    Progress and wait dialogs will ignore this option since they do not respond to
252                                    user actions and can only be closed programmatically, so any required function
253                                    should be called by the same code after it closes the dialog.
254 icon              String           A CSS class that provides a background image to be used as an icon for
255                                    the dialog (e.g., Roo.MessageBox.WARNING or 'custom-class', defaults to '')
256 maxWidth          Number           The maximum width in pixels of the message box (defaults to 600)
257 minWidth          Number           The minimum width in pixels of the message box (defaults to 100)
258 modal             Boolean          False to allow user interaction with the page while the message box is
259                                    displayed (defaults to true)
260 msg               String           A string that will replace the existing message box body text (defaults
261                                    to the XHTML-compliant non-breaking space character '&#160;')
262 multiline         Boolean          True to prompt the user to enter multi-line text (defaults to false)
263 progress          Boolean          True to display a progress bar (defaults to false)
264 progressText      String           The text to display inside the progress bar if progress = true (defaults to '')
265 prompt            Boolean          True to prompt the user to enter single-line text (defaults to false)
266 proxyDrag         Boolean          True to display a lightweight proxy while dragging (defaults to false)
267 title             String           The title text
268 value             String           The string value to set into the active textbox element if displayed
269 wait              Boolean          True to display a progress bar (defaults to false)
270 width             Number           The width of the dialog in pixels
271 </pre>
272          *
273          * Example usage:
274          * <pre><code>
275 Roo.Msg.show({
276    title: 'Address',
277    msg: 'Please enter your address:',
278    width: 300,
279    buttons: Roo.MessageBox.OKCANCEL,
280    multiline: true,
281    fn: saveAddress,
282    animEl: 'addAddressBtn'
283 });
284 </code></pre>
285          * @param {Object} config Configuration options
286          * @return {Roo.MessageBox} This message box
287          */
288         show : function(options)
289         {
290             
291             // this causes nightmares if you show one dialog after another
292             // especially on callbacks..
293              
294             if(this.isVisible()){
295                 
296                 this.hide();
297                 Roo.log("Old Dialog Message:" +  msgEl.innerHTML )
298                 //this.alert("ERROR", "Multiple dialogs where displayed at the same time");
299                 //throw "Roo.MessageBox ERROR : Multiple dialogs where displayed at the same time";
300                 
301             }
302             var d = this.getDialog();
303             opt = options;
304             d.setTitle(opt.title || "&#160;");
305             d.close.setDisplayed(opt.closable !== false);
306             activeTextEl = textboxEl;
307             opt.prompt = opt.prompt || (opt.multiline ? true : false);
308             if(opt.prompt){
309                 if(opt.multiline){
310                     textboxEl.hide();
311                     textareaEl.show();
312                     textareaEl.setHeight(typeof opt.multiline == "number" ?
313                         opt.multiline : this.defaultTextHeight);
314                     activeTextEl = textareaEl;
315                 }else{
316                     textboxEl.show();
317                     textareaEl.hide();
318                 }
319             }else{
320                 textboxEl.hide();
321                 textareaEl.hide();
322             }
323             progressEl.setDisplayed(opt.progress === true);
324             this.updateProgress(0);
325             activeTextEl.dom.value = opt.value || "";
326             if(opt.prompt){
327                 dlg.setDefaultButton(activeTextEl);
328             }else{
329                 var bs = opt.buttons;
330                 var db = null;
331                 if(bs && bs.ok){
332                     db = buttons["ok"];
333                 }else if(bs && bs.yes){
334                     db = buttons["yes"];
335                 }
336                 dlg.setDefaultButton(db);
337             }
338             bwidth = updateButtons(opt.buttons);
339             this.updateText(opt.msg);
340             if(opt.cls){
341                 d.el.addClass(opt.cls);
342             }
343             d.proxyDrag = opt.proxyDrag === true;
344             d.modal = opt.modal !== false;
345             d.mask = opt.modal !== false ? mask : false;
346             if(!d.isVisible()){
347                 // force it to the end of the z-index stack so it gets a cursor in FF
348                 document.body.appendChild(dlg.el.dom);
349                 d.animateTarget = null;
350                 d.show(options.animEl);
351             }
352             return this;
353         },
354
355         /**
356          * Displays a message box with a progress bar.  This message box has no buttons and is not closeable by
357          * the user.  You are responsible for updating the progress bar as needed via {@link Roo.MessageBox#updateProgress}
358          * and closing the message box when the process is complete.
359          * @param {String} title The title bar text
360          * @param {String} msg The message box body text
361          * @return {Roo.MessageBox} This message box
362          */
363         progress : function(title, msg){
364             this.show({
365                 title : title,
366                 msg : msg,
367                 buttons: false,
368                 progress:true,
369                 closable:false,
370                 minWidth: this.minProgressWidth,
371                 modal : true
372             });
373             return this;
374         },
375
376         /**
377          * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript Window.alert).
378          * If a callback function is passed it will be called after the user clicks the button, and the
379          * id of the button that was clicked will be passed as the only parameter to the callback
380          * (could also be the top-right close button).
381          * @param {String} title The title bar text
382          * @param {String} msg The message box body text
383          * @param {Function} fn (optional) The callback function invoked after the message box is closed
384          * @param {Object} scope (optional) The scope of the callback function
385          * @return {Roo.MessageBox} This message box
386          */
387         alert : function(title, msg, fn, scope){
388             this.show({
389                 title : title,
390                 msg : msg,
391                 buttons: this.OK,
392                 fn: fn,
393                 scope : scope,
394                 modal : true
395             });
396             return this;
397         },
398
399         /**
400          * Displays a message box with an infinitely auto-updating progress bar.  This can be used to block user
401          * interaction while waiting for a long-running process to complete that does not have defined intervals.
402          * You are responsible for closing the message box when the process is complete.
403          * @param {String} msg The message box body text
404          * @param {String} title (optional) The title bar text
405          * @return {Roo.MessageBox} This message box
406          */
407         wait : function(msg, title){
408             this.show({
409                 title : title,
410                 msg : msg,
411                 buttons: false,
412                 closable:false,
413                 progress:true,
414                 modal:true,
415                 width:300,
416                 wait:true
417             });
418             waitTimer = Roo.TaskMgr.start({
419                 run: function(i){
420                     Roo.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
421                 },
422                 interval: 1000
423             });
424             return this;
425         },
426
427         /**
428          * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's Window.confirm).
429          * If a callback function is passed it will be called after the user clicks either button, and the id of the
430          * button that was clicked will be passed as the only parameter to the callback (could also be the top-right close button).
431          * @param {String} title The title bar text
432          * @param {String} msg The message box body text
433          * @param {Function} fn (optional) The callback function invoked after the message box is closed
434          * @param {Object} scope (optional) The scope of the callback function
435          * @return {Roo.MessageBox} This message box
436          */
437         confirm : function(title, msg, fn, scope){
438             this.show({
439                 title : title,
440                 msg : msg,
441                 buttons: this.YESNO,
442                 fn: fn,
443                 scope : scope,
444                 modal : true
445             });
446             return this;
447         },
448
449         /**
450          * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to
451          * JavaScript's Window.prompt).  The prompt can be a single-line or multi-line textbox.  If a callback function
452          * is passed it will be called after the user clicks either button, and the id of the button that was clicked
453          * (could also be the top-right close button) and the text that was entered will be passed as the two
454          * parameters to the callback.
455          * @param {String} title The title bar text
456          * @param {String} msg The message box body text
457          * @param {Function} fn (optional) The callback function invoked after the message box is closed
458          * @param {Object} scope (optional) The scope of the callback function
459          * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
460          * property, or the height in pixels to create the textbox (defaults to false / single-line)
461          * @return {Roo.MessageBox} This message box
462          */
463         prompt : function(title, msg, fn, scope, multiline){
464             this.show({
465                 title : title,
466                 msg : msg,
467                 buttons: this.OKCANCEL,
468                 fn: fn,
469                 minWidth:250,
470                 scope : scope,
471                 prompt:true,
472                 multiline: multiline,
473                 modal : true
474             });
475             return this;
476         },
477
478         /**
479          * Button config that displays a single OK button
480          * @type Object
481          */
482         OK : {ok:true},
483         /**
484          * Button config that displays Yes and No buttons
485          * @type Object
486          */
487         YESNO : {yes:true, no:true},
488         /**
489          * Button config that displays OK and Cancel buttons
490          * @type Object
491          */
492         OKCANCEL : {ok:true, cancel:true},
493         /**
494          * Button config that displays Yes, No and Cancel buttons
495          * @type Object
496          */
497         YESNOCANCEL : {yes:true, no:true, cancel:true},
498
499         /**
500          * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
501          * @type Number
502          */
503         defaultTextHeight : 75,
504         /**
505          * The maximum width in pixels of the message box (defaults to 600)
506          * @type Number
507          */
508         maxWidth : 600,
509         /**
510          * The minimum width in pixels of the message box (defaults to 100)
511          * @type Number
512          */
513         minWidth : 100,
514         /**
515          * The minimum width in pixels of the message box if it is a progress-style dialog.  This is useful
516          * for setting a different minimum width than text-only dialogs may need (defaults to 250)
517          * @type Number
518          */
519         minProgressWidth : 250,
520         /**
521          * An object containing the default button text strings that can be overriden for localized language support.
522          * Supported properties are: ok, cancel, yes and no.
523          * Customize the default text like so: Roo.MessageBox.buttonText.yes = "S?";
524          * @type Object
525          */
526         buttonText : {
527             ok : "OK",
528             cancel : "Cancel",
529             yes : "Yes",
530             no : "No"
531         }
532     };
533 }();
534
535 /**
536  * Shorthand for {@link Roo.MessageBox}
537  */
538 Roo.Msg = Roo.MessageBox;