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

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //***************** Class NameValuePair ***************
  4.  
  5. //Creates and manages a list of name/value pairs.
  6. //Names can contain any character.
  7. //Values can be blank, but cannot be set to null (this is the same as deleting them).
  8.  
  9. function NameValuePair() {
  10.   this.nameList = new Array();
  11.   this.valueList = new Array();
  12. }
  13. NameValuePair.prototype.test       = NameValuePair_test;
  14. NameValuePair.prototype.set        = NameValuePair_set;        //set(name,value);      //creates or sets record
  15. NameValuePair.prototype.get        = NameValuePair_get;        //get(name);            //returns value
  16. NameValuePair.prototype.getName    = NameValuePair_getName;    //getName(index);       //returns value
  17. NameValuePair.prototype.del        = NameValuePair_del;        //del(name);            //deletes record by name
  18.                                                                //del(index);           //deletes record by index
  19. NameValuePair.prototype.changeName = NameValuePair_changeName; //changeName(name,newName);
  20. NameValuePair.prototype.length     = NameValuePair_length;     //length();
  21. NameValuePair.prototype.getNames   = NameValuePair_getNames;   //getNames();           //returns array of names
  22. NameValuePair.prototype.getAll     = NameValuePair_getAll;     //getAll(separator);    //returns array of name = value
  23.  
  24.  
  25. function NameValuePair_test() {
  26.   return this.prop;
  27. }
  28.  
  29. //Returns true if set, false if added
  30.  
  31. function NameValuePair_set(name,value) {
  32.   var i, retVal = true;
  33.  
  34.   with (this) {
  35.     for (i=0; i<nameList.length; i++) {
  36.       if (nameList[i] == name) {     //if name found in list
  37.         valueList[i] = value;        //set value
  38.         break;
  39.     } }
  40.     if (i==nameList.length) {        //if not found, add pair
  41.       nameList.push(name);
  42.       valueList.push(value);
  43.       retVal = false;
  44.     }
  45.   }
  46.   return retVal;
  47. }
  48.  
  49.  
  50. //Returns value, or null if pair not found
  51.  
  52. function NameValuePair_get(name) {
  53.   var i, retVal = null;
  54.  
  55.   with (this) {
  56.     for (i=0; i<nameList.length; i++) {
  57.       if (nameList[i] == name) {     //if name found in list
  58.         retVal = valueList[i];       //get value
  59.         break;
  60.   } } }
  61.   return retVal;
  62. }
  63.  
  64.  
  65. //Returns value, or null if pair not found
  66.  
  67. function NameValuePair_getName(index) {
  68.   var i, j, retVal = null;
  69.  
  70.   with (this) {
  71.     index = (parseInt(index) == index)? parseInt(index) : -1; //must be valid number
  72.     if (index != -1) { //indexed get
  73.       j = -1;
  74.       for (i=0; i<nameList.length && j<index; i++) if (valueList[i]!=null) j++; //count non-null items
  75.       if (j==index) retVal = nameList[i-1];
  76.   } }
  77.   return retVal;
  78. }
  79.  
  80.  
  81. //Can be passed name or integer index
  82. //Returns true, or false if pair not found
  83.  
  84. function NameValuePair_del(name) {
  85.   var i, j, index, retVal = false;
  86.  
  87.   with (this) {
  88.     index = (parseInt(name) == name)? parseInt(name) : -1; //if number passed in, index is set
  89.     if (index != -1) { //indexed delete
  90.       j = -1;
  91.       for (i=0; i<nameList.length && j<index; i++) if (valueList[i]!=null) j++; //count non-null items
  92.       if (j==index) {
  93.         nameList[i-1] = null;
  94.         valueList[i-1] = null;
  95.         retVal = true;
  96.       }
  97.  
  98.     } else {
  99.       for (i=0; i<nameList.length; i++) {
  100.         if (nameList[i] == name) { //if name found in list
  101.           nameList[i] = null;
  102.           valueList[i] = null;
  103.           retVal = true;
  104.           break;
  105.   } } } }
  106.   return retVal;
  107. }
  108.  
  109.  
  110. //Returns true, or false if pair not found
  111.  
  112. function NameValuePair_changeName(name,newName) {
  113.   var i, retVal = false;
  114.  
  115.   with (this) {
  116.     for (i=0; i<nameList.length; i++) {
  117.       if (nameList[i] == name) {     //if name found in list
  118.         nameList[i] = newName;
  119.         retVal = true;
  120.         break;
  121.   } } }
  122.   return retVal;
  123. }
  124.  
  125.  
  126. //Returns the number of valid pairs
  127.  
  128. function NameValuePair_length() {
  129.   var i, retVal = 0;
  130.  
  131.   with (this) {
  132.     for (i=0; i<nameList.length; i++) {
  133.       if (valueList[i] != null) retVal++;
  134.   } }
  135.   return retVal;
  136. }
  137.  
  138.  
  139. //Returns array of valid names
  140.  
  141. function NameValuePair_getNames() {
  142.   var i, retVal = new Array;
  143.  
  144.   with (this) {
  145.     for (i=0; i<nameList.length; i++) {
  146.       if (valueList[i] != null) retVal.push(nameList[i]);
  147.   } }
  148.   return retVal;
  149. }
  150.  
  151.  
  152. //Returns array of valid pairs, separated by = or a separator passed in
  153.  
  154. function NameValuePair_getAll(separator) {
  155.   var i, retVal = new Array();
  156.  
  157.   if (!separator) separator = "=";    //default is =
  158.   separator = " " + separator + " ";
  159.   with (this) {
  160.     for (i=0; i<nameList.length; i++) {
  161.       if (valueList[i] != null) retVal.push(nameList[i] + separator + valueList[i]);
  162.   } }
  163.   return retVal;
  164. }
  165.