-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
188 lines (144 loc) · 5.02 KB
/
script.js
File metadata and controls
188 lines (144 loc) · 5.02 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
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
let loggedIn = false;
let isAdmin = false;
let calculationHistory = [];
function login() {
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
// Simulated login check
if (username === 'user' && password === 'password') {
loggedIn = true;
showCalculator();
} else if (username === 'admin' && password === 'adminpassword') {
isAdmin = true;
showCalculator();
showAdminPanel();
} else {
alert('Invalid username or password');
}
}
function showCalculator() {
document.getElementById('login').classList.add('hidden');
document.getElementById('calculator').classList.remove('hidden');
}
function showAdminPanel() {
const adminPanel = document.createElement('div');
adminPanel.id = 'admin';
const disableLabel = document.createElement('label');
disableLabel.innerHTML = 'Disable User:';
const disableSelect = document.createElement('select');
disableSelect.id = 'disableSelect';
const disableButton = document.createElement('button');
disableButton.innerHTML = 'Disable';
disableButton.onclick = disableUser;
const userOption = document.createElement('option');
userOption.value = 'user';
userOption.innerHTML = 'User';
disableSelect.appendChild(userOption);
disableLabel.appendChild(disableSelect);
adminPanel.appendChild(disableLabel);
adminPanel.appendChild(disableButton);
document.body.appendChild(adminPanel);
}
function disableUser() {
const selectedUser = document.getElementById('disableSelect').value;
// Perform actions to disable the selected user
alert('User ' + selectedUser + ' disabled');
}
function appendNumber(number) {
document.getElementById('result').value += number;
}
function appendOperator(operator) {
document.getElementById('result').value += operator;
}
function calculate() {
const expression = document.getElementById('result').value;
const result = eval(expression);
document.getElementById('result').value = result;
if (document.getElementById('saveHistory').checked) {
calculationHistory.push(expression + ' = ' + result);
}
}
function clearResult() {
document.getElementById('result').value = '';
}
function saveCalculation() {
if (loggedIn) {
const saveHistory = document.getElementById('saveHistory').checked;
if (saveHistory) {
// Save calculation history
alert('Calculation history saved');
} else {
alert('Calculation not saved');
}
} else {
alert('Please log in first');
}
}
// javascipt functionality
const buttons = document.querySelectorAll('.buttons button')
const inputEl = document.querySelector('input')
const output = document.querySelector('#output')
const historyContainer = document.querySelector('.historyContainer')
const STORAGE_NAME = 'historyStorage';
if (localStorage.getItem(STORAGE_NAME) == null) {
localStorage.setItem(STORAGE_NAME, JSON.stringify([]))
}
refreshHistory()
for (let button of buttons) {
const symbol = button.innerHTML
button.addEventListener('pointerdown', () => {
if (symbol == '=') {
const historyElements = JSON.parse(localStorage.getItem(STORAGE_NAME))
if (!historyElements.includes(inputEl.value)) {
historyElements.push(inputEl.value)
}
localStorage.setItem(STORAGE_NAME, JSON.stringify(historyElements))
inputEl.value = output.value
refreshHistory()
}
else if (symbol == 'DEL') {
inputEl.value = inputEl.value.slice(0, inputEl.value.length - 1)
} else if (symbol == 'AC') {
inputEl.value = ''
} else {
inputEl.value += symbol;
}
registrateChange()
})
}
inputEl.addEventListener('input', registrateChange)
function registrateChange() {
let newValue = eval(inputEl.value) || ''
output.value = newValue
}
function refreshHistory() {
historyContainer.innerHTML = ''
let historyElements = JSON.parse(localStorage.getItem(STORAGE_NAME))
for (let i = historyElements.length - 1; i >= 0; i--) {
const div = document.createElement('div')
div.className = 'historyItem'
let evaluated = ''
try {
evaluated = eval(historyElements[i])
} catch (error) {
evaluated = 'INVALID RESULT'
}
div.innerHTML = `
<div>${truncate(historyElements[i], 14)}</div>
<div>${truncate(evaluated, 14)}</div>
`
historyContainer.appendChild(div)
div.addEventListener('pointerdown', () => {
inputEl.value = historyElements[i];
registrateChange()
})
}
}
function truncate(string, max) {
string = String(string)
if (string.length > max) {
return string.substring(0, max - 3) + '...'
} else {
return string
}
}