Skip to content

Commit f3901eb

Browse files
author
Henrik jJaven
committed
old version
1 parent 3ea69fc commit f3901eb

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

frameworks/non-keyed/doohtml/js/Main.class.js

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "b
77
const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"]
88

99
const lenA = adjectives.length, lenB = colours.length, lenC = nouns.length
10-
const DEFAULT_SIZE = 1000
11-
const CHILD_1 = 1
12-
const CHILD_998 = 998
10+
1311
Doo.define(
1412
class Main extends Doo {
1513
constructor() {
1614
super(100)
15+
this.scrollTarget = '.table'
1716
this.defaultDataSet = 'rows'
1817
this.ID = 1
1918
this.data = {
@@ -24,7 +23,7 @@ Doo.define(
2423
this.runLots = this.runLots.bind(this)
2524
this.update = this.update.bind(this)
2625
this.clear = this.clear.bind(this)
27-
this.swapRows = this.swapRows.bind(this)
26+
this.swaprows = this.swapRows.bind(this)
2827
this.addEventListeners()
2928
this.selectedRow = undefined
3029
document.querySelector(".ver").innerHTML += ` ${Doo.version} (non-keyed)`
@@ -33,7 +32,7 @@ Doo.define(
3332

3433
async dooAfterRender() {
3534
this.tbody = this.shadow.querySelector('#tbody')
36-
this.tbody.addEventListener('click', e => {
35+
this.shadow.querySelector(this.scrollTarget).addEventListener('click', e => {
3736
e.preventDefault()
3837
if (e.target.parentElement.matches('.remove')) {
3938
this.delete(e.target.parentElement)
@@ -51,10 +50,10 @@ Doo.define(
5150
return undefined
5251
}
5352

54-
buildData(count = DEFAULT_SIZE) {
55-
const data = new Array(count)
53+
buildData(count = 1000) {
54+
const data = [];
5655
for (let i = 0; i < count; i++) {
57-
data[i] = {id: this.ID++,label: `${adjectives[_random(lenA)]} ${colours[_random(lenB)]} ${nouns[_random(lenC)]}`}
56+
data.push({id: this.ID++,label: adjectives[_random(lenA)] + " " + colours[_random(lenB)] + " " + nouns[_random(lenC)]})
5857
}
5958
return data
6059
}
@@ -63,13 +62,13 @@ Doo.define(
6362
let row = this.getParentRow(elem)
6463
if (row) {
6564
this.tbody.removeChild(row)
66-
this.data.rows.splice(row.rowIndex,1)
65+
this.data.rows[row.getAttribute('key')] = undefined
6766
}
6867
}
6968

7069
run() {
7170
this.data.rows = this.buildData()
72-
if (this.tbody.childNodes.length >= this.data.rows.length) {
71+
if (this.tbody.childNodes.length > this.data.rows.length) {
7372
this.tbody.textContent = ''
7473
}
7574
this.renderTable()
@@ -114,40 +113,49 @@ Doo.define(
114113
isRowSelected(elem) {
115114
return elem.classList.contains('danger')
116115
}
117-
118116
swapRows() {
119-
if (this.data.rows.length > CHILD_998) {
120-
let node1 = this.tbody.childNodes[CHILD_1],
121-
swapRow = this.tbody.childNodes[CHILD_998],
122-
node999 = swapRow.nextSibling,
123-
row1 = this.data.rows[CHILD_1]
117+
const A=1,B=998
118+
if (this.data.rows.length > B) {
119+
120+
let a = this.tbody.childNodes[A],
121+
b = this.tbody.childNodes[B],
122+
row1 = this.data.rows[1]
123+
124+
this.data.rows[A] = this.data.rows[B];
125+
this.data.rows[B] = row1
124126

125-
this.data.rows[CHILD_1] = this.data.rows[CHILD_998];
126-
this.data.rows[CHILD_998] = row1
127-
this.tbody.insertBefore(swapRow, node1)
128-
this.tbody.insertBefore(node1, node999)
127+
const selected1 = this.isRowSelected(a)
128+
const selected2 = this.isRowSelected(b)
129+
this.updateRow(A, this.data.rows, this.tbody)
130+
this.updateRow(B, this.data.rows, this.tbody)
131+
132+
if (selected1) {
133+
this.select(this.tbody.childNodes[B])
134+
}
135+
if (selected2) {
136+
this.select(this.tbody.childNodes[A])
137+
}
129138
}
130-
}
139+
}
140+
131141

132142
addEventListeners() {
133-
const actions = {
134-
'run': this.run,
135-
'runlots': this.runLots,
136-
'add': this.add,
137-
'update': this.update,
138-
'clear': this.clear,
139-
'swaprows': this.swapRows,
140-
runAction: (e) => {
141-
e.preventDefault()
142-
if (actions[e.target.id]) {
143-
actions[e.target.id]()
144-
}
143+
document.getElementById("main").addEventListener('click', e => {
144+
e.preventDefault()
145+
if (e.target.matches('#runlots')) {
146+
this.runLots()
147+
} else if (e.target.matches('#run')) {
148+
this.run()
149+
} else if (e.target.matches('#add')) {
150+
this.add()
151+
} else if (e.target.matches('#update')) {
152+
this.update()
153+
} else if (e.target.matches('#clear')) {
154+
this.clear()
155+
} else if (e.target.matches('#swaprows')) {
156+
this.swapRows()
145157
}
146-
}
147-
148-
document.getElementById("main").addEventListener('click', e => actions.runAction(e))
158+
})
149159
}
150-
async connectedCallback() {
151-
super.connectedCallback()
152-
}
153-
})
160+
}
161+
)

0 commit comments

Comments
 (0)