3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.MessageBox"></div>/**
9 * @class Ext.MessageBox
10 * <p>Utility class for generating different styles of message boxes. The alias Ext.Msg can also be used.<p/>
11 * <p>Note that the MessageBox is asynchronous. Unlike a regular JavaScript <code>alert</code> (which will halt
12 * browser execution), showing a MessageBox will not cause the code to stop. For this reason, if you have code
13 * that should only run <em>after</em> some user feedback from the MessageBox, you must use a callback function
14 * (see the <code>function</code> parameter for {@link #show} for more details).</p>
15 * <p>Example usage:</p>
18 Ext.Msg.alert('Status', 'Changes saved successfully.');
20 // Prompt for user data and process the result using a callback:
21 Ext.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
23 // process text value and close...
27 // Show a dialog using config options:
29 title:'Save Changes?',
30 msg: 'You are closing a tab that has unsaved changes. Would you like to save your changes?',
31 buttons: Ext.Msg.YESNOCANCEL,
34 icon: Ext.MessageBox.QUESTION
39 Ext.MessageBox = function(){
40 var dlg, opt, mask, waitTimer;
41 var bodyEl, msgEl, textboxEl, textareaEl, progressBar, pp, iconEl, spacerEl;
42 var buttons, activeTextEl, bwidth, iconCls = '';
45 var handleButton = function(button){
49 Ext.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value, opt], 1);
54 var handleHide = function(){
56 dlg.el.removeClass(opt.cls);
62 var handleEsc = function(d, k, e){
63 if(opt && opt.closable !== false){
73 var updateButtons = function(b){
77 buttons['cancel'].hide();
78 buttons['yes'].hide();
82 dlg.footer.dom.style.display = '';
83 for(var k in buttons){
84 if(typeof buttons[k] != 'function'){
87 buttons[k].setText(typeof b[k] == 'string' ? b[k] : Ext.MessageBox.buttonText[k]);
88 width += buttons[k].el.getWidth()+15;
98 <div id="method-Ext.MessageBox-getDialog"></div>/**
99 * Returns a reference to the underlying {@link Ext.Window} element
100 * @return {Ext.Window} The window
102 getDialog : function(titleText){
104 dlg = new Ext.Window({
109 constrainHeader:true,
115 buttonAlign:'center',
123 if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
126 handleButton('cancel');
131 var bt = this.buttonText;
132 //TODO: refactor this block into a buttons config to pass into the Window constructor
133 buttons['ok'] = dlg.addButton(bt['ok'], handleButton.createCallback('ok'));
134 buttons['yes'] = dlg.addButton(bt['yes'], handleButton.createCallback('yes'));
135 buttons['no'] = dlg.addButton(bt['no'], handleButton.createCallback('no'));
136 buttons['cancel'] = dlg.addButton(bt['cancel'], handleButton.createCallback('cancel'));
137 buttons['ok'].hideMode = buttons['yes'].hideMode = buttons['no'].hideMode = buttons['cancel'].hideMode = 'offsets';
138 dlg.render(document.body);
139 dlg.getEl().addClass('x-window-dlg');
141 bodyEl = dlg.body.createChild({
142 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>'
144 iconEl = Ext.get(bodyEl.dom.firstChild);
145 var contentEl = bodyEl.dom.childNodes[1];
146 msgEl = Ext.get(contentEl.firstChild);
147 textboxEl = Ext.get(contentEl.childNodes[2].firstChild);
148 textboxEl.enableDisplayMode();
149 textboxEl.addKeyListener([10,13], function(){
150 if(dlg.isVisible() && opt && opt.buttons){
153 }else if(opt.buttons.yes){
158 textareaEl = Ext.get(contentEl.childNodes[2].childNodes[1]);
159 textareaEl.enableDisplayMode();
160 progressBar = new Ext.ProgressBar({
163 bodyEl.createChild({cls:'x-clear'});
168 <div id="method-Ext.MessageBox-updateText"></div>/**
169 * Updates the message box body text
170 * @param {String} text (optional) Replaces the message box element's innerHTML with the specified string (defaults to
171 * the XHTML-compliant non-breaking space character '&#160;')
172 * @return {Ext.MessageBox} this
174 updateText : function(text){
175 if(!dlg.isVisible() && !opt.width){
176 dlg.setSize(this.maxWidth, 100); // resize first so content is never clipped from previous shows
178 msgEl.update(text || ' ');
180 var iw = iconCls != '' ? (iconEl.getWidth() + iconEl.getMargins('lr')) : 0;
181 var mw = msgEl.getWidth() + msgEl.getMargins('lr');
182 var fw = dlg.getFrameWidth('lr');
183 var bw = dlg.body.getFrameWidth('lr');
184 if (Ext.isIE && iw > 0){
185 //3 pixels get subtracted in the icon CSS for an IE margin issue,
186 //so we have to add it back here for the overall width to be consistent
189 var w = Math.max(Math.min(opt.width || iw+mw+fw+bw, this.maxWidth),
190 Math.max(opt.minWidth || this.minWidth, bwidth || 0));
192 if(opt.prompt === true){
193 activeTextEl.setWidth(w-iw-fw-bw);
195 if(opt.progress === true || opt.wait === true){
196 progressBar.setSize(w-iw-fw-bw);
198 if(Ext.isIE && w == bwidth){
199 w += 4; //Add offset when the content width is smaller than the buttons.
201 dlg.setSize(w, 'auto').center();
205 <div id="method-Ext.MessageBox-updateProgress"></div>/**
206 * Updates a progress-style message box's text and progress bar. Only relevant on message boxes
207 * initiated via {@link Ext.MessageBox#progress} or {@link Ext.MessageBox#wait},
208 * or by calling {@link Ext.MessageBox#show} with progress: true.
209 * @param {Number} value Any number between 0 and 1 (e.g., .5, defaults to 0)
210 * @param {String} progressText The progress text to display inside the progress bar (defaults to '')
211 * @param {String} msg The message box's body text is replaced with the specified string (defaults to undefined
212 * so that any existing body text will not get overwritten by default unless a new value is passed in)
213 * @return {Ext.MessageBox} this
215 updateProgress : function(value, progressText, msg){
216 progressBar.updateProgress(value, progressText);
218 this.updateText(msg);
223 <div id="method-Ext.MessageBox-isVisible"></div>/**
224 * Returns true if the message box is currently displayed
225 * @return {Boolean} True if the message box is visible, else false
227 isVisible : function(){
228 return dlg && dlg.isVisible();
231 <div id="method-Ext.MessageBox-hide"></div>/**
232 * Hides the message box if it is displayed
233 * @return {Ext.MessageBox} this
236 var proxy = dlg ? dlg.activeGhost : null;
237 if(this.isVisible() || proxy){
241 // unghost is a private function, but i saw no better solution
242 // to fix the locking problem when dragging while it closes
243 dlg.unghost(false, false);
249 <div id="method-Ext.MessageBox-show"></div>/**
250 * Displays a new message box, or reinitializes an existing message box, based on the config options
251 * passed in. All display functions (e.g. prompt, alert, etc.) on MessageBox call this function internally,
252 * although those calls are basic shortcuts and do not support all of the config options allowed here.
253 * @param {Object} config The following config options are supported: <ul>
254 * <li><b>animEl</b> : String/Element<div class="sub-desc">An id or Element from which the message box should animate as it
255 * opens and closes (defaults to undefined)</div></li>
256 * <li><b>buttons</b> : Object/Boolean<div class="sub-desc">A button config object (e.g., Ext.MessageBox.OKCANCEL or {ok:'Foo',
257 * cancel:'Bar'}), or false to not show any buttons (defaults to false)</div></li>
258 * <li><b>closable</b> : Boolean<div class="sub-desc">False to hide the top-right close button (defaults to true). Note that
259 * progress and wait dialogs will ignore this property and always hide the close button as they can only
260 * be closed programmatically.</div></li>
261 * <li><b>cls</b> : String<div class="sub-desc">A custom CSS class to apply to the message box's container element</div></li>
262 * <li><b>fn</b> : Function<div class="sub-desc">A callback function which is called when the dialog is dismissed either
263 * by clicking on the configured buttons, or on the dialog close button, or by pressing
264 * the return button to enter input.
265 * <p>Progress and wait dialogs will ignore this option since they do not respond to user
266 * actions and can only be closed programmatically, so any required function should be called
267 * by the same code after it closes the dialog. Parameters passed:<ul>
268 * <li><b>buttonId</b> : String<div class="sub-desc">The ID of the button pressed, one of:<div class="sub-desc"><ul>
269 * <li><tt>ok</tt></li>
270 * <li><tt>yes</tt></li>
271 * <li><tt>no</tt></li>
272 * <li><tt>cancel</tt></li>
273 * </ul></div></div></li>
274 * <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>
275 * or <tt><a href="#show-option-multiline" ext:member="show-option-multiline" ext:cls="Ext.MessageBox">multiline</a></tt> is true</div></li>
276 * <li><b>opt</b> : Object<div class="sub-desc">The config object passed to show.</div></li>
277 * </ul></p></div></li>
278 * <li><b>scope</b> : Object<div class="sub-desc">The scope of the callback function</div></li>
279 * <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
280 * dialog (e.g. Ext.MessageBox.WARNING or 'custom-class') (defaults to '')</div></li>
281 * <li><b>iconCls</b> : String<div class="sub-desc">The standard {@link Ext.Window#iconCls} to
282 * add an optional header icon (defaults to '')</div></li>
283 * <li><b>maxWidth</b> : Number<div class="sub-desc">The maximum width in pixels of the message box (defaults to 600)</div></li>
284 * <li><b>minWidth</b> : Number<div class="sub-desc">The minimum width in pixels of the message box (defaults to 100)</div></li>
285 * <li><b>modal</b> : Boolean<div class="sub-desc">False to allow user interaction with the page while the message box is
286 * displayed (defaults to true)</div></li>
287 * <li><b>msg</b> : String<div class="sub-desc">A string that will replace the existing message box body text (defaults to the
288 * XHTML-compliant non-breaking space character '&#160;')</div></li>
289 * <li><a id="show-option-multiline"></a><b>multiline</b> : Boolean/Number<div class="sub-desc">
290 * If <code>prompt</code> is true, instead of prompting the user with a single line textbox,
291 * specify <code>multiline = true</code> to prompt the user with a multiline textarea
292 * using the <code>{@link #defaultTextHeight}</code> property, or specify a height in pixels
293 * to create the textarea (defaults to false creating a single-line textbox)</div></li>
294 * <li><b>progress</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
295 * <li><b>progressText</b> : String<div class="sub-desc">The text to display inside the progress bar if progress = true (defaults to '')</div></li>
296 * <li><a id="show-option-prompt"></a><b>prompt</b> : Boolean<div class="sub-desc">
297 * True to prompt the user with a textbox or textarea (defaults to false)</div></li>
298 * <li><b>proxyDrag</b> : Boolean<div class="sub-desc">True to display a lightweight proxy while dragging (defaults to false)</div></li>
299 * <li><b>title</b> : String<div class="sub-desc">The title text</div></li>
300 * <li><b>value</b> : String<div class="sub-desc">The string value to set into the active textbox element if displayed</div></li>
301 * <li><b>wait</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
302 * <li><b>waitConfig</b> : Object<div class="sub-desc">A {@link Ext.ProgressBar#waitConfig} object (applies only if wait = true)</div></li>
303 * <li><b>width</b> : Number<div class="sub-desc">The width of the dialog in pixels</div></li>
309 msg: 'Please enter your address:',
311 buttons: Ext.MessageBox.OKCANCEL,
312 multiline: true, // show a multiline textarea using the {@link #defaultTextHeight}
314 animEl: 'addAddressBtn',
315 icon: Ext.MessageBox.INFO
318 * @return {Ext.MessageBox} this
320 show : function(options){
321 if(this.isVisible()){
325 var d = this.getDialog(opt.title || ' ');
327 d.setTitle(opt.title || ' ');
328 var allowClose = (opt.closable !== false && opt.progress !== true && opt.wait !== true);
329 d.tools.close.setDisplayed(allowClose);
330 activeTextEl = textboxEl;
331 opt.prompt = opt.prompt || (opt.multiline ? true : false);
336 textareaEl.setHeight(typeof opt.multiline == 'number' ?
337 opt.multiline : this.defaultTextHeight);
338 activeTextEl = textareaEl;
347 activeTextEl.dom.value = opt.value || '';
349 d.focusEl = activeTextEl;
351 var bs = opt.buttons;
355 }else if(bs && bs.yes){
363 d.setIconClass(opt.iconCls);
365 this.setIcon(opt.icon);
367 d.el.addClass(opt.cls);
369 d.proxyDrag = opt.proxyDrag === true;
370 d.modal = opt.modal !== false;
371 d.mask = opt.modal !== false ? mask : false;
373 d.on('show', function(){
374 //workaround for window internally enabling keymap in afterShow
375 d.keyMap.setDisabled(allowClose !== true);
377 this.setIcon(opt.icon);
378 bwidth = updateButtons(opt.buttons);
379 progressBar.setVisible(opt.progress === true || opt.wait === true);
380 this.updateProgress(0, opt.progressText);
381 this.updateText(opt.msg);
382 if(opt.wait === true){
383 progressBar.wait(opt.waitConfig);
386 }, this, {single:true});
388 // force it to the end of the z-index stack so it gets a cursor in FF
389 document.body.appendChild(dlg.el.dom);
390 d.setAnimateTarget(opt.animEl);
396 <div id="method-Ext.MessageBox-setIcon"></div>/**
397 * Adds the specified icon to the dialog. By default, the class 'ext-mb-icon' is applied for default
398 * styling, and the class passed in is expected to supply the background image url. Pass in empty string ('')
399 * to clear any existing icon. The following built-in icon classes are supported, but you can also pass
400 * in a custom class name:
403 Ext.MessageBox.WARNING
404 Ext.MessageBox.QUESTION
407 * @param {String} icon A CSS classname specifying the icon's background image url, or empty string to clear the icon
408 * @return {Ext.MessageBox} this
410 setIcon : function(icon){
411 if(icon && icon != ''){
412 iconEl.removeClass('x-hidden');
413 iconEl.replaceClass(iconCls, icon);
414 bodyEl.addClass('x-dlg-icon');
417 iconEl.replaceClass(iconCls, 'x-hidden');
418 bodyEl.removeClass('x-dlg-icon');
424 <div id="method-Ext.MessageBox-progress"></div>/**
425 * Displays a message box with a progress bar. This message box has no buttons and is not closeable by
426 * the user. You are responsible for updating the progress bar as needed via {@link Ext.MessageBox#updateProgress}
427 * and closing the message box when the process is complete.
428 * @param {String} title The title bar text
429 * @param {String} msg The message box body text
430 * @param {String} progressText (optional) The text to display inside the progress bar (defaults to '')
431 * @return {Ext.MessageBox} this
433 progress : function(title, msg, progressText){
440 minWidth: this.minProgressWidth,
441 progressText: progressText
446 <div id="method-Ext.MessageBox-wait"></div>/**
447 * Displays a message box with an infinitely auto-updating progress bar. This can be used to block user
448 * interaction while waiting for a long-running process to complete that does not have defined intervals.
449 * You are responsible for closing the message box when the process is complete.
450 * @param {String} msg The message box body text
451 * @param {String} title (optional) The title bar text
452 * @param {Object} config (optional) A {@link Ext.ProgressBar#waitConfig} object
453 * @return {Ext.MessageBox} this
455 wait : function(msg, title, config){
463 minWidth: this.minProgressWidth,
469 <div id="method-Ext.MessageBox-alert"></div>/**
470 * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript alert prompt).
471 * If a callback function is passed it will be called after the user clicks the button, and the
472 * id of the button that was clicked will be passed as the only parameter to the callback
473 * (could also be the top-right close button).
474 * @param {String} title The title bar text
475 * @param {String} msg The message box body text
476 * @param {Function} fn (optional) The callback function invoked after the message box is closed
477 * @param {Object} scope (optional) The scope of the callback function
478 * @return {Ext.MessageBox} this
480 alert : function(title, msg, fn, scope){
491 <div id="method-Ext.MessageBox-confirm"></div>/**
492 * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's confirm).
493 * If a callback function is passed it will be called after the user clicks either button,
494 * and the id of the button that was clicked will be passed as the only parameter to the callback
495 * (could also be the top-right close button).
496 * @param {String} title The title bar text
497 * @param {String} msg The message box body text
498 * @param {Function} fn (optional) The callback function invoked after the message box is closed
499 * @param {Object} scope (optional) The scope of the callback function
500 * @return {Ext.MessageBox} this
502 confirm : function(title, msg, fn, scope){
514 <div id="method-Ext.MessageBox-prompt"></div>/**
515 * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to JavaScript's prompt).
516 * The prompt can be a single-line or multi-line textbox. If a callback function is passed it will be called after the user
517 * clicks either button, and the id of the button that was clicked (could also be the top-right
518 * close button) and the text that was entered will be passed as the two parameters to the callback.
519 * @param {String} title The title bar text
520 * @param {String} msg The message box body text
521 * @param {Function} fn (optional) The callback function invoked after the message box is closed
522 * @param {Object} scope (optional) The scope of the callback function
523 * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
524 * property, or the height in pixels to create the textbox (defaults to false / single-line)
525 * @param {String} value (optional) Default value of the text input element (defaults to '')
526 * @return {Ext.MessageBox} this
528 prompt : function(title, msg, fn, scope, multiline, value){
532 buttons: this.OKCANCEL,
537 multiline: multiline,
543 <div id="prop-Ext.MessageBox-OK"></div>/**
544 * Button config that displays a single OK button
548 <div id="prop-Ext.MessageBox-CANCEL"></div>/**
549 * Button config that displays a single Cancel button
552 CANCEL : {cancel:true},
553 <div id="prop-Ext.MessageBox-OKCANCEL"></div>/**
554 * Button config that displays OK and Cancel buttons
557 OKCANCEL : {ok:true, cancel:true},
558 <div id="prop-Ext.MessageBox-YESNO"></div>/**
559 * Button config that displays Yes and No buttons
562 YESNO : {yes:true, no:true},
563 <div id="prop-Ext.MessageBox-YESNOCANCEL"></div>/**
564 * Button config that displays Yes, No and Cancel buttons
567 YESNOCANCEL : {yes:true, no:true, cancel:true},
568 <div id="prop-Ext.MessageBox-INFO"></div>/**
569 * The CSS class that provides the INFO icon image
572 INFO : 'ext-mb-info',
573 <div id="prop-Ext.MessageBox-WARNING"></div>/**
574 * The CSS class that provides the WARNING icon image
577 WARNING : 'ext-mb-warning',
578 <div id="prop-Ext.MessageBox-QUESTION"></div>/**
579 * The CSS class that provides the QUESTION icon image
582 QUESTION : 'ext-mb-question',
583 <div id="prop-Ext.MessageBox-ERROR"></div>/**
584 * The CSS class that provides the ERROR icon image
587 ERROR : 'ext-mb-error',
589 <div id="prop-Ext.MessageBox-defaultTextHeight"></div>/**
590 * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
593 defaultTextHeight : 75,
594 <div id="prop-Ext.MessageBox-maxWidth"></div>/**
595 * The maximum width in pixels of the message box (defaults to 600)
599 <div id="prop-Ext.MessageBox-minWidth"></div>/**
600 * The minimum width in pixels of the message box (defaults to 110)
604 <div id="prop-Ext.MessageBox-minProgressWidth"></div>/**
605 * The minimum width in pixels of the message box if it is a progress-style dialog. This is useful
606 * for setting a different minimum width than text-only dialogs may need (defaults to 250)
609 minProgressWidth : 250,
610 <div id="prop-Ext.MessageBox-buttonText"></div>/**
611 * An object containing the default button text strings that can be overriden for localized language support.
612 * Supported properties are: ok, cancel, yes and no. Generally you should include a locale-specific
613 * resource file for handling language support across the framework.
614 * Customize the default text like so: Ext.MessageBox.buttonText.yes = 'oui'; //french
626 <div id="prop-Ext.MessageBox-Msg"></div>/**
627 * Shorthand for {@link Ext.MessageBox}
629 Ext.Msg = Ext.MessageBox;</pre>
\r