Skip to content

Commit

Permalink
Update v0.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigaoWang committed Nov 15, 2023
1 parent 445f841 commit 2e00700
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 134 deletions.
33 changes: 16 additions & 17 deletions app/dazcalc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Calculator</title>
<meta name="description" content="Simple Calculator With Dark Mode." />
<title>Professional Calculator</title>
<meta name="description" content="Professional Calculator With Dark Mode." />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="assets/calculator.ico">
<link rel="stylesheet" href="styles/dark.css" id="theme" />
Expand All @@ -19,7 +19,6 @@
<div class="wrapper">
<div class="container">
<div class="header-container">
<!-- This heading also notifies about the theme change.-->
<h1 id="toast">Calculator</h1>
<div class="top-buttons">
<button type="button" id="changeTheme" class="theme-button">
Expand All @@ -32,28 +31,28 @@ <h1 id="toast">Calculator</h1>
<input type="button" value="C" id="clear-button" />
</div>
<div class="second-row">
<input type="button" class="number" value="1" />
<input type="button" class="number" value="2" />
<input type="button" class="number" value="3" />
<input type="button" class="operator" value="+" />
<input type="button" class="function" value="^2" />
<input type="button" class="function" value="^3" />
<input type="button" class="operator" value="/" />
<input type="button" id="equals" value="=" />
</div>
<div class="third-row">
<input type="button" class="number" value="4" />
<input type="button" class="number" value="5" />
<input type="button" class="number" value="6" />
<input type="button" class="operator" value="-" />
</div>
<div class="fourth-row">
<input type="button" class="number" value="7" />
<input type="button" class="number" value="8" />
<input type="button" class="number" value="9" />
<input type="button" class="operator" value="*" />
</div>
<div class="fourth-row">
<input type="button" class="number" value="4" />
<input type="button" class="number" value="5" />
<input type="button" class="number" value="6" />
<input type="button" class="operator" value="-" />
</div>
<div class="fifth-row">
<input type="button" class="operator" value="/" />
<input type="button" class="number" value="0" />
<input type="button" class="number" value="." />
<input type="button" id="equals" value="=" />
<input type="button" class="number" value="1" />
<input type="button" class="number" value="2" />
<input type="button" class="number" value="3" />
<input type="button" class="operator" value="+" />
</div>
</div>
</div>
Expand Down
220 changes: 104 additions & 116 deletions app/dazcalc/scripts/script.js
Original file line number Diff line number Diff line change
@@ -1,140 +1,128 @@
const lightTheme = "styles/light.css";
const darkTheme = "styles/dark.css";
const sunIcon = "assets/SunIcon.svg";
const moonIcon = "assets/MoonIcon.svg";
const themeIcon = document.getElementById("theme-icon");
const res = document.getElementById("result");
const toast = document.getElementById("toast");

// Import the math.js library if you're using it as a module
// import math from 'mathjs';

