📄 basicdialog.js
字号:
/*
* Ext JS Library 2.0.2
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
/**
* @class Ext.BasicDialog
* @extends Ext.util.Observable
* Lightweight Dialog Class. The code below shows the creation of a typical dialog using existing HTML markup:
* <pre><code>
var dlg = new Ext.BasicDialog("my-dlg", {
height: 200,
width: 300,
minHeight: 100,
minWidth: 150,
modal: true,
proxyDrag: true,
shadow: true
});
dlg.addKeyListener(27, dlg.hide, dlg); // ESC can also close the dialog
dlg.addButton('OK', dlg.hide, dlg); // Could call a save function instead of hiding
dlg.addButton('Cancel', dlg.hide, dlg);
dlg.show();
</code></pre>
<b>A Dialog should always be a direct child of the body element.</b>
* @cfg {Boolean/DomHelper} autoCreate True to auto create from scratch, or using a DomHelper Object (defaults to false)
* @cfg {String} title Default text to display in the title bar (defaults to null)
* @cfg {Number} width Width of the dialog in pixels (can also be set via CSS). Determined by browser if unspecified.
* @cfg {Number} height Height of the dialog in pixels (can also be set via CSS). Determined by browser if unspecified.
* @cfg {Number} x The default left page coordinate of the dialog (defaults to center screen)
* @cfg {Number} y The default top page coordinate of the dialog (defaults to center screen)
* @cfg {String/Element} animateTarget Id or element from which the dialog should animate while opening
* (defaults to null with no animation)
* @cfg {Boolean} resizable False to disable manual dialog resizing (defaults to true)
* @cfg {String} resizeHandles Which resize handles to display - see the {@link Ext.Resizable} handles config
* property for valid values (defaults to 'all')
* @cfg {Number} minHeight The minimum allowable height for a resizable dialog (defaults to 80)
* @cfg {Number} minWidth The minimum allowable width for a resizable dialog (defaults to 200)
* @cfg {Boolean} modal True to show the dialog modally, preventing user interaction with the rest of the page (defaults to false)
* @cfg {Boolean} autoScroll True to allow the dialog body contents to overflow and display scrollbars (defaults to false)
* @cfg {Boolean} closable False to remove the built-in top-right corner close button (defaults to true)
* @cfg {Boolean} collapsible False to remove the built-in top-right corner collapse button (defaults to true)
* @cfg {Boolean} constraintoviewport True to keep the dialog constrained within the visible viewport boundaries (defaults to true)
* @cfg {Boolean} syncHeightBeforeShow True to cause the dimensions to be recalculated before the dialog is shown (defaults to false)
* @cfg {Boolean} draggable False to disable dragging of the dialog within the viewport (defaults to true)
* @cfg {Boolean} autoTabs If true, all elements with class 'x-dlg-tab' will get automatically converted to tabs (defaults to false)
* @cfg {String} tabTag The tag name of tab elements, used when autoTabs = true (defaults to 'div')
* @cfg {Boolean} proxyDrag True to drag a lightweight proxy element rather than the dialog itself, used when
* draggable = true (defaults to false)
* @cfg {Boolean} fixedcenter True to ensure that anytime the dialog is shown or resized it gets centered (defaults to false)
* @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop" for bottom-right
* shadow (defaults to false)
* @cfg {Number} shadowOffset The number of pixels to offset the shadow if displayed (defaults to 5)
* @cfg {String} buttonAlign Valid values are "left," "center" and "right" (defaults to "right")
* @cfg {Number} minButtonWidth Minimum width of all dialog buttons (defaults to 75)
* @cfg {Boolean} shim True to create an iframe shim that prevents selects from showing through (defaults to false)
* @constructor
* Create a new BasicDialog.
* @param {Mixed} el The container element or DOM node, or its id
* @param {Object} config Configuration options
*/
Ext.BasicDialog = function(el, config){
this.el = Ext.get(el);
var dh = Ext.DomHelper;
if(!this.el && config && config.autoCreate){
if(typeof config.autoCreate == "object"){
if(!config.autoCreate.id){
config.autoCreate.id = el;
}
this.el = dh.append(document.body,
config.autoCreate, true);
}else{
this.el = dh.append(document.body,
{tag: "div", id: el, style:'visibility:hidden;'}, true);
}
}
el = this.el;
el.setDisplayed(true);
el.hide = this.hideAction;
this.id = el.id;
el.addClass("x-dlg");
Ext.apply(this, config);
this.proxy = el.createProxy("x-dlg-proxy");
this.proxy.hide = this.hideAction;
this.proxy.setOpacity(.5);
this.proxy.hide();
if(config.width){
el.setWidth(config.width);
}
if(config.height){
el.setHeight(config.height);
}
this.size = el.getSize();
if(typeof config.x != "undefined" && typeof config.y != "undefined"){
this.xy = [config.x,config.y];
}else{
this.xy = el.getCenterXY(true);
}
/** The header element @type Ext.Element */
this.header = el.child("> .x-dlg-hd");
/** The body element @type Ext.Element */
this.body = el.child("> .x-dlg-bd");
/** The footer element @type Ext.Element */
this.footer = el.child("> .x-dlg-ft");
if(!this.header){
this.header = el.createChild({tag: "div", cls:"x-dlg-hd", html: " "}, this.body ? this.body.dom : null);
}
if(!this.body){
this.body = el.createChild({tag: "div", cls:"x-dlg-bd"});
}
this.header.unselectable();
if(this.title){
this.header.update(this.title);
}
// this element allows the dialog to be focused for keyboard event
this.focusEl = el.createChild({tag: "a", href:"#", cls:"x-dlg-focus", tabIndex:"-1"});
this.focusEl.swallowEvent("click", true);
this.header.wrap({cls:"x-dlg-hd-right"}).wrap({cls:"x-dlg-hd-left"}, true);
// wrap the body and footer for special rendering
this.bwrap = this.body.wrap({tag: "div", cls:"x-dlg-dlg-body"});
if(this.footer){
this.bwrap.dom.appendChild(this.footer.dom);
}
this.bg = this.el.createChild({
tag: "div", cls:"x-dlg-bg",
html: '<div class="x-dlg-bg-left"><div class="x-dlg-bg-right"><div class="x-dlg-bg-center"> </div></div></div>'
});
this.centerBg = this.bg.child("div.x-dlg-bg-center");
if(this.autoScroll !== false && !this.autoTabs){
this.body.setStyle("overflow", "auto");
}
this.toolbox = this.el.createChild({cls: "x-dlg-toolbox"});
if(this.closable !== false){
this.el.addClass("x-dlg-closable");
this.close = this.toolbox.createChild({cls:"x-dlg-close"});
this.close.on("click", this.closeClick, this);
this.close.addClassOnOver("x-dlg-close-over");
}
if(this.collapsible !== false){
this.collapseBtn = this.toolbox.createChild({cls:"x-dlg-collapse"});
this.collapseBtn.on("click", this.collapseClick, this);
this.collapseBtn.addClassOnOver("x-dlg-collapse-over");
this.header.on("dblclick", this.collapseClick, this);
}
if(this.resizable !== false){
this.el.addClass("x-dlg-resizable");
this.resizer = new Ext.Resizable(el, {
minWidth: this.minWidth || 80,
minHeight:this.minHeight || 80,
handles: this.resizeHandles || "all",
pinned: true
});
this.resizer.on("beforeresize", this.beforeResize, this);
this.resizer.on("resize", this.onResize, this);
}
if(this.draggable !== false){
el.addClass("x-dlg-draggable");
if (!this.proxyDrag) {
var dd = new Ext.dd.DD(el.dom.id, "WindowDrag");
}
else {
var dd = new Ext.dd.DDProxy(el.dom.id, "WindowDrag", {dragElId: this.proxy.id});
}
dd.setHandleElId(this.header.id);
dd.endDrag = this.endMove.createDelegate(this);
dd.startDrag = this.startMove.createDelegate(this);
dd.onDrag = this.onDrag.createDelegate(this);
dd.scroll = false;
this.dd = dd;
}
if(this.modal){
this.mask = dh.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
this.mask.enableDisplayMode("block");
this.mask.hide();
this.el.addClass("x-dlg-modal");
}
if(this.shadow){
this.shadow = new Ext.Shadow({
mode : typeof this.shadow == "string" ? this.shadow : "sides",
offset : this.shadowOffset
});
}else{
this.shadowOffset = 0;
}
if(Ext.useShims && this.shim !== false){
this.shim = this.el.createShim();
this.shim.hide = this.hideAction;
this.shim.hide();
}else{
this.shim = false;
}
if(this.autoTabs){
this.initTabs();
}
this.addEvents({
/**
* @event keydown
* Fires when a key is pressed
* @param {Ext.BasicDialog} this
* @param {Ext.EventObject} e
*/
"keydown" : true,
/**
* @event move
* Fires when this dialog is moved by the user.
* @param {Ext.BasicDialog} this
* @param {Number} x The new page X
* @param {Number} y The new page Y
*/
"move" : true,
/**
* @event resize
* Fires when this dialog is resized by the user.
* @param {Ext.BasicDialog} this
* @param {Number} width The new width
* @param {Number} height The new height
*/
"resize" : true,
/**
* @event beforehide
* Fires before this dialog is hidden.
* @param {Ext.BasicDialog} this
*/
"beforehide" : true,
/**
* @event hide
* Fires when this dialog is hidden.
* @param {Ext.BasicDialog} this
*/
"hide" : true,
/**
* @event beforeshow
* Fires before this dialog is shown.
* @param {Ext.BasicDialog} this
*/
"beforeshow" : true,
/**
* @event show
* Fires when this dialog is shown.
* @param {Ext.BasicDialog} this
*/
"show" : true
});
el.on("keydown", this.onKeyDown, this);
el.on("mousedown", this.toFront, this);
Ext.EventManager.onWindowResize(this.adjustViewport, this, true);
this.el.hide();
Ext.DialogManager.register(this);
Ext.BasicDialog.superclass.constructor.call(this);
};
Ext.extend(Ext.BasicDialog, Ext.util.Observable, {
shadowOffset: Ext.isIE ? 6 : 5,
minHeight: 80,
minWidth: 200,
minButtonWidth: 75,
defaultButton: null,
buttonAlign: "right",
tabTag: 'div',
firstShow: true,
/**
* Sets the dialog title text
* @param {String} text The title text to display
* @return {Ext.BasicDialog} this
*/
setTitle : function(text){
this.header.update(text);
return this;
},
// private
closeClick : function(){
this.hide();
},
// private
collapseClick : function(){
this[this.collapsed ? "expand" : "collapse"]();
},
/**
* Collapses the dialog to its minimized state (only the title bar is visible).
* Equivalent to the user clicking the collapse dialog button.
*/
collapse : function(){
if(!this.collapsed){
this.collapsed = true;
this.el.addClass("x-dlg-collapsed");
this.restoreHeight = this.el.getHeight();
this.resizeTo(this.el.getWidth(), this.header.getHeight());
}
},
/**
* Expands a collapsed dialog back to its normal state. Equivalent to the user
* clicking the expand dialog button.
*/
expand : function(){
if(this.collapsed){
this.collapsed = false;
this.el.removeClass("x-dlg-collapsed");
this.resizeTo(this.el.getWidth(), this.restoreHeight);
}
},
/**
* Reinitializes the tabs component, clearing out old tabs and finding new ones.
* @return {Ext.TabPanel} The tabs component
*/
initTabs : function(){
var tabs = this.getTabs();
while(tabs.getTab(0)){
tabs.removeTab(0);
}
this.el.select(this.tabTag+'.x-dlg-tab').each(function(el){
var dom = el.dom;
tabs.addTab(Ext.id(dom), dom.title);
dom.title = "";
});
tabs.activate(0);
return tabs;
},
// private
beforeResize : function(){
this.resizer.minHeight = Math.max(this.minHeight, this.getHeaderFooterHeight(true)+40);
},
// private
onResize : function(){
this.refreshSize();
this.syncBodyHeight();
this.adjustAssets();
this.focus();
this.fireEvent("resize", this, this.size.width, this.size.height);
},
// private
onKeyDown : function(e){
if(this.isVisible()){
this.fireEvent("keydown", this, e);
}
},
/**
* Resizes the dialog.
* @param {Number} width
* @param {Number} height
* @return {Ext.BasicDialog} this
*/
resizeTo : function(width, height){
this.el.setSize(width, height);
this.size = {width: width, height: height};
this.syncBodyHeight();
if(this.fixedcenter){
this.center();
}
if(this.isVisible()){
this.constrainXY();
this.adjustAssets();
}
this.fireEvent("resize", this, width, height);
return this;
},
/**
* Resizes the dialog to fit the specified content size.
* @param {Number} width
* @param {Number} height
* @return {Ext.BasicDialog} this
*/
setContentSize : function(w, h){
h += this.getHeaderFooterHeight() + this.body.getMargins("tb");
w += this.body.getMargins("lr") + this.bwrap.getMargins("lr") + this.centerBg.getPadding("lr");
//if(!this.el.isBorderBox()){
h += this.body.getPadding("tb") + this.bwrap.getBorderWidth("tb") + this.body.getBorderWidth("tb") + this.el.getBorderWidth("tb");
w += this.body.getPadding("lr") + this.bwrap.getBorderWidth("lr") + this.body.getBorderWidth("lr") + this.bwrap.getPadding("lr") + this.el.getBorderWidth("lr");
//}
if(this.tabs){
h += this.tabs.stripWrap.getHeight() + this.tabs.bodyEl.getMargins("tb") + this.tabs.bodyEl.getPadding("tb");
w += this.tabs.bodyEl.getMargins("lr") + this.tabs.bodyEl.getPadding("lr");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -