4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-window-MessageBox'>/**
19 </span> * @class Ext.window.MessageBox
20 * @extends Ext.window.Window
22 Utility class for generating different styles of message boxes. The singleton instance, `Ext.Msg` can also be used.
23 Note that a MessageBox is asynchronous. Unlike a regular JavaScript `alert` (which will halt
24 browser execution), showing a MessageBox will not cause the code to stop. For this reason, if you have code
25 that should only run *after* some user feedback from the MessageBox, you must use a callback function
26 (see the `function` parameter for {@link #show} for more details).
28 {@img Ext.window.MessageBox/messagebox1.png alert MessageBox}
29 {@img Ext.window.MessageBox/messagebox2.png prompt MessageBox}
30 {@img Ext.window.MessageBox/messagebox3.png show MessageBox}
34 Ext.Msg.alert('Status', 'Changes saved successfully.');
36 // Prompt for user data and process the result using a callback:
37 Ext.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
39 // process text value and close...
43 // Show a dialog using config options:
45 title:'Save Changes?',
46 msg: 'You are closing a tab that has unsaved changes. Would you like to save your changes?',
47 buttons: Ext.Msg.YESNOCANCEL,
49 animateTarget: 'elId',
50 icon: Ext.window.MessageBox.QUESTION
56 Ext.define('Ext.window.MessageBox', {
57 extend: 'Ext.window.Window',
60 'Ext.toolbar.Toolbar',
61 'Ext.form.field.Text',
62 'Ext.form.field.TextArea',
64 'Ext.layout.container.Anchor',
65 'Ext.layout.container.HBox',
69 alternateClassName: 'Ext.MessageBox',
71 alias: 'widget.messagebox',
73 <span id='Ext-window-MessageBox-property-OK'> /**
74 </span> * Button config that displays a single OK button
78 <span id='Ext-window-MessageBox-property-YES'> /**
79 </span> * Button config that displays a single Yes button
83 <span id='Ext-window-MessageBox-property-NO'> /**
84 </span> * Button config that displays a single No button
88 <span id='Ext-window-MessageBox-property-CANCEL'> /**
89 </span> * Button config that displays a single Cancel button
93 <span id='Ext-window-MessageBox-property-OKCANCEL'> /**
94 </span> * Button config that displays OK and Cancel buttons
98 <span id='Ext-window-MessageBox-property-YESNO'> /**
99 </span> * Button config that displays Yes and No buttons
103 <span id='Ext-window-MessageBox-property-YESNOCANCEL'> /**
104 </span> * Button config that displays Yes, No and Cancel buttons
108 <span id='Ext-window-MessageBox-property-INFO'> /**
109 </span> * The CSS class that provides the INFO icon image
112 INFO : 'ext-mb-info',
113 <span id='Ext-window-MessageBox-property-WARNING'> /**
114 </span> * The CSS class that provides the WARNING icon image
117 WARNING : 'ext-mb-warning',
118 <span id='Ext-window-MessageBox-property-QUESTION'> /**
119 </span> * The CSS class that provides the QUESTION icon image
122 QUESTION : 'ext-mb-question',
123 <span id='Ext-window-MessageBox-property-ERROR'> /**
124 </span> * The CSS class that provides the ERROR icon image
127 ERROR : 'ext-mb-error',
129 // hide it by offsets. Windows are hidden on render by default.
143 cls: Ext.baseCSSPrefix + 'message-box',
149 <span id='Ext-window-MessageBox-property-defaultTextHeight'> /**
150 </span> * The default height in pixels of the message box's multiline textarea if displayed (defaults to 75)
153 defaultTextHeight : 75,
154 <span id='Ext-window-MessageBox-property-minProgressWidth'> /**
155 </span> * The minimum width in pixels of the message box if it is a progress-style dialog. This is useful
156 * for setting a different minimum width than text-only dialogs may need (defaults to 250).
159 minProgressWidth : 250,
160 <span id='Ext-window-MessageBox-property-minPromptWidth'> /**
161 </span> * The minimum width in pixels of the message box if it is a prompt dialog. This is useful
162 * for setting a different minimum width than text-only dialogs may need (defaults to 250).
166 <span id='Ext-window-MessageBox-property-buttonText'> /**
167 </span> * An object containing the default button text strings that can be overriden for localized language support.
168 * Supported properties are: ok, cancel, yes and no. Generally you should include a locale-specific
169 * resource file for handling language support across the framework.
170 * Customize the default text like so: Ext.window.MessageBox.buttonText.yes = "oui"; //french
181 'ok', 'yes', 'no', 'cancel'
193 makeButton: function(btnIdx) {
194 var btnId = this.buttonIds[btnIdx];
195 return Ext.create('Ext.button.Button', {
196 handler: this.btnCallback,
199 text: this.buttonText[btnId],
204 btnCallback: function(btn) {
209 if (me.cfg.prompt || me.cfg.multiline) {
210 if (me.cfg.multiline) {
213 field = me.textField;
215 value = field.getValue();
219 // Important not to have focus remain in the hidden Window; Interferes with DnD.
222 me.userCallback(btn.itemId, value, me.cfg);
228 me.progressBar.reset();
229 me.removeCls(me.cfg.cls);
233 initComponent: function() {
237 me.title = '&#160;';
239 me.topContainer = Ext.create('Ext.container.Container', {
246 me.iconComponent = Ext.create('Ext.Component', {
249 height: me.iconHeight,
254 me.promptContainer = Ext.create('Ext.container.Container', {
259 me.msg = Ext.create('Ext.Component', {
260 autoEl: { tag: 'span' },
263 me.textField = Ext.create('Ext.form.field.Text', {
265 enableKeyEvents: true,
267 keydown: me.onPromptKey,
271 me.textArea = Ext.create('Ext.form.field.TextArea', {
279 me.progressBar = Ext.create('Ext.ProgressBar', {
281 style: 'margin-left:10px'
284 me.items = [me.topContainer, me.progressBar];
286 // Create the buttons based upon passed bitwise config
288 for (i = 0; i < 4; i++) {
289 button = me.makeButton(i);
290 me.msgButtons[button.itemId] = button;
291 me.msgButtons.push(button);
293 me.bottomTb = Ext.create('Ext.toolbar.Toolbar', {
306 me.dockedItems = [me.bottomTb];
311 onPromptKey: function(textField, e) {
315 if (e.keyCode === Ext.EventObject.RETURN || e.keyCode === 10) {
316 if (me.msgButtons.ok.isVisible()) {
318 me.msgButtons.ok.handler.call(me, me.msgButtons.ok);
319 } else if (me.msgButtons.yes.isVisible()) {
320 me.msgButtons.yes.handler.call(me, me.msgButtons.yes);
330 reconfigure: function(cfg) {
332 buttons = cfg.buttons || 0,
334 initialWidth = me.maxWidth,
340 initialWidth = cfg.width;
343 // Default to allowing the Window to take focus.
344 delete me.defaultFocus;
346 // clear any old animateTarget
347 me.animateTarget = cfg.animateTarget || undefined;
350 me.modal = cfg.modal !== false;
354 me.setTitle(cfg.title||'&#160;');
358 me.width = initialWidth;
359 me.render(Ext.getBody());
362 me.setSize(initialWidth, me.maxHeight);
364 me.setPosition(-10000, -10000);
366 // Hide or show the close tool
367 me.closable = cfg.closable && !cfg.wait;
368 if (cfg.closable === false) {
369 me.tools.close.hide();
371 me.tools.close.show();
374 // Hide or show the header
375 if (!cfg.title && !me.closable) {
381 // Default to dynamic drag: drag the window, not a ghost
382 me.liveDrag = !cfg.proxyDrag;
384 // wrap the user callback
385 me.userCallback = Ext.Function.bind(cfg.callback ||cfg.fn || Ext.emptyFn, cfg.scope || Ext.global);
387 // Hide or show the icon Component
388 me.setIcon(cfg.icon);
390 // Hide or show the message area
392 me.msg.update(cfg.msg);
398 // Hide or show the input field
399 if (cfg.prompt || cfg.multiline) {
400 me.multiline = cfg.multiline;
402 me.textArea.setValue(cfg.value);
403 me.textArea.setHeight(cfg.defaultTextHeight || me.defaultTextHeight);
406 me.defaultFocus = me.textArea;
408 me.textField.setValue(cfg.value);
411 me.defaultFocus = me.textField;
418 // Hide or show the progress bar
419 if (cfg.progress || cfg.wait) {
420 me.progressBar.show();
421 me.updateProgress(0, cfg.progressText);
422 if(cfg.wait === true){
423 me.progressBar.wait(cfg.waitConfig);
426 me.progressBar.hide();
429 // Hide or show buttons depending on flag value sent.
430 for (i = 0; i < 4; i++) {
431 if (buttons & Math.pow(2, i)) {
433 // Default to focus on the first visible button if focus not already set
434 if (!me.defaultFocus) {
435 me.defaultFocus = me.msgButtons[i];
437 me.msgButtons[i].show();
440 me.msgButtons[i].hide();
444 // Hide toolbar if no buttons to show
453 <span id='Ext-window-MessageBox-method-show'> /**
454 </span> * Displays a new message box, or reinitializes an existing message box, based on the config options
455 * passed in. All display functions (e.g. prompt, alert, etc.) on MessageBox call this function internally,
456 * although those calls are basic shortcuts and do not support all of the config options allowed here.
457 * @param {Object} config The following config options are supported: <ul>
458 * <li><b>animateTarget</b> : String/Element<div class="sub-desc">An id or Element from which the message box should animate as it
459 * opens and closes (defaults to undefined)</div></li>
460 * <li><b>buttons</b> : Number<div class="sub-desc">A bitwise button specifier consisting of the sum of any of the following constants:<ul>
461 * <li>Ext.window.MessageBox.OK</li>
462 * <li>Ext.window.MessageBox.YES</li>
463 * <li>Ext.window.MessageBox.NO</li>
464 * <li>Ext.window.MessageBox.CANCEL</li>
465 * </ul>Or false to not show any buttons (defaults to false)</div></li>
466 * <li><b>closable</b> : Boolean<div class="sub-desc">False to hide the top-right close button (defaults to true). Note that
467 * progress and wait dialogs will ignore this property and always hide the close button as they can only
468 * be closed programmatically.</div></li>
469 * <li><b>cls</b> : String<div class="sub-desc">A custom CSS class to apply to the message box's container element</div></li>
470 * <li><b>defaultTextHeight</b> : Number<div class="sub-desc">The default height in pixels of the message box's multiline textarea
471 * if displayed (defaults to 75)</div></li>
472 * <li><b>fn</b> : Function<div class="sub-desc">A callback function which is called when the dialog is dismissed either
473 * by clicking on the configured buttons, or on the dialog close button, or by pressing
474 * the return button to enter input.
475 * <p>Progress and wait dialogs will ignore this option since they do not respond to user
476 * actions and can only be closed programmatically, so any required function should be called
477 * by the same code after it closes the dialog. Parameters passed:<ul>
478 * <li><b>buttonId</b> : String<div class="sub-desc">The ID of the button pressed, one of:<div class="sub-desc"><ul>
479 * <li><tt>ok</tt></li>
480 * <li><tt>yes</tt></li>
481 * <li><tt>no</tt></li>
482 * <li><tt>cancel</tt></li>
483 * </ul></div></div></li>
484 * <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.window.MessageBox">prompt</a></tt>
485 * or <tt><a href="#show-option-multiline" ext:member="show-option-multiline" ext:cls="Ext.window.MessageBox">multiline</a></tt> is true</div></li>
486 * <li><b>opt</b> : Object<div class="sub-desc">The config object passed to show.</div></li>
487 * </ul></p></div></li>
488 * <li><b>scope</b> : Object<div class="sub-desc">The scope (<code>this</code> reference) in which the function will be executed.</div></li>
489 * <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
490 * dialog (e.g. Ext.window.MessageBox.WARNING or 'custom-class') (defaults to '')</div></li>
491 * <li><b>iconCls</b> : String<div class="sub-desc">The standard {@link Ext.window.Window#iconCls} to
492 * add an optional header icon (defaults to '')</div></li>
493 * <li><b>maxWidth</b> : Number<div class="sub-desc">The maximum width in pixels of the message box (defaults to 600)</div></li>
494 * <li><b>minWidth</b> : Number<div class="sub-desc">The minimum width in pixels of the message box (defaults to 100)</div></li>
495 * <li><b>modal</b> : Boolean<div class="sub-desc">False to allow user interaction with the page while the message box is
496 * displayed (defaults to true)</div></li>
497 * <li><b>msg</b> : String<div class="sub-desc">A string that will replace the existing message box body text (defaults to the
498 * XHTML-compliant non-breaking space character '&amp;#160;')</div></li>
499 * <li><a id="show-option-multiline"></a><b>multiline</b> : Boolean<div class="sub-desc">
500 * True to prompt the user to enter multi-line text (defaults to false)</div></li>
501 * <li><b>progress</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
502 * <li><b>progressText</b> : String<div class="sub-desc">The text to display inside the progress bar if progress = true (defaults to '')</div></li>
503 * <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>
504 * <li><b>proxyDrag</b> : Boolean<div class="sub-desc">True to display a lightweight proxy while dragging (defaults to false)</div></li>
505 * <li><b>title</b> : String<div class="sub-desc">The title text</div></li>
506 * <li><b>value</b> : String<div class="sub-desc">The string value to set into the active textbox element if displayed</div></li>
507 * <li><b>wait</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
508 * <li><b>waitConfig</b> : Object<div class="sub-desc">A {@link Ext.ProgressBar#waitConfig} object (applies only if wait = true)</div></li>
509 * <li><b>width</b> : Number<div class="sub-desc">The width of the dialog in pixels</div></li>
512 * <pre><code>
515 msg: 'Please enter your address:',
517 buttons: Ext.window.MessageBox.OKCANCEL,
520 animateTarget: 'addAddressBtn',
521 icon: Ext.window.MessageBox.INFO
523 </code></pre>
524 * @return {Ext.window.MessageBox} this
526 show: function(cfg) {
531 if (cfg.animateTarget) {
532 me.doAutoSize(false);
541 afterShow: function(){
542 if (this.animateTarget) {
545 this.callParent(arguments);
548 doAutoSize: function(center) {
550 icon = me.iconComponent,
551 iconHeight = me.iconHeight;
553 if (!Ext.isDefined(me.frameWidth)) {
554 me.frameWidth = me.el.getWidth() - me.body.getWidth();
557 // reset to the original dimensions
558 icon.setHeight(iconHeight);
560 // Allow per-invocation override of minWidth
561 me.minWidth = me.cfg.minWidth || Ext.getClass(this).prototype.minWidth;
563 // Set best possible size based upon allowing the text to wrap in the maximized Window, and
564 // then constraining it to within the max with. Then adding up constituent element heights.
565 me.topContainer.doLayout();
566 if (Ext.isIE6 || Ext.isIEQuirks) {
567 // In IE quirks, the initial full width of the prompt fields will prevent the container element
568 // from collapsing once sized down, so temporarily force them to a small width. They'll get
569 // layed out to their final width later when setting the final window size.
570 me.textField.setCalculatedSize(9);
571 me.textArea.setCalculatedSize(9);
573 var width = me.cfg.width || me.msg.getWidth() + icon.getWidth() + 25, /* topContainer's layout padding */
574 height = (me.header.rendered ? me.header.getHeight() : 0) +
575 Math.max(me.promptContainer.getHeight(), icon.getHeight()) +
576 me.progressBar.getHeight() +
577 (me.bottomTb.rendered ? me.bottomTb.getHeight() : 0) + 20 ;/* topContainer's layout padding */
579 // Update to the size of the content, this way the text won't wrap under the icon.
580 icon.setHeight(Math.max(iconHeight, me.msg.getHeight()));
581 me.setSize(width + me.frameWidth, height + me.frameWidth);
588 updateText: function(text) {
589 this.msg.update(text);
590 return this.doAutoSize(true);
593 <span id='Ext-window-MessageBox-method-setIcon'> /**
594 </span> * Adds the specified icon to the dialog. By default, the class 'ext-mb-icon' is applied for default
595 * styling, and the class passed in is expected to supply the background image url. Pass in empty string ('')
596 * to clear any existing icon. This method must be called before the MessageBox is shown.
597 * The following built-in icon classes are supported, but you can also pass in a custom class name:
599 Ext.window.MessageBox.INFO
600 Ext.window.MessageBox.WARNING
601 Ext.window.MessageBox.QUESTION
602 Ext.window.MessageBox.ERROR
604 * @param {String} icon A CSS classname specifying the icon's background image url, or empty string to clear the icon
605 * @return {Ext.window.MessageBox} this
607 setIcon : function(icon) {
609 me.iconComponent.removeCls(me.iconCls);
611 me.iconComponent.show();
612 me.iconComponent.addCls(Ext.baseCSSPrefix + 'dlg-icon');
613 me.iconComponent.addCls(me.iconCls = icon);
615 me.iconComponent.removeCls(Ext.baseCSSPrefix + 'dlg-icon');
616 me.iconComponent.hide();
621 <span id='Ext-window-MessageBox-method-updateProgress'> /**
622 </span> * Updates a progress-style message box's text and progress bar. Only relevant on message boxes
623 * initiated via {@link Ext.window.MessageBox#progress} or {@link Ext.window.MessageBox#wait},
624 * or by calling {@link Ext.window.MessageBox#show} with progress: true.
625 * @param {Number} value Any number between 0 and 1 (e.g., .5, defaults to 0)
626 * @param {String} progressText The progress text to display inside the progress bar (defaults to '')
627 * @param {String} msg The message box's body text is replaced with the specified string (defaults to undefined
628 * so that any existing body text will not get overwritten by default unless a new value is passed in)
629 * @return {Ext.window.MessageBox} this
631 updateProgress : function(value, progressText, msg){
632 this.progressBar.updateProgress(value, progressText);
634 this.updateText(msg);
640 if (this.closable !== false) {
641 this.callParent(arguments);
645 <span id='Ext-window-MessageBox-method-confirm'> /**
646 </span> * Displays a confirmation message box with Yes and No buttons (comparable to JavaScript's confirm).
647 * If a callback function is passed it will be called after the user clicks either button,
648 * and the id of the button that was clicked will be passed as the only parameter to the callback
649 * (could also be the top-right close button).
650 * @param {String} title The title bar text
651 * @param {String} msg The message box body text
652 * @param {Function} fn (optional) The callback function invoked after the message box is closed
653 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser wnidow.
654 * @return {Ext.window.MessageBox} this
656 confirm: function(cfg, msg, fn, scope) {
657 if (Ext.isString(cfg)) {
660 icon: 'ext-mb-question',
667 return this.show(cfg);
670 <span id='Ext-window-MessageBox-method-prompt'> /**
671 </span> * Displays a message box with OK and Cancel buttons prompting the user to enter some text (comparable to JavaScript's prompt).
672 * The prompt can be a single-line or multi-line textbox. If a callback function is passed it will be called after the user
673 * clicks either button, and the id of the button that was clicked (could also be the top-right
674 * close button) and the text that was entered will be passed as the two parameters to the callback.
675 * @param {String} title The title bar text
676 * @param {String} msg The message box body text
677 * @param {Function} fn (optional) The callback function invoked after the message box is closed
678 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser wnidow.
679 * @param {Boolean/Number} multiline (optional) True to create a multiline textbox using the defaultTextHeight
680 * property, or the height in pixels to create the textbox (defaults to false / single-line)
681 * @param {String} value (optional) Default value of the text input element (defaults to '')
682 * @return {Ext.window.MessageBox} this
684 prompt : function(cfg, msg, fn, scope, multiline, value){
685 if (Ext.isString(cfg)) {
689 minWidth: this.minPromptWidth,
691 buttons: this.OKCANCEL,
694 multiline: multiline,
698 return this.show(cfg);
701 <span id='Ext-window-MessageBox-method-wait'> /**
702 </span> * Displays a message box with an infinitely auto-updating progress bar. This can be used to block user
703 * interaction while waiting for a long-running process to complete that does not have defined intervals.
704 * You are responsible for closing the message box when the process is complete.
705 * @param {String} msg The message box body text
706 * @param {String} title (optional) The title bar text
707 * @param {Object} config (optional) A {@link Ext.ProgressBar#waitConfig} object
708 * @return {Ext.window.MessageBox} this
710 wait : function(cfg, title, config){
711 if (Ext.isString(cfg)) {
718 minWidth: this.minProgressWidth,
722 return this.show(cfg);
725 <span id='Ext-window-MessageBox-method-alert'> /**
726 </span> * Displays a standard read-only message box with an OK button (comparable to the basic JavaScript alert prompt).
727 * If a callback function is passed it will be called after the user clicks the button, and the
728 * id of the button that was clicked will be passed as the only parameter to the callback
729 * (could also be the top-right close button).
730 * @param {String} title The title bar text
731 * @param {String} msg The message box body text
732 * @param {Function} fn (optional) The callback function invoked after the message box is closed
733 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser wnidow.
734 * @return {Ext.window.MessageBox} this
736 alert: function(cfg, msg, fn, scope) {
737 if (Ext.isString(cfg)) {
744 minWidth: this.minWidth
747 return this.show(cfg);
750 <span id='Ext-window-MessageBox-method-progress'> /**
751 </span> * Displays a message box with a progress bar. This message box has no buttons and is not closeable by
752 * the user. You are responsible for updating the progress bar as needed via {@link Ext.window.MessageBox#updateProgress}
753 * and closing the message box when the process is complete.
754 * @param {String} title The title bar text
755 * @param {String} msg The message box body text
756 * @param {String} progressText (optional) The text to display inside the progress bar (defaults to '')
757 * @return {Ext.window.MessageBox} this
759 progress : function(cfg, msg, progressText){
760 if (Ext.isString(cfg)) {
764 progressText: progressText
767 return this.show(cfg);
770 Ext.MessageBox = Ext.Msg = new this();