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             dlg.setContentSize(w, bodyEl.getHeight());
182             if(dlg.isVisible()){
183                 dlg.fixedcenter = true;
184             }
185             return this;
186         },
187
188         /**
189          * Updates a progress-style message box's text and progress bar.  Only relevant on message boxes
190          * initiated via {@link Roo.MessageBox#progress} or by calling {@link Roo.MessageBox#show} with progress: true.
191          * @param {Number} value Any number between 0 and 1 (e.g., .5)
192          * @param {String} text (optional) If defined, the message box's body text is replaced with the specified string (defaults to undefined)
193          * @return {Roo.MessageBox} This message box
194          */
195         updateProgress : function(value, text){
196             if(text){
197                 this.updateText(text);
198             }
199             if (pp) { // weird bug on my firefox - for some reason this is not defined
200                 pp.setWidth(Math.floor(value*progressEl.dom.firstChild.offsetWidth));
201             }
202             return this;
203         },        
204
205         /**
206          * Returns true if the message box is currently displayed
207          * @return {Boolean} True if the message box is visible, else false
208          */
209         isVisible : function(){
210             return dlg && dlg.isVisible();  
211         },
212
213         /**
214          * Hides the message box if it is displayed
215          */
216         hide : function(){
217             if(this.isVisible()){
218                 dlg.hide();
219             }  
220         },
221
222         /**
223          * Displays a new message box, or reinitializes an existing message box, based on the config options
224          * passed in. All functions (e.g. prompt, alert, etc) on MessageBox call this function internally.
225          * The following config object properties are supported:
226          * <pre>
227 Property    Type             Description
228 ----------  ---------------  ------------------------------------------------------------------------------------
229 animEl            String/Element   An id or Element from which the message box should animate as it opens and
230                                    closes (defaults to undefined)
231 buttons           Object/Boolean   A button config object (e.g., Roo.MessageBox.OKCANCEL or {ok:'Foo',
232                                    cancel:'Bar'}), or false to not show any buttons (defaults to false)
233 closable          Boolean          False to hide the top-right close button (defaults to true).  Note that
234                                    progress and wait dialogs will ignore this property and always hide the
235                                    close button as they can only be closed programmatically.
236 cls               String           A custom CSS class to apply to the message box element
237 defaultTextHeight Number           The default height in pixels of the message box's multiline textarea if
238                                    displayed (defaults to 75)
239 fn                Function         A callback function to execute after closing the dialog.  The arguments to the
240                                    function will be btn (the name of the button that was clicked, if applicable,
241                                    e.g. "ok"), and text (the value of the active text field, if applicable).
242                                    Progress and wait dialogs will ignore this option since they do not respond to
243                                    user actions and can only be closed programmatically, so any required function
244                                    should be called by the same code after it closes the dialog.
245 icon              String           A CSS class that provides a background image to be used as an icon for
246                                    the dialog (e.g., Roo.MessageBox.WARNING or 'custom-class', defaults to '')
247 maxWidth          Number           The maximum width in pixels of the message box (defaults to 600)
248 minWidth          Number           The minimum width in pixels of the message box (defaults to 100)
249 modal             Boolean          False to allow user interaction with the page while the message box is
250                                    displayed (defaults to true)
251 msg               String           A string that will replace the existing message box body text (defaults
252                                    to the XHTML-compliant non-breaking space character '&#160;')
253 multiline         Boolean          True to prompt the user to enter multi-line text (defaults to false)
254 progress          Boolean          True to display a progress bar (defaults to false)
255 progressText      String           The text to display inside the progress bar if progress = true (defaults to '')
256 prompt            Boolean          True to prompt the user to enter single-line text (defaults to false)
257 proxyDrag         Boolean          True to display a lightweight proxy while dragging (defaults to false)
258 title             String           The title text
259 value             String           The string value to set into the active textbox element if displayed
260 wait              Boolean          True to display a progress bar (defaults to false)
261 width             Number           The width of the dialog in pixels
262 </pre>
263          *
264          * Example usage:
265          * <pre><code>
266 Roo.Msg.show({
267    title: 'Address',
268    msg: 'Please enter your address:',
269    width: 300,
270    buttons: Roo.MessageBox.OKCANCEL,
271    multiline: true,
272    fn: saveAddress,
273    animEl: 'addAddressBtn'
274 });
275 </code></pre>
276          * @param {Object} config Configuration options
277          * @return {Roo.MessageBox} This message box
278          */
279         show : function(options)
280         {
281             
282             // this causes nightmares if you show one dialog after another
283             // especially on callbacks..
284              
285             if(this.isVisible()){
286                 
287                 this.hide();
288                 //this.alert("ERROR", "Multiple dialogs where displayed at the same time");
289                 //throw "Roo.MessageBox ERROR : Multiple dialogs where displayed at the same time";
290                 
291             }
292             var d = this.getDialog();
293             opt = options;
294             d.setTitle(opt.title || "&#160;");
295             d.close.setDisplayed(opt.closable !== false);
296             activeTextEl = textboxEl;
297             opt.prompt = opt.prompt || (opt.multiline ? true : false);
298             if(opt.prompt){
299                 if(opt.multiline){
300                     textboxEl.hide();
301                     textareaEl.show();
302                     textareaEl.setHeight(typeof opt.multiline == "number" ?
303                         opt.multiline : this.defaultTextHeight);
304                     activeTextEl = textareaEl;
305                 }else{
306                     textboxEl.show();
307                     textareaEl.hide();
308                 }
309             }else{
310                 textboxEl.hide();
311                 textareaEl.hide();
312             }
313             progressEl.setDisplayed(opt.progress === true);
314             this.updateProgress(0);
315             activeTextEl.dom.value = opt.value || "";
316             if(opt.prompt){
317                 dlg.setDefaultButton(activeTextEl);
318             }else{
319                 var bs = opt.buttons;
320                 var db = null;
321                 if(bs && bs.ok){
322                     db = buttons["ok"];
323                 }else if(bs && bs.yes){
324                     db = buttons["yes"];
325                 }
326                 dlg.setDefaultButton(db);
327             }
328             bwidth = updateButtons(opt.buttons);
329             this.updateText(opt.msg);
330             if(opt.cls){
331                 d.el.addClass(opt.cls);
332             }
333             d.proxyDrag = opt.proxyDrag === true;
334             d.modal = opt.modal !== false;
335             d.mask = opt.modal !== false ? mask : false;
336             if(!d.isVisible()){
337                 // force it to the end of the z-index stack so it gets a cursor in FF
338                 document.body.appendChild(dlg.el.dom);
339                 d.animateTarget = null;
340                 d.show(options.animEl);
341             }
342             return this;
343         },
344
345         /**
346          * Displays a message box with a progress bar.  This message box has no buttons and is not closeable by
347          * the user.  You are responsible for updating the progress bar as needed via {@link Roo.MessageBox#updateProgress}
348          * and closing the message box when the process is complete.
349          * @param {String} title The title bar text
350          * @param {String} msg The message box body text
351          * @return {Roo.MessageBox} This message box
352          */
353         progress : function(title, msg){
354             this.show({
355                 title : title,
356                 msg : msg,
357                 buttons: false,
358                 progress:true,
359                 closable:false,
360                 minWidth: this.minProgressWidth,
361                 modal : true
362             });
363             return this;
364         },
365
366         /**
367          * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript Window.alert).
368          * If a callback function is passed it will be called after the user clicks the button, and the
369          * id of the button that was clicked will be passed as the only parameter to the callback
370          * (could also be the top-right close button).
371          * @param {String} title The title bar text
372          * @param {String} msg The message box body text
373          * @param {Function} fn (optional) The callback function invoked after the message box is closed
374          * @param {Object} scope (optional) The scope of the callback function
375          * @return {Roo.MessageBox} This message box
376          */
377         alert : function(title, msg, fn, scope){
378             this.show({
379                 title : title,
380                 msg : msg,
381                 buttons: this.OK,
382                 fn: fn,
383                 scope : scope,
384                 modal : true
385             });
386             return this;
387         },
388
389         /**
390          * Displays a message box with an infinitely auto-updating progress bar.  This can be used to block user
391          * interaction while waiting for a long-running process to complete that does not have defined intervals.
392          * You are responsible for closing the message box when the process is complete.
393          * @param {String} msg The message box body text
394          * @param {String} title (optional) The title bar text
395          * @return {Roo.MessageBox} This message box
396          */
397         wait : function(msg, title){
398             this.show({
399                 title : title,
400                 msg : msg,
401                 buttons: false,
402                 closable:false,
403                 progress:true,
404                 modal:true,
405                 width:300,
406                 wait:true
407             });
408             waitTimer = Roo.TaskMgr.start({
409                 run: function(i){
410                     Roo.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
411                 },
412                 interval: 1000
413             });
414             return this;
415         },
416
417         /**
418          * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's Window.confirm).
419          * If a callback function is passed it will be called after the user clicks either button, and the id of the
420          * button that was clicked will be passed as the only parameter to the callback (could also be the top-right close button).
421          * @param {String} title The title bar text
422          * @param {String} msg The message box body text
423          * @param {Function} fn (optional) The callback function invoked after the message box is closed
424          * @param {Object} scope (optional) The scope of the callback function
425          * @return {Roo.MessageBox} This message box
426          */
427         confirm : function(title, msg, fn, scope){
428             this.show({
429                 title : title,
430                 msg : msg,
431                 buttons: this.YESNO,
432                 fn: fn,
433                 scope : scope,
434                 modal : true
435             });
436             return this;
437         },
438
439         /**
440          * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to
441          * JavaScript's Window.prompt).  The prompt can be a single-line or multi-line textbox.  If a callback function
442          * is passed it will be called after the user clicks either button, and the id of the button that was clicked
443          * (could also be the top-right close button) and the text that was entered will be passed as the two
444          * parameters to the callback.
445          * @param {String} title The title bar text
446          * @param {String} msg The message box body text
447          * @param {Function} fn (optional) The callback function invoked after the message box is closed
448          * @param {Object} scope (optional) The scope of the callback function
449          * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
450          * property, or the height in pixels to create the textbox (defaults to false / single-line)
451          * @return {Roo.MessageBox} This message box
452          */
453         prompt : function(title, msg, fn, scope, multiline){
454             this.show({
455                 title : title,
456                 msg : msg,
457                 buttons: this.OKCANCEL,
458                 fn: fn,
459                 minWidth:250,
460                 scope : scope,
461                 prompt:true,
462                 multiline: multiline,
463                 modal : true
464             });
465             return this;
466         },
467
468         /**
469          * Button config that displays a single OK button
470          * @type Object
471          */
472         OK : {ok:true},
473         /**
474          * Button config that displays Yes and No buttons
475          * @type Object
476          */
477         YESNO : {yes:true, no:true},
478         /**
479          * Button config that displays OK and Cancel buttons
480          * @type Object
481          */
482         OKCANCEL : {ok:true, cancel:true},
483         /**
484          * Button config that displays Yes, No and Cancel buttons
485          * @type Object
486          */
487         YESNOCANCEL : {yes:true, no:true, cancel:true},
488
489         /**
490          * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
491          * @type Number
492          */
493         defaultTextHeight : 75,
494         /**
495          * The maximum width in pixels of the message box (defaults to 600)
496          * @type Number
497          */
498         maxWidth : 600,
499         /**
500          * The minimum width in pixels of the message box (defaults to 100)
501          * @type Number
502          */
503         minWidth : 100,
504         /**
505          * The minimum width in pixels of the message box if it is a progress-style dialog.  This is useful
506          * for setting a different minimum width than text-only dialogs may need (defaults to 250)
507          * @type Number
508          */
509         minProgressWidth : 250,
510         /**
511          * An object containing the default button text strings that can be overriden for localized language support.
512          * Supported properties are: ok, cancel, yes and no.
513          * Customize the default text like so: Roo.MessageBox.buttonText.yes = "S?";
514          * @type Object
515          */
516         buttonText : {
517             ok : "OK",
518             cancel : "Cancel",
519             yes : "Yes",
520             no : "No"
521         }
522     };
523 }();
524
525 /**
526  * Shorthand for {@link Roo.MessageBox}
527  */
528 Roo.Msg = Roo.MessageBox;