home *** CD-ROM | disk | FTP | other *** search
- // Copyright 1998,1999 Macromedia, Inc. All rights reserved.
-
- /* ----------------------------------------------------------------------*/
- //List Control Class
-
- // Include: common.js
-
- //!!! IMPORTANT !!! THIS CONTROL ONLY WORKS WITHIN DREAMWEAVER !!!
- //
- //This control manages a SELECT control, identified by number.
- //
- //To define a new ListControl, create a global variable and define it after onLoad:
- // MYLIST = new ListControl(selectName);
- //
- //The layerObj parameter is optional. If specified, that layer will be searched
- // for the select list named selectName.
- //
- //Thereafter, you can call methods and get properties, for example:
- // MYLIST.add("newItem"); MYLIST.get(); length = MYLIST.getLen;
- //
- //See properties and methods below:
-
- function ListControl(selName, layerObj) {
- // properties
- this.selectName = selName;
- this.object = (layerObj) ? findObject(selName, layerObj) : findObject(selName);
- this.list = new Array();
- this.valueList = new Array();
- this.index = -1;
- }
-
- // methods
- ListControl.prototype.setAll = ListControlSetAll; // setAll(list,valueList) //set the entire list at once
- ListControl.prototype.add = ListControlAdd; // add() //add a new blank line after the selected line.
- // add('default') //add default text
- // add('default',value) // add text and an associated value
- ListControl.prototype.append = ListControlAppend; // append() //append a new blank line to the end of the list
- // append('default') //append default text
- // append('default',value) // append text and an associated value
- ListControl.prototype.del = ListControlDel; // del() //delete the selected line
- ListControl.prototype.set = ListControlSet; // set('text') //set the text of the current selection
- // set('text', n) //set the text of the nth item
- ListControl.prototype.setValue = ListControlSetValue; // setValue(value) //set the value of the current selection
- // setValue(value, n) //set the value of the nth item
- ListControl.prototype.get = ListControlGet; // get() //return the current selection text
- // get(n) //return text item n (starts at zero)
- // get('all') //return array of all text items
- ListControl.prototype.getValue = ListControlGetValue; // getValue() //return the current selection value
- // getValue(n) //return value item n (starts at zero)
- // getValue('all') //return array of all value items
- ListControl.prototype.setIndex = ListControlSetIndex; // setIndex() //set the selection to the given index
- ListControl.prototype.pickValue = ListControlPickValue;// pickValue() //set the selection to the item with the given value
- ListControl.prototype.getIndex = ListControlGetIndex; // getIndex() //pulls out the selected index.
- ListControl.prototype.getLen = ListControlGetLen; // getLen() //returns the list length
- ListControl.prototype.refresh = ListControlRefresh // refresh() //captures the current selection
-
- ListControl.prototype.updateContents = ListControlUpdateContents;
- ListControl.prototype.escHTMLChars = ListControlEscHTMLChars;
-
-
-
- //Adds a new, blank item after the currently selected item (or end of list).
- //If there is no selection, it replaces the first item.
-
- function ListControlAdd(newItemStr, newValueStr){
- var i, retVal = false;
- with (this) {
- if (!newItemStr) newItemStr = ""; //if no newItemStr, make it blank
- if (!newValueStr) newValueStr = "";
- index = object.selectedIndex;
- if (index >= 0 || list.length == 0) { //if there is a selection or no list
- index++;
- list.splice(index, 0, newItemStr);
- valueList.splice(index, 0, newValueStr);
- updateContents();
- object.selectedIndex = index;
- retVal = true;
- } }
- return retVal
- }
-
-
- //Append a new, blank item to the end of the list.
- //If there is no selection, it replaces the first item.
-
- function ListControlAppend(newItemStr, newValueStr){
- var i, retVal = false;
- with (this) {
- if (!newItemStr) newItemStr = ""; //if no newItemStr, make it blank
- if (!newValueStr) newValueStr = "";
- index = list.length;
- list[index] = newItemStr;
- valueList[index] = newValueStr;
- updateContents();
- object.selectedIndex = index;
- retVal = true;
- }
- return retVal;
- }
-
-
- //Deletes the currently selected item, and selects the one that followed it.
-
- function ListControlDel() {
- var i, retVal = false;
- with (this) {
- index = object.selectedIndex; //get prior selection
- if (index >= 0) { //if there is a selection
- list.splice(index, 1);
- valueList.splice(index, 1);
- updateContents();
- object.selectedIndex = (index >= list.length)? --index : index; //if del last, move sel up one
- retVal = true;
- } }
- return retVal;
- }
-
-
- //Replaces the list selection with the given value.
-
- function ListControlSet(newItemStr, itemNum) {
- var retVal = false;
- with (this) {
- index = object.selectedIndex;
- if (itemNum == null) itemNum = index; //if not passed in, use selection
- if (itemNum >= 0 && itemNum < list.length) { // if selection in range
- if (list[itemNum] != newItemStr) { //if text has been changed
- list[itemNum] = newItemStr; //replace text
- object.options[itemNum].text = newItemStr;
- }
- retVal = true;
- } }
- return retVal;
- }
-
-
- //Replaces the value list selection with the given value.
-
- function ListControlSetValue(newValueStr, itemNum) {
- var retVal = false;
- with (this) {
- index = object.selectedIndex;
- if (itemNum == null) itemNum = index; //if not passed in, use selection
- if (itemNum >= 0 && itemNum < list.length) { // if selection in range
- if (list[itemNum] != newValueStr) { //if text has been changed
- list[itemNum] = newValueStr; //replace text
- object.options[itemNum].text = newValueStr;
- }
- retVal = true;
- } }
- return retVal;
- }
-
-
- //Gets the currently selected item, or optionally the one at the given index
-
- function ListControlGet(optIndex) {
- var retVal = ""; //return blank if all else fails
- with (this) {
- index = object.selectedIndex; //get prior selection
- if (optIndex == null) optIndex = index; //if they don't pass num, use selection
- if (optIndex == "all") retVal = list;
- else if (optIndex > -1) retVal = list[optIndex];
- }
- return retVal;
- }
-
-
- //Gets the currently selected value, or optionally the one at the given index
-
- function ListControlGetValue(optIndex) {
- var retVal = ""; //return blank if all else fails
- with (this) {
- index = object.selectedIndex; //get prior selection
- if (optIndex == null) optIndex = index; //if they don't pass num, use selection
- if (optIndex == "all") retVal = valueList;
- else if (optIndex > -1) retVal = valueList[optIndex];
- }
- return retVal;
- }
-
-
- //Sets the list selection to the given index
-
- function ListControlSetIndex(theIndex) {
- var retVal = false;
- with (this) {
- if (theIndex >= 0 && theIndex < list.length) { //if theIndex between 0 and length
- object.selectedIndex = theIndex;
- index = theIndex;
- retVal = true;
- } }
- return retVal
- }
-
-
- //Sets the list selection to the given index
-
- function ListControlPickValue(theValue) {
- var retVal = false;
- with (this) {
- for (var i=0; i < valueList.length; i++) {
- if (valueList[i] == theValue) { // TO DO: what if value is an object?
- object.selectedIndex = i;
- index = i;
- retVal = true;
- break;
- } } }
- return retVal
- }
-
-
- //Gets the list selection
-
- function ListControlGetIndex() {
- this.index = this.object.selectedIndex; //get prior selection
- return this.index;
- }
-
-
- //Returns the length of the current list
-
- function ListControlGetLen() {
- this.index = this.object.selectedIndex; //get prior selection
- return this.list.length
- }
-
-
- //Sets the entire list to the contents of newList, expanding the list
- // as necessary
-
- function ListControlSetAll(newList, newValueList) {
- var retVal = false;
- with (this) {
- index = object.selectedIndex; //get prior selection
- if (index < 0 || newList.length <= index) index = 0; //if outta range
- list = new Array();
- valueList = new Array();
- for (i=0; i < newList.length; i++) {
- list[i] = newList[i]; //dupe array
- valueList[i] = (newValueList && newValueList.length > i) ? newValueList[i] : '';
- }
- updateContents();
- object.selectedIndex = index;
- retVal = true;
- }
- return retVal;
- }
-
-
- function ListControlRefresh() {
- this.index = this.object.selectedIndex; //get prior selection
- }
-
-
- function ListControlUpdateContents() {
- var i, optionStr = '';
- for (i=0; i < this.list.length; i++) optionStr += "<option>" + this.escHTMLChars(this.list[i]) + "</option>";
- this.object.innerHTML = optionStr;
- }
-
-
- function ListControlEscHTMLChars(theStr) {
- theStr = theStr.replace(/\&/g,"&");
- theStr = theStr.replace(/\</g,"<");
- theStr = theStr.replace(/\>/g,">");
- return theStr;
- }
-