/************************************************************************
* DropDownCheckList.js
*
* Client-side javascript to support the ASP.NET DropDownCheckList
* server control.
*
* written by Mike Ellison, 20-September-2005
*
************************************************************************
* Copyright (c) 2005 Mike Ellison. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions, the following disclaimer, and the following
* acknowledgements.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Acknowledgements:
* - The "shim" technique used to support Internet Explorer 6.x
* is courtesy Joe King of Coalesys, Inc.,
* http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx
*
* - Javascript functions findPosX and findPosY for locating objects absolutely
* are courtesy Peter-Paul Koch, http://www.quirksmode.org
*
************************************************************************/
/************************************************************************
* Utility Functions (global to the page)
************************************************************************/
var DDCL_DROPDOWNMODE_INLINE = 0;
var DDCL_DROPDOWNMODE_ONTOP = 1;
var DDCL_DROPDOWNMODE_ONTOPWITHSHIM = 2;
var DDCL_DISPLAYTEXTLIST_LABELS = 0;
var DDCL_DISPLAYTEXTLIST_VALUES = 1;
ddcl_Objects = new Array();
function DDCL_GetObject(id)
{
for (var i=0; i<ddcl_Objects.length; i++)
{
// alert(id.substring(ddcl_Objects[i].length) + ", " + ddcl_Objects[i].id);
if (id.substring(0,ddcl_Objects[i].id.length) == ddcl_Objects[i].id)
{
// alert('gotit');
return ddcl_Objects[i];
}
}
return null;
}
// -----------------------------------------------------
function DDCL_findPosX(obj)
{
/***
Original script by Peter-Paul Koch, http://www.quirksmode.org
***/
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
return curleft;
}
function DDCL_findPosY(obj)
{
/***
Original script by Peter-Paul Koch, http://www.quirksmode.org
***/
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return curtop;
}
// -----------------------------------------------------
function DDCL_HandleCheckboxClickById(id)
{
var obj = DDCL_GetObject(id);
obj.HandleCheckboxClick();
}
// called as the onclick event handler for a check box
function DDCL_HandleCheckboxClick(e)
{
if (!e) var e = window.event;
if (e) {
var elem = this;
if (elem) {
var obj = DDCL_GetObject(elem.id);
if (obj)
{
obj.HandleCheckboxClick();
obj.inCheckboxDiv = true;
}
}
}
}
// called as the onclick event handler for a display box
function DDCL_HandleDisplayBoxClick(e)
{
if (!e) var e = window.event;
if (e) {
var elem = this;
if (elem) {
var obj = DDCL_GetObject(elem.id);
if (obj)
{
obj.HandleDisplayBoxClick();
obj.inCheckboxDiv = true;
}
}
}
}
// called as the onclick event handler for a checkbox div
function DDCL_HandleCheckboxDivClick(e)
{
if (!e) var e = window.event;
if (e) {
var obj=DDCL_GetObject(this.id);
if (obj)
obj.inCheckboxDiv = true;
}
}
function DDCL_HandleDocumentClick(id)
{
var obj = DDCL_GetObject(id);
if (obj)
{
if (obj.inCheckboxDiv == true)
obj.inCheckboxDiv = false;
else
obj.CloseCheckList();
}
}
/************************************************************************
* Object definition
************************************************************************/
// constructor; takes a required id as a parameter; this will be the
// same as the ClientID of the ASP.NET control
function DDCL_DropDownCheckList(id, textWhenNone, separator, truncateString, dropDownMode, allowExpand, displayList)
{
// remember the id, displayMode, and text to display when no
// boxes are checked
this.id = id;
this.textWhenNone = textWhenNone;
this.separator = separator;
this.truncateString = truncateString;
this.dropDownMode = dropDownMode;
this.allowExpand = allowExpand;
this.displayList = displayList;
// get the elements representing the display box div and
// checklist div; these will be referenced by object methods
this.divCheckboxes = document.getElementById(id + "_checkboxes");
this.divBoundingBox = document.getElementById(id + "_boundingbox");
this.divText = document.getElementById(id + "_text");
this.divDisplayBox = document.getElementById(id + "_displaybox");
this.img = document.getElementById(id + "_img");
this.shim = document.getElementById(id + "_shim");
this.boundingBoxWidth = this.divBoundingBox.offsetWidth;
this.divCheckboxes.style.display = "none";
// wire click events for the display box div and the checkboxes div
this.divDisplayBox.onclick = DDCL_HandleDisplayBoxClick;
this.divCheckboxes.onclick = DDCL_HandleCheckboxDivClick;
// wire click events for the checkboxes
var e = this.divCheckboxes.getElementsByTagName("input");
for (var i=0; i<e.length; i++)
{
if (e[i].type == "checkbox")
{
e[i].onclick = DDCL_HandleCheckboxClick;
}
}
// if the browser supports bubbling events, install a default click
// handler for the document too, that will close the checkboxes div
// if there is a click outside it
if (document.attachEvent)
{
document.attachEvent('onclick'
, function() { eval("DDCL_HandleDocumentClick('" + id + "');") }
);
}
else if (document.addEventListener)
{
document.addEventListener('click'
, function() { eval("DDCL_HandleDocumentClick('" + id + "');") }
, false);
}
// initial display of checked items, or the textWhen
评论25