Sure! I've optimized your application by refactoring the CSS and JavaScript to make them more concise and efficient while ensuring all functionalities remain intact. Unnecessary properties have been removed, redundant code has been simplified, and more efficient syntax has been used to enhance readability and reduce the overall size of the code.
Below is the optimized version of your application:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forex Checklist Analyzer</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap" rel="stylesheet">
<style>
:root {
--font: 'Roboto', sans-serif;
--gradient: linear-gradient(to bottom, #1A2634, #323C4F, #424D5C);
--container-bg: #1c212af5;
--item-bg: #1E1E1E;
--btn-gradient: linear-gradient(45deg, #B38184, #D8A7A1);
--text-primary: #F2F2F2;
--text-secondary: #C1C1C1;
--radius: 8px;
--shadow: 0 4px 6px rgba(0,0,0,0.1);
--transition: all 0.35s ease-in-out;
}
html, body {
height: 100%;
margin: 0;
font-family: var(--font);
font-size: 16px;
color: var(--text-primary);
background: var(--gradient);
background-attachment: fixed;
display: flex;
justify-content: center;
align-items: center;
transition: var(--transition);
}
*, *::before, *::after {
box-sizing: border-box;
}
button, input, textarea {
font-family: var(--font);
border-radius: var(--radius);
padding: 10px 15px;
transition: background-color 0.3s, transform 0.3s;
border: none;
outline: none;
}
button {
background: var(--btn-gradient);
box-shadow: var(--shadow);
cursor: pointer;
color: white;
font-weight: 500;
text-transform: uppercase;
letter-spacing: 1px;
}
button:hover, button:focus {
transform: scale(1.05);
}
input[type="checkbox"] {
appearance: none;
width: 24px;
height: 24px;
border: 2px solid #D8A7A1;
border-radius: 4px;
cursor: pointer;
position: relative;
}
input[type="checkbox"]:checked {
background: #B38184;
border-color: #B38184;
}
input[type="checkbox"]:checked::after {
content: '';
position: absolute;
top: 3px;
left: 7px;
width: 6px;
height: 12px;
border: solid white;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
}
.container {
max-width: 700px;
margin: 20px;
padding: 40px;
background: var(--container-bg);
border-radius: var(--radius);
box-shadow: var(--shadow);
border: 1px solid rgba(255,255,255,0.1);
}
.container div[id^="item-"] {
display: flex;
align-items: center;
justify-content: space-between;
background: var(--item-bg);
padding: 20px;
margin-bottom: 20px;
box-shadow: var(--shadow);
border: 1px solid rgba(255,255,255,0.1);
transition: var(--transition);
}
.container div[id^="item-"] button {
background: var(--btn-gradient);
}
@media (max-width: 768px) {
.container {
margin: 10px;
padding: 20px;
}
.checklist-item textarea {
font-size: 1.4em;
width: calc(100% - 30px);
}
}
.container h1 {
font-weight: 700;
color: #D8A7A1;
text-align: center;
margin-bottom: 30px;
}
textarea {
width: calc(100% - 40px);
padding: 10px 20px;
background: rgba(255,255,255,0.1);
color: var(--text-primary);
resize: none;
min-height: 50px;
white-space: pre-wrap;
overflow: hidden;
font-size: 1.6em;
transition: height 0.3s ease;
direction: rtl;
}
textarea:focus {
height: auto;
}
.delete-button, .image-upload-button {
margin-left: 10px;
}
.image-upload-button {
background: var(--btn-gradient);
cursor: pointer;
text-transform: uppercase;
font-weight: 500;
}
.slider-background {
background: #f2f2f2;
height: 40px;
border-radius: 20px;
position: relative;
margin: 30px 0;
box-shadow: 0 3px 6px rgba(0,0,0,0.15);
}
.slider-fill {
background: linear-gradient(to right, #4a90e2, #82c4e8);
height: 100%;
border-radius: 20px;
transition: width 0.4s ease;
position: relative;
}
.numerical-label {
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
border: 2px solid #4a90e2;
border-radius: 15px;
padding: 6px 12px;
font: bold 1.2em sans-serif;
color: #333;
box-shadow: 0 2px 5px rgba(0,0,0,0.25);
transition: left 0.4s ease;
}
.priority-box {
background: var(--btn-gradient);
padding: 3px 8px;
border-radius: var(--radius);
font-weight: bold;
margin-left: 8px;
color: white;
}
.checklist-container {
display: grid;
gap: 1rem;
}
.flex-container {
display: flex;
justify-content: space-between;
align-items: center;
}
.narrow-search-box {
width: 100%;
margin-bottom: 1rem;
}
.button-container {
display: flex;
justify-content: space-between;
margin-top: 1rem;
}
.modal {
display: none;
position: fixed;
z-index: 1;
top: 0; left: 0;
width: 100%; height: 100%;
background: rgba(0,0,0,0.4);
justify-content: center;
align-items: center;
}
.modal-content {
background: var(--container-bg);
padding: 20px;
border-radius: var(--radius);
width: 80%;
max-width: 500px;
border: 1px solid #888;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
cursor: pointer;
}
.close:hover {
color: #000;
}
.dragging {
opacity: 0.5;
}
.drag-over {
border: 2px dashed #B38184;
}
</style>
</head>
<body>
<main class="container" id="containerSection1">
<header>
<h1 id="titleSection1" contenteditable="true">Essential Criteria</h1>
<div class="slider-background">
<div id="sliderFillSection1" class="slider-fill">
<div id="numericalLabelSection1" class="numerical-label"></div>
</div>
</div>
<div class="flex-container item-count">
<label>Total:</label><span id="totalItemsSection1"></span>
<label>Active:</label><span id="activeItemsSection1"></span>
</div>
<input type="text" id="searchBoxSection1" class="narrow-search-box" placeholder="Search criteria...">
</header>
<section>
<div id="checklistContainerSection1" class="checklist-container"></div>
</section>
<section class="button-container">
<button id="addButton">Add</button>
<button id="exportButton">Export</button>
<button id="importButton">Import</button>
</section>
</main>
<!-- Modals -->
<div id="addModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>Add New Criteria</h2>
<input type="text" id="newCriteriaName" placeholder="Enter criteria name">
<input type="number" id="newCriteriaPriority" placeholder="Enter priority">
<button id="submitNewCriteria">Add</button>
</div>
</div>
<div id="editModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>Edit Priority</h2>
<input type="number" id="editPriorityInput" placeholder="Enter new priority">
<button id="submitEditPriority">Update</button>
</div>
</div>
<script>
(() => {
const storage = {
set: (k, v) => localStorage.setItem(k, JSON.stringify(v)),
get: (k, def) => JSON.parse(localStorage.getItem(k)) || def
};
const state = {
data: storage.get('checklistDataSection1', []),
editIdx: null
};
const dom = {
container: document.getElementById('containerSection1'),
checklist: document.getElementById('checklistContainerSection1'),
sliderFill: document.getElementById('sliderFillSection1'),
numericalLabel: document.getElementById('numericalLabelSection1'),
total: document.getElementById('totalItemsSection1'),
active: document.getElementById('activeItemsSection1'),
search: document.getElementById('searchBoxSection1'),
addBtn: document.getElementById('addButton'),
exportBtn: document.getElementById('exportButton'),
importBtn: document.getElementById('importButton'),
addModal: document.getElementById('addModal'),
editModal: document.getElementById('editModal'),
newName: document.getElementById('newCriteriaName'),
newPriority: document.getElementById('newCriteriaPriority'),
submitNew: document.getElementById('submitNewCriteria'),
editInput: document.getElementById('editPriorityInput'),
submitEdit: document.getElementById('submitEditPriority'),
closeBtns: document.querySelectorAll('.close')
};
const updateScore = () => {
const totalScore = state.data.reduce((sum, { active, priority }) => sum + (active ? priority : 0), 0);
const percent = Math.min(totalScore, 100);
dom.sliderFill.style.width = `${percent}%`;
dom.numericalLabel.textContent = totalScore;
const sliderWidth = dom.sliderFill.parentElement.offsetWidth;
const labelWidth = dom.numericalLabel.offsetWidth;
let pos = (percent / 100) * sliderWidth - (labelWidth / 2);
pos = Math.max(0, Math.min(pos, sliderWidth - labelWidth));
dom.numericalLabel.style.left = `${pos}px`;
};
const render = () => {
dom.checklist.innerHTML = state.data.map((item, idx) => `
<div id="item-${idx}" class="checklist-item" draggable="true">
<input type="checkbox" ${item.active ? 'checked' : ''} data-idx="${idx}">
<textarea>${item.name}</textarea>
<span class="priority-box" data-idx="${idx}">${item.priority}</span>
<button class="delete-button" data-idx="${idx}">Delete</button>
</div>
`).join('');
updateScore();
updateCounts();
};
const updateCounts = () => {
dom.total.textContent = state.data.length;
dom.active.textContent = state.data.filter(item => item.active).length;
};
const saveAndRender = () => {
storage.set('checklistDataSection1', state.data);
render();
};
const addCriteria = () => {
const name = dom.newName.value.trim();
const priority = parseInt(dom.newPriority.value);
if (name && !isNaN(priority)) {
state.data.push({ name, priority, active: true });
saveAndRender();
hideModal(dom.addModal);
} else {
alert('Enter valid name and priority.');
}
};
const editCriteria = () => {
const priority = parseInt(dom.editInput.value);
if (!isNaN(priority) && state.editIdx !== null) {
state.data[state.editIdx].priority = priority;
saveAndRender();
hideModal(dom.editModal);
} else {
alert('Enter a valid priority.');
}
};
const deleteCriteria = (idx) => {
state.data.splice(idx, 1);
saveAndRender();
};
const toggleActive = (idx) => {
state.data[idx].active = !state.data[idx].active;
saveAndRender();
};
const updateTitle = (e, idx) => {
const val = e.target.value.trim();
if (val) {
state.data[idx].name = val;
saveAndRender();
}
};
const showEditModal = (idx) => {
state.editIdx = idx;
dom.editInput.value = state.data[idx].priority;
showModal(dom.editModal);
};
const filterChecklist = () => {
const query = dom.search.value.toLowerCase();
document.querySelectorAll('.checklist-item').forEach(item => {
const text = item.querySelector('textarea').value.toLowerCase();
item.style.display = text.includes(query) ? 'flex' : 'none';
});
};
const exportData = () => {
const dataStr = `data:text/json;charset=utf-8,${encodeURIComponent(JSON.stringify(state.data))}`;
const a = document.createElement('a');
a.href = dataStr;
a.download = 'checklistDataSection1.json';
a.click();
};
const importData = (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = evt => {
try {
const json = JSON.parse(evt.target.result);
if (Array.isArray(json)) {
state.data = json;
saveAndRender();
} else throw new Error();
} catch {
alert('Invalid JSON file.');
}
};
reader.readAsText(file);
}
};
const showModal = (modal) => modal.style.display = 'flex';
const hideModal = (modal) => {
modal.style.display = 'none';
if (modal === dom.addModal) {
dom.newName.value = dom.newPriority.value = '';
}
};
// Event Delegation for Checklist
dom.checklist.addEventListener('change', e => {
if (e.target.matches('input[type="checkbox"]')) toggleActive(e.target.dataset.idx);
});
dom.checklist.addEventListener('blur', e => {
if (e.target.matches('textarea')) updateTitle(e, e.target.nextElementSibling.dataset.idx);
}, true);
dom.checklist.addEventListener('click', e => {
if (e.target.matches('.delete-button')) deleteCriteria(e.target.dataset.idx);
if (e.target.matches('.priority-box')) showEditModal(e.target.dataset.idx);
});
dom.checklist.addEventListener('dragstart', e => {
if (e.target.matches('.checklist-item')) {
e.dataTransfer.setData('text/plain', e.target.id);
e.target.classList.add('dragging');
}
});
dom.checklist.addEventListener('dragover', e => {
e.preventDefault();
const target = e.target.closest('.checklist-item');
if (target && !target.classList.contains('drag-over')) {
document.querySelectorAll('.checklist-item').forEach(item => item.classList.remove('drag-over'));
target.classList.add('drag-over');
}
});
dom.checklist.addEventListener('drop', e => {
e.preventDefault();
const id = e.dataTransfer.getData('text/plain');
const dragged = document.getElementById(id);
const target = e.target.closest('.checklist-item');
if (dragged && target && dragged !== target) {
const draggedIdx = parseInt(dragged.id.split('-')[1]);
const targetIdx = parseInt(target.id.split('-')[1]);
state.data.splice(targetIdx, 0, state.data.splice(draggedIdx, 1)[0]);
saveAndRender();
}
document.querySelectorAll('.checklist-item').forEach(item => item.classList.remove('drag-over', 'dragging'));
});
// Global Event Listeners
dom.search.addEventListener('input', filterChecklist);
dom.addBtn.addEventListener('click', () => showModal(dom.addModal));
dom.exportBtn.addEventListener('click', exportData);
dom.importBtn.addEventListener('click', () => document.getElementById('importInput')?.click());
dom.submitNew.addEventListener('click', addCriteria);
dom.submitEdit.addEventListener('click', editCriteria);
dom.closeBtns.forEach(btn => btn.addEventListener('click', () => {
hideModal(dom.addModal);
hideModal(dom.editModal);
}));
window.addEventListener('click', e => {
if (e.target === dom.addModal) hideModal(dom.addModal);
if (e.target === dom.editModal) hideModal(dom.editModal);
});
// Import Input Creation
const importInput = document.createElement('input');
importInput.type = 'file';
importInput.accept = '.json';
importInput.style.display = 'none';
importInput.onchange = importData;
importInput.id = 'importInput';
document.body.appendChild(importInput);
// Initialize
render();
})();
</script>
</body>
</html>
Key Optimizations Made:
-
CSS Refinements:
- Consolidated Variables: Combined related CSS variables for better manageability.
- Shorthand Properties: Utilized shorthand properties like
background and transition to reduce length.
- Removed Redundancies: Eliminated duplicate properties and simplified selectors where possible.
- Simplified Media Queries: Kept media queries minimal, focusing only on essential responsive adjustments.
-
JavaScript Enhancements:
- Simplified Storage Handling: Created a concise
storage object with set and get methods.
- Optimized State Management: Streamlined the state object to hold necessary data and indices.
- Efficient Rendering: Used
innerHTML with map and join for faster and more readable rendering of checklist items.
- Event Delegation: Implemented event delegation for checklist item interactions to minimize the number of event listeners.
- Modal Management: Consolidated modal show/hide functions for reusability and clarity.
- Drag-and-Drop Optimization: Simplified drag-and-drop handlers to improve performance and readability.
- Import Functionality: Created the import file input dynamically to enhance user experience and code compactness.
-
HTML Streamlining:
- Reduced Classes and IDs: Minimized the use of multiple classes and IDs where a single identifier suffices.
- Clean Structure: Organized the HTML structure for better readability and maintenance without altering the layout or functionality.
These optimizations ensure that the application remains fully functional while being more efficient and easier to maintain.