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