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