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

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3. var CONFIG_FOLDER = dw.getConfigurationPath();
  4. var CHECKBOX      = CONFIG_FOLDER + "/Shared/MM/Images/checkbox.gif";
  5. var CHECKBOX_SEL  = CONFIG_FOLDER + "/Shared/MM/Images/checkbox_sel.gif";
  6. var CHECKBOX_DIS  = CONFIG_FOLDER + "/Shared/MM/Images/checkbox_dis.gif";
  7.  
  8. //**************** CheckboxSet CLASS ****************
  9.  
  10. //PUBLIC METHODS
  11. //--------------
  12. //addCheckbox(name,parents)  - name is required, parents (a comma-separated list) is optional
  13. //                           - IMPORTANT! name and parents cannot contain special characters
  14. //clicked()                  - the HTML tag should pass this: <... onClick="G.clicked(this)">
  15. //isChecked(name)
  16. //check(name,boolean)
  17.  
  18.  
  19. //Class Constructor
  20.  
  21. function CheckboxSet() {
  22.   this.list = new Array();
  23. }
  24. CheckboxSet.prototype.addCheckbox    = CheckboxSet_addCheckbox;
  25. CheckboxSet.prototype.clicked        = CheckboxSet_clicked;
  26. CheckboxSet.prototype.update         = CheckboxSet_update;
  27. CheckboxSet.prototype.updateParent   = CheckboxSet_updateParent;
  28. CheckboxSet.prototype.getCheckboxObj = CheckboxSet_getCheckboxObj;
  29. CheckboxSet.prototype.isChecked      = CheckboxSet_isChecked;
  30. CheckboxSet.prototype.check          = CheckboxSet_check;
  31.  
  32.  
  33. //Adds a Checkbox object to the CheckboxSet object.
  34. //Requires a unique name without spaces
  35.  
  36. function CheckboxSet_addCheckbox(name,parents) {
  37.   var i, newObj, parentList, parentObj;
  38.  
  39.   this.list.push(new Checkbox(name)); //adds new Checkbox object to array
  40.   if (parents != null) {
  41.     newObj = this.list[this.list.length-1];
  42.     parentList = parents.split(",");
  43.     newObj.parents = parentList;                          //set parentList of Checkbox
  44.     for (i=0; i<parentList.length; i++) {
  45.       parentObj = this.getCheckboxObj(parentList[i]);
  46.       parentObj.isParent = true; //mark parent objects
  47.   } }
  48. }
  49.  
  50.  
  51. //Called by the HTML object, updates flags, parents, and children
  52. function CheckboxSet_clicked(htmlObj) {
  53.    var objName = htmlObj.name;
  54.    var checkbox = this.getCheckboxObj(objName);
  55.    checkbox.clicked();
  56.    this.update(htmlObj);
  57. }
  58.  
  59. function CheckboxSet_update(htmlObj) {
  60.   var i, j, objName = htmlObj.name;
  61.   var obj = this.getCheckboxObj(objName);
  62.   var childObj, foundChild;
  63.  
  64.   if (obj.parents != null) { //if part of group, update parents
  65.     for (i=0; i<obj.parents.length; i++) {
  66.       this.updateParent(obj.parents[i]);
  67.     }
  68.   }
  69.   if (obj.isParent) {          //if parent, toggle child Checkboxes
  70.     for (i=0; i<this.list.length; i++) { //look through all objects
  71.       childObj = this.list[i];
  72.       if (childObj.parents) {            //if has parent
  73.         foundChild = false;
  74.         for (j=0; j<childObj.parents.length; j++) { //see if I'm in the parent list
  75.           if (childObj.parents[j] == objName) {
  76.             foundChild = true;
  77.         } }
  78.         if (foundChild) for (j=0; j<childObj.parents.length; j++) {
  79.           if (childObj.parents[j] == objName) {
  80.             childObj.check(obj.isChecked());
  81.           } else {
  82.             this.getCheckboxObj(childObj.parents[j]).check(obj.isChecked());
  83.   } } } } }
  84. }
  85.  
  86. //Called by update() if object has a parent that needs updating
  87.  
  88. function CheckboxSet_updateParent(name) {
  89.   var i, obj, parentObj, possChecked=0, numChecked=0;
  90.  
  91.   for (i=0; i<this.list.length; i++) { //look through all objects
  92.     obj = this.list[i];
  93.     if (obj.parents) { //if has parent
  94.       for (j=0; j<obj.parents.length; j++) {
  95.         if (obj.parents[j] == name) { //if I'm in the parent list
  96.           possChecked++;
  97.           if (obj.isChecked()) numChecked++;
  98.   } } } }
  99.   if (possChecked > 0) { //group exists
  100.     parentObj = this.getCheckboxObj(name);
  101.     if (numChecked == 0) parentObj.check(false);
  102.     else if (numChecked == possChecked) parentObj.check(true);
  103.     else parentObj.check(null); //do grayout here if possible
  104.   }
  105. }
  106.  
  107. //Returns boolean: true if the object is checked
  108.  
  109. function CheckboxSet_isChecked(name) {
  110.   var checkboxObj = this.getCheckboxObj(name);
  111.   return checkboxObj.isChecked();
  112. }
  113.  
  114. //Finds checkbox object in array, and returns instance pointer.
  115.  
  116. function CheckboxSet_getCheckboxObj(name) {
  117.   var i, retVal=null;
  118.  
  119.   for (i=0; i<this.list.length && this.list[i].getName() != name; i++) ;
  120.   if (i< this.list.length) retVal = this.list[i];
  121.   else alert("Could not find checkbox object with name "+name);
  122.   return retVal; //return Checkbox object
  123. }
  124.  
  125. //Forcibly checks or unchecks the object, then updates everything.
  126.  
  127. function CheckboxSet_check(name, checkIt) {
  128.   var checkboxObj = this.getCheckboxObj(name);
  129.   checkboxObj.check(checkIt);
  130.   this.update(checkboxObj);
  131. }
  132.  
  133. //**************** GRAPHIC 3-STATE CHECKBOX CLASS ****************
  134.  
  135. //PUBLIC PROPERTIES
  136. //-----------------
  137. //isParent
  138. //parents
  139. //
  140. //PUBLIC METHODS
  141. //--------------
  142. //check(boolean)
  143. //clicked()
  144. //isChecked()
  145. //getName()
  146.  
  147. function Checkbox(objName){
  148.   this.name = objName;
  149.   this.objRef = findObject(objName);
  150.   this.parents = null;
  151.   this.isParent = false;
  152.   this.checked = false;
  153.   this.objRef.src = CHECKBOX;
  154. }
  155.  
  156. Checkbox.prototype.update = Checkbox_update;
  157. Checkbox.prototype.clicked  = Checkbox_clicked;
  158. Checkbox.prototype.check  = Checkbox_check;
  159. Checkbox.prototype.isChecked = Checkbox_isChecked;
  160. Checkbox.prototype.getName   = Checkbox_getName;
  161.  
  162.  
  163. function Checkbox_update() {
  164.   if (this.checked == null) { //gray out the control
  165.     this.objRef.src = CHECKBOX_DIS;
  166.   } else if (this.checked) {  //activate control
  167.     this.objRef.src = CHECKBOX_SEL;
  168.   } else if (!this.checked) { //de-activate control
  169.     this.objRef.src = CHECKBOX;
  170.   }
  171. }
  172.  
  173. function Checkbox_check(checkIt) {
  174.   this.checked = checkIt;
  175.   this.update();
  176. }
  177.  
  178. function Checkbox_clicked() {
  179.   if(this.checked == false || this.checked == null)
  180.     this.checked = true;
  181.   else
  182.     this.checked = false;
  183.  
  184.   this.update();
  185. }
  186.  
  187. function Checkbox_isChecked() {
  188.   return this.checked;
  189. }
  190.  
  191. function Checkbox_getName() {
  192.   return this.name;
  193. }
  194.