|
1 | 1 | import { html, render } from '../node_modules/lit-html/lit-html.js'; |
2 | 2 | import { repeat } from '../node_modules/lit-html/directives/repeat.js'; |
3 | | -import { guard } from '../node_modules/lit-html/directives/guard.js'; |
4 | 3 |
|
5 | 4 | const adjectives = [ |
6 | 5 | 'pretty', 'large', 'big', 'small', 'tall', 'short', 'long', 'handsome', 'plain', 'quaint', 'clean', 'elegant', 'easy', 'angry', 'crazy', 'helpful', 'mushy', 'odd', 'unsightly', 'adorable', 'important', 'inexpensive', 'cheap', 'expensive', 'fancy']; |
@@ -28,49 +27,46 @@ const clear = () => { |
28 | 27 | _render(); |
29 | 28 | }; |
30 | 29 | const interact = e => { |
31 | | - const interaction = e.target.getAttribute('data-interaction'); |
32 | | - const id = parseInt( |
33 | | - e.target.parentNode.id || |
34 | | - e.target.parentNode.parentNode.id || |
35 | | - e.target.parentNode.parentNode.parentNode.id |
36 | | - ); |
| 30 | + const td = e.target.closest('td'); |
| 31 | + const interaction = td.getAttribute('data-interaction'); |
| 32 | + const id = parseInt(td.parentNode.id); |
37 | 33 | if (interaction === 'delete') { |
38 | | - del(id) |
| 34 | + del(id); |
39 | 35 | } else { |
40 | | - select(id) |
| 36 | + select(id); |
41 | 37 | } |
42 | 38 | }; |
43 | 39 | const del = id => { |
44 | 40 | const idx = data.findIndex(d => d.id === id); |
45 | | - data.splice(idx, 1) |
| 41 | + data.splice(idx, 1); |
46 | 42 | _render(); |
47 | 43 | }; |
48 | 44 | const select = id => { |
49 | 45 | if (selected > -1) { |
50 | | - data[selected] = { ...data[selected], selected: false } |
| 46 | + data[selected].selected = false; |
51 | 47 | } |
52 | 48 | selected = data.findIndex(d => d.id === id); |
53 | | - data[selected] = { ...data[selected], selected: true } |
| 49 | + data[selected].selected = true; |
54 | 50 | _render(); |
55 | 51 | }; |
56 | 52 | const swapRows = () => { |
57 | 53 | if (data.length > 998) { |
58 | | - const tmp = data[1] |
59 | | - data[1] = data[998] |
60 | | - data[998] = tmp |
| 54 | + const tmp = data[1]; |
| 55 | + data[1] = data[998]; |
| 56 | + data[998] = tmp; |
61 | 57 | } |
62 | 58 | _render(); |
63 | 59 | }; |
64 | 60 | const update = () => { |
65 | | - for(let i = 0; i < data.length; i += 10) { |
66 | | - const item = data[i] |
67 | | - data[i] = { ...item, label: item.label + ' !!!' } |
| 61 | + for (let i = 0; i < data.length; i += 10) { |
| 62 | + const item = data[i]; |
| 63 | + data[i].label += ' !!!'; |
68 | 64 | } |
69 | 65 | _render(); |
70 | 66 | }; |
71 | 67 | const buildData = count => { |
72 | 68 | const data = []; |
73 | | - for (var i = 0; i < count; i++) { |
| 69 | + for (let i = 0; i < count; i++) { |
74 | 70 | data.push({ |
75 | 71 | id: did++, |
76 | 72 | label: `${adjectives[_random(adjectives.length)]} ${colours[_random(colours.length)]} ${nouns[_random(nouns.length)]}`, |
@@ -126,20 +122,19 @@ const template = () => html` |
126 | 122 | <table @click=${interact} class="table table-hover table-striped test-data"> |
127 | 123 | <tbody>${repeat(data, |
128 | 124 | item => item.id, |
129 | | - item => guard(item, () => html` |
| 125 | + item => html` |
130 | 126 | <tr id=${item.id} class=${item.selected ? 'danger' : ''}> |
131 | 127 | <td class="col-md-1">${item.id}</td> |
132 | | - <td data-interaction='select' class="col-md-4"> |
133 | | - <a data-interaction='select'>${item.label}</a> |
| 128 | + <td class="col-md-4"> |
| 129 | + <a>${item.label}</a> |
134 | 130 | </td> |
135 | 131 | <td data-interaction='delete' class="col-md-1"> |
136 | | - <a data-interaction='delete'> |
137 | | - <span data-interaction='delete' class="glyphicon glyphicon-remove" aria-hidden="true"></span> |
| 132 | + <a> |
| 133 | + <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> |
138 | 134 | </a> |
139 | 135 | </td> |
140 | 136 | <td class="col-md-6"></td> |
141 | | - </tr>`) |
142 | | - )} |
| 137 | + </tr>`)} |
143 | 138 | </tbody> |
144 | 139 | </table> |
145 | 140 | <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span> |
|
0 commit comments