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