Roo/bootstrap/MessageBox.js
[roojs1] / Roo / bootstrap / MessageBox.js
1 /*
2  * - LGPL
3  *
4  * messagebox - can be used as a replace
5  * 
6  */
7 /**
8  * @class Roo.MessageBox
9  * Utility class for generating different styles of message boxes.  The alias Roo.Msg can also be used.
10  * Example usage:
11  *<pre><code>
12 // Basic alert:
13 Roo.Msg.alert('Status', 'Changes saved successfully.');
14
15 // Prompt for user data:
16 Roo.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
17     if (btn == 'ok'){
18         // process text value...
19     }
20 });
21
22 // Show a dialog using config options:
23 Roo.Msg.show({
24    title:'Save Changes?',
25    msg: 'Your are closing a tab that has unsaved changes. Would you like to save your changes?',
26    buttons: Roo.Msg.YESNOCANCEL,
27    fn: processResult,
28    animEl: 'elId'
29 });
30 </code></pre>
31  * @singleton
32  */
33 Roo.bootstrap.MessageBox = function(){
34     var dlg, opt, mask, waitTimer;
35     var bodyEl, msgEl, textboxEl, textareaEl, progressEl, pp;
36     var buttons, activeTextEl, bwidth;
37
38     // private
39     var handleButton = function(button){
40         dlg.hide();
41         Roo.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value], 1);
42     };
43
44     // private
45     var handleHide = function(){
46         if(opt && opt.cls){
47             dlg.el.removeClass(opt.cls);
48         }
49         //if(waitTimer){
50         //    Roo.TaskMgr.stop(waitTimer);
51         //    waitTimer = null;
52         //}
53     };
54
55     // private
56     var updateButtons = function(b){
57         var width = 0;
58         if(!b){
59             buttons["ok"].hide();
60             buttons["cancel"].hide();
61             buttons["yes"].hide();
62             buttons["no"].hide();
63             //dlg.footer.dom.style.display = 'none';
64             return width;
65         }
66         dlg.footer.dom.style.display = '';
67         for(var k in buttons){
68             if(typeof buttons[k] != "function"){
69                 if(b[k]){
70                     buttons[k].show();
71                     buttons[k].setText(typeof b[k] == "string" ? b[k] : Roo.bootstrap.MessageBox.buttonText[k]);
72                     width += buttons[k].el.getWidth()+15;
73                 }else{
74                     buttons[k].hide();
75                 }
76             }
77         }
78         return width;
79     };
80
81     // private
82     var handleEsc = function(d, k, e){
83         if(opt && opt.closable !== false){
84             dlg.hide();
85         }
86         if(e){
87             e.stopEvent();
88         }
89     };
90
91     return {
92         /**
93          * Returns a reference to the underlying {@link Roo.BasicDialog} element
94          * @return {Roo.BasicDialog} The BasicDialog element
95          */
96         getDialog : function(){
97            if(!dlg){
98                 dlg = new Roo.bootstrap.Modal( {
99                     //draggable: true,
100                     //resizable:false,
101                     //constraintoviewport:false,
102                     //fixedcenter:true,
103                     //collapsible : false,
104                     //shim:true,
105                     //modal: true,
106                     width:400,
107                     height:100,
108                     //buttonAlign:"center",
109                     closeClick : function(){
110                         if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
111                             handleButton("no");
112                         }else{
113                             handleButton("cancel");
114                         }
115                     }
116                 });
117                 dlg.render();
118                 dlg.on("hide", handleHide);
119                 mask = dlg.mask;
120                 //dlg.addKeyListener(27, handleEsc);
121                 buttons = {};
122                 var bt = this.buttonText;
123                 buttons["ok"] = dlg.addButton(bt["ok"], handleButton.createCallback("ok"));
124                 buttons["yes"] = dlg.addButton(bt["yes"], handleButton.createCallback("yes"));
125                 buttons["no"] = dlg.addButton(bt["no"], handleButton.createCallback("no"));
126                 buttons["cancel"] = dlg.addButton(bt["cancel"], handleButton.createCallback("cancel"));
127                 bodyEl = dlg.body.createChild({
128
129                     html:'<span class="roo-mb-text"></span><br /><input type="text" class="roo-mb-input" />' +
130                         '<textarea class="roo-mb-textarea"></textarea>' +
131                         '<div class="roo-mb-progress-wrap"><div class="roo-mb-progress"><div class="roo-mb-progress-bar">&#160;</div></div></div>'
132                 });
133                 msgEl = bodyEl.dom.firstChild;
134                 textboxEl = Roo.get(bodyEl.dom.childNodes[2]);
135                 textboxEl.enableDisplayMode();
136                 textboxEl.addKeyListener([10,13], function(){
137                     if(dlg.isVisible() && opt && opt.buttons){
138                         if(opt.buttons.ok){
139                             handleButton("ok");
140                         }else if(opt.buttons.yes){
141                             handleButton("yes");
142                         }
143                     }
144                 });
145                 textareaEl = Roo.get(bodyEl.dom.childNodes[3]);
146                 textareaEl.enableDisplayMode();
147                 progressEl = Roo.get(bodyEl.dom.childNodes[4]);
148                 progressEl.enableDisplayMode();
149                 var pf = progressEl.dom.firstChild;
150                 if (pf) {
151                     pp = Roo.get(pf.firstChild);
152                     pp.setHeight(pf.offsetHeight);
153                 }
154                 
155             }
156             return dlg;
157         },
158
159         /**
160          * Updates the message box body text
161          * @param {String} text (optional) Replaces the message box element's innerHTML with the specified string (defaults to
162          * the XHTML-compliant non-breaking space character '&amp;#160;')
163          * @return {Roo.MessageBox} This message box
164          */
165         updateText : function(text){
166             if(!dlg.isVisible() && !opt.width){
167                 dlg.resizeTo(this.maxWidth, 100); // resize first so content is never clipped from previous shows
168             }
169             msgEl.innerHTML = text || '&#160;';
170       
171             var cw =  Math.max(msgEl.offsetWidth, msgEl.parentNode.scrollWidth);
172             //Roo.log("guesed size: " + JSON.stringify([cw,msgEl.offsetWidth, msgEl.parentNode.scrollWidth]));
173             var w = Math.max(
174                     Math.min(opt.width || cw , this.maxWidth), 
175                     Math.max(opt.minWidth || this.minWidth, bwidth)
176             );
177             if(opt.prompt){
178                 activeTextEl.setWidth(w);
179             }
180             if(dlg.isVisible()){
181                 dlg.fixedcenter = false;
182             }
183             // to big, make it scroll. = But as usual stupid IE does not support
184             // !important..
185             
186             if ( bodyEl.getHeight() > (Roo.lib.Dom.getViewHeight() - 100)) {
187                 bodyEl.setHeight ( Roo.lib.Dom.getViewHeight() - 100 );
188                 bodyEl.dom.style.overflowY = 'auto' + ( Roo.isIE ? '' : ' !important');
189             } else {
190                 bodyEl.dom.style.height = '';
191                 bodyEl.dom.style.overflowY = '';
192             }
193             if (cw > w) {
194                 bodyEl.dom.style.get = 'auto' + ( Roo.isIE ? '' : ' !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("[Roo.Messagebox] Show called while message displayed:" );
307                 Roo.log("Old Dialog Message:" +  msgEl.innerHTML );
308                 Roo.log("New Dialog Message:" +  options.msg )
309                 //this.alert("ERROR", "Multiple dialogs where displayed at the same time");
310                 //throw "Roo.MessageBox ERROR : Multiple dialogs where displayed at the same time";
311                 
312             }
313             var d = this.getDialog();
314             opt = options;
315             d.setTitle(opt.title || "&#160;");
316             d.close.setDisplayed(opt.closable !== false);
317             activeTextEl = textboxEl;
318             opt.prompt = opt.prompt || (opt.multiline ? true : false);
319             if(opt.prompt){
320                 if(opt.multiline){
321                     textboxEl.hide();
322                     textareaEl.show();
323                     textareaEl.setHeight(typeof opt.multiline == "number" ?
324                         opt.multiline : this.defaultTextHeight);
325                     activeTextEl = textareaEl;
326                 }else{
327                     textboxEl.show();
328                     textareaEl.hide();
329                 }
330             }else{
331                 textboxEl.hide();
332                 textareaEl.hide();
333             }
334             progressEl.setDisplayed(opt.progress === true);
335             this.updateProgress(0);
336             activeTextEl.dom.value = opt.value || "";
337             if(opt.prompt){
338                 dlg.setDefaultButton(activeTextEl);
339             }else{
340                 var bs = opt.buttons;
341                 var db = null;
342                 if(bs && bs.ok){
343                     db = buttons["ok"];
344                 }else if(bs && bs.yes){
345                     db = buttons["yes"];
346                 }
347                 dlg.setDefaultButton(db);
348             }
349             bwidth = updateButtons(opt.buttons);
350             this.updateText(opt.msg);
351             if(opt.cls){
352                 d.el.addClass(opt.cls);
353             }
354             d.proxyDrag = opt.proxyDrag === true;
355             d.modal = opt.modal !== false;
356             d.mask = opt.modal !== false ? mask : false;
357             if(!d.isVisible()){
358                 // force it to the end of the z-index stack so it gets a cursor in FF
359                 document.body.appendChild(dlg.el.dom);
360                 d.animateTarget = null;
361                 d.show(options.animEl);
362             }
363             return this;
364         },
365
366         /**
367          * Displays a message box with a progress bar.  This message box has no buttons and is not closeable by
368          * the user.  You are responsible for updating the progress bar as needed via {@link Roo.MessageBox#updateProgress}
369          * and closing the message box when the process is complete.
370          * @param {String} title The title bar text
371          * @param {String} msg The message box body text
372          * @return {Roo.MessageBox} This message box
373          */
374         progress : function(title, msg){
375             this.show({
376                 title : title,
377                 msg : msg,
378                 buttons: false,
379                 progress:true,
380                 closable:false,
381                 minWidth: this.minProgressWidth,
382                 modal : true
383             });
384             return this;
385         },
386
387         /**
388          * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript Window.alert).
389          * If a callback function is passed it will be called after the user clicks the button, and the
390          * id of the button that was clicked will be passed as the only parameter to the callback
391          * (could also be the top-right close button).
392          * @param {String} title The title bar text
393          * @param {String} msg The message box body text
394          * @param {Function} fn (optional) The callback function invoked after the message box is closed
395          * @param {Object} scope (optional) The scope of the callback function
396          * @return {Roo.MessageBox} This message box
397          */
398         alert : function(title, msg, fn, scope){
399             this.show({
400                 title : title,
401                 msg : msg,
402                 buttons: this.OK,
403                 fn: fn,
404                 scope : scope,
405                 modal : true
406             });
407             return this;
408         },
409
410         /**
411          * Displays a message box with an infinitely auto-updating progress bar.  This can be used to block user
412          * interaction while waiting for a long-running process to complete that does not have defined intervals.
413          * You are responsible for closing the message box when the process is complete.
414          * @param {String} msg The message box body text
415          * @param {String} title (optional) The title bar text
416          * @return {Roo.MessageBox} This message box
417          */
418         wait : function(msg, title){
419             this.show({
420                 title : title,
421                 msg : msg,
422                 buttons: false,
423                 closable:false,
424                 progress:true,
425                 modal:true,
426                 width:300,
427                 wait:true
428             });
429             waitTimer = Roo.TaskMgr.start({
430                 run: function(i){
431                     Roo.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
432                 },
433                 interval: 1000
434             });
435             return this;
436         },
437
438         /**
439          * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's Window.confirm).
440          * If a callback function is passed it will be called after the user clicks either button, and the id of the
441          * button that was clicked will be passed as the only parameter to the callback (could also be the top-right close button).
442          * @param {String} title The title bar text
443          * @param {String} msg The message box body text
444          * @param {Function} fn (optional) The callback function invoked after the message box is closed
445          * @param {Object} scope (optional) The scope of the callback function
446          * @return {Roo.MessageBox} This message box
447          */
448         confirm : function(title, msg, fn, scope){
449             this.show({
450                 title : title,
451                 msg : msg,
452                 buttons: this.YESNO,
453                 fn: fn,
454                 scope : scope,
455                 modal : true
456             });
457             return this;
458         },
459
460         /**
461          * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to
462          * JavaScript's Window.prompt).  The prompt can be a single-line or multi-line textbox.  If a callback function
463          * is passed it will be called after the user clicks either button, and the id of the button that was clicked
464          * (could also be the top-right close button) and the text that was entered will be passed as the two
465          * parameters to the callback.
466          * @param {String} title The title bar text
467          * @param {String} msg The message box body text
468          * @param {Function} fn (optional) The callback function invoked after the message box is closed
469          * @param {Object} scope (optional) The scope of the callback function
470          * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
471          * property, or the height in pixels to create the textbox (defaults to false / single-line)
472          * @return {Roo.MessageBox} This message box
473          */
474         prompt : function(title, msg, fn, scope, multiline){
475             this.show({
476                 title : title,
477                 msg : msg,
478                 buttons: this.OKCANCEL,
479                 fn: fn,
480                 minWidth:250,
481                 scope : scope,
482                 prompt:true,
483                 multiline: multiline,
484                 modal : true
485             });
486             return this;
487         },
488
489         /**
490          * Button config that displays a single OK button
491          * @type Object
492          */
493         OK : {ok:true},
494         /**
495          * Button config that displays Yes and No buttons
496          * @type Object
497          */
498         YESNO : {yes:true, no:true},
499         /**
500          * Button config that displays OK and Cancel buttons
501          * @type Object
502          */
503         OKCANCEL : {ok:true, cancel:true},
504         /**
505          * Button config that displays Yes, No and Cancel buttons
506          * @type Object
507          */
508         YESNOCANCEL : {yes:true, no:true, cancel:true},
509
510         /**
511          * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
512          * @type Number
513          */
514         defaultTextHeight : 75,
515         /**
516          * The maximum width in pixels of the message box (defaults to 600)
517          * @type Number
518          */
519         maxWidth : 600,
520         /**
521          * The minimum width in pixels of the message box (defaults to 100)
522          * @type Number
523          */
524         minWidth : 100,
525         /**
526          * The minimum width in pixels of the message box if it is a progress-style dialog.  This is useful
527          * for setting a different minimum width than text-only dialogs may need (defaults to 250)
528          * @type Number
529          */
530         minProgressWidth : 250,
531         /**
532          * An object containing the default button text strings that can be overriden for localized language support.
533          * Supported properties are: ok, cancel, yes and no.
534          * Customize the default text like so: Roo.MessageBox.buttonText.yes = "S?";
535          * @type Object
536          */
537         buttonText : {
538             ok : "OK",
539             cancel : "Cancel",
540             yes : "Yes",
541             no : "No"
542         }
543     };
544 }();
545
546 /**
547  * Shorthand for {@link Roo.MessageBox}
548  */
549 Roo.MessageBox = Roo.MessageBox || Roo.bootstrap.MessageBox 
550 Roo.Msg = Roo.Msg || Roo.MessageBox;