|
1 |
| -import { App, TModel, $Dom } from "targetj"; |
| 1 | +import { App, TModel, $Dom, Template } from "targetj"; |
2 | 2 | import { buildData } from './data';
|
3 | 3 |
|
4 | 4 | App(new TModel("benchmark", {
|
5 | 5 | canHaveDom: false,
|
6 | 6 | runButton() {
|
7 | 7 | return new TModel('run', {
|
8 |
| - onClickEvent() { |
| 8 | + onClick() { |
9 | 9 | const rows = this.getParent().findChild('rows');
|
10 | 10 | rows.removeAll();
|
11 |
| - rows.activateTarget('createRows', 1000); |
| 11 | + rows.activateTarget('buildData', 1000); |
| 12 | + rows.activateTarget('createRows'); |
12 | 13 | }
|
13 | 14 | });
|
14 | 15 | },
|
15 | 16 | runAlotButton() {
|
16 | 17 | return new TModel('runlots', {
|
17 |
| - onClickEvent() { |
| 18 | + onClick() { |
18 | 19 | const rows = this.getParent().findChild('rows');
|
19 | 20 | rows.removeAll();
|
20 |
| - rows.activateTarget('createRows', 10000); |
| 21 | + rows.activateTarget('buildData', 10000); |
| 22 | + rows.activateTarget('createRows'); |
21 | 23 | }
|
22 | 24 | });
|
23 | 25 | },
|
24 | 26 | addButton() {
|
25 | 27 | return new TModel('add', {
|
26 |
| - onClickEvent() { |
| 28 | + onClick() { |
27 | 29 | const rows = this.getParent().findChild('rows');
|
28 |
| - rows.activateTarget('createRows', 1000); |
| 30 | + rows.activateTarget('buildData', 1000); |
| 31 | + rows.activateTarget('createRows'); |
29 | 32 | }
|
30 | 33 | });
|
31 | 34 | },
|
32 | 35 | updateButton() {
|
33 | 36 | return new TModel('update', {
|
34 |
| - onClickEvent() { |
| 37 | + onClick() { |
35 | 38 | const rows = this.getParent().findChild('rows');
|
36 |
| - const length = rows.getChildren().length; |
37 |
| - for (let i = 0; i < length; i += 10) { |
38 |
| - rows.getChild(i).activateTarget('updateContent'); |
39 |
| - }; |
| 39 | + rows.activateTarget('updateEvery10thRow'); |
40 | 40 | }
|
41 | 41 | });
|
42 | 42 | },
|
43 | 43 | clearButton() {
|
44 | 44 | return new TModel('clear', {
|
45 |
| - onClickEvent() { |
| 45 | + onClick() { |
46 | 46 | this.getParent().findChild('rows').removeAll();
|
47 | 47 | }
|
48 | 48 | });
|
49 | 49 | },
|
50 | 50 | swapRows() {
|
51 | 51 | return new TModel('swaprows', {
|
52 |
| - onClickEvent() { |
| 52 | + onClick() { |
53 | 53 | const rows = this.getParent().findChild('rows');
|
54 |
| - const rowChildren = rows.allChildrenList; |
55 |
| - if (rowChildren.length > 998) { |
56 |
| - rows.moveChild(rowChildren[1], 998); |
57 |
| - rows.moveChild(rowChildren[998], 1); |
| 54 | + const rowCount = rows.$dom.elementCount(); |
| 55 | + if (rowCount > 998) { |
| 56 | + rows.activateTarget('swap', [1, 998]); |
58 | 57 | }
|
59 | 58 | }
|
60 | 59 | });
|
61 | 60 | },
|
62 | 61 | rows() {
|
63 | 62 | const rows = new TModel('rows', {
|
64 | 63 | isVisible: true,
|
65 |
| - containerOverflowMode: 'always', |
66 |
| - rectTop() { return this.$dom.getBoundingClientRect().top; }, |
67 |
| - absY() { return this.val('rectTop') - $Dom.getWindowScrollTop(); }, |
68 | 64 | domHolder: true,
|
69 |
| - onDomEvent: [ 'rectTop', 'absY' ], |
70 |
| - onWindowScrollEvent: 'absY', |
71 |
| - _createRows: { |
72 |
| - parallel: true, |
73 |
| - cycles: 4, |
74 |
| - value() { |
75 |
| - buildData(this._createRows / 5).forEach(data => { |
76 |
| - this.addChild(new TModel('row', { |
77 |
| - keepEventDefault: true, |
78 |
| - baseElement: 'tr', |
79 |
| - defaultStyling: false, |
80 |
| - excludeHeight: true, |
81 |
| - height: 36, |
82 |
| - canDeleteDom: false, |
83 |
| - textOnly: false, |
84 |
| - domHolder: true, |
85 |
| - _css() { return this._css; }, |
86 |
| - onClickEvent(target) { |
87 |
| - if (target?.className?.endsWith('remove')) { |
88 |
| - rows.removeChild(this); |
89 |
| - } else { |
90 |
| - rows.val('selectedRow')?.activateTarget('css', ''); |
91 |
| - rows.val('selectedRow', this); |
92 |
| - this.activateTarget('css', 'danger'); |
93 |
| - } |
94 |
| - }, |
95 |
| - _updateContent() { |
96 |
| - this.$dom.child(1).children[0].textContent += ' !!!'; |
97 |
| - }, |
98 |
| - html: `<td class="col-md-1">${data.id}</td> |
99 |
| - <td class="col-md-4"><a>${data.label}</a></td> |
100 |
| - <td class="col-md-1"><a><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td> |
101 |
| - <td class="col-md-6"></td>` |
102 |
| - })); |
103 |
| - }); |
| 65 | + rowTemplate() { |
| 66 | + const template = document.createElement('template'); |
| 67 | + template.innerHTML = ` |
| 68 | + <tr> |
| 69 | + <td class="col-md-1 id-cell"></td> |
| 70 | + <td class="col-md-4 label-cell"><a></a></td> |
| 71 | + <td class="col-md-1 remove-cell"><a><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td> |
| 72 | + <td class="col-md-6"></td> |
| 73 | + </tr> |
| 74 | + `; |
| 75 | + return template; |
| 76 | + }, |
| 77 | + _buildData() { |
| 78 | + return buildData(this._buildData); |
| 79 | + }, |
| 80 | + _createRows() { |
| 81 | + const fragment = document.createDocumentFragment(); |
| 82 | + this.val('buildData').forEach((data, index) => { |
| 83 | + const rowClone = this.val('rowTemplate').content.cloneNode(true); |
| 84 | + rowClone.querySelector('tr').setAttribute('data-id', `${index}`); |
| 85 | + rowClone.querySelector('.id-cell').textContent = data.id; |
| 86 | + rowClone.querySelector('.label-cell a').textContent = data.label; |
| 87 | + |
| 88 | + fragment.appendChild(rowClone); |
| 89 | + }); |
| 90 | + this.$dom.appendElement(fragment); |
| 91 | + }, |
| 92 | + _updateEvery10thRow() { |
| 93 | + const rows = this.$dom.querySelectorAll('tr'); |
| 94 | + |
| 95 | + for (let i = 0; i < rows.length; i += 10) { |
| 96 | + const labelCell = rows[i].querySelector('.label-cell a'); |
| 97 | + if (labelCell) { |
| 98 | + labelCell.textContent += ' !!!'; |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | + _swap() { |
| 103 | + const row1 = this.$dom.querySelector(`[data-id="${this._swap[0]}"]`); |
| 104 | + const row2 = this.$dom.querySelector(`[data-id="${this._swap[1]}"]`); |
| 105 | + this.$dom.swapElements(row1, row2); |
| 106 | + }, |
| 107 | + onClick(target) { |
| 108 | + const rowElement = target.closest('tr'); |
| 109 | + if (target?.className?.endsWith('remove')) { |
| 110 | + this.$dom.removeElement(rowElement); |
| 111 | + } else { |
| 112 | + this.val('selectedRow', rowElement); |
| 113 | + this.val('selectedRow').setAttribute('class', 'danger'); |
| 114 | + this.lastVal('selectedRow')?.setAttribute('class', ''); |
104 | 115 | }
|
105 |
| - } |
| 116 | + } |
106 | 117 | });
|
107 |
| - |
108 | 118 | return rows;
|
109 | 119 | },
|
110 | 120 | children() {
|
|
0 commit comments