4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.MessageBox
15 * Utility class for generating different styles of message boxes. The alias Roo.Msg can also be used.
19 Roo.Msg.alert('Status', 'Changes saved successfully.');
21 // Prompt for user data:
22 Roo.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
24 // process text value...
28 // Show a dialog using config options:
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,
39 Roo.MessageBox = function(){
40 var dlg, opt, mask, waitTimer;
41 var bodyEl, msgEl, textboxEl, textareaEl, progressEl, pp;
42 var buttons, activeTextEl, bwidth;
45 var handleButton = function(button){
47 Roo.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value], 1);
51 var handleHide = function(){
53 dlg.el.removeClass(opt.cls);
56 Roo.TaskMgr.stop(waitTimer);
62 var updateButtons = function(b){
66 buttons["cancel"].hide();
67 buttons["yes"].hide();
69 dlg.footer.dom.style.display = 'none';
72 dlg.footer.dom.style.display = '';
73 for(var k in buttons){
74 if(typeof buttons[k] != "function"){
77 buttons[k].setText(typeof b[k] == "string" ? b[k] : Roo.MessageBox.buttonText[k]);
78 width += buttons[k].el.getWidth()+15;
88 var handleEsc = function(d, k, e){
89 if(opt && opt.closable !== false){
99 * Returns a reference to the underlying {@link Roo.BasicDialog} element
100 * @return {Roo.BasicDialog} The BasicDialog element
102 getDialog : function(){
104 dlg = new Roo.BasicDialog("x-msg-box", {
109 constraintoviewport:false,
114 width:400, height:100,
115 buttonAlign:"center",
116 closeClick : function(){
117 if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
120 handleButton("cancel");
125 dlg.on("hide", handleHide);
127 dlg.addKeyListener(27, handleEsc);
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({
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"> </div></div></div>'
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){
145 }else if(opt.buttons.yes){
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;
156 pp = Roo.get(pf.firstChild);
157 pp.setHeight(pf.offsetHeight);
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 '&#160;')
168 * @return {Roo.MessageBox} This message box
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
174 msgEl.innerHTML = text || ' ';
176 var cw = Math.max(msgEl.offsetWidth, msgEl.parentNode.scrollWidth);
177 //Roo.log("guesed size: " + JSON.stringify([cw,msgEl.offsetWidth, msgEl.parentNode.scrollWidth]));
179 Math.min(opt.width || cw , this.maxWidth),
180 Math.max(opt.minWidth || this.minWidth, bwidth)
183 activeTextEl.setWidth(w);
186 dlg.fixedcenter = false;
188 // to big, make it scroll. = But as usual stupid IE does not support
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');
195 bodyEl.dom.style.height = '';
196 bodyEl.dom.style.overflowY = '';
199 bodyEl.dom.style.get = 'auto' + ( Roo.isIE ? '' : ' !important');
201 bodyEl.dom.style.overflowX = '';
204 dlg.setContentSize(w, bodyEl.getHeight());
206 dlg.fixedcenter = true;
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
218 updateProgress : function(value, text){
220 this.updateText(text);
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));
229 * Returns true if the message box is currently displayed
230 * @return {Boolean} True if the message box is visible, else false
232 isVisible : function(){
233 return dlg && dlg.isVisible();
237 * Hides the message box if it is displayed
240 if(this.isVisible()){
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:
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 ' ')
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
291 msg: 'Please enter your address:',
293 buttons: Roo.MessageBox.OKCANCEL,
296 animEl: 'addAddressBtn'
299 * @param {Object} config Configuration options
300 * @return {Roo.MessageBox} This message box
302 show : function(options)
305 // this causes nightmares if you show one dialog after another
306 // especially on callbacks..
308 if(this.isVisible()){
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";
318 var d = this.getDialog();
320 d.setTitle(opt.title || " ");
321 d.close.setDisplayed(opt.closable !== false);
322 activeTextEl = textboxEl;
323 opt.prompt = opt.prompt || (opt.multiline ? true : false);
328 textareaEl.setHeight(typeof opt.multiline == "number" ?
329 opt.multiline : this.defaultTextHeight);
330 activeTextEl = textareaEl;
339 progressEl.setDisplayed(opt.progress === true);
340 this.updateProgress(0);
341 activeTextEl.dom.value = opt.value || "";
343 dlg.setDefaultButton(activeTextEl);
345 var bs = opt.buttons;
349 }else if(bs && bs.yes){
352 dlg.setDefaultButton(db);
354 bwidth = updateButtons(opt.buttons);
355 this.updateText(opt.msg);
357 d.el.addClass(opt.cls);
359 d.proxyDrag = opt.proxyDrag === true;
360 d.modal = opt.modal !== false;
361 d.mask = opt.modal !== false ? mask : false;
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);
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
380 progress : function(title, msg){
387 minWidth: this.minProgressWidth,
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
404 alert : function(title, msg, fn, scope){
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
424 wait : function(msg, title){
435 waitTimer = Roo.TaskMgr.start({
437 Roo.MessageBox.updateProgress(((((i+20)%20)+1)*5)*.01);
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
454 confirm : function(title, msg, fn, scope){
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
480 prompt : function(title, msg, fn, scope, multiline){
484 buttons: this.OKCANCEL,
489 multiline: multiline,
496 * Button config that displays a single OK button
501 * Button config that displays Yes and No buttons
504 YESNO : {yes:true, no:true},
506 * Button config that displays OK and Cancel buttons
509 OKCANCEL : {ok:true, cancel:true},
511 * Button config that displays Yes, No and Cancel buttons
514 YESNOCANCEL : {yes:true, no:true, cancel:true},
517 * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
520 defaultTextHeight : 75,
522 * The maximum width in pixels of the message box (defaults to 600)
527 * The minimum width in pixels of the message box (defaults to 100)
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)
536 minProgressWidth : 250,
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?";
553 * Shorthand for {@link Roo.MessageBox}
555 Roo.Msg = Roo.MessageBox;