📄 messagebox.js
字号:
/*
* Ext JS Library 2.2.1
* Copyright(c) 2006-2009, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
/**
* @class Ext.MessageBox
* <p>Utility class for generating different styles of message boxes. The alias Ext.Msg can also be used.<p/>
* <p>Note that the MessageBox is asynchronous. Unlike a regular JavaScript <code>alert</code> (which will halt
* browser execution), showing a MessageBox will not cause the code to stop. For this reason, if you have code
* that should only run <em>after</em> some user feedback from the MessageBox, you must use a callback function
* (see the <code>function</code> parameter for {@link #show} for more details).</p>
* <p>Example usage:</p>
*<pre><code>
// Basic alert:
Ext.Msg.alert('Status', 'Changes saved successfully.');
// Prompt for user data and process the result using a callback:
Ext.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
if (btn == 'ok'){
// process text value and close...
}
});
// Show a dialog using config options:
Ext.Msg.show({
title:'Save Changes?',
msg: 'You are closing a tab that has unsaved changes. Would you like to save your changes?',
buttons: Ext.Msg.YESNOCANCEL,
fn: processResult,
animEl: 'elId',
icon: Ext.MessageBox.QUESTION
});
</code></pre>
* @singleton
*/
Ext.MessageBox = function(){
var dlg, opt, mask, waitTimer;
var bodyEl, msgEl, textboxEl, textareaEl, progressBar, pp, iconEl, spacerEl;
var buttons, activeTextEl, bwidth, iconCls = '';
// private
var handleButton = function(button){
if(dlg.isVisible()){
dlg.hide();
Ext.callback(opt.fn, opt.scope||window, [button, activeTextEl.dom.value, opt], 1);
}
};
// private
var handleHide = function(){
if(opt && opt.cls){
dlg.el.removeClass(opt.cls);
}
progressBar.reset();
};
// private
var handleEsc = function(d, k, e){
if(opt && opt.closable !== false){
dlg.hide();
}
if(e){
e.stopEvent();
}
};
// private
var updateButtons = function(b){
var width = 0;
if(!b){
buttons["ok"].hide();
buttons["cancel"].hide();
buttons["yes"].hide();
buttons["no"].hide();
return width;
}
dlg.footer.dom.style.display = '';
for(var k in buttons){
if(typeof buttons[k] != "function"){
if(b[k]){
buttons[k].show();
buttons[k].setText(typeof b[k] == "string" ? b[k] : Ext.MessageBox.buttonText[k]);
width += buttons[k].el.getWidth()+15;
}else{
buttons[k].hide();
}
}
}
return width;
};
return {
/**
* Returns a reference to the underlying {@link Ext.Window} element
* @return {Ext.Window} The window
*/
getDialog : function(titleText){
if(!dlg){
dlg = new Ext.Window({
autoCreate : true,
title:titleText,
resizable:false,
constrain:true,
constrainHeader:true,
minimizable : false,
maximizable : false,
stateful: false,
modal: true,
shim:true,
buttonAlign:"center",
width:400,
height:100,
minHeight: 80,
plain:true,
footer:true,
closable:true,
close : function(){
if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
handleButton("no");
}else{
handleButton("cancel");
}
}
});
buttons = {};
var bt = this.buttonText;
//TODO: refactor this block into a buttons config to pass into the Window constructor
buttons["ok"] = dlg.addButton(bt["ok"], handleButton.createCallback("ok"));
buttons["yes"] = dlg.addButton(bt["yes"], handleButton.createCallback("yes"));
buttons["no"] = dlg.addButton(bt["no"], handleButton.createCallback("no"));
buttons["cancel"] = dlg.addButton(bt["cancel"], handleButton.createCallback("cancel"));
buttons["ok"].hideMode = buttons["yes"].hideMode = buttons["no"].hideMode = buttons["cancel"].hideMode = 'offsets';
dlg.render(document.body);
dlg.getEl().addClass('x-window-dlg');
mask = dlg.mask;
bodyEl = dlg.body.createChild({
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>'
});
iconEl = Ext.get(bodyEl.dom.firstChild);
var contentEl = bodyEl.dom.childNodes[1];
msgEl = Ext.get(contentEl.firstChild);
textboxEl = Ext.get(contentEl.childNodes[2].firstChild);
textboxEl.enableDisplayMode();
textboxEl.addKeyListener([10,13], function(){
if(dlg.isVisible() && opt && opt.buttons){
if(opt.buttons.ok){
handleButton("ok");
}else if(opt.buttons.yes){
handleButton("yes");
}
}
});
textareaEl = Ext.get(contentEl.childNodes[2].childNodes[1]);
textareaEl.enableDisplayMode();
progressBar = new Ext.ProgressBar({
renderTo:bodyEl
});
bodyEl.createChild({cls:'x-clear'});
}
return dlg;
},
/**
* Updates the message box body text
* @param {String} text (optional) Replaces the message box element's innerHTML with the specified string (defaults to
* the XHTML-compliant non-breaking space character '&#160;')
* @return {Ext.MessageBox} this
*/
updateText : function(text){
if(!dlg.isVisible() && !opt.width){
dlg.setSize(this.maxWidth, 100); // resize first so content is never clipped from previous shows
}
msgEl.update(text || ' ');
var iw = iconCls != '' ? (iconEl.getWidth() + iconEl.getMargins('lr')) : 0;
var mw = msgEl.getWidth() + msgEl.getMargins('lr');
var fw = dlg.getFrameWidth('lr');
var bw = dlg.body.getFrameWidth('lr');
if (Ext.isIE && iw > 0){
//3 pixels get subtracted in the icon CSS for an IE margin issue,
//so we have to add it back here for the overall width to be consistent
iw += 3;
}
var w = Math.max(Math.min(opt.width || iw+mw+fw+bw, this.maxWidth),
Math.max(opt.minWidth || this.minWidth, bwidth || 0));
if(opt.prompt === true){
activeTextEl.setWidth(w-iw-fw-bw);
}
if(opt.progress === true || opt.wait === true){
progressBar.setSize(w-iw-fw-bw);
}
if(Ext.isIE && w == bwidth){
w += 4; //Add offset when the content width is smaller than the buttons.
}
dlg.setSize(w, 'auto').center();
return this;
},
/**
* Updates a progress-style message box's text and progress bar. Only relevant on message boxes
* initiated via {@link Ext.MessageBox#progress} or {@link Ext.MessageBox#wait},
* or by calling {@link Ext.MessageBox#show} with progress: true.
* @param {Number} value Any number between 0 and 1 (e.g., .5, defaults to 0)
* @param {String} progressText The progress text to display inside the progress bar (defaults to '')
* @param {String} msg The message box's body text is replaced with the specified string (defaults to undefined
* so that any existing body text will not get overwritten by default unless a new value is passed in)
* @return {Ext.MessageBox} this
*/
updateProgress : function(value, progressText, msg){
progressBar.updateProgress(value, progressText);
if(msg){
this.updateText(msg);
}
return this;
},
/**
* Returns true if the message box is currently displayed
* @return {Boolean} True if the message box is visible, else false
*/
isVisible : function(){
return dlg && dlg.isVisible();
},
/**
* Hides the message box if it is displayed
* @return {Ext.MessageBox} this
*/
hide : function(){
var proxy = dlg.activeGhost;
if(this.isVisible() || proxy) {
dlg.hide();
handleHide();
if (proxy) {
proxy.hide();
}
}
return this;
},
/**
* Displays a new message box, or reinitializes an existing message box, based on the config options
* passed in. All display functions (e.g. prompt, alert, etc.) on MessageBox call this function internally,
* although those calls are basic shortcuts and do not support all of the config options allowed here.
* @param {Object} config The following config options are supported: <ul>
* <li><b>animEl</b> : String/Element<div class="sub-desc">An id or Element from which the message box should animate as it
* opens and closes (defaults to undefined)</div></li>
* <li><b>buttons</b> : Object/Boolean<div class="sub-desc">A button config object (e.g., Ext.MessageBox.OKCANCEL or {ok:'Foo',
* cancel:'Bar'}), or false to not show any buttons (defaults to false)</div></li>
* <li><b>closable</b> : Boolean<div class="sub-desc">False to hide the top-right close button (defaults to true). Note that
* progress and wait dialogs will ignore this property and always hide the close button as they can only
* be closed programmatically.</div></li>
* <li><b>cls</b> : String<div class="sub-desc">A custom CSS class to apply to the message box's container element</div></li>
* <li><b>defaultTextHeight</b> : Number<div class="sub-desc">The default height in pixels of the message box's multiline textarea
* if displayed (defaults to 75)</div></li>
* <li><b>fn</b> : Function<div class="sub-desc">A callback function which is called when the dialog is dismissed either
* by clicking on the configured buttons, or on the dialog close button, or by pressing
* the return button to enter input.
* <p>Progress and wait dialogs will ignore this option since they do not respond to user
* actions and can only be closed programmatically, so any required function should be called
* by the same code after it closes the dialog. Parameters passed:<ul>
* <li><b>buttonId</b> : String<div class="sub-desc">The ID of the button pressed, one of:<div class="sub-desc"><ul>
* <li><tt>ok</tt></li>
* <li><tt>yes</tt></li>
* <li><tt>no</tt></li>
* <li><tt>cancel</tt></li>
* </ul></div></div></li>
* <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>
* or <tt><a href="#show-option-multiline" ext:member="show-option-multiline" ext:cls="Ext.MessageBox">multiline</a></tt> is true</div></li>
* <li><b>opt</b> : Object<div class="sub-desc">The config object passed to show.</div></li>
* </ul></p></div></li>
* <li><b>scope</b> : Object<div class="sub-desc">The scope of the callback function</div></li>
* <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
* dialog (e.g. Ext.MessageBox.WARNING or 'custom-class') (defaults to '')</div></li>
* <li><b>iconCls</b> : String<div class="sub-desc">The standard {@link Ext.Window#iconCls} to
* add an optional header icon (defaults to '')</div></li>
* <li><b>maxWidth</b> : Number<div class="sub-desc">The maximum width in pixels of the message box (defaults to 600)</div></li>
* <li><b>minWidth</b> : Number<div class="sub-desc">The minimum width in pixels of the message box (defaults to 100)</div></li>
* <li><b>modal</b> : Boolean<div class="sub-desc">False to allow user interaction with the page while the message box is
* displayed (defaults to true)</div></li>
* <li><b>msg</b> : String<div class="sub-desc">A string that will replace the existing message box body text (defaults to the
* XHTML-compliant non-breaking space character '&#160;')</div></li>
* <li><a id="show-option-multiline"></a><b>multiline</b> : Boolean<div class="sub-desc">
* True to prompt the user to enter multi-line text (defaults to false)</div></li>
* <li><b>progress</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
* <li><b>progressText</b> : String<div class="sub-desc">The text to display inside the progress bar if progress = true (defaults to '')</div></li>
* <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>
* <li><b>proxyDrag</b> : Boolean<div class="sub-desc">True to display a lightweight proxy while dragging (defaults to false)</div></li>
* <li><b>title</b> : String<div class="sub-desc">The title text</div></li>
* <li><b>value</b> : String<div class="sub-desc">The string value to set into the active textbox element if displayed</div></li>
* <li><b>wait</b> : Boolean<div class="sub-desc">True to display a progress bar (defaults to false)</div></li>
* <li><b>waitConfig</b> : Object<div class="sub-desc">A {@link Ext.ProgressBar#waitConfig} object (applies only if wait = true)</div></li>
* <li><b>width</b> : Number<div class="sub-desc">The width of the dialog in pixels</div></li>
* </ul>
* Example usage:
* <pre><code>
Ext.Msg.show({
title: 'Address',
msg: 'Please enter your address:',
width: 300,
buttons: Ext.MessageBox.OKCANCEL,
multiline: true,
fn: saveAddress,
animEl: 'addAddressBtn',
icon: Ext.MessageBox.INFO
});
</code></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -