Create and manage table checkboxes

by Jagrati Tomar

JavaScript

// const UNCHECKED = 0;
// const CHECKED = 1;
// const INDETERMINATE = 2;

// class TableController {
//   constructor() {
//     this.listSize = 0;
//     this.seenList = new Map();
//     this.selectedCount = 0;
//     this.previousState = UNCHECKED;
//   }

//   // When called, should return true when the site
//   // with the specified site is checked. "site" is
//   // a string.
//   getIsSiteCheckboxSelected(site) {
//     return this.seenList.get(site) === CHECKED;
//   }

//   // When called, should return either CHECKED, UNCHECKED,
//   // or INDETERMINATE indicating the current state of the
//   // 'select all' checkbox.
//   getSelectAllCheckboxState() {
//     if (this.selectedCount > 0 && this.selectedCount === this.listSize) {
//       return CHECKED;
//     } else if (this.selectedCount > 0 && this.listSize > 1) {
//       return INDETERMINATE;
//     }
//     return UNCHECKED;
//   }

//   ////////////////////////////////////////
//   // Information received by this class
//   ////////////////////////////////////////

//   // Will be called by the UI whenever the "select all" checkbox
//   // is clicked.
//   handleSelectAllCheckboxClicked() {
//     const previousState = this.previousState;
//     if (previousState === UNCHECKED) {
//       this.selectedCount = 0;
//       this.seenList.forEach((site, value) => {
//         this.seenList.set(
//           site,
//           previousState === UNCHECKED ? CHECKED : UNCHECKED,
//         );
//       });
//     }
//   }

//   // Will be called by the UI whenever any of the checkboxes
//   // next to a single site are clicked. site is a string.
//   handleSingleSiteCheckboxClicked(site) {
//     if (this.seenList.has(site)) {
//       const currentState = this.seenList.get(site);
//       const newState = currentState === CHECKED ? UNCHECKED : CHECKED;
//       this.previousState = newState;
//       this.seenList.set(site, newState);
//      ...