📄 collapsiblepanelbehavior.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="../Compat/Timer/Timer.js" />
/// <reference path="../Animation/Animations.js" />
Type.registerNamespace('AjaxControlToolkit');
AjaxControlToolkit.CollapsiblePanelExpandDirection = function() {
/// <summary>
/// The CollapsiblePanelExpandDirection enumeration describes whether the panel is opened vertically or horizontally
/// </summary>
/// <field name="Horizontal" type="Number" integer="true" />
/// <field name="Vertical" type="Number" integer="true" />
throw Error.invalidOperation();
}
AjaxControlToolkit.CollapsiblePanelExpandDirection.prototype = {
Horizontal : 0,
Vertical: 1
}
AjaxControlToolkit.CollapsiblePanelExpandDirection.registerEnum("AjaxControlToolkit.CollapsiblePanelExpandDirection", false);
AjaxControlToolkit.CollapsiblePanelBehavior = function(element) {
/// <summary>
/// The CollapsiblePanelBehavior allows you to add collapsible sections to your page
/// </summary>
/// <param name="element" type="Sys.UI.DomElement" domElement="true">
/// Element to associate the behavior with
/// </param>
AjaxControlToolkit.CollapsiblePanelBehavior.initializeBase(this, [element]);
// property values
this._collapsedSize = 0;
this._expandedSize = 0;
this._scrollContents = null;
this._collapsed = false;
this._expandControlID = null;
this._collapseControlID = null;
this._textLabelID = null;
this._collapsedText = null;
this._expandedText = null;
this._imageControlID = null;
this._expandedImage = null;
this._collapsedImage = null;
this._suppressPostBack = null;
this._autoExpand = null;
this._autoCollapse = null;
this._expandDirection = AjaxControlToolkit.CollapsiblePanelExpandDirection.Vertical;
// handler delegates
this._collapseClickHandler = null;
this._expandClickHandler = null;
this._panelMouseEnterHandler = null;
this._panelMouseLeaveHandler = null;
// the div we wrap around the panel contents
this._childDiv = null;
// Animation used to open/close the panel
this._animation = null;
}
AjaxControlToolkit.CollapsiblePanelBehavior.prototype = {
initialize : function() {
/// <summary>
/// Initialize the behavior
/// </summary>
AjaxControlToolkit.CollapsiblePanelBehavior.callBaseMethod(this, 'initialize');
var element = this.get_element();
this._animation = new AjaxControlToolkit.Animation.LengthAnimation(element, .25, 10, 'style', null, 0, 0, 'px');
if (this._expandDirection == AjaxControlToolkit.CollapsiblePanelExpandDirection.Vertical) {
this._animation.set_propertyKey('height');
} else if (this._expandDirection == AjaxControlToolkit.CollapsiblePanelExpandDirection.Horizontal) {
this._animation.set_propertyKey('width');
}
this._animation.add_ended(Function.createDelegate(this, this._onAnimateComplete));
// for checkboxes, we don't want to suppress the posback (e.g. return false)
// because that stops them from toggling their state.
//
if (this._suppressPostBack == null) {
if (element.tagName == "INPUT" && element.type == "checkbox") {
this._suppressPostBack = false;
this.raisePropertyChanged('SuppressPostBack');
}
else if (element.tagName == "A") {
this._suppressPostBack = true;
this.raisePropertyChanged('SuppressPostBack');
}
}
// Check our client state. If it's present,
// that means this is a postback, so we restore the state.
//
var lastState = AjaxControlToolkit.CollapsiblePanelBehavior.callBaseMethod(this, 'get_ClientState');
if (lastState && lastState != "") {
var wasCollapsed = Boolean.parse(lastState);
if (this._collapsed != wasCollapsed) {
this._collapsed = wasCollapsed;
this.raisePropertyChanged('Collapsed');
}
}
this._setupChildDiv();
// setup the initial size and state
//
if (this._collapsed) {
this._setTargetSize(this._getCollapsedSize());
} else {
this._setTargetSize(this._getExpandedSize());
}
this._setupState(this._collapsed);
// setup all of our handlers.
if (this._collapseControlID == this._expandControlID) {
this._collapseClickHandler = Function.createDelegate(this, this.togglePanel);
this._expandClickHandler = null; // we don't need both if we're toggling.
} else {
this._collapseClickHandler = Function.createDelegate(this, this.collapsePanel);
this._expandClickHandler = Function.createDelegate(this, this.expandPanel);
}
if (this._autoExpand) {
this._panelMouseEnterHandler = Function.createDelegate(this, this._onMouseEnter);
$addHandler(element, 'mouseover', this._panelMouseEnterHandler);
}
if (this._autoCollapse) {
this._panelMouseLeaveHandler = Function.createDelegate(this, this._onMouseLeave);
$addHandler(element, 'mouseout', this._panelMouseLeaveHandler);
}
// attach the click handlers
//
if (this._collapseControlID) {
var collapseElement = $get(this._collapseControlID);
if (!collapseElement) {
throw Error.argument('CollapseControlID', String.format(AjaxControlToolkit.Resources.CollapsiblePanel_NoControlID, this._collapseControlID));
} else {
$addHandler(collapseElement, 'click', this._collapseClickHandler);
}
}
if (this._expandControlID) {
if (this._expandClickHandler) { // if it's a toggle don't set up again
var expandElement = $get(this._expandControlID);
if (!expandElement) {
throw Error.argument('ExpandControlID', String.format(AjaxControlToolkit.Resources.CollapsiblePanel_NoControlID, this._expandControlID));
} else {
$addHandler(expandElement, 'click', this._expandClickHandler);
}
}
}
},
dispose : function() {
/// <summary>
/// Dispose the behavior
/// </summary>
var element = this.get_element();
if (this._collapseClickHandler) {
var collapseElement = (this._collapseControlID ? $get(this._collapseControlID) : null);
if (collapseElement) {
$removeHandler(collapseElement, 'click', this._collapseClickHandler);
}
this._collapseClickHandler = null;
}
if (this._expandClickHandler) {
var expandElement = (this._expandControlID ? $get(this._expandControlID) : null);
if (expandElement) {
$removeHandler(expandElement, 'click', this._expandClickHandler);
}
this._expandClickHandler = null;
}
if (this._panelMouseEnterHandler) {
$removeHandler(element, 'mouseover', this._panelMouseEnterHandler);
}
if (this._panelMouseLeaveHandler) {
$removeHandler(element, 'mouseout', this._panelMouseLeaveHandler);
}
if (this._animation) {
this._animation.dispose();
this._animation = null;
}
AjaxControlToolkit.CollapsiblePanelBehavior.callBaseMethod(this, 'dispose');
},
togglePanel : function(eventObj) {
/// <summary>
/// Event handler to epxand or collapse the panel (based on its current state)
/// This is the public function that should be called instead of _toggle if
/// you wish to programmatically open and close the collapsible panel.
/// </summary>
/// <param name="eventObj" type="Sys.UI.DomEvent" mayBeNull="true" optional="true">
/// Event Info
/// </param>
this._toggle(eventObj);
},
expandPanel : function(eventObj) {
/// <summary>
/// Open the panel. Public function that users should call if they
/// wish to operate the collapsible panel programmatically.
/// _doOpen should be treated as private since it has an underscore
/// to begin the function name.
/// </summary>
/// <param name="eventObj" type="Sys.UI.DomEvent" mayBeNull="true" optional="true">
/// Event Info
/// </param>
this._doOpen(eventObj);
},
collapsePanel : function(eventObj) {
/// <summary>
/// Collapse the panel. Public function that users should call if they
/// wish to operate the collapsible panel programmatically.
/// _doClose should be treated as private since it has an underscore
/// to begin the function name.
/// </summary>
/// <param name="eventObj" type="Sys.UI.DomEvent" mayBeNull="true" optional="true">
/// Event Info
/// </param>
this._doClose(eventObj);
},
_checkCollapseHide : function() {
/// <summary>
/// Check if a control is collapsed and hidden
/// (and set its display to none if it is supposed to be hidden)
/// </summary>
/// <returns type="Boolean">
/// Whether the control is collapsed and hidden
/// </returns>
if (this._collapsed && this._getTargetSize() == 0) {
var e = this.get_element();
var display = $common.getCurrentStyle(e, 'display');
if (!e.oldDisplay && display != "none") {
e.oldDisplay = display;
e.style.display = "none";
}
return true;
}
return false;
},
_doClose : function(eventObj) {
/// <summary>
/// Collapse the panel. Internal function, to close call "collapsePanel".
/// </summary>
/// <param name="eventObj" type="Sys.UI.DomEvent" mayBeNull="true" optional="true">
/// Event Info
/// </param>
var eventArgs = new Sys.CancelEventArgs();
this.raiseCollapsing(eventArgs);
if (eventArgs.get_cancel()) {
return;
}
if (this._animation) {
// stop any running animation.
this._animation.stop();
// setup the animation state
this._animation.set_startValue(this._getTargetSize());
this._animation.set_endValue(this._getCollapsedSize());
// do it!
this._animation.play();
}
this._setupState(true);
// stop postback if necessary.
if (this._suppressPostBack) {
if (eventObj && eventObj.preventDefault) {
eventObj.preventDefault();
} else {
if (event) {
event.returnValue = false;
}
return false;
}
}
},
_doOpen : function(eventObj) {
/// <summary>
/// Expand the Panel. Internal function, to close call "expandPanel".
/// </summary>
/// <param name="eventObj" type="Sys.UI.DomEvent" mayBeNull="true" optional="true">
/// Event Info
/// </param>
var eventArgs = new Sys.CancelEventArgs();
this.raiseExpanding(eventArgs);
if (eventArgs.get_cancel()) {
return;
}
// stop any existing animation
if (this._animation) {
this._animation.stop();
var e = this.get_element();
if (this._checkCollapseHide() && $common.getCurrentStyle(e, 'display', e.style.display)) {
if (e.oldDisplay) {
e.style.display = e.oldDisplay;
} else {
// IE isn't compliant on this one
if (e.style.removeAttribute) {
e.style.removeAttribute("display");
} else {
e.style.removeProperty("display");
}
}
e.oldDisplay = null;
}
// setup the animation state
this._animation.set_startValue(this._getTargetSize());
this._animation.set_endValue(this._getExpandedSize());
// do it!
this._animation.play();
}
// relect our state changes
this._setupState(false);
// stop the postback if necessary.
//
if (this._suppressPostBack) {
if (eventObj && eventObj.preventDefault) {
eventObj.preventDefault();
} else {
if (event) {
event.returnValue = false;
}
return false;
}
}
},
_onAnimateComplete : function() {
/// <summary>
/// Handler to listen for the end of the expand/collapse animation
/// </summary>
var e = this.get_element();
// if we're fully expanded and the inner pannel is still smaller
// than the size we've expanded to, fall back to auto
//
if (!this._collapsed && !this._expandedSize)
{
if(this._expandDirection == AjaxControlToolkit.CollapsiblePanelExpandDirection.Vertical) {
if(this._childDiv.offsetHeight <= e.offsetHeight) {
e.style.height = "auto";
this.raisePropertyChanged('TargetHeight');
}
else {
this._checkCollapseHide();
}
}
else // horizontal
{
if( this._childDiv.offsetWidth <= e.offsetWidth) {
e.style.width = "auto";
this.raisePropertyChanged('TargetWidth');
}
else {
this._checkCollapseHide();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -