-
Notifications
You must be signed in to change notification settings - Fork 844
/
Copy pathMain.class.js
158 lines (136 loc) · 4.1 KB
/
Main.class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict'
const _random = max => Math.random() * max | 0
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"]
const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"]
const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"]
const lenA = adjectives.length, lenB = colours.length, lenC = nouns.length
Doo.define(
class Main extends Doo {
constructor() {
super(100)
this.scrollTarget = '.table'
this.defaultDataSet = 'rows'
this.ID = 1
this.data = {
[this.defaultDataSet]: []
}
this.add = this.add.bind(this)
this.run = this.run.bind(this)
this.runLots = this.runLots.bind(this)
this.update = this.update.bind(this)
this.clear = this.clear.bind(this)
this.swaprows = this.swapRows.bind(this)
this.addEventListeners()
this.selectedRow = undefined
document.querySelector(".ver").innerHTML += ` ${Doo.version} (keyed)`
document.title += ` ${Doo.version} (keyed)`
}
async dooAfterRender() {
this.tbody = this.shadow.querySelector('#tbody')
this.shadow.querySelector(this.scrollTarget).addEventListener('click', e => {
e.preventDefault()
if (e.target.parentElement.matches('.remove')) {
this.delete(e.target.parentElement)
} else if (e.target.tagName === 'A') {
this.select(e.target)
}
})
}
getParentRow(elem) {
while (elem) {
if (elem.tagName === "TR") {return elem}
elem = elem.parentNode
}
return undefined
}
buildData(count = 1000) {
const data = []
for (let i = 0; i < count; i++) {
data.push({id: this.ID++,label: adjectives[_random(lenA)] + " " + colours[_random(lenB)] + " " + nouns[_random(lenC)]})
}
return data
}
getIndex(row) {
let idx = this.data.rows.findIndex((item, i) => {
if (item.id === row.key) {
return i
}
})
return idx
}
delete(elem) {
let row = this.getParentRow(elem)
if (row) {
let idx = this.getIndex(row)
this.tbody.removeChild(row)
if (idx !== undefined) {
this.data.rows.splice(idx,1)
}
}
}
run() {
this.data.rows = this.buildData()
this.tbody.textContent = ''
this.renderTable()
}
add() {
let startRow = this.data.rows.length
this.data.rows = this.data.rows.concat(this.buildData())
this.renderTable(this.data.rows, startRow)
}
runLots() {
this.data.rows = this.buildData(10000)
this.tbody.textContent = ''
this.renderTable()
}
update() {
for (let i=0, len = this.data.rows.length;i<len;i+=10) {
this.tbody.childNodes[i].childNodes[1].childNodes[0].innerText = this.data.rows[i].label += ' !!!'
}
}
select(elem) {
if (this.selectedRow) {
this.selectedRow.classList.remove('danger')
this.selectedRow = undefined
}
let row = this.getParentRow(elem)
if (row) {
row.classList.toggle('danger')
this.selectedRow = row
}
}
clear() {
this.data.rows = []
this.tbody.textContent = ''
}
swapRows() {
if (this.data.rows.length>10) {
let node1 = this.tbody.childNodes[1]
let node2 = this.tbody.childNodes[998]
let row1 = this.data.rows[1]
this.data.rows[1] = this.data.rows[998]
this.data.rows[998] = row1
this.tbody.insertBefore(node2, node1)
this.tbody.insertBefore(node1, this.tbody.childNodes[999])
}
}
addEventListeners() {
document.getElementById("main").addEventListener('click', e => {
e.preventDefault()
if (e.target.matches('#runlots')) {
this.runLots()
} else if (e.target.matches('#run')) {
this.run()
} else if (e.target.matches('#add')) {
this.add()
} else if (e.target.matches('#update')) {
this.update()
} else if (e.target.matches('#clear')) {
this.clear()
} else if (e.target.matches('#swaprows')) {
this.swapRows()
}
})
}
}
)