function calculate(value) {
try {
// Use math.evaluate to safely evaluate the expression
const calculatedValue = math.evaluate(value);
if (isNaN(calculatedValue) || !isFinite(calculatedValue)) {
document.addEventListener("DOMContentLoaded", function () {
const lightTheme = "styles/light.css";
const darkTheme = "styles/dark.css";
const sunIcon = "assets/SunIcon.svg";
const moonIcon = "assets/MoonIcon.svg";
const themeIcon = document.getElementById("theme-icon");
const res = document.getElementById("result");
const toast = document.getElementById("toast");


function calculate(value) {
try {
let result;
if (value.includes("^2")) {
const num = value.substring(0, value.length - 2);
result = parseFloat(num) ** 2;
} else if (value.includes("^3")) {
const num = value.substring(0, value.length - 2);
result = parseFloat(num) ** 3;
} else {
result = math.evaluate(value);
if (isNaN(result) || !isFinite(result)) {
throw new Error("Invalid input");
}
}

if (Number.isInteger(result)) {
res.value = result;
} else {
res.value = parseFloat(result.toFixed(10));
}
} catch (error) {
res.value = "Invalid input";
setTimeout(() => {
res.value = "";
}, 1300);
} else {
// Check if the result is a whole number (integer)
if (Number.isInteger(calculatedValue)) {
res.value = calculatedValue;
} else {
res.value = parseFloat(calculatedValue.toFixed(10)); // Round to 10 decimal places
}
}
} catch (error) {
res.value = "Invalid input";
setTimeout(() => {
res.value = "";
}, 1300);
}
}



// Get a reference to the button element using its ID
var changeThemeButton = document.getElementById("changeTheme");

// Add a click event listener to the button
changeThemeButton.addEventListener("click", function() {
// Call the changeTheme() function when the button is clicked
changeTheme();
});

// Swaps the stylesheet to achieve dark mode.
function changeTheme() {
const theme = document.getElementById("theme");
setTimeout(() => {
toast.innerHTML = "Calculator";
}, 1500);
if (theme.getAttribute("href") === lightTheme) {
theme.setAttribute("href", darkTheme);
themeIcon.setAttribute("src", sunIcon);
toast.innerHTML = "Dark Mode 🌙";
} else {
theme.setAttribute("href", lightTheme);
themeIcon.setAttribute("src", moonIcon);
toast.innerHTML = "Light Mode ☀️";
function changeTheme() {
const theme = document.getElementById("theme");
setTimeout(() => {
toast.innerHTML = "Professional Calculator";
}, 1500);
if (theme.getAttribute("href") === lightTheme) {
theme.setAttribute("href", darkTheme);
themeIcon.setAttribute("src", sunIcon);
toast.innerHTML = "Dark Mode 🌙";
} else {
theme.setAttribute("href", lightTheme);
themeIcon.setAttribute("src", moonIcon);
toast.innerHTML = "Light Mode ☀️";
}
}
}

// Displays entered value on screen.
function liveScreen(enteredValue) {
if (!res.value) {
res.value = "";
function liveScreen(enteredValue) {
if (!res.value || res.value === "Result") {
res.value = "";
}
res.value += enteredValue;
}
res.value += enteredValue;
}

// Add event listeners for number buttons
const numberButtons = document.querySelectorAll(".number");
numberButtons.forEach((button) => {
button.addEventListener("click", () => {
liveScreen(button.value);

document.getElementById("changeTheme").addEventListener("click", changeTheme);

const numberButtons = document.querySelectorAll(".number");
numberButtons.forEach((button) => {
button.addEventListener("click", () => {
liveScreen(button.value);
});
});
});

// Add event listeners for operator buttons
const operatorButtons = document.querySelectorAll(".operator");
operatorButtons.forEach((button) => {
button.addEventListener("click", () => {
liveScreen(button.value);
const operatorButtons = document.querySelectorAll(".operator");
operatorButtons.forEach((button) => {
button.addEventListener("click", () => {
liveScreen(button.value);
});
});
});

// Add event listener for the equals button
document.getElementById("equals").addEventListener("click", () => {
calculate(res.value); // Use res.value as the argument for calculate
});
const functionButtons = document.querySelectorAll(".function");
functionButtons.forEach((button) => {
button.addEventListener("click", () => {
liveScreen(button.value);
});
});

// Add event listener for the clear button
document.getElementById("clear-button").addEventListener("click", () => {
result.value = "";
});
document.getElementById("equals").addEventListener("click", () => {
calculate(res.value);
});

// Adding event handler on the document to handle keyboard inputs
document.addEventListener("keydown", keyboardInputHandler);
document.getElementById("clear-button").addEventListener("click", () => {
res.value = "Result";
});

// Function to handle keyboard inputs
function keyboardInputHandler(e) {
// To fix the default behavior of the browser,
// Enter and backspace were causing undesired behavior when some key was already in focus.
e.preventDefault();
document.addEventListener("keydown", keyboardInputHandler);

// Grabbing the liveScreen
const resultInput = res.value;
function keyboardInputHandler(e) {
e.preventDefault();
const resultInput = res.value;

// Numbers
if (e.key >= "0" && e.key <= "9") {
res.value += e.key;
}
if (e.key >= "0" && e.key <= "9") {
res.value += e.key;
}

// Operators
if (e.key === "+") {
res.value += "+";
} else if (e.key === "-") {
res.value += "-";
} else if (e.key === "*") {
res.value += "*";
} else if (e.key === "/") {
res.value += "/";
}
if (e.key === "+") {
res.value += "+";
} else if (e.key === "-") {
res.value += "-";
} else if (e.key === "*") {
res.value += "*";
} else if (e.key === "/") {
res.value += "/";
}

// Decimal key
if (e.key === ".") {
res.value += ".";
}
if (e.key === ".") {
res.value += ".";
}

// Press Enter to see the result
if (e.key === "Enter") {
calculate(resultInput);
}
if (e.key === "Enter") {
calculate(resultInput);
}

// Backspace for removing the last input
if (e.key === "Backspace") {
// Remove the last element in the string
res.value = resultInput.substring(0, res.value.length - 1);
if (e.key === "Backspace") {
res.value = resultInput.substring(0, res.value.length - 1);
}
}
}
});
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name":"DazTab",
"description":"The perfect start page extension for browsers",
"manifest_version":3,
"version":"0.3.2",
"version":"0.3.3",
"author": "Zigao Wang",
"homepage_url": "https://github.com/ZigaoWang/daztab/",
"icons": {
Expand Down

0 comments on commit 2e00700

Please sign in to comment.