|
| 1 | +import { buildData } from "./utils"; |
| 2 | +import { createDOMView } from "@isotope/core/lib/view"; |
| 3 | +import menu from "./menu"; |
| 4 | +import "@isotope/core/lib/configurators/attribs"; |
| 5 | +import "@isotope/core/lib/configurators/classes"; |
| 6 | +import "@isotope/core/lib/nodes/html"; |
| 7 | +import "@isotope/core/lib/nodes/map"; |
| 8 | +import "@isotope/core/lib/nodes/text"; |
| 9 | + |
| 10 | +const view = createDOMView(document.querySelector("#main")); |
| 11 | +const container = view.div({ |
| 12 | + state: { |
| 13 | + data: [], |
| 14 | + selected: undefined, |
| 15 | + }, |
| 16 | +}); |
| 17 | +const select = (id) => { |
| 18 | + return container.setState({ selected: id }); |
| 19 | +}; |
| 20 | +const remove = (num) => { |
| 21 | + const data = container.getState("data"); |
| 22 | + const idx = data.findIndex((d) => d.id === num); |
| 23 | + |
| 24 | + container.setState({ data: [...data.slice(0, idx), ...data.slice(idx + 1)] }); |
| 25 | +}; |
| 26 | +const add = () => { |
| 27 | + return container.setState({ |
| 28 | + data: container.getState("data").concat(buildData(1000)), |
| 29 | + }); |
| 30 | +}; |
| 31 | +const clear = () => { |
| 32 | + container.setState({ data: [], selected: undefined }); |
| 33 | +}; |
| 34 | +const partialUpdate = () => { |
| 35 | + const data = container.getState("data"); |
| 36 | + |
| 37 | + for (let i = 0; i < data.length; i += 10) { |
| 38 | + data[i].label += " !!!"; |
| 39 | + } |
| 40 | + container.setState({ data }); |
| 41 | +}; |
| 42 | +const run = () => { |
| 43 | + container.setState({ |
| 44 | + data: buildData(1000), |
| 45 | + selected: undefined, |
| 46 | + }); |
| 47 | +}; |
| 48 | +const runLots = () => { |
| 49 | + container.setState({ |
| 50 | + data: buildData(10000), |
| 51 | + selected: undefined, |
| 52 | + }); |
| 53 | +}; |
| 54 | +const swapRows = () => { |
| 55 | + const data = container.getState("data"); |
| 56 | + if (data.length > 998) { |
| 57 | + container.setState({ |
| 58 | + data: [data[0], data[998], ...data.slice(2, 998), data[1], data[999]], |
| 59 | + }); |
| 60 | + } |
| 61 | +}; |
| 62 | +const btns = [ |
| 63 | + { id: "run", onClick: run, text: "Create 1,000 rows" }, |
| 64 | + { id: "runlots", onClick: runLots, text: "Create 10,000 rows" }, |
| 65 | + { id: "add", onClick: add, text: "Append 1,000 rows" }, |
| 66 | + { id: "update", onClick: partialUpdate, text: "Update every 10th row" }, |
| 67 | + { id: "clear", onClick: clear, text: "Clear" }, |
| 68 | + { id: "swaprows", onClick: swapRows, text: "Swap Rows" }, |
| 69 | +]; |
| 70 | + |
| 71 | +container.$(menu(btns)); |
| 72 | + |
| 73 | +container.link( |
| 74 | + view |
| 75 | + .table({ |
| 76 | + classes: ["table", "table-hover", "table-striped", "test-data"], |
| 77 | + }) |
| 78 | + .tbody() |
| 79 | + .map( |
| 80 | + () => container.getState("data"), |
| 81 | + (row, tbody) => { |
| 82 | + if (row) { |
| 83 | + const tableRow = tbody.tr({ |
| 84 | + classes: () => ({ |
| 85 | + danger: container.getState("selected") === row.id, |
| 86 | + }), |
| 87 | + }); |
| 88 | + tableRow.td({ classes: ["col-md-1"] }).text(`${row.id}`); |
| 89 | + tableRow.link( |
| 90 | + tableRow |
| 91 | + .td({ classes: ["col-md-4"] }) |
| 92 | + .a() |
| 93 | + .text(() => row.label) |
| 94 | + .on("click", () => { |
| 95 | + select(row.id); |
| 96 | + }) |
| 97 | + ); |
| 98 | + tableRow |
| 99 | + .td({ classes: ["col-md-1"] }) |
| 100 | + .a() |
| 101 | + .on("click", () => remove(row.id)) |
| 102 | + .span({ |
| 103 | + attribs: { "aria-hidden": "true" }, |
| 104 | + classes: ["glyphicon", "glyphicon-remove"], |
| 105 | + }); |
| 106 | + tableRow.td({ classes: ["col-md-6"] }); |
| 107 | + |
| 108 | + return tableRow; |
| 109 | + } |
| 110 | + } |
| 111 | + ) |
| 112 | +); |
| 113 | +view.span({ |
| 114 | + attribs: { "aria-hidden": "true" }, |
| 115 | + classes: ["preloadicon", "glyphicon", "glyphicon-remove"], |
| 116 | +}); |
0 commit comments