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