Skip to content

Commit a2cff34

Browse files
committed
mutraction implementation
1 parent 38c8ee7 commit a2cff34

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["@babel/plugin-syntax-jsx", "mutraction-dom/compile-jsx"]
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>Mutraction-"non-keyed"</title>
6+
<link href="/css/currentStyle.css" rel="stylesheet"/>
7+
</head>
8+
<body>
9+
<span className="preloadicon glyphicon glyphicon-remove" ariaHidden="true"></span>
10+
<div id='main'>
11+
</div>
12+
<script src='dist/index.js'></script>
13+
</body>
14+
</html>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "mutraction-dom-template",
3+
"type": "module",
4+
"version": "1.0.0",
5+
"description": "Mutraction demo",
6+
"main": "index.js",
7+
"js-framework-benchmark": {
8+
"frameworkVersionFromPackage": "mutraction-dom",
9+
"frameworkHomeURL": "https://mutraction.dev/"
10+
},
11+
"scripts": {
12+
"dev": "npm run build",
13+
"build-prod": "npm run build",
14+
"build": "npx tsc && npm run transform && npm run bundle",
15+
"transform": "npx babel out -d out2",
16+
"bundle": "npx esbuild out2/index.js --bundle --format=esm --outfile=dist/index.js"
17+
},
18+
"author": "Tom Theisen",
19+
"license": "MIT",
20+
"devDependencies": {
21+
"@babel/cli": "^7.22.10",
22+
"@babel/core": "^7.22.10",
23+
"@babel/preset-env": "^7.22.10",
24+
"esbuild": "^0.19.1",
25+
"typescript": "^5.1.6"
26+
},
27+
"dependencies": {
28+
"mutraction-dom": "0.24.0"
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export interface RowItem {
2+
label: string;
3+
id: number;
4+
}
5+
6+
function choose(words: string[]) {
7+
return words[Math.floor(Math.random() * words.length)];
8+
}
9+
10+
let id = 1;
11+
12+
const adjectives = ['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'];
13+
const colors = ['red', 'yellow', 'blue', 'green', 'pink', 'brown', 'purple', 'brown', 'white', 'black', 'orange'];
14+
const nouns = ['table', 'chair', 'house', 'bbq', 'desk', 'car', 'pony', 'cookie', 'sandwich', 'burger', 'pizza', 'mouse', 'keyboard'];
15+
16+
export function buildData(count = 1000): RowItem[] {
17+
const data = [];
18+
19+
for (let i = 0; i < count; i++) {
20+
const label = `${ choose(adjectives) } ${ choose(colors) } ${ choose(nouns) }`;
21+
data.push({ id, label });
22+
id++;
23+
}
24+
25+
return data;
26+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { track, defaultTracker, ForEach } from "mutraction-dom";
2+
import { buildData, type RowItem } from "./build-dummy-data.js";
3+
4+
const model = track({
5+
items: [] as RowItem[],
6+
selected: undefined as RowItem | undefined,
7+
});
8+
9+
function select(item: RowItem) {
10+
model.selected = item;
11+
}
12+
13+
function create(n: number) {
14+
model.items.splice(0, model.items.length, ...buildData(n));
15+
}
16+
17+
function append(n: number) {
18+
model.items.push(...buildData(n));
19+
}
20+
21+
function update() {
22+
defaultTracker.startTransaction();
23+
for (let i = 0; i < model.items.length; i += 10) {
24+
model.items[i].label += " !!!";
25+
}
26+
defaultTracker.commit();
27+
}
28+
29+
function clear() {
30+
model.items.length = 0;
31+
}
32+
33+
function swapRows() {
34+
if (model.items.length > 998) {
35+
const i1 = 1, i2 = 998;
36+
[model.items[i1], model.items[i2]] = [model.items[i2], model.items[i1]];
37+
}
38+
}
39+
40+
function remove(i: number) {
41+
model.items.splice(i, 1);
42+
}
43+
44+
const app =
45+
<div className="container">
46+
<div className="jumbotron">
47+
<div className="row">
48+
<div className="col-md-6">
49+
<h1>Mutraction-"non-keyed"</h1>
50+
</div>
51+
<div className="col-md-6">
52+
<div className="row">
53+
<div className="col-sm-6 smallpad">
54+
<button type='button' className='btn btn-primary btn-block' id='run' onclick={ () => create(1_000) }>Create 1,000 rows</button>
55+
</div>
56+
<div className="col-sm-6 smallpad">
57+
<button type='button' className='btn btn-primary btn-block' id='runlots' onclick={ () => create(10_000) }>Create 10,000 rows</button>
58+
</div>
59+
<div className="col-sm-6 smallpad">
60+
<button type='button' className='btn btn-primary btn-block' id='add' onclick={ () => append(1_000) }>Append 1,000 rows</button>
61+
</div>
62+
<div className="col-sm-6 smallpad">
63+
<button type='button' className='btn btn-primary btn-block' id='update' onclick={ update }>Update every 10th row</button>
64+
</div>
65+
<div className="col-sm-6 smallpad">
66+
<button type='button' className='btn btn-primary btn-block' id='clear' onclick={ clear }>Clear</button>
67+
</div>
68+
<div className="col-sm-6 smallpad">
69+
<button type='button' className='btn btn-primary btn-block' id='swaprows' onclick={ swapRows }>Swap Rows</button>
70+
</div>
71+
</div>
72+
</div>
73+
</div>
74+
</div>
75+
<table className="table table-hover table-striped test-data">
76+
<tbody id="tbody">
77+
{ ForEach(model.items, (item, i) =>
78+
<tr classList={{ danger: item === model.selected }}>
79+
<td className="col-md-1">{ item.id }</td>
80+
<td className="col-md-4">
81+
<a className="lbl" onclick={() => select(item)}>{ item.label }</a>
82+
</td>
83+
<td className="col-md-1">
84+
<a className="remove" onclick={ () => remove(i) }><span className="remove glyphicon glyphicon-remove" ariaHidden="true"></span></a>
85+
</td>
86+
<td className="col-md-6"></td>
87+
</tr>
88+
) }
89+
</tbody>
90+
</table>
91+
</div>;
92+
93+
document.querySelector("#main")!.append(app);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2022",
4+
"jsx": "preserve",
5+
"jsxImportSource": "mutraction-dom",
6+
"module": "Node16",
7+
"moduleResolution": "Node16",
8+
"outDir": "./out/",
9+
"strict": true,
10+
"noImplicitAny": true,
11+
},
12+
"include": ["src"]
13+
}

0 commit comments

Comments
 (0)