-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
77 lines (63 loc) · 1.94 KB
/
main.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
import { add, remove, search } from './helper/rbtree';
import { Tree } from './classes/tree';
require('./styles.css');
const addForm = document.forms.insert_form;
const delForm = document.forms.delete_form;
const searchForm = document.forms.search_form;
const inputForAdd = addForm.elements.numberadd;
const inputForDel = delForm.elements.numberdel;
const inputForSearch = searchForm.elements.numbersearch;
const btnAdd = addForm.elements.insert;
const btnDel = delForm.elements.del;
const btnSearch = searchForm.elements.search;
let rbTreeList = null;
let tree = {};
function handlerAdd(e) {
e.preventDefault();
const n = inputForAdd.value;
if (n.search(/^\d{1,6}$/) === -1) {
alert('You must enter only numbers');
return;
}
rbTreeList = add(rbTreeList, parseInt(n));
inputForAdd.value = '';
// Reset canvas
for (const row of document.querySelectorAll('.row')) {
row.innerHTML = '';
}
// Print Canvas
tree = new Tree(rbTreeList);
console.log(tree);
tree.display();
}
function handlerDel(e) {
e.preventDefault();
const n = inputForDel.value;
if (n.search(/^\d{1,6}$/) === -1) {
alert('You must enter only numbers');
return;
}
rbTreeList = remove(rbTreeList, parseInt(n, 10));
inputForDel.value = '';
for (const row of document.querySelectorAll('.row')) {
row.innerHTML = '';
}
tree = new Tree(rbTreeList);
console.log(tree);
tree.display();
}
function handlerSearch(e) {
e.preventDefault();
const n = inputForSearch.value;
if (n.search(/^\d{1,6}$/) === -1) {
alert('You must enter only numbers');
return;
}
search(rbTreeList, n) ? alert(`Node ${n} found`) : alert('Node not found');
}
btnAdd.addEventListener('click', handlerAdd);
addForm.addEventListener('submit', handlerAdd);
btnDel.addEventListener('click', handlerDel);
delForm.addEventListener('submit', handlerDel);
btnSearch.addEventListener('submit', handlerSearch);
searchForm.addEventListener('submit', handlerSearch);