home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / Class / ListControlClass.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  9.2 KB  |  269 lines

  1. // Copyright 1998,1999 Macromedia, Inc. All rights reserved.
  2.  
  3. /* ----------------------------------------------------------------------*/
  4. //List Control Class
  5.  
  6. // Include: common.js
  7.  
  8. //!!! IMPORTANT !!! THIS CONTROL ONLY WORKS WITHIN DREAMWEAVER !!!
  9. //
  10. //This control manages a SELECT control, identified by number.
  11. //
  12. //To define a new ListControl, create a global variable and define it after onLoad:
  13. //  MYLIST = new ListControl(selectName);
  14. //
  15. //The layerObj parameter is optional.  If specified, that layer will be searched
  16. // for the select list named selectName.
  17. //
  18. //Thereafter, you can call methods and get properties, for example:
  19. //  MYLIST.add("newItem");    MYLIST.get();   length = MYLIST.getLen;
  20. //
  21. //See properties and methods below:
  22.  
  23. function ListControl(selName, layerObj) {
  24.   // properties
  25.   this.selectName  = selName;
  26.   this.object = (layerObj) ? findObject(selName, layerObj) : findObject(selName);
  27.   this.list = new Array();
  28.   this.valueList = new Array();
  29.   this.index = -1;
  30. }
  31.  
  32. // methods
  33. ListControl.prototype.setAll    = ListControlSetAll;   // setAll(list,valueList)  //set the entire list at once
  34. ListControl.prototype.add       = ListControlAdd;      // add()  //add a new blank line after the selected line.
  35.                                                        // add('default')  //add default text
  36.                                                        // add('default',value)  // add text and an associated value
  37. ListControl.prototype.append    = ListControlAppend;   // append()   //append a new blank line to the end of the list
  38.                                                        // append('default')  //append default text
  39.                                                        // append('default',value)  // append text and an associated value
  40. ListControl.prototype.del       = ListControlDel;      // del()  //delete the selected line
  41. ListControl.prototype.set       = ListControlSet;      // set('text')  //set the text of the current selection
  42.                                                        // set('text', n)  //set the text of the nth item
  43. ListControl.prototype.setValue  = ListControlSetValue; // setValue(value)  //set the value of the current selection
  44.                                                        // setValue(value, n)  //set the value of the nth item
  45. ListControl.prototype.get       = ListControlGet;      // get()  //return the current selection text
  46.                                                        // get(n)  //return text item n (starts at zero)
  47.                                                        // get('all')  //return array of all text items
  48. ListControl.prototype.getValue  = ListControlGetValue; // getValue()  //return the current selection value
  49.                                                        // getValue(n)  //return value item n (starts at zero)
  50.                                                        // getValue('all')  //return array of all value items
  51. ListControl.prototype.setIndex  = ListControlSetIndex; // setIndex()  //set the selection to the given index
  52. ListControl.prototype.pickValue = ListControlPickValue;// pickValue()  //set the selection to the item with the given value
  53. ListControl.prototype.getIndex  = ListControlGetIndex; // getIndex()  //pulls out the selected index.
  54. ListControl.prototype.getLen    = ListControlGetLen;   // getLen()  //returns the list length
  55. ListControl.prototype.refresh   = ListControlRefresh   // refresh()  //captures the current selection
  56.  
  57. ListControl.prototype.updateContents = ListControlUpdateContents;
  58. ListControl.prototype.escHTMLChars = ListControlEscHTMLChars;
  59.  
  60.  
  61.  
  62. //Adds a new, blank item after the currently selected item (or end of list).
  63. //If there is no selection, it replaces the first item.
  64.  
  65. function ListControlAdd(newItemStr, newValueStr){
  66.   var i, retVal = false;
  67.   with (this) {
  68.     if (!newItemStr) newItemStr = "";  //if no newItemStr, make it blank
  69.     if (!newValueStr) newValueStr = "";
  70.     index = object.selectedIndex;
  71.     if (index >= 0 || list.length == 0) {  //if there is a selection or no list
  72.       index++;
  73.       list.splice(index, 0, newItemStr);
  74.       valueList.splice(index, 0, newValueStr);
  75.       updateContents();
  76.       object.selectedIndex =  index;
  77.       retVal = true;
  78.   } }
  79.   return retVal
  80. }
  81.  
  82.  
  83. //Append a new, blank item to the end of the list.
  84. //If there is no selection, it replaces the first item.
  85.  
  86. function ListControlAppend(newItemStr, newValueStr){
  87.   var i, retVal = false;
  88.   with (this) {
  89.     if (!newItemStr) newItemStr = "";  //if no newItemStr, make it blank
  90.     if (!newValueStr) newValueStr = "";
  91.     index = list.length;
  92.     list[index] = newItemStr;
  93.     valueList[index] = newValueStr;
  94.     updateContents();
  95.     object.selectedIndex = index;
  96.     retVal = true;
  97.   }
  98.   return retVal;
  99. }
  100.  
  101.  
  102. //Deletes the currently selected item, and selects the one that followed it.
  103.  
  104. function ListControlDel() {
  105.   var i, retVal = false;
  106.   with (this) {
  107.     index = object.selectedIndex; //get prior selection
  108.     if (index >= 0) {  //if there is a selection
  109.       list.splice(index, 1);
  110.       valueList.splice(index, 1);
  111.       updateContents();
  112.       object.selectedIndex = (index >= list.length)? --index : index; //if del last, move sel up one
  113.       retVal = true;
  114.   } }
  115.   return retVal;
  116. }
  117.  
  118.  
  119. //Replaces the list selection with the given value.
  120.  
  121. function ListControlSet(newItemStr, itemNum) {
  122.   var retVal = false;
  123.   with (this) {
  124.     index = object.selectedIndex;
  125.     if (itemNum == null) itemNum = index; //if not passed in, use selection
  126.     if (itemNum >= 0 && itemNum < list.length) {  // if selection in range
  127.       if (list[itemNum] != newItemStr) {  //if text has been changed
  128.         list[itemNum] = newItemStr;  //replace text
  129.         object.options[itemNum].text = newItemStr;
  130.       }
  131.       retVal = true;
  132.   } }
  133.   return retVal;
  134. }
  135.  
  136.  
  137. //Replaces the value list selection with the given value.
  138.  
  139. function ListControlSetValue(newValueStr, itemNum) {
  140.   var retVal = false;
  141.   with (this) {
  142.     index = object.selectedIndex;
  143.     if (itemNum == null) itemNum = index; //if not passed in, use selection
  144.     if (itemNum >= 0 && itemNum < list.length) {  // if selection in range
  145.       if (list[itemNum] != newValueStr) {  //if text has been changed
  146.         list[itemNum] = newValueStr;  //replace text
  147.         object.options[itemNum].text = newValueStr;
  148.       }
  149.       retVal = true;
  150.   } }
  151.   return retVal;
  152. }
  153.  
  154.  
  155. //Gets the currently selected item, or optionally the one at the given index
  156.  
  157. function ListControlGet(optIndex) {
  158.   var retVal = "";  //return blank if all else fails
  159.   with (this) {
  160.     index = object.selectedIndex; //get prior selection
  161.     if (optIndex == null) optIndex = index;  //if they don't pass num, use selection
  162.     if (optIndex == "all")  retVal = list;
  163.     else if (optIndex > -1) retVal = list[optIndex];
  164.   }
  165.   return retVal;
  166. }
  167.  
  168.  
  169. //Gets the currently selected value, or optionally the one at the given index
  170.  
  171. function ListControlGetValue(optIndex) {
  172.   var retVal = "";  //return blank if all else fails
  173.   with (this) {
  174.     index = object.selectedIndex; //get prior selection
  175.     if (optIndex == null) optIndex = index;  //if they don't pass num, use selection
  176.     if (optIndex == "all")  retVal = valueList;
  177.     else if (optIndex > -1) retVal = valueList[optIndex];
  178.   }
  179.   return retVal;
  180. }
  181.  
  182.  
  183. //Sets the list selection to the given index
  184.  
  185. function ListControlSetIndex(theIndex) {
  186.   var retVal = false;
  187.   with (this) {
  188.     if (theIndex >= 0 && theIndex < list.length) {  //if theIndex between 0 and length
  189.       object.selectedIndex = theIndex;
  190.       index = theIndex;
  191.       retVal = true;
  192.   } }
  193.   return retVal
  194. }
  195.  
  196.  
  197. //Sets the list selection to the given index
  198.  
  199. function ListControlPickValue(theValue) {
  200.   var retVal = false;
  201.   with (this) {
  202.     for (var i=0; i < valueList.length; i++) {
  203.       if (valueList[i] == theValue) {  // TO DO: what if value is an object?
  204.         object.selectedIndex = i;
  205.         index = i;
  206.         retVal = true;
  207.         break;
  208.   } } }
  209.   return retVal
  210. }
  211.  
  212.  
  213. //Gets the list selection
  214.  
  215. function ListControlGetIndex() {
  216.   this.index = this.object.selectedIndex; //get prior selection
  217.   return this.index;
  218. }
  219.  
  220.  
  221. //Returns the length of the current list
  222.  
  223. function ListControlGetLen() {
  224.   this.index = this.object.selectedIndex; //get prior selection
  225.   return this.list.length
  226. }
  227.  
  228.  
  229. //Sets the entire list to the contents of newList, expanding the list
  230. // as necessary
  231.  
  232. function ListControlSetAll(newList, newValueList) {
  233.   var retVal = false;
  234.   with (this) {
  235.     index = object.selectedIndex; //get prior selection
  236.     if (index < 0 || newList.length <= index) index = 0; //if outta range
  237.     list = new Array();
  238.     valueList = new Array();
  239.     for (i=0; i < newList.length; i++) {
  240.       list[i] = newList[i]; //dupe array
  241.       valueList[i] = (newValueList && newValueList.length > i) ? newValueList[i] : '';
  242.     }
  243.     updateContents();
  244.     object.selectedIndex = index;
  245.     retVal = true;
  246.   }
  247.   return retVal;
  248. }
  249.  
  250.  
  251. function ListControlRefresh() {
  252.   this.index = this.object.selectedIndex; //get prior selection
  253. }
  254.  
  255.  
  256. function ListControlUpdateContents() {
  257.   var i, optionStr = '';
  258.   for (i=0; i < this.list.length; i++) optionStr += "<option>" + this.escHTMLChars(this.list[i]) + "</option>";
  259.   this.object.innerHTML = optionStr;
  260. }
  261.  
  262.  
  263. function ListControlEscHTMLChars(theStr) {
  264.   theStr = theStr.replace(/\&/g,"&");
  265.   theStr = theStr.replace(/\</g,"<");
  266.   theStr = theStr.replace(/\>/g,">");
  267.   return theStr;
  268. }
  269.