Array2DData.js 1.3 KB
Newer Older
J
Jason Park 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import { Data } from '/core/datas';

class Array2DData extends Data {
  set(array2d = []) {
    this.data = [];
    for (const array1d of array2d) {
      const row = [];
      for (const value of array1d) {
        const col = {
          value,
          notified: false,
          selected: false,
        };
        row.push(col);
      }
      this.data.push(row);
    }
    super.set();
  }

J
Jason Park 已提交
21
  notify(x, y, v = this.data[x][y].value) {
J
Jason Park 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    this.data[x][y].value = v;
    this.data[x][y].notified = true;
    this.render();
  }

  denotify(x, y) {
    this.data[x][y].notified = false;
    this.render();
  }

  select(sx, sy, ex = sx, ey = sy) {
    for (let x = sx; x <= ex; x++) {
      for (let y = sy; y <= ey; y++) {
        this.data[x][y].selected = true;
      }
    }
    this.render();
  }

  selectRow(x, sy, ey) {
    this.select(x, sy, x, ey);
  }

  selectCol(y, sx, ex) {
    this.select(sx, y, ex, y);
  }

  deselect(sx, sy, ex = sx, ey = sy) {
    for (let x = sx; x <= ex; x++) {
      for (let y = sy; y <= ey; y++) {
        this.data[x][y].selected = false;
      }
    }
    this.render();
  }

  deselectRow(x, sy, ey) {
    this.deselect(x, sy, x, ey);
  }

  deselectCol(y, sx, ex) {
    this.deselect(sx, y, ex, y);
  }
}

export default Array2DData;