3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.MessageBox
9 * <p>Utility class for generating different styles of message boxes. The alias Ext.Msg can also be used.<p/>
10 * <p>Note that the MessageBox is asynchronous. Unlike a regular JavaScript <code>alert</code> (which will halt
11 * browser execution), showing a MessageBox will not cause the code to stop. For this reason, if you have code
12 * that should only run <em>after</em> some user feedback from the MessageBox, you must use a callback function
13 * (see the <code>function</code> parameter for {@link #show} for more details).</p>
14 * <p>Example usage:</p>
17 Ext.Msg.alert('Status', 'Changes saved successfully.');
19 // Prompt for user data and process the result using a callback:
20 Ext.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
22 // process text value and close...
26 // Show a dialog using config options:
28 title:'Save Changes?',
29 msg: 'You are closing a tab that has unsaved changes. Would you like to save your changes?',
30 buttons: Ext.Msg.YESNOCANCEL,
33 icon: Ext.MessageBox.QUESTION
38 Ext.MessageBox = function(){
39 var dlg, opt, mask, waitTimer,
40 bodyEl, msgEl, textboxEl, textareaEl, progressBar, pp, iconEl, spacerEl,
41 buttons, activeTextEl, bwidth, bufferIcon = '', iconCls = '',
42 buttonNames = ['ok', 'yes', 'no', 'cancel'];
45 var handleButton = function(button){
46 buttons[button].blur();
50 Ext.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value, opt], 1);
55 var handleHide = function(){
57 dlg.el.removeClass(opt.cls);
63 var handleEsc = function(d, k, e){
64 if(opt && opt.closable !== false){
74 var updateButtons = function(b){
78 Ext.each(buttonNames, function(name){
83 dlg.footer.dom.style.display = '';
84 Ext.iterate(buttons, function(name, btn){
88 btn.setText(Ext.isString(cfg) ? cfg : Ext.MessageBox.buttonText[name]);
89 width += btn.getEl().getWidth() + 15;
99 * Returns a reference to the underlying {@link Ext.Window} element
100 * @return {Ext.Window} The window
102 getDialog : function(titleText){
107 Ext.each(buttonNames, function(name){
108 btns.push(buttons[name] = new Ext.Button({
109 text: this.buttonText[name],
110 handler: handleButton.createCallback(name),
114 dlg = new Ext.Window({
119 constrainHeader:true,
125 buttonAlign:"center",
133 if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
136 handleButton("cancel");
139 fbar: new Ext.Toolbar({
141 enableOverflow: false
144 dlg.render(document.body);
145 dlg.getEl().addClass('x-window-dlg');
147 bodyEl = dlg.body.createChild({
148 html:'<div class="ext-mb-icon"></div><div class="ext-mb-content"><span class="ext-mb-text"></span><br /><div class="ext-mb-fix-cursor"><input type="text" class="ext-mb-input" /><textarea class="ext-mb-textarea"></textarea></div></div>'
150 iconEl = Ext.get(bodyEl.dom.firstChild);
151 var contentEl = bodyEl.dom.childNodes[1];
152 msgEl = Ext.get(contentEl.firstChild);
153 textboxEl = Ext.get(contentEl.childNodes[2].firstChild);
154 textboxEl.enableDisplayMode();
155 textboxEl.addKeyListener([10,13], function(){
156 if(dlg.isVisible() && opt && opt.buttons){
159 }else if(opt.buttons.yes){
164 textareaEl = Ext.get(contentEl.childNodes[2].childNodes[1]);
165 textareaEl.enableDisplayMode();
166 progressBar = new Ext.ProgressBar({
169 bodyEl.createChild({cls:'x-clear'});
175 * Updates the message box body text
176 * @param {String} text (optional) Replaces the message box element's innerHTML with the specified string (defaults to
177 * the XHTML-compliant non-breaking space character '&#160;')
178 * @return {Ext.MessageBox} this
180 updateText : function(text){
181 if(!dlg.isVisible() && !opt.width){
182 dlg.setSize(this.maxWidth, 100); // resize first so content is never clipped from previous shows
184 msgEl.update(text || ' ');
186 var iw = iconCls != '' ? (iconEl.getWidth() + iconEl.getMargins('lr')) : 0,
187 mw = msgEl.getWidth() + msgEl.getMargins('lr'),
188 fw = dlg.getFrameWidth('lr'),
189 bw = dlg.body.getFrameWidth('lr'),
192 if (Ext.isIE && iw > 0){
193 //3 pixels get subtracted in the icon CSS for an IE margin issue,
194 //so we have to add it back here for the overall width to be consistent
197 w = Math.max(Math.min(opt.width || iw+mw+fw+bw, opt.maxWidth || this.maxWidth),
198 Math.max(opt.minWidth || this.minWidth, bwidth || 0));
200 if(opt.prompt === true){
201 activeTextEl.setWidth(w-iw-fw-bw);
203 if(opt.progress === true || opt.wait === true){
204 progressBar.setSize(w-iw-fw-bw);
206 if(Ext.isIE && w == bwidth){
207 w += 4; //Add offset when the content width is smaller than the buttons.
209 dlg.setSize(w, 'auto').center();
214 * Updates a progress-style message box's text and progress bar. Only relevant on message boxes
215 * initiated via {@link Ext.MessageBox#progress} or {@link Ext.MessageBox#wait},
216 * or by calling {@link Ext.MessageBox#show} with progress: true.
217 * @param {Number} value Any number between 0 and 1 (e.g., .5, defaults to 0)
218 * @param {String} progressText The progress text to display inside the progress bar (defaults to '')
219 * @param {String} msg The message box's body text is replaced with the specified string (defaults to undefined
220 * so that any existing body text will not get overwritten by default unless a new value is passed in)
221 * @return {Ext.MessageBox} this
223 updateProgress : function(value, progressText, msg){
224 progressBar.updateProgress(value, progressText);
226 this.updateText(msg);
232 * Returns true if the message box is currently displayed
233 * @return {Boolean} True if the message box is visible, else false
235 isVisible : function(){
236 return dlg && dlg.isVisible();
240 * Hides the message box if it is displayed
241 * @return {Ext.MessageBox} this
244 var proxy = dlg ? dlg.activeGhost : null;
245 if(this.isVisible() || proxy){
249 // unghost is a private function, but i saw no better solution
250 // to fix the locking problem when dragging while it closes
251 dlg.unghost(false, false);
258 * Displays a new message box, or reinitializes an existing message box, based on the config options
259 * passed in. All display functions (e.g. prompt, alert, etc.) on MessageBox call this function internally,
260 * although those calls are basic shortcuts and do not support all of the config options allowed here.
261 * @param {Object} config The following config options are supported: <ul>
262 * <li><b>animEl</b> : String/Element<div class="sub-desc">An id or Element from which the message box should animate as it
263 * opens and closes (defaults to undefined)</div></li>
264 * <li><b>buttons</b> : Object/Boolean<div class="sub-desc">A button config object (e.g., Ext.MessageBox.OKCANCEL or {ok:'Foo',
265 * cancel:'Bar'}), or false to not show any buttons (defaults to false)</div></li>
266 * <li><b>closable</b> : Boolean<div class="sub-desc">False to hide the top-right close button (defaults to true). Note that
267 * progress and wait dialogs will ignore this property and always hide the close button as they can only
268 * be closed programmatically.</div></li>
269 * <li><b>cls</b> : String<div class="sub-desc">A custom CSS class to apply to the message box's container element</div></li>
270 * <li><b>defaultTextHeight</b> : Number<div class="sub-desc">The default height in pixels of the message box's multiline textarea
271 * if displayed (defaults to 75)</div></li>
272 * <li><b>fn</b> : Function<div class="sub-desc">A callback function which is called when the dialog is dismissed either
273 * by clicking on the configured buttons, or on the dialog close button, or by pressing
274 * the return button to enter input.
275 * <p>Progress and wait dialogs will ignore this option since they do not respond to user
276 * actions and can only be closed programmatically, so any required function should be called
277 * by the same code after it closes the dialog. Parameters passed:<ul>
278 * <li><b>buttonId</b> : String<div class="sub-desc">The ID of the button pressed, one of:<div class="sub-desc"><ul>
279 * <li><tt>ok</tt></li>
280 * <li><tt>yes</tt></li>
281 * <li><tt>no</tt></li>
282 * <li><tt>cancel</tt></li>
283 * </ul></div></div></li>
284 * <li><b>text</b> : String<div class="sub-desc">Value of the input field if either <tt><a href="#show-option-prompt" ext:member="show-option-prompt" ext:cls="Ext.MessageBox">prompt</a></tt>
285 * or <tt><a href="#show-option-multiline" ext:member="show-option-multiline" ext:cls="Ext.MessageBox">multiline</a></tt> is true</div></li>
286 * <li><b>opt</b> : Object<div class="sub-desc">The config object passed to show.</div></li>
287 * </ul></p></div></li>
288 * <li><b>scope</b> : Object<div class="sub-desc">The scope of the callback function</div></li>
289 * <li><b>icon</b> : String<div class="sub-desc">A CSS class that provides a background image to be used as the body icon for the
290 * dialog (e.g. Ext.MessageBox.WARNING or 'custom-class') (defaults to '')</div></li>
291 * <li><b>iconCls</b> : String<div class="sub-desc">The standard {@link Ext.Window#iconCls} to
292 * add an optional header icon (defaults to '')</div></li>
293 * <li><b>maxWidth</b> : Number<div class="sub-desc">The maximum width in pixels of the message box (defaults to 600)</div></li>
294 * <li><b>minWidth</b> : Number<div class="sub-desc">The minimum width in pixels of the message box (defaults to 100)</div></li>
295 * <li><b>modal</b> : Boolean<div class="sub-desc">False to allow user interaction with the page while the message box is
296 * displayed (defaults to true)</div></li>
297 * <li><b>msg</b> : String<div class="sub-desc">A string that will replace the existing message box body text (defaults to the
298 * XHTML-compliant non-breaking space character '&#160;')</div></li>
299 * <li><a id="show-option-multiline"></a><b>multiline</b> : Boolean<div class="sub-desc">
300 * True to prompt the user to enter multi-line text (defaults to false)</div></li>
301 * <li><b>progress</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
302 * <li><b>progressText</b> : String<div class="sub-desc">The text to display inside the progress bar if progress = true (defaults to '')</div></li>
303 * <li><a id="show-option-prompt"></a><b>prompt</b> : Boolean<div class="sub-desc">True to prompt the user to enter single-line text (defaults to false)</div></li>
304 * <li><b>proxyDrag</b> : Boolean<div class="sub-desc">True to display a lightweight proxy while dragging (defaults to false)</div></li>
305 * <li><b>title</b> : String<div class="sub-desc">The title text</div></li>
306 * <li><b>value</b> : String<div class="sub-desc">The string value to set into the active textbox element if displayed</div></li>
307 * <li><b>wait</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
308 * <li><b>waitConfig</b> : Object<div class="sub-desc">A {@link Ext.ProgressBar#waitConfig} object (applies only if wait = true)</div></li>
309 * <li><b>width</b> : Number<div class="sub-desc">The width of the dialog in pixels</div></li>
315 msg: 'Please enter your address:',
317 buttons: Ext.MessageBox.OKCANCEL,
320 animEl: 'addAddressBtn',
321 icon: Ext.MessageBox.INFO
324 * @return {Ext.MessageBox} this
326 show : function(options){
327 if(this.isVisible()){
331 var d = this.getDialog(opt.title || " ");
333 d.setTitle(opt.title || " ");
334 var allowClose = (opt.closable !== false && opt.progress !== true && opt.wait !== true);
335 d.tools.close.setDisplayed(allowClose);
336 activeTextEl = textboxEl;
337 opt.prompt = opt.prompt || (opt.multiline ? true : false);
342 textareaEl.setHeight(Ext.isNumber(opt.multiline) ? opt.multiline : this.defaultTextHeight);
343 activeTextEl = textareaEl;
352 activeTextEl.dom.value = opt.value || "";
354 d.focusEl = activeTextEl;
356 var bs = opt.buttons;
360 }else if(bs && bs.yes){
368 d.setIconClass(opt.iconCls);
370 this.setIcon(Ext.isDefined(opt.icon) ? opt.icon : bufferIcon);
371 bwidth = updateButtons(opt.buttons);
372 progressBar.setVisible(opt.progress === true || opt.wait === true);
373 this.updateProgress(0, opt.progressText);
374 this.updateText(opt.msg);
376 d.el.addClass(opt.cls);
378 d.proxyDrag = opt.proxyDrag === true;
379 d.modal = opt.modal !== false;
380 d.mask = opt.modal !== false ? mask : false;
382 // force it to the end of the z-index stack so it gets a cursor in FF
383 document.body.appendChild(dlg.el.dom);
384 d.setAnimateTarget(opt.animEl);
385 //workaround for window internally enabling keymap in afterShow
386 d.on('show', function(){
387 if(allowClose === true){
392 }, this, {single:true});
395 if(opt.wait === true){
396 progressBar.wait(opt.waitConfig);
402 * Adds the specified icon to the dialog. By default, the class 'ext-mb-icon' is applied for default
403 * styling, and the class passed in is expected to supply the background image url. Pass in empty string ('')
404 * to clear any existing icon. This method must be called before the MessageBox is shown.
405 * The following built-in icon classes are supported, but you can also pass in a custom class name:
408 Ext.MessageBox.WARNING
409 Ext.MessageBox.QUESTION
412 * @param {String} icon A CSS classname specifying the icon's background image url, or empty string to clear the icon
413 * @return {Ext.MessageBox} this
415 setIcon : function(icon){
420 bufferIcon = undefined;
421 if(icon && icon != ''){
422 iconEl.removeClass('x-hidden');
423 iconEl.replaceClass(iconCls, icon);
424 bodyEl.addClass('x-dlg-icon');
427 iconEl.replaceClass(iconCls, 'x-hidden');
428 bodyEl.removeClass('x-dlg-icon');
435 * Displays a message box with a progress bar. This message box has no buttons and is not closeable by
436 * the user. You are responsible for updating the progress bar as needed via {@link Ext.MessageBox#updateProgress}
437 * and closing the message box when the process is complete.
438 * @param {String} title The title bar text
439 * @param {String} msg The message box body text
440 * @param {String} progressText (optional) The text to display inside the progress bar (defaults to '')
441 * @return {Ext.MessageBox} this
443 progress : function(title, msg, progressText){
450 minWidth: this.minProgressWidth,
451 progressText: progressText
457 * Displays a message box with an infinitely auto-updating progress bar. This can be used to block user
458 * interaction while waiting for a long-running process to complete that does not have defined intervals.
459 * You are responsible for closing the message box when the process is complete.
460 * @param {String} msg The message box body text
461 * @param {String} title (optional) The title bar text
462 * @param {Object} config (optional) A {@link Ext.ProgressBar#waitConfig} object
463 * @return {Ext.MessageBox} this
465 wait : function(msg, title, config){
473 minWidth: this.minProgressWidth,
480 * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript alert prompt).
481 * If a callback function is passed it will be called after the user clicks the button, and the
482 * id of the button that was clicked will be passed as the only parameter to the callback
483 * (could also be the top-right close button).
484 * @param {String} title The title bar text
485 * @param {String} msg The message box body text
486 * @param {Function} fn (optional) The callback function invoked after the message box is closed
487 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser wnidow.
488 * @return {Ext.MessageBox} this
490 alert : function(title, msg, fn, scope){
497 minWidth: this.minWidth
503 * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's confirm).
504 * If a callback function is passed it will be called after the user clicks either button,
505 * and the id of the button that was clicked will be passed as the only parameter to the callback
506 * (could also be the top-right close button).
507 * @param {String} title The title bar text
508 * @param {String} msg The message box body text
509 * @param {Function} fn (optional) The callback function invoked after the message box is closed
510 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser wnidow.
511 * @return {Ext.MessageBox} this
513 confirm : function(title, msg, fn, scope){
521 minWidth: this.minWidth
527 * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to JavaScript's prompt).
528 * The prompt can be a single-line or multi-line textbox. If a callback function is passed it will be called after the user
529 * clicks either button, and the id of the button that was clicked (could also be the top-right
530 * close button) and the text that was entered will be passed as the two parameters to the callback.
531 * @param {String} title The title bar text
532 * @param {String} msg The message box body text
533 * @param {Function} fn (optional) The callback function invoked after the message box is closed
534 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser wnidow.
535 * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
536 * property, or the height in pixels to create the textbox (defaults to false / single-line)
537 * @param {String} value (optional) Default value of the text input element (defaults to '')
538 * @return {Ext.MessageBox} this
540 prompt : function(title, msg, fn, scope, multiline, value){
544 buttons: this.OKCANCEL,
546 minWidth: this.minPromptWidth,
549 multiline: multiline,
556 * Button config that displays a single OK button
561 * Button config that displays a single Cancel button
564 CANCEL : {cancel:true},
566 * Button config that displays OK and Cancel buttons
569 OKCANCEL : {ok:true, cancel:true},
571 * Button config that displays Yes and No buttons
574 YESNO : {yes:true, no:true},
576 * Button config that displays Yes, No and Cancel buttons
579 YESNOCANCEL : {yes:true, no:true, cancel:true},
581 * The CSS class that provides the INFO icon image
584 INFO : 'ext-mb-info',
586 * The CSS class that provides the WARNING icon image
589 WARNING : 'ext-mb-warning',
591 * The CSS class that provides the QUESTION icon image
594 QUESTION : 'ext-mb-question',
596 * The CSS class that provides the ERROR icon image
599 ERROR : 'ext-mb-error',
602 * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
605 defaultTextHeight : 75,
607 * The maximum width in pixels of the message box (defaults to 600)
612 * The minimum width in pixels of the message box (defaults to 100)
617 * The minimum width in pixels of the message box if it is a progress-style dialog. This is useful
618 * for setting a different minimum width than text-only dialogs may need (defaults to 250).
621 minProgressWidth : 250,
623 * The minimum width in pixels of the message box if it is a prompt dialog. This is useful
624 * for setting a different minimum width than text-only dialogs may need (defaults to 250).
629 * An object containing the default button text strings that can be overriden for localized language support.
630 * Supported properties are: ok, cancel, yes and no. Generally you should include a locale-specific
631 * resource file for handling language support across the framework.
632 * Customize the default text like so: Ext.MessageBox.buttonText.yes = "oui"; //french
645 * Shorthand for {@link Ext.MessageBox}
647 Ext.Msg = Ext.MessageBox;