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