📄 dropdownbehavior.js
字号:
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference path="../ExtenderBase/BaseScripts.js" />
/// <reference path="../Common/Common.js" />
/// <reference path="../DynamicPopulate/DynamicPopulateBehavior.js" />
/// <reference path="../Compat/Timer/Timer.js" />
/// <reference path="../Animation/Animations.js" />
/// <reference path="../Animation/AnimationBehavior.js" />
/// <reference path="../PopupExtender/PopupBehavior.js" />
/// <reference path="../HoverExtender/HoverBehavior.js" />
Type.registerNamespace('AjaxControlToolkit');
AjaxControlToolkit.DropDownBehavior = function(element) {
AjaxControlToolkit.DropDownBehavior.initializeBase(this, [element]);
this._dropDownControl = null;
this._highlightBorderColor = "#2353B2";
this._highlightBackgroundColor = "#FFF3DB";
this._dropArrowBackgroundColor = "#C6E1FF";
this._dropArrowImageUrl = null;
this._dropArrowWidth = "16px";
this._oldBackgroundColor = null;
this._dropFrame = null;
this._dropArrow = null;
this._dropArrowImage = null;
this._dropWrapper = null;
this._isOpen = false;
this._isOver = false;
this._wasClicked = null;
this._dropWrapperHoverBehavior = null;
this._dropPopupPopupBehavior = null;
this._onShowJson = null;
this._onHideJson = null;
this._dropDownControl$delegates = {
click : Function.createDelegate(this, this._dropDownControl_onclick),
contextmenu : Function.createDelegate(this, this._dropDownControl_oncontextmenu)
}
this._dropFrame$delegates = {
click : Function.createDelegate(this, this._dropFrame_onclick),
contextmenu : Function.createDelegate(this, this._dropFrame_oncontextmenu)
}
this._dropWrapper$delegates = {
click : Function.createDelegate(this, this._dropWrapper_onclick),
contextmenu : Function.createDelegate(this, this._dropWrapper_oncontextmenu)
}
this._document$delegates = {
click : Function.createDelegate(this, this._document_onclick),
contextmenu : Function.createDelegate(this, this._document_oncontextmenu)
}
this._dropWrapperHoverBehavior$delegates = {
hover : Function.createDelegate(this, this._dropWrapperHoverBehavior_onhover),
unhover : Function.createDelegate(this, this._dropWrapperHoverBehavior_onunhover)
}
}
AjaxControlToolkit.DropDownBehavior.prototype = {
initialize : function() {
AjaxControlToolkit.DropDownBehavior.callBaseMethod(this, 'initialize');
var elt = this.get_element();
var parent = elt.parentNode;
//
// _dropDownControl
//
if (this._dropDownControl == null) {
$common.createElementFromTemplate({
parent : parent,
nameTable : this,
name : "_dropDownControl",
nodeName : "div",
visible : false,
cssClasses : this._dropDownControl ? null : ["ajax__dropdown_panel"],
properties : {
__GENERATED : true
}
});
}
$addHandlers(this._dropDownControl, this._dropDownControl$delegates);
//
// _dropFrame
// _dropArrow
// _dropArrowWrapper
// _dropArrowImage
//
var dropArrowImageProperties = {};
if (this._dropArrowImageUrl) {
// Only set src if it has a value - otherwise IE gets a HTTP 404 response
dropArrowImageProperties["src"] = this._dropArrowImageUrl;
}
$common.createElementFromTemplate({
parent : parent,
nameTable : this,
name : "_dropFrame",
nodeName : "span",
visible : false,
children : [{
name : "_dropFrameTop",
nodeName : "div",
cssClasses : ["ajax__dropdown_frame_line"]
}, {
name : "_dropFrameRight",
nodeName : "div",
cssClasses : ["ajax__dropdown_frame_line"]
}, {
name : "_dropFrameBottom",
nodeName : "div",
cssClasses : ["ajax__dropdown_frame_line"]
}, {
name : "_dropFrameLeft",
nodeName : "div",
cssClasses : ["ajax__dropdown_frame_line"]
}, {
name : "_dropArrow",
nodeName : "div",
cssClasses : (!this._dropArrowImageUrl) ? ["ajax__dropdown_arrow", "ajax__dropdown_arrow_image"] : ["ajax__dropdown_arrow"],
properties : {
style : {
width : this._dropArrowWidth,
backgroundColor : this._dropArrowBackgroundColor
}
},
events : this._dropFrame$delegates,
children : [{
name : "_dropArrowWrapper",
nodeName : "div",
visible : !!this._dropArrowImageUrl,
cssClasses : ["ajax__dropdown_arrow_wrapper"],
children : [{
name : "_dropArrowImage",
nodeName : "img",
properties : dropArrowImageProperties
}]
}]
}]
});
//
// _dropWrapper
//
$common.createElementFromTemplate({
parent : null,
nameTable : this,
name : "_dropWrapper",
nodeName : "span",
properties : {
id : elt.id + "_dropWrapper",
style : {
cursor : "default"
}
},
events : this._dropWrapper$delegates,
content : elt
});
//
// _dropPopupPopupBehavior
//
this._dropPopupPopupBehavior = $create(AjaxControlToolkit.PopupBehavior, {
positioningMode : AjaxControlToolkit.PositioningMode.BottomRight,
parentElement : elt,
y : -1
}, null, null, this._dropDownControl);
// Create the animations (if they were set before initialize was called)
if (this._onShowJson) {
this._dropPopupPopupBehavior.set_onShow(this._onShowJson);
}
if (this._onHideJson) {
this._dropPopupPopupBehavior.set_onHide(this._onHideJson);
}
//
// _dropWrapperHoverBehavior
//
this._dropWrapperHoverBehavior = $create(AjaxControlToolkit.HoverBehavior, {
hoverElement : this._dropFrame
}, this._dropWrapperHoverBehavior$delegates, null, this._dropWrapper);
//
// wire events
//
$addHandlers(document, this._document$delegates);
},
dispose : function() {
var elt = this.get_element();
if (this._isOpen) {
this.hide();
this.unhover();
this._isOpen = false;
}
$common.removeHandlers(document, this._document$delegates);
this._onShowJson = null;
this._onHideJson = null;
if (this._dropPopupPopupBehavior) {
this._dropPopupPopupBehavior.dispose();
this._dropPopupPopupBehavior = null;
}
if (this._dropWrapperHoverBehavior) {
this._dropWrapperHoverBehavior.dispose();
this._dropWrapperHoverBehavior = null;
}
if (this._dropFrame) {
$common.removeElement(this._dropFrame);
this._dropFrame = null;
this._dropFrameTop = null;
this._dropFrameRight = null;
this._dropFrameBottom = null;
this._dropFrameLeft = null;
this._dropArrow = null;
this._dropArrowWrapper = null;
this._dropArrowImage = null;
}
if (this._dropWrapper) {
$common.removeHandlers(this._dropWrapper, this._dropWrapper$delegates);
$common.unwrapElement(elt, this._dropWrapper);
this._dropWrapper = null;
}
if (this._dropDownControl) {
$common.removeHandlers(this._dropDownControl, this._dropDownControl$delegates);
if (this._dropDownControl.__GENERATED) {
$common.removeElement(this._dropDownControl);
}
this._dropDownControl = null;
}
AjaxControlToolkit.DropDownBehavior.callBaseMethod(this, 'dispose');
},
hover : function() {
var elt = this.get_element();
if (!this._isOver) {
this._isOver = true;
this.raiseHoverOver(Sys.EventArgs.Empty);
var bounds = $common.getBounds(elt);
// measure the offset caused by absolute/relative positioning:
// - set position to (0,0)
$common.setLocation(this._dropFrame, {x:0, y:0});
// - show element
$common.setVisible(this._dropFrame, true);
// - get the actual position
var offset = $common.getLocation(this._dropFrame);
// - hide element
$common.setVisible(this._dropFrame, false);
// apply the measured offset
bounds.x -= offset.x;
bounds.y -= offset.y;
$common.setBounds(this._dropFrameTop, {
x : bounds.x,
y : bounds.y,
width : bounds.width,
height : 1
});
$common.setBounds(this._dropFrameRight, {
x : bounds.x + bounds.width - 1,
y : bounds.y,
width : 1,
height : bounds.height
});
$common.setBounds(this._dropFrameBottom, {
x : bounds.x,
y : bounds.y + bounds.height - 1,
width : bounds.width,
height : 1
});
$common.setBounds(this._dropFrameLeft, {
x : bounds.x,
y : bounds.y,
width : 1,
height : bounds.height
});
$common.setBounds(this._dropArrow, {
x : bounds.x + bounds.width - 17,
y : bounds.y + 1,
width : 16,
height : bounds.height - 2
});
this._dropFrameTop.style.backgroundColor = this._highlightBorderColor;
this._dropFrameRight.style.backgroundColor = this._highlightBorderColor;
this._dropFrameBottom.style.backgroundColor = this._highlightBorderColor;
this._dropFrameLeft.style.backgroundColor = this._highlightBorderColor;
$common.setVisible(this._dropFrame, true);
if (!this._oldBackgroundColor) {
this._oldBackgroundColor = $common.getCurrentStyle(elt, 'backgroundColor');
}
elt.style.backgroundColor = this._highlightBackgroundColor;
}
},
unhover : function() {
var elt = this.get_element();
if (this._isOver || !this._isOpen) {
this._isOver = false;
if (!this._isOpen) {
$common.setVisible(this._dropFrame, false);
if (this._oldBackgroundColor) {
elt.style.backgroundColor = this._oldBackgroundColor;
this._oldBackgroundColor = null;
} else {
elt.style.backgroundColor = "transparent";
}
}
this.raiseHoverOut(Sys.EventArgs.Empty);
}
},
show : function() {
if (!this._isOpen) {
this.hover();
var eventArgs = new Sys.CancelEventArgs();
this.raiseShowing(eventArgs);
this.raisePopup(eventArgs);
if (eventArgs.get_cancel()) {
return;
}
this._isOpen = true;
this.populate();
if (!this._dynamicPopulateBehavior || (this._dynamicPopulateBehavior._populated && this._cacheDynamicResults)) {
this._showPopup();
}
}
},
_showPopup : function() {
/// <summary>
/// Show the drop down's popup
/// </summary>
/// <returns />
this._dropPopupPopupBehavior.show();
this.raiseShown(Sys.EventArgs.Empty);
},
hide : function() {
if (this._isOpen) {
var eventArgs = new Sys.CancelEventArgs();
this.raiseHiding(eventArgs);
if (eventArgs.get_cancel()) {
return;
}
this._isOpen = false;
this._dropPopupPopupBehavior.hide();
this.raiseHidden(Sys.EventArgs.Empty);
}
},
_dropWrapperHoverBehavior_onhover : function(sender, e) {
this.hover();
},
_dropWrapperHoverBehavior_onunhover : function(sender, e) {
this.unhover();
},
_dropWrapper_onclick : function(e) {
if(e.target.tagName != "A") {
if(!this._isOpen) {
this.show();
} else {
this.hide();
}
this._wasClicked = true;
}
},
_dropWrapper_oncontextmenu : function(e) {
if(e.target.tagName != "A") {
this._wasClicked = true;
e.preventDefault();
this.show();
}
},
_dropFrame_onclick : function(e) {
if(!this._isOpen) {
this.show();
} else {
this.hide();
}
this._wasClicked = true;
},
_dropFrame_oncontextmenu : function(e) {
this._wasClicked = true;
e.preventDefault();
this.show();
},
_dropDownControl_onclick : function(e) {
//this._wasClicked = true;
},
_dropDownControl_oncontextmenu : function(e) {
this._wasClicked = true;
e.preventDefault();
},
_document_onclick : function(e) {
if(this._wasClicked) {
this._wasClicked = false;
} else if(this._isOpen) {
this.hide();
this.unhover();
}
},
_document_oncontextmenu : function(e) {
if(this._wasClicked) {
this._wasClicked = false;
} else if(this._isOpen) {
this.hide();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -