Roo/bootstrap/MessageBox.js
[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  * @singleton
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             this.updateProgress(0);
346             activeTextEl.dom.value = opt.value || "";
347             if(opt.prompt){
348                 dlg.setDefaultButton(activeTextEl);
349             }else{
350                 var bs = opt.buttons;
351                 var db = null;
352                 if(bs && bs.ok){
353                     db = buttons["ok"];
354                 }else if(bs && bs.yes){
355                     db = buttons["yes"];
356                 }
357                 dlg.setDefaultButton(db);
358             }
359             bwidth = updateButtons(opt.buttons);
360             this.updateText(opt.msg);
361             if(opt.cls){
362                 d.el.addClass(opt.cls);
363             }
364             d.proxyDrag = opt.proxyDrag === true;
365             d.modal = opt.modal !== false;
366             d.mask = opt.modal !== false ? mask : false;
367             if(!d.isVisible()){
368                 // force it to the end of the z-index stack so it gets a cursor in FF
369                 document.body.appendChild(dlg.el.dom);
370                 d.animateTarget = null;
371                 d.show(options.animEl);
372             }
373             return this;
374         },
375
376         /**
377          * Displays a message box with a progress bar.  This message box has no buttons and is not closeable by
378          * the user.  You are responsible for updating the progress bar as needed via {@link Roo.MessageBox#updateProgress}
379          * and closing the message box when the process is complete.
380          * @param {String} title The title bar text
381          * @param {String} msg The message box body text
382          * @return {Roo.MessageBox} This message box
383          */
384         progress : function(title, msg){
385             this.show({
386                 title : title,
387                 msg : msg,
388                 buttons: false,
389                 progress:true,
390                 closable:false,
391                 minWidth: this.minProgressWidth,
392                 modal : true
393             });
394             return this;
395         },
396
397         /**
398          * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript Window.alert).
399          * If a callback function is passed it will be called after the user clicks the button, and the
400          * id of the button that was clicked will be passed as the only parameter to the callback
401          * (could also be the top-right close button).
402          * @param {String} title The title bar text
403          * @param {String} msg The message box body text
404          * @param {Function} fn (optional) The callback function invoked after the message box is closed
405          * @param {Object} scope (optional) The scope of the callback function
406          * @return {Roo.MessageBox} This message box
407          */
408         alert : function(title, msg, fn, scope)
409         {
410             this.show({
411                 title : title,
412                 msg : msg,
413                 buttons: this.OK,
414                 fn: fn,
415                 closable : false,
416                 scope : scope,
417                 modal : true
418             });
419             return this;
420         },
421
422         /**
423          * Displays a message box with an infinitely auto-updating progress bar.  This can be used to block user
424          * interaction while waiting for a long-running process to complete that does not have defined intervals.
425          * You are responsible for closing the message box when the process is complete.
426          * @param {String} msg The message box body text
427          * @param {String} title (optional) The title bar text
428          * @return {Roo.MessageBox} This message box
429          */
430         wait : function(msg, title){
431             this.show({
432                 title : title,
433                 msg : msg,
434                 buttons: false,
435                 closable:false,
436                 progress:true,
437                 modal:true,
438                 width:300,
439                 wait:true
440             });
441             waitTimer = Roo.TaskMgr.start({
442                 run: function(i){
443                     Roo.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
444                 },
445                 interval: 1000
446             });
447             return this;
448         },
449
450         /**
451          * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's Window.confirm).
452          * If a callback function is passed it will be called after the user clicks either button, and the id of the
453          * button that was clicked will be passed as the only parameter to the callback (could also be the top-right close button).
454          * @param {String} title The title bar text
455          * @param {String} msg The message box body text
456          * @param {Function} fn (optional) The callback function invoked after the message box is closed
457          * @param {Object} scope (optional) The scope of the callback function
458          * @return {Roo.MessageBox} This message box
459          */
460         confirm : function(title, msg, fn, scope){
461             this.show({
462                 title : title,
463                 msg : msg,
464                 buttons: this.YESNO,
465                 fn: fn,
466                 scope : scope,
467                 modal : true
468             });
469             return this;
470         },
471
472         /**
473          * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to
474          * JavaScript's Window.prompt).  The prompt can be a single-line or multi-line textbox.  If a callback function
475          * is passed it will be called after the user clicks either button, and the id of the button that was clicked
476          * (could also be the top-right close button) and the text that was entered will be passed as the two
477          * parameters to the callback.
478          * @param {String} title The title bar text
479          * @param {String} msg The message box body text
480          * @param {Function} fn (optional) The callback function invoked after the message box is closed
481          * @param {Object} scope (optional) The scope of the callback function
482          * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
483          * property, or the height in pixels to create the textbox (defaults to false / single-line)
484          * @return {Roo.MessageBox} This message box
485          */
486         prompt : function(title, msg, fn, scope, multiline){
487             this.show({
488                 title : title,
489                 msg : msg,
490                 buttons: this.OKCANCEL,
491                 fn: fn,
492                 minWidth:250,
493                 scope : scope,
494                 prompt:true,
495                 multiline: multiline,
496                 modal : true
497             });
498             return this;
499         },
500
501         /**
502          * Button config that displays a single OK button
503          * @type Object
504          */
505         OK : {ok:true},
506         /**
507          * Button config that displays Yes and No buttons
508          * @type Object
509          */
510         YESNO : {yes:true, no:true},
511         /**
512          * Button config that displays OK and Cancel buttons
513          * @type Object
514          */
515         OKCANCEL : {ok:true, cancel:true},
516         /**
517          * Button config that displays Yes, No and Cancel buttons
518          * @type Object
519          */
520         YESNOCANCEL : {yes:true, no:true, cancel:true},
521
522         /**
523          * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
524          * @type Number
525          */
526         defaultTextHeight : 75,
527         /**
528          * The maximum width in pixels of the message box (defaults to 600)
529          * @type Number
530          */
531         maxWidth : 600,
532         /**
533          * The minimum width in pixels of the message box (defaults to 100)
534          * @type Number
535          */
536         minWidth : 100,
537         /**
538          * The minimum width in pixels of the message box if it is a progress-style dialog.  This is useful
539          * for setting a different minimum width than text-only dialogs may need (defaults to 250)
540          * @type Number
541          */
542         minProgressWidth : 250,
543         /**
544          * An object containing the default button text strings that can be overriden for localized language support.
545          * Supported properties are: ok, cancel, yes and no.
546          * Customize the default text like so: Roo.MessageBox.buttonText.yes = "S?";
547          * @type Object
548          */
549         buttonText : {
550             ok : "OK",
551             cancel : "Cancel",
552             yes : "Yes",
553             no : "No"
554         }
555     };
556 }();
557
558 /**
559  * Shorthand for {@link Roo.MessageBox}
560  */
561 Roo.MessageBox = Roo.MessageBox || Roo.bootstrap.MessageBox;
562 Roo.Msg = Roo.Msg || Roo.MessageBox;