Repository navigation
Expand file tree
/
Copy pathscripts.js
More file actions
127 lines (101 loc) · 3.69 KB
/
Copy pathscripts.js
File metadata and controls
127 lines (101 loc) · 3.69 KB
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
function Stack() {
this.items = []
this.push = () => {
let error = document.getElementById("error");
error.innerText = "";
let input = document.getElementById("value");
let value = input.value;
input.value = "";
if (!value) {
error.innerText = "Valor não informado";
return;
}
if (value > 1000 || value < -999) {
error.innerText = "O valor deve ser entre: -999 e 999";
return;
}
this.items.push(value);
this.updateStackView();
}
this.pop = () => {
if (this.items.length == 0) return;
this.items.pop();
this.updateStackView();
}
this.clear = () => {
this.items = [];
this.updateStackView();
}
this.updateStackView = () => {
let stackTag = document.getElementById("stack-view");
let size = this.items.length;
let viewSize = this.getViewSize(stackTag);
if (size <= 0) {
stackTag.innerHTML = "";
let content = document.createElement('div');
content.className = 'empty';
stackTag.appendChild(content);
this.updateSizeTag(0);
this.updateTopItemTag("");
return;
} else {
this.removeEmptyItem(stackTag);
}
if (size == viewSize - 1) {
stackTag.removeChild(stackTag.firstElementChild);
} else if (size == viewSize + 1) {
let content = document.createElement('div');
content.innerText = this.items[this.items.length-1];
stackTag.prepend(content);
}
this.createClassNameForItems(stackTag);
this.updateSizeTag(size);
this.updateTopItemTag(this.items[size-1])
}
this.removeEmptyItem = (stackTag) => {
if (this.items.length > 1) return;
stackTag.childNodes.forEach(item => {
if (item.className.includes('empty')) item.parentNode.removeChild(item);
});
}
this.getViewSize = (stackTag) => {
let size = 0;
stackTag.childNodes.forEach(item => {
if (!item.className.includes('empty')) size++;
});
return size;
}
this.updateSizeTag = (size) => {
let sizeTag = document.getElementById("size");
sizeTag.innerText = `Tamanho: ${size}`;
}
this.updateTopItemTag = (topItem) => {
let topItemTag = document.getElementById("top-item");
topItemTag.innerText = `Objeto topo: ${topItem}`;
}
this.createClassNameForItems = (stackTag) => {
if (stackTag.childNodes.length < 1) return;
stackTag.childNodes.forEach((item, index) => {
if (!item.className.includes('value')) {
item.className = 'value';
}
if (item.className.includes('last') && index > 0 && index != stackTag.childNodes.length - 1) {
item.className = item.className.replaceAll('last', '');
}
if (index == 0) {
if (stackTag.childNodes.length < 3) {
item.className = item.className.replaceAll('last', '');
} else if (!item.className.includes('last')) {
item.className += ' last';
}
} else if (index == stackTag.childNodes.length - 1) {
if (stackTag.childNodes.length < 2) {
item.className = item.className.replaceAll('base', '');
} else if (!item.className.includes('base')) {
item.className += ' base';
}
}
});
}
}
const stack = new Stack();