📄 messagebox.js
字号:
/*
* Ext JS Library 2.1
* Copyright(c) 2006-2008, 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], 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);
}
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 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(){
if(this.isVisible()){
dlg.hide();
handleHide();
}
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.
* The following config object properties are supported:
* <ul>
* <li>animEl {String/Element} : An id or Element from which the message box should animate as it
* opens and closes (defaults to undefined)</li>
* <li>buttons {Object/Boolean} : A button config object (e.g., Ext.MessageBox.OKCANCEL or {ok:'Foo',
* cancel:'Bar'}), or false to not show any buttons (defaults to false)</li>
* <li>closable {Boolean} : 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.</li>
* <li>cls {String} : A custom CSS class to apply to the message box's container element</li>
* <li>defaultTextHeight {Number} : The default height in pixels of the message box's multiline textarea
* if displayed (defaults to 75)</li>
* <li>fn {Function} : 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:</p>
* <p>
* <div class="mdetail-params"><ul>
* <li><code>buttonId</code>
* <div class="sub-desc">The ID of the button pressed, one of:<div class="sub-desc"><ul><li><b>ok</b></li><li><b>yes</b></li><li><b>no</b></li><li><b>cancel</b></li></ul></div></div></li>
* <li><code>text</code>
* <div class="sub-desc">Value of input field if <b>prompt</b> or <b>multiline</b> was selected</div></li>
* </ul></div>
* </p></li>
* <li>scope {Object} : The scope of the callback function</li>
* <li>icon {String} : 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 '')</li>
* <li>iconCls {String} : The standard {@link Ext.Window#iconCls} to add an optional header icon (defaults to '')</li>
* <li>maxWidth {Number} : The maximum width in pixels of the message box (defaults to 600)</li>
* <li>minWidth {Number} : The minimum width in pixels of the message box (defaults to 100)</li>
* <li>modal {Boolean} : False to allow user interaction with the page while the message box is
* displayed (defaults to true)</li>
* <li>msg {String} : A string that will replace the existing message box body text (defaults to the
* XHTML-compliant non-breaking space character ' ')</li>
* <li>multiline {Boolean} : True to prompt the user to enter multi-line text (defaults to false)</li>
* <li>progress {Boolean} : True to display a progress bar (defaults to false)</li>
* <li>progressText {String} : The text to display inside the progress bar if progress = true (defaults to '')</li>
* <li>prompt {Boolean} : True to prompt the user to enter single-line text (defaults to false)</li>
* <li>proxyDrag {Boolean} : True to display a lightweight proxy while dragging (defaults to false)</li>
* <li>title {String} : The title text</li>
* <li>value {String} : The string value to set into the active textbox element if displayed</li>
* <li>wait {Boolean} : True to display a progress bar (defaults to false)</li>
* <li>waitConfig {Object} : A {@link Ext.ProgressBar#waitConfig} object (applies only if wait = true)</li>
* <li>width {Number} : The width of the dialog in pixels</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>
* @param {Object} config Configuration options
* @return {Ext.MessageBox} this
*/
show : function(options){
if(this.isVisible()){
this.hide();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -