forked from szymonSys/memoryGameJs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.js
289 lines (224 loc) · 8.61 KB
/
Table.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
class Table {
constructor(selector = '#results-table', rankTable = null) {
this._htmlTable = document.querySelector(selector);
this._rankTable = rankTable ? rankTable : this.loadFromStorage();
this._htmlIsSorted = false;
this._tableIsSorted = false;
this._tableProps = {
display: "flex",
width: "1300px",
flexDirection: "column",
alignItems: "center",
backgroundColor: "#ffffff",
color: "#111111"
};
this._resultElementProps = {
id: "rank-table-result",
typeClass: "result-part",
typeId: {
number: "result-number",
playerName: "result-name",
time: "result-time",
actions: "result-counter",
timeValue: "time-value",
difficulty: "result-difficulty"
},
style: {
'display': "flex",
'width': "1300px",
'height': "100px",
'flex-direction': "row",
'flex-wrap': "nowrap",
'justify-content': 'space-evenly',
'align-items': 'center',
'text-align': "center",
'line-height': "100px",
'border-bottom': "2px solid #999999"
},
childWidth: "25%",
};
this.safeToStorage();
this.renderRankTable();
}
get htmlTable() {
return this._htmlTable;
}
get rankTable() {
return this._rankTable;
}
set rankTable(table) {
return this._rankTable = table;
}
get htmlTable() {
return this._htmlTable;
}
set htmlTable(elem) {
return this._htmlTable = elem;
}
get htmlIsSorted() {
return this._htmlIsSorted
}
set htmlIsSorted(bool) {
return this._htmlIsSorted = bool;
}
get tableIsSorted() {
return this._tableIsSorted
}
set tableIsSorted(bool) {
return this._tableIsSorted = bool;
}
get resultElementProps() {
return this._resultElementProps;
}
set resultElementProps(props) {
return this._resultElementProps = props;
}
get tableProps() {
return this._tableProps;
}
set tableProps(props) {
return this._tableProps = props;
}
pushResult(result) {
if (!(result instanceof Result)) throw new Error("element which you want add is not instance of Result")
this.rankTable.push(result);
}
removeResult(result) {
if (result instanceof Result) {
this.rankTable.splice(this.rankTable.indexOf(result), 1)
} else {
throw new Error("element which you want remove is not instance of Result")
}
}
addSortedResult(result, table = this.rankTable, by = 'time', order = 'ASC') {
if (!(result instanceof Result))
throw new Error("element which you want add is not instance of Result");
if (!(by === 'time' || by === 'userName' || by === 'actions'))
throw new Error("set correct type of sorting");
this._add(result, table, this.tableIsSorted, by, order);
this.safeToStorage();
}
sortTable(tab = this.rankTable, by = 'timeValue', order = 'ASC') {
tab.sort((a, b) => {
if (!(a instanceof Result && b instanceof Result))
throw new Error("Element of Array has not require type");
if (tab === this.rankTable) this.tableIsSorted = true;
else if (tab === this.htmlTable) this.htmlIsSorted = true;
return order === 'ASC' ? a[by] - b[by] : b[by] - a[by];
});
}
safeToStorage() {
localStorage.setItem('rankTable', JSON.stringify(this.rankTable));
}
loadFromStorage() {
if (this.hasStorage()) {
return JSON.parse(localStorage.getItem('rankTable')).map(result => new Result(result._timeValue, result._time, result._actions, result._difficulty, result._playerName));
} else return [];
}
setRankTableFromStorage() {
return this.rankTable = this.loadFromStorage();
}
hasStorage() {
return localStorage.hasOwnProperty('rankTable') && JSON.parse(localStorage.getItem('rankTable')) instanceof Array ? true : false;
}
renderRankTable() {
const docFragment = document.createDocumentFragment();
if (!this.rankTable instanceof Array) throw new Error('incorrect instance of argument - it is not Array');
for (const prop in this.tableProps) {
this.htmlTable.style[prop] = this.tableProps[prop];
}
for (let i = 0; i < this.rankTable.length; i++) {
const result = document.createElement('div');
// result.id = `result-${i+1}`;
result.dataset.type = this.resultElementProps.id;
result.className = this.resultElementProps.id;
for (const prop in this.resultElementProps.style) {
result.style[prop] = this.resultElementProps.style[prop];
}
// result.dataset.timeValue = result.timeValue;
for (const id in this.resultElementProps.typeId) {
result.dataset[id] = id === 'number' ? i + 1 : this.rankTable[i][id];
if (id === 'timeValue') continue;
result.innerHTML += `<div id="${this.resultElementProps.typeId[id]}" style = "${this.resultElementProps.childWidth}" class="${this.resultElementProps.typeClass}">
<span>${id === 'number' ? i+1 : this.rankTable[i][id]}</span>
</div>`;
}
docFragment.appendChild(result);
}
this.htmlTable.appendChild(docFragment);
}
addResultToView(result, by = 'timeValue') {
function addNewNode(i) {
const newNode = document.createElement('div');
newNode.dataset.type = this.resultElementProps.id;
newNode.className = this.resultElementProps.id;
for (const prop in this.resultElementProps.style) {
newNode.style[prop] = this.resultElementProps.style[prop];
}
newNode.dataset.timeValue = result.timeValue;
for (const id in this.resultElementProps.typeId) {
if (id === "timeValue") continue;
newNode.dataset[id] = id === 'number' ? +i : result[id];
newNode.innerHTML += `<div id="${this.resultElementProps.typeId[id]}" style="${this.resultElementProps.childWidth}" class="${this.resultElementProps.typeClass}">
<span>${id === 'number' ? +i: result[id]}</span>
</div>`;
}
return newNode
}
if (!result instanceof Result)
throw new Error('parameter must be instance of Result');
let index = 0;
if (!this.htmlTable.childNodes.length)
this.htmlTable.appendChild(addNewNode.call(this, 0));
else {
for (const node of [...this.htmlTable.childNodes]) {
index++;
if (!node.previousSibling && result.timeValue <= node.dataset.timeValue) {
this.htmlTable.insertBefore(addNewNode.call(this, node.dataset.number), node);
break;
} else if (!node.nextSibling && result.timeValue >= node.dataset.timeValue) {
this.htmlTable.appendChild(addNewNode.call(this, node.dataset.number));
break;
} else if (result.timeValue > node.dataset.timeValue && result.timeValue <= node.nextSibling.dataset.timeValue) {
this.htmlTable.insertBefore(addNewNode.call(this, node.dataset.number), node.nextSibling);
break;
}
}
}
const htmlNodes = [...this.htmlTable.childNodes];
for (let i = index; i <= htmlNodes.length - 1; i++) {
htmlNodes[i].dataset.number++;
htmlNodes[i].firstChild.innerHTML = `<span>${htmlNodes[i].dataset.number}</span>`;
}
}
binarSearching = (elem, tab) => {
let searched = false,
left = 0,
right = tab.length - 1,
mid;
while (left <= right && !searched) {
mid = (left + right) / 2;
if (tab[mid] === elem) searched = true;
else if (tab[mid] < elem) left = mid + 1;
else right = mid - 1;
if (searched) return mid;
}
return -1;
}
_add = (result, table, isSorted, by = 'timeValue', order = 'ASC') => {
if (result[by] === undefined)
throw new Error("set correct type of sorting");
if (!this.rankTable.length) {
this.pushResult(result);
return;
};
if (!isSorted) this.sortTable(table, by, order);
if (table[0][by] >= result[by]) return table.unshift(result);
if (table[table.length - 1][by] < result[by]) return table.push(result);
for (let i = 0; i < table.length - 1; i++) {
if (table[i][by] < result[by] && table[i + 1][by] >= result[by])
return table.splice(i + 1, 0, result);
}
return -1;
}
}