3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.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 // Append a space here for sizing. In IE, for some reason, it wraps text incorrectly without one in some cases
185 msgEl.update(text ? text + ' ' : ' ');
187 var iw = iconCls != '' ? (iconEl.getWidth() + iconEl.getMargins('lr')) : 0,
188 mw = msgEl.getWidth() + msgEl.getMargins('lr'),
189 fw = dlg.getFrameWidth('lr'),
190 bw = dlg.body.getFrameWidth('lr'),
193 w = Math.max(Math.min(opt.width || iw+mw+fw+bw, opt.maxWidth || this.maxWidth),
194 Math.max(opt.minWidth || this.minWidth, bwidth || 0));
196 if(opt.prompt === true){
197 activeTextEl.setWidth(w-iw-fw-bw);
199 if(opt.progress === true || opt.wait === true){
200 progressBar.setSize(w-iw-fw-bw);
202 if(Ext.isIE && w == bwidth){
203 w += 4; //Add offset when the content width is smaller than the buttons.
205 msgEl.update(text || ' ');
206 dlg.setSize(w, 'auto').center();
211 * Updates a progress-style message box's text and progress bar. Only relevant on message boxes
212 * initiated via {@link Ext.MessageBox#progress} or {@link Ext.MessageBox#wait},
213 * or by calling {@link Ext.MessageBox#show} with progress: true.
214 * @param {Number} value Any number between 0 and 1 (e.g., .5, defaults to 0)
215 * @param {String} progressText The progress text to display inside the progress bar (defaults to '')
216 * @param {String} msg The message box's body text is replaced with the specified string (defaults to undefined
217 * so that any existing body text will not get overwritten by default unless a new value is passed in)
218 * @return {Ext.MessageBox} this
220 updateProgress : function(value, progressText, msg){
221 progressBar.updateProgress(value, progressText);
223 this.updateText(msg);
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
238 * @return {Ext.MessageBox} this
241 var proxy = dlg ? dlg.activeGhost : null;
242 if(this.isVisible() || proxy){
246 // unghost is a private function, but i saw no better solution
247 // to fix the locking problem when dragging while it closes
248 dlg.unghost(false, false);
255 * Displays a new message box, or reinitializes an existing message box, based on the config options
256 * passed in. All display functions (e.g. prompt, alert, etc.) on MessageBox call this function internally,
257 * although those calls are basic shortcuts and do not support all of the config options allowed here.
258 * @param {Object} config The following config options are supported: <ul>
259 * <li><b>animEl</b> : String/Element<div class="sub-desc">An id or Element from which the message box should animate as it
260 * opens and closes (defaults to undefined)</div></li>
261 * <li><b>buttons</b> : Object/Boolean<div class="sub-desc">A button config object (e.g., Ext.MessageBox.OKCANCEL or {ok:'Foo',
262 * cancel:'Bar'}), or false to not show any buttons (defaults to false)</div></li>
263 * <li><b>closable</b> : Boolean<div class="sub-desc">False to hide the top-right close button (defaults to true). Note that
264 * progress and wait dialogs will ignore this property and always hide the close button as they can only
265 * be closed programmatically.</div></li>
266 * <li><b>cls</b> : String<div class="sub-desc">A custom CSS class to apply to the message box's container element</div></li>
267 * <li><b>defaultTextHeight</b> : Number<div class="sub-desc">The default height in pixels of the message box's multiline textarea
268 * if displayed (defaults to 75)</div></li>
269 * <li><b>fn</b> : Function<div class="sub-desc">A callback function which is called when the dialog is dismissed either
270 * by clicking on the configured buttons, or on the dialog close button, or by pressing
271 * the return button to enter input.
272 * <p>Progress and wait dialogs will ignore this option since they do not respond to user
273 * actions and can only be closed programmatically, so any required function should be called
274 * by the same code after it closes the dialog. Parameters passed:<ul>
275 * <li><b>buttonId</b> : String<div class="sub-desc">The ID of the button pressed, one of:<div class="sub-desc"><ul>
276 * <li><tt>ok</tt></li>
277 * <li><tt>yes</tt></li>
278 * <li><tt>no</tt></li>
279 * <li><tt>cancel</tt></li>
280 * </ul></div></div></li>
281 * <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>
282 * or <tt><a href="#show-option-multiline" ext:member="show-option-multiline" ext:cls="Ext.MessageBox">multiline</a></tt> is true</div></li>
283 * <li><b>opt</b> : Object<div class="sub-desc">The config object passed to show.</div></li>
284 * </ul></p></div></li>
285 * <li><b>scope</b> : Object<div class="sub-desc">The scope of the callback function</div></li>
286 * <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
287 * dialog (e.g. Ext.MessageBox.WARNING or 'custom-class') (defaults to '')</div></li>
288 * <li><b>iconCls</b> : String<div class="sub-desc">The standard {@link Ext.Window#iconCls} to
289 * add an optional header icon (defaults to '')</div></li>
290 * <li><b>maxWidth</b> : Number<div class="sub-desc">The maximum width in pixels of the message box (defaults to 600)</div></li>
291 * <li><b>minWidth</b> : Number<div class="sub-desc">The minimum width in pixels of the message box (defaults to 100)</div></li>
292 * <li><b>modal</b> : Boolean<div class="sub-desc">False to allow user interaction with the page while the message box is
293 * displayed (defaults to true)</div></li>
294 * <li><b>msg</b> : String<div class="sub-desc">A string that will replace the existing message box body text (defaults to the
295 * XHTML-compliant non-breaking space character '&#160;')</div></li>
296 * <li><a id="show-option-multiline"></a><b>multiline</b> : Boolean<div class="sub-desc">
297 * True to prompt the user to enter multi-line text (defaults to false)</div></li>
298 * <li><b>progress</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
299 * <li><b>progressText</b> : String<div class="sub-desc">The text to display inside the progress bar if progress = true (defaults to '')</div></li>
300 * <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>
301 * <li><b>proxyDrag</b> : Boolean<div class="sub-desc">True to display a lightweight proxy while dragging (defaults to false)</div></li>
302 * <li><b>title</b> : String<div class="sub-desc">The title text</div></li>
303 * <li><b>value</b> : String<div class="sub-desc">The string value to set into the active textbox element if displayed</div></li>
304 * <li><b>wait</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
305 * <li><b>waitConfig</b> : Object<div class="sub-desc">A {@link Ext.ProgressBar#waitConfig} object (applies only if wait = true)</div></li>
306 * <li><b>width</b> : Number<div class="sub-desc">The width of the dialog in pixels</div></li>
312 msg: 'Please enter your address:',
314 buttons: Ext.MessageBox.OKCANCEL,
317 animEl: 'addAddressBtn',
318 icon: Ext.MessageBox.INFO
321 * @return {Ext.MessageBox} this
323 show : function(options){
324 if(this.isVisible()){
328 var d = this.getDialog(opt.title || " ");
330 d.setTitle(opt.title || " ");
331 var allowClose = (opt.closable !== false && opt.progress !== true && opt.wait !== true);
332 d.tools.close.setDisplayed(allowClose);
333 activeTextEl = textboxEl;
334 opt.prompt = opt.prompt || (opt.multiline ? true : false);
339 textareaEl.setHeight(Ext.isNumber(opt.multiline) ? opt.multiline : this.defaultTextHeight);
340 activeTextEl = textareaEl;
349 activeTextEl.dom.value = opt.value || "";
351 d.focusEl = activeTextEl;
353 var bs = opt.buttons;
357 }else if(bs && bs.yes){
364 if(Ext.isDefined(opt.iconCls)){
365 d.setIconClass(opt.iconCls);
367 this.setIcon(Ext.isDefined(opt.icon) ? opt.icon : bufferIcon);
368 bwidth = updateButtons(opt.buttons);
369 progressBar.setVisible(opt.progress === true || opt.wait === true);
370 this.updateProgress(0, opt.progressText);
371 this.updateText(opt.msg);
373 d.el.addClass(opt.cls);
375 d.proxyDrag = opt.proxyDrag === true;
376 d.modal = opt.modal !== false;
377 d.mask = opt.modal !== false ? mask : false;
379 // force it to the end of the z-index stack so it gets a cursor in FF
380 document.body.appendChild(dlg.el.dom);
381 d.setAnimateTarget(opt.animEl);
382 //workaround for window internally enabling keymap in afterShow
383 d.on('show', function(){
384 if(allowClose === true){
389 }, this, {single:true});
392 if(opt.wait === true){
393 progressBar.wait(opt.waitConfig);
399 * Adds the specified icon to the dialog. By default, the class 'ext-mb-icon' is applied for default
400 * styling, and the class passed in is expected to supply the background image url. Pass in empty string ('')
401 * to clear any existing icon. This method must be called before the MessageBox is shown.
402 * The following built-in icon classes are supported, but you can also pass in a custom class name:
405 Ext.MessageBox.WARNING
406 Ext.MessageBox.QUESTION
409 * @param {String} icon A CSS classname specifying the icon's background image url, or empty string to clear the icon
410 * @return {Ext.MessageBox} this
412 setIcon : function(icon){
417 bufferIcon = undefined;
418 if(icon && icon != ''){
419 iconEl.removeClass('x-hidden');
420 iconEl.replaceClass(iconCls, icon);
421 bodyEl.addClass('x-dlg-icon');
424 iconEl.replaceClass(iconCls, 'x-hidden');
425 bodyEl.removeClass('x-dlg-icon');
432 * Displays a message box with a progress bar. This message box has no buttons and is not closeable by
433 * the user. You are responsible for updating the progress bar as needed via {@link Ext.MessageBox#updateProgress}
434 * and closing the message box when the process is complete.
435 * @param {String} title The title bar text
436 * @param {String} msg The message box body text
437 * @param {String} progressText (optional) The text to display inside the progress bar (defaults to '')
438 * @return {Ext.MessageBox} this
440 progress : function(title, msg, progressText){
447 minWidth: this.minProgressWidth,
448 progressText: progressText
454 * Displays a message box with an infinitely auto-updating progress bar. This can be used to block user
455 * interaction while waiting for a long-running process to complete that does not have defined intervals.
456 * You are responsible for closing the message box when the process is complete.
457 * @param {String} msg The message box body text
458 * @param {String} title (optional) The title bar text
459 * @param {Object} config (optional) A {@link Ext.ProgressBar#waitConfig} object
460 * @return {Ext.MessageBox} this
462 wait : function(msg, title, config){
470 minWidth: this.minProgressWidth,
477 * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript alert prompt).
478 * If a callback function is passed it will be called after the user clicks the button, and the
479 * id of the button that was clicked will be passed as the only parameter to the callback
480 * (could also be the top-right close button).
481 * @param {String} title The title bar text
482 * @param {String} msg The message box body text
483 * @param {Function} fn (optional) The callback function invoked after the message box is closed
484 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser wnidow.
485 * @return {Ext.MessageBox} this
487 alert : function(title, msg, fn, scope){
494 minWidth: this.minWidth
500 * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's confirm).
501 * If a callback function is passed it will be called after the user clicks either button,
502 * and the id of the button that was clicked will be passed as the only parameter to the callback
503 * (could also be the top-right close button).
504 * @param {String} title The title bar text
505 * @param {String} msg The message box body text
506 * @param {Function} fn (optional) The callback function invoked after the message box is closed
507 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser wnidow.
508 * @return {Ext.MessageBox} this
510 confirm : function(title, msg, fn, scope){
518 minWidth: this.minWidth
524 * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to JavaScript's prompt).
525 * The prompt can be a single-line or multi-line textbox. If a callback function is passed it will be called after the user
526 * clicks either button, and the id of the button that was clicked (could also be the top-right
527 * close button) and the text that was entered will be passed as the two parameters to the callback.
528 * @param {String} title The title bar text
529 * @param {String} msg The message box body text
530 * @param {Function} fn (optional) The callback function invoked after the message box is closed
531 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser wnidow.
532 * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
533 * property, or the height in pixels to create the textbox (defaults to false / single-line)
534 * @param {String} value (optional) Default value of the text input element (defaults to '')
535 * @return {Ext.MessageBox} this
537 prompt : function(title, msg, fn, scope, multiline, value){
541 buttons: this.OKCANCEL,
543 minWidth: this.minPromptWidth,
546 multiline: multiline,
553 * Button config that displays a single OK button
558 * Button config that displays a single Cancel button
561 CANCEL : {cancel:true},
563 * Button config that displays OK and Cancel buttons
566 OKCANCEL : {ok:true, cancel:true},
568 * Button config that displays Yes and No buttons
571 YESNO : {yes:true, no:true},
573 * Button config that displays Yes, No and Cancel buttons
576 YESNOCANCEL : {yes:true, no:true, cancel:true},
578 * The CSS class that provides the INFO icon image
581 INFO : 'ext-mb-info',
583 * The CSS class that provides the WARNING icon image
586 WARNING : 'ext-mb-warning',
588 * The CSS class that provides the QUESTION icon image
591 QUESTION : 'ext-mb-question',
593 * The CSS class that provides the ERROR icon image
596 ERROR : 'ext-mb-error',
599 * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
602 defaultTextHeight : 75,
604 * The maximum width in pixels of the message box (defaults to 600)
609 * The minimum width in pixels of the message box (defaults to 100)
614 * The minimum width in pixels of the message box if it is a progress-style dialog. This is useful
615 * for setting a different minimum width than text-only dialogs may need (defaults to 250).
618 minProgressWidth : 250,
620 * The minimum width in pixels of the message box if it is a prompt dialog. This is useful
621 * for setting a different minimum width than text-only dialogs may need (defaults to 250).
626 * An object containing the default button text strings that can be overriden for localized language support.
627 * Supported properties are: ok, cancel, yes and no. Generally you should include a locale-specific
628 * resource file for handling language support across the framework.
629 * Customize the default text like so: Ext.MessageBox.buttonText.yes = "oui"; //french
642 * Shorthand for {@link Ext.MessageBox}
644 Ext.Msg = Ext.MessageBox;