@oscarpalmer/tabela 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/components/body.component.cjs +40 -8
  2. package/dist/components/body.component.js +40 -8
  3. package/dist/components/column.component.cjs +1 -1
  4. package/dist/components/column.component.js +1 -1
  5. package/dist/components/footer.component.cjs +15 -9
  6. package/dist/components/footer.component.js +15 -9
  7. package/dist/components/header.component.cjs +9 -6
  8. package/dist/components/header.component.js +9 -6
  9. package/dist/components/row.component.cjs +20 -8
  10. package/dist/components/row.component.js +21 -9
  11. package/dist/helpers/dom.helpers.cjs +10 -3
  12. package/dist/helpers/dom.helpers.js +10 -3
  13. package/dist/managers/virtualization.manager.cjs +88 -0
  14. package/dist/managers/virtualization.manager.js +88 -0
  15. package/dist/tabela.cjs +19 -1
  16. package/dist/tabela.js +19 -1
  17. package/package.json +7 -7
  18. package/src/components/body.component.ts +59 -9
  19. package/src/components/column.component.ts +1 -1
  20. package/src/components/footer.component.ts +22 -9
  21. package/src/components/header.component.ts +15 -6
  22. package/src/components/row.component.ts +31 -11
  23. package/src/helpers/dom.helpers.ts +12 -3
  24. package/src/managers/virtualization.manager.ts +123 -0
  25. package/src/tabela.ts +39 -15
  26. package/types/components/body.component.d.cts +56 -31
  27. package/types/components/body.component.d.ts +11 -2
  28. package/types/components/column.component.d.cts +56 -31
  29. package/types/components/footer.component.d.cts +56 -31
  30. package/types/components/footer.component.d.ts +8 -3
  31. package/types/components/header.component.d.cts +56 -31
  32. package/types/components/header.component.d.ts +7 -2
  33. package/types/components/row.component.d.cts +56 -31
  34. package/types/components/row.component.d.ts +4 -3
  35. package/types/helpers/dom.helpers.d.cts +2 -2
  36. package/types/helpers/dom.helpers.d.ts +2 -2
  37. package/types/index.d.cts +56 -31
  38. package/types/{components/cell.component.d.cts → managers/virtualization.manager.d.cts} +56 -31
  39. package/types/managers/virtualization.manager.d.ts +16 -0
  40. package/types/tabela.d.cts +56 -31
  41. package/types/tabela.d.ts +2 -1
  42. package/dist/components/cell.component.cjs +0 -17
  43. package/dist/components/cell.component.js +0 -17
  44. package/src/components/cell.component.ts +0 -18
  45. package/types/components/cell.component.d.ts +0 -10
@@ -4,23 +4,55 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
4
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
6
  const helpers_dom_helpers = require("../helpers/dom.helpers.cjs");
7
+ const managers_virtualization_manager = require("../managers/virtualization.manager.cjs");
7
8
  const components_row_component = require("./row.component.cjs");
9
+ function createFaker() {
10
+ const element = document.createElement("div");
11
+ element.style.height = "0";
12
+ element.style.inset = "0 auto auto 0";
13
+ element.style.opacity = "0";
14
+ element.style.pointerEvents = "none";
15
+ element.style.position = "absolute";
16
+ element.style.width = "1px";
17
+ return element;
18
+ }
8
19
  class BodyComponent {
9
20
  constructor(tabela) {
10
- __publicField(this, "group");
21
+ __publicField(this, "elements", {
22
+ faker: createFaker(),
23
+ group: void 0
24
+ });
11
25
  __publicField(this, "rows", []);
26
+ __publicField(this, "virtualization");
12
27
  this.tabela = tabela;
13
- this.group = helpers_dom_helpers.createRowGroup(false);
14
- this.group.className += " tabela__rowgroup-body";
15
- this.addData(tabela.options.data);
28
+ const group = helpers_dom_helpers.createRowGroup(false);
29
+ this.elements.group = group;
30
+ this.virtualization = new managers_virtualization_manager.VirtualizationManager(this);
31
+ group.className += " tabela__rowgroup-body";
32
+ group.style.height = "100%";
33
+ group.style.overflow = "auto";
34
+ group.style.position = "relative";
35
+ group.append(this.elements.faker);
36
+ void this.addData(tabela.options.data).then(() => {
37
+ this.virtualization.update(true);
38
+ });
16
39
  }
17
- addData(data) {
40
+ async addData(data) {
18
41
  const { length } = data;
19
42
  for (let index = 0; index < length; index += 1) {
20
- const row = new components_row_component.RowComponent(this.tabela, data[index]);
21
- this.rows.push(row);
22
- this.group.append(row.element);
43
+ this.rows.push(new components_row_component.RowComponent(this.tabela, data[index]));
23
44
  }
45
+ this.updateVirtualization();
46
+ }
47
+ destroy() {
48
+ this.virtualization.destroy();
49
+ this.elements.faker = void 0;
50
+ this.elements.group = void 0;
51
+ this.rows.length = 0;
52
+ }
53
+ updateVirtualization() {
54
+ this.elements.faker.style.height = `${this.rows.length * 32}px`;
55
+ this.virtualization.update(true);
24
56
  }
25
57
  }
26
58
  exports.BodyComponent = BodyComponent;
@@ -2,23 +2,55 @@ var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
  import { createRowGroup } from "../helpers/dom.helpers.js";
5
+ import { VirtualizationManager } from "../managers/virtualization.manager.js";
5
6
  import { RowComponent } from "./row.component.js";
7
+ function createFaker() {
8
+ const element = document.createElement("div");
9
+ element.style.height = "0";
10
+ element.style.inset = "0 auto auto 0";
11
+ element.style.opacity = "0";
12
+ element.style.pointerEvents = "none";
13
+ element.style.position = "absolute";
14
+ element.style.width = "1px";
15
+ return element;
16
+ }
6
17
  class BodyComponent {
7
18
  constructor(tabela) {
8
- __publicField(this, "group");
19
+ __publicField(this, "elements", {
20
+ faker: createFaker(),
21
+ group: void 0
22
+ });
9
23
  __publicField(this, "rows", []);
24
+ __publicField(this, "virtualization");
10
25
  this.tabela = tabela;
11
- this.group = createRowGroup(false);
12
- this.group.className += " tabela__rowgroup-body";
13
- this.addData(tabela.options.data);
26
+ const group = createRowGroup(false);
27
+ this.elements.group = group;
28
+ this.virtualization = new VirtualizationManager(this);
29
+ group.className += " tabela__rowgroup-body";
30
+ group.style.height = "100%";
31
+ group.style.overflow = "auto";
32
+ group.style.position = "relative";
33
+ group.append(this.elements.faker);
34
+ void this.addData(tabela.options.data).then(() => {
35
+ this.virtualization.update(true);
36
+ });
14
37
  }
15
- addData(data) {
38
+ async addData(data) {
16
39
  const { length } = data;
17
40
  for (let index = 0; index < length; index += 1) {
18
- const row = new RowComponent(this.tabela, data[index]);
19
- this.rows.push(row);
20
- this.group.append(row.element);
41
+ this.rows.push(new RowComponent(this.tabela, data[index]));
21
42
  }
43
+ this.updateVirtualization();
44
+ }
45
+ destroy() {
46
+ this.virtualization.destroy();
47
+ this.elements.faker = void 0;
48
+ this.elements.group = void 0;
49
+ this.rows.length = 0;
50
+ }
51
+ updateVirtualization() {
52
+ this.elements.faker.style.height = `${this.rows.length * 32}px`;
53
+ this.virtualization.update(true);
22
54
  }
23
55
  }
24
56
  export {
@@ -16,7 +16,7 @@ class ColumnComponent {
16
16
  __publicField(this, "element");
17
17
  __publicField(this, "options");
18
18
  this.tabela = tabela;
19
- const width = Number.parseInt(getComputedStyle(tabela.element).fontSize, 10) * (options.width ?? 4);
19
+ const width = Number.parseInt(getComputedStyle(tabela.element).fontSize, 10) * (options.width ?? options.title.length * 1.5);
20
20
  this.options = {
21
21
  ...options,
22
22
  width
@@ -14,7 +14,7 @@ class ColumnComponent {
14
14
  __publicField(this, "element");
15
15
  __publicField(this, "options");
16
16
  this.tabela = tabela;
17
- const width = Number.parseInt(getComputedStyle(tabela.element).fontSize, 10) * (options.width ?? 4);
17
+ const width = Number.parseInt(getComputedStyle(tabela.element).fontSize, 10) * (options.width ?? options.title.length * 1.5);
18
18
  this.options = {
19
19
  ...options,
20
20
  width
@@ -6,24 +6,30 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
6
  const helpers_dom_helpers = require("../helpers/dom.helpers.cjs");
7
7
  class FooterComponent {
8
8
  constructor(tabela) {
9
- __publicField(this, "cells", []);
10
- __publicField(this, "group");
11
- __publicField(this, "row");
9
+ __publicField(this, "elements");
12
10
  this.tabela = tabela;
13
11
  const { group, row } = helpers_dom_helpers.createRowGroup();
14
- this.group = group;
15
- this.row = row;
16
- this.group.className += " tabela__rowgroup-footer";
17
- this.row.className += " tabela__row-footer";
12
+ this.elements = {
13
+ group,
14
+ row,
15
+ cells: []
16
+ };
17
+ group.className += " tabela__rowgroup-footer";
18
+ row.className += " tabela__row-footer";
18
19
  const { columns } = tabela.header;
19
20
  const { length } = columns;
20
21
  for (let index = 0; index < length; index += 1) {
21
- const cell = helpers_dom_helpers.createCell(columns[index].options.width ?? 4);
22
+ const cell = helpers_dom_helpers.createCell(columns[index].options.width ?? 4, false);
22
23
  cell.className += " tabela__cell-footer";
23
24
  cell.innerHTML = "&nbsp;";
24
- this.cells.push(cell);
25
+ this.elements.cells.push(cell);
25
26
  row.append(cell);
26
27
  }
27
28
  }
29
+ destroy() {
30
+ this.elements.cells = [];
31
+ this.elements.group = void 0;
32
+ this.elements.row = void 0;
33
+ }
28
34
  }
29
35
  exports.FooterComponent = FooterComponent;
@@ -4,25 +4,31 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4
4
  import { createRowGroup, createCell } from "../helpers/dom.helpers.js";
5
5
  class FooterComponent {
6
6
  constructor(tabela) {
7
- __publicField(this, "cells", []);
8
- __publicField(this, "group");
9
- __publicField(this, "row");
7
+ __publicField(this, "elements");
10
8
  this.tabela = tabela;
11
9
  const { group, row } = createRowGroup();
12
- this.group = group;
13
- this.row = row;
14
- this.group.className += " tabela__rowgroup-footer";
15
- this.row.className += " tabela__row-footer";
10
+ this.elements = {
11
+ group,
12
+ row,
13
+ cells: []
14
+ };
15
+ group.className += " tabela__rowgroup-footer";
16
+ row.className += " tabela__row-footer";
16
17
  const { columns } = tabela.header;
17
18
  const { length } = columns;
18
19
  for (let index = 0; index < length; index += 1) {
19
- const cell = createCell(columns[index].options.width ?? 4);
20
+ const cell = createCell(columns[index].options.width ?? 4, false);
20
21
  cell.className += " tabela__cell-footer";
21
22
  cell.innerHTML = "&nbsp;";
22
- this.cells.push(cell);
23
+ this.elements.cells.push(cell);
23
24
  row.append(cell);
24
25
  }
25
26
  }
27
+ destroy() {
28
+ this.elements.cells = [];
29
+ this.elements.group = void 0;
30
+ this.elements.row = void 0;
31
+ }
26
32
  }
27
33
  export {
28
34
  FooterComponent
@@ -8,18 +8,21 @@ const components_column_component = require("./column.component.cjs");
8
8
  class HeaderComponent {
9
9
  constructor(tabela) {
10
10
  __publicField(this, "columns");
11
- __publicField(this, "group");
12
- __publicField(this, "row");
11
+ __publicField(this, "elements");
13
12
  this.tabela = tabela;
14
13
  this.columns = tabela.options.columns.map(
15
14
  (column) => new components_column_component.ColumnComponent(tabela, column)
16
15
  );
17
16
  const { group, row } = helpers_dom_helpers.createRowGroup();
18
- this.group = group;
19
- this.row = row;
20
- this.group.className += " tabela__rowgroup-header";
21
- this.row.className += " tabela__row-header";
17
+ this.elements = { group, row };
18
+ group.className += " tabela__rowgroup-header";
19
+ row.className += " tabela__row-header";
22
20
  row.append(...this.columns.map((column) => column.element));
23
21
  }
22
+ destroy() {
23
+ this.columns.length = 0;
24
+ this.elements.group = void 0;
25
+ this.elements.row = void 0;
26
+ }
24
27
  }
25
28
  exports.HeaderComponent = HeaderComponent;
@@ -6,19 +6,22 @@ import { ColumnComponent } from "./column.component.js";
6
6
  class HeaderComponent {
7
7
  constructor(tabela) {
8
8
  __publicField(this, "columns");
9
- __publicField(this, "group");
10
- __publicField(this, "row");
9
+ __publicField(this, "elements");
11
10
  this.tabela = tabela;
12
11
  this.columns = tabela.options.columns.map(
13
12
  (column) => new ColumnComponent(tabela, column)
14
13
  );
15
14
  const { group, row } = createRowGroup();
16
- this.group = group;
17
- this.row = row;
18
- this.group.className += " tabela__rowgroup-header";
19
- this.row.className += " tabela__row-header";
15
+ this.elements = { group, row };
16
+ group.className += " tabela__rowgroup-header";
17
+ row.className += " tabela__row-header";
20
18
  row.append(...this.columns.map((column) => column.element));
21
19
  }
20
+ destroy() {
21
+ this.columns.length = 0;
22
+ this.elements.group = void 0;
23
+ this.elements.row = void 0;
24
+ }
22
25
  }
23
26
  export {
24
27
  HeaderComponent
@@ -4,22 +4,34 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
4
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
6
  const helpers_dom_helpers = require("../helpers/dom.helpers.cjs");
7
- const components_cell_component = require("./cell.component.cjs");
8
7
  class RowComponent {
9
8
  constructor(tabela, data) {
10
- __publicField(this, "cells", []);
11
9
  __publicField(this, "element");
12
10
  this.tabela = tabela;
13
11
  this.data = data;
14
- this.element = helpers_dom_helpers.createRow();
15
- this.element.className += " tabela__row-body";
12
+ }
13
+ remove(pool) {
14
+ if (this.element != null) {
15
+ this.element.innerHTML = "";
16
+ pool.rows.push(this.element);
17
+ this.element.remove();
18
+ this.element = void 0;
19
+ }
20
+ }
21
+ render(pool) {
22
+ var _a, _b;
23
+ const element = this.element ?? pool.rows.shift() ?? helpers_dom_helpers.createRow();
24
+ this.element = element;
25
+ element.innerHTML = "";
26
+ const { tabela } = this;
16
27
  const { columns } = tabela.header;
17
28
  const { length } = columns;
18
29
  for (let index = 0; index < length; index += 1) {
19
- const cell = new components_cell_component.CellComponent(tabela, columns[index], this);
20
- cell.element.className += " tabela__cell-body";
21
- this.cells.push(cell);
22
- this.element.append(cell.element);
30
+ const { options } = columns[index];
31
+ (_a = pool.cells)[_b = options.field] ?? (_a[_b] = []);
32
+ const cell = pool.cells[columns[index].options.field].shift() ?? helpers_dom_helpers.createCell(options.width);
33
+ cell.textContent = String(this.data[options.field]);
34
+ element.append(cell);
23
35
  }
24
36
  }
25
37
  }
@@ -1,23 +1,35 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import { createRow } from "../helpers/dom.helpers.js";
5
- import { CellComponent } from "./cell.component.js";
4
+ import { createRow, createCell } from "../helpers/dom.helpers.js";
6
5
  class RowComponent {
7
6
  constructor(tabela, data) {
8
- __publicField(this, "cells", []);
9
7
  __publicField(this, "element");
10
8
  this.tabela = tabela;
11
9
  this.data = data;
12
- this.element = createRow();
13
- this.element.className += " tabela__row-body";
10
+ }
11
+ remove(pool) {
12
+ if (this.element != null) {
13
+ this.element.innerHTML = "";
14
+ pool.rows.push(this.element);
15
+ this.element.remove();
16
+ this.element = void 0;
17
+ }
18
+ }
19
+ render(pool) {
20
+ var _a, _b;
21
+ const element = this.element ?? pool.rows.shift() ?? createRow();
22
+ this.element = element;
23
+ element.innerHTML = "";
24
+ const { tabela } = this;
14
25
  const { columns } = tabela.header;
15
26
  const { length } = columns;
16
27
  for (let index = 0; index < length; index += 1) {
17
- const cell = new CellComponent(tabela, columns[index], this);
18
- cell.element.className += " tabela__cell-body";
19
- this.cells.push(cell);
20
- this.element.append(cell.element);
28
+ const { options } = columns[index];
29
+ (_a = pool.cells)[_b = options.field] ?? (_a[_b] = []);
30
+ const cell = pool.cells[columns[index].options.field].shift() ?? createCell(options.width);
31
+ cell.textContent = String(this.data[options.field]);
32
+ element.append(cell);
21
33
  }
22
34
  }
23
35
  }
@@ -1,10 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function createCell(width) {
3
+ function createCell(width, body) {
4
4
  const cell = document.createElement("div");
5
5
  cell.className = "tabela__cell";
6
6
  cell.role = "cell";
7
7
  cell.style.width = `${width}px`;
8
+ if (body ?? true) {
9
+ cell.classList.add("tabela__cell-body");
10
+ }
8
11
  return cell;
9
12
  }
10
13
  function createRowGroup(withRow) {
@@ -14,14 +17,18 @@ function createRowGroup(withRow) {
14
17
  if (!(withRow ?? true)) {
15
18
  return group;
16
19
  }
17
- const row = createRow();
20
+ const row = createRow(false);
18
21
  group.append(row);
19
22
  return { group, row };
20
23
  }
21
- function createRow() {
24
+ function createRow(withStyle) {
22
25
  const row = document.createElement("div");
23
26
  row.className = "tabela__row";
24
27
  row.role = "row";
28
+ if (withStyle ?? true) {
29
+ row.style.inset = "0 auto auto 0";
30
+ row.style.position = "absolute";
31
+ }
25
32
  return row;
26
33
  }
27
34
  exports.createCell = createCell;
@@ -1,8 +1,11 @@
1
- function createCell(width) {
1
+ function createCell(width, body) {
2
2
  const cell = document.createElement("div");
3
3
  cell.className = "tabela__cell";
4
4
  cell.role = "cell";
5
5
  cell.style.width = `${width}px`;
6
+ if (body ?? true) {
7
+ cell.classList.add("tabela__cell-body");
8
+ }
6
9
  return cell;
7
10
  }
8
11
  function createRowGroup(withRow) {
@@ -12,14 +15,18 @@ function createRowGroup(withRow) {
12
15
  if (!(withRow ?? true)) {
13
16
  return group;
14
17
  }
15
- const row = createRow();
18
+ const row = createRow(false);
16
19
  group.append(row);
17
20
  return { group, row };
18
21
  }
19
- function createRow() {
22
+ function createRow(withStyle) {
20
23
  const row = document.createElement("div");
21
24
  row.className = "tabela__row";
22
25
  row.role = "row";
26
+ if (withStyle ?? true) {
27
+ row.style.inset = "0 auto auto 0";
28
+ row.style.position = "absolute";
29
+ }
23
30
  return row;
24
31
  }
25
32
  export {
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
+ function getRange(body, down) {
7
+ const { elements, rows } = body;
8
+ const { clientHeight, scrollTop } = elements.group;
9
+ const first = Math.floor(scrollTop / 32);
10
+ const last = Math.min(
11
+ rows.length - 1,
12
+ Math.ceil((scrollTop + clientHeight) / 32) - 1
13
+ );
14
+ const before = Math.ceil(clientHeight / 32) * (down ? 1 : 2);
15
+ const after = Math.ceil(clientHeight / 32) * (down ? 2 : 1);
16
+ const start = Math.max(0, first - before);
17
+ const end = Math.min(rows.length - 1, last + after);
18
+ return { end, start };
19
+ }
20
+ class VirtualizationManager {
21
+ constructor(body) {
22
+ __publicField(this, "active", false);
23
+ __publicField(this, "pool", {
24
+ cells: {},
25
+ rows: []
26
+ });
27
+ __publicField(this, "top", 0);
28
+ __publicField(this, "visible", /* @__PURE__ */ new Map());
29
+ this.body = body;
30
+ this.body.elements.group.addEventListener(
31
+ "scroll",
32
+ () => {
33
+ this.onScroll();
34
+ },
35
+ {
36
+ passive: true
37
+ }
38
+ );
39
+ }
40
+ destroy() {
41
+ const { visible, pool } = this;
42
+ for (const [index, row] of visible) {
43
+ row.remove(pool);
44
+ visible.delete(index);
45
+ }
46
+ pool.cells = {};
47
+ pool.rows = [];
48
+ }
49
+ update(down) {
50
+ const { body, pool, visible } = this;
51
+ const indices = /* @__PURE__ */ new Set();
52
+ const range = getRange(body, down);
53
+ for (let index = range.start; index <= range.end; index += 1) {
54
+ indices.add(index);
55
+ }
56
+ for (const [index, row] of visible) {
57
+ if (!indices.has(index)) {
58
+ visible.delete(index);
59
+ row.remove(pool);
60
+ }
61
+ }
62
+ const fragment = document.createDocumentFragment();
63
+ for (let index = range.start; index <= range.end; index += 1) {
64
+ if (!visible.has(index)) {
65
+ const row = body.rows[index];
66
+ row.render(pool);
67
+ visible.set(index, row);
68
+ if (row.element != null) {
69
+ row.element.style.transform = `translateY(${index * 32}px)`;
70
+ fragment.append(row.element);
71
+ }
72
+ }
73
+ body.elements.group.append(fragment);
74
+ }
75
+ }
76
+ onScroll() {
77
+ if (!this.active) {
78
+ requestAnimationFrame(() => {
79
+ const top = this.body.elements.group.scrollTop;
80
+ this.update(top > this.top);
81
+ this.active = false;
82
+ this.top = top;
83
+ });
84
+ this.active = true;
85
+ }
86
+ }
87
+ }
88
+ exports.VirtualizationManager = VirtualizationManager;
@@ -0,0 +1,88 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ function getRange(body, down) {
5
+ const { elements, rows } = body;
6
+ const { clientHeight, scrollTop } = elements.group;
7
+ const first = Math.floor(scrollTop / 32);
8
+ const last = Math.min(
9
+ rows.length - 1,
10
+ Math.ceil((scrollTop + clientHeight) / 32) - 1
11
+ );
12
+ const before = Math.ceil(clientHeight / 32) * (down ? 1 : 2);
13
+ const after = Math.ceil(clientHeight / 32) * (down ? 2 : 1);
14
+ const start = Math.max(0, first - before);
15
+ const end = Math.min(rows.length - 1, last + after);
16
+ return { end, start };
17
+ }
18
+ class VirtualizationManager {
19
+ constructor(body) {
20
+ __publicField(this, "active", false);
21
+ __publicField(this, "pool", {
22
+ cells: {},
23
+ rows: []
24
+ });
25
+ __publicField(this, "top", 0);
26
+ __publicField(this, "visible", /* @__PURE__ */ new Map());
27
+ this.body = body;
28
+ this.body.elements.group.addEventListener(
29
+ "scroll",
30
+ () => {
31
+ this.onScroll();
32
+ },
33
+ {
34
+ passive: true
35
+ }
36
+ );
37
+ }
38
+ destroy() {
39
+ const { visible, pool } = this;
40
+ for (const [index, row] of visible) {
41
+ row.remove(pool);
42
+ visible.delete(index);
43
+ }
44
+ pool.cells = {};
45
+ pool.rows = [];
46
+ }
47
+ update(down) {
48
+ const { body, pool, visible } = this;
49
+ const indices = /* @__PURE__ */ new Set();
50
+ const range = getRange(body, down);
51
+ for (let index = range.start; index <= range.end; index += 1) {
52
+ indices.add(index);
53
+ }
54
+ for (const [index, row] of visible) {
55
+ if (!indices.has(index)) {
56
+ visible.delete(index);
57
+ row.remove(pool);
58
+ }
59
+ }
60
+ const fragment = document.createDocumentFragment();
61
+ for (let index = range.start; index <= range.end; index += 1) {
62
+ if (!visible.has(index)) {
63
+ const row = body.rows[index];
64
+ row.render(pool);
65
+ visible.set(index, row);
66
+ if (row.element != null) {
67
+ row.element.style.transform = `translateY(${index * 32}px)`;
68
+ fragment.append(row.element);
69
+ }
70
+ }
71
+ body.elements.group.append(fragment);
72
+ }
73
+ }
74
+ onScroll() {
75
+ if (!this.active) {
76
+ requestAnimationFrame(() => {
77
+ const top = this.body.elements.group.scrollTop;
78
+ this.update(top > this.top);
79
+ this.active = false;
80
+ this.top = top;
81
+ });
82
+ this.active = true;
83
+ }
84
+ }
85
+ }
86
+ export {
87
+ VirtualizationManager
88
+ };
package/dist/tabela.cjs CHANGED
@@ -13,13 +13,31 @@ class Tabela {
13
13
  __publicField(this, "header");
14
14
  this.element = element;
15
15
  this.options = options;
16
+ element.innerHTML = "";
16
17
  element.role = "table";
17
18
  element.classList.add("tabela");
18
19
  element.setAttribute("aria-label", options.label);
19
20
  this.header = new components_header_component.HeaderComponent(this);
20
21
  this.body = new components_body_component.BodyComponent(this);
21
22
  this.footer = new components_footer_component.FooterComponent(this);
22
- element.append(this.header.group, this.body.group, this.footer.group);
23
+ element.append(
24
+ this.header.elements.group,
25
+ this.body.elements.group,
26
+ this.footer.elements.group
27
+ );
28
+ }
29
+ destroy() {
30
+ this.body.destroy();
31
+ this.footer.destroy();
32
+ this.header.destroy();
33
+ this.options.columns = [];
34
+ this.options.data = [];
35
+ this.element.innerHTML = "";
36
+ this.element.role = "";
37
+ this.element.classList.remove("tabela");
38
+ this.element.removeAttribute("aria-label");
39
+ this.element.removeAttribute("role");
40
+ this.element = void 0;
23
41
  }
24
42
  }
25
43
  function tabela(element, options) {