I need JavaScript code that powers a Forex Checklist Analyzer web application. This app should allow users (traders) to manage, prioritize, and track their trading strategies using an interactive checklist. Here's what I need the JavaScript to handle:
Core Functionalities:
Checklist Management:
Add new criteria: The user should be able to add a new criterion by entering a name and a priority (via a prompt). Each criterion consists of:
A checkbox to mark the item as active or inactive.
A text area to edit the criterion's name.
A priority label to display the item's priority.
Delete criteria: The user should be able to delete any criterion.
Drag-and-drop: Users should be able to drag and reorder criteria within the list. The order of items in the data should be updated accordingly.
Real-Time Progress Calculation:
A progress bar should reflect the cumulative score of active criteria. Each active criterion contributes its priority value to the total score.
A numerical label next to the progress bar updates in real time, reflecting the total score. The score should be capped at 100 to avoid exceeding the bar's width.
When criteria are re-ordered or a new criterion is added, the progress bar and numerical score should update dynamically.
Checkbox and Priority Interaction:
Checkbox toggle: When a criterion’s checkbox is toggled (checked/unchecked), update the progress bar and numerical score accordingly.
Priority editing: The priority value for each criterion should be editable via a prompt. When the priority is updated, the numerical score and progress bar should adjust in real time.
Persistent Data Storage:
LocalStorage: The checklist data should be saved to the browser's LocalStorage so that the user's progress and checklist items persist even after the page is refreshed or closed.
Save and load: On page load, the checklist should automatically load from LocalStorage. On any changes (e.g., adding, deleting, re-ordering items), the data should be saved to LocalStorage.
Data Import/Export:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Export: The user should be able to export their checklist data as a JSON file. This should include the criteria names, their priorities, and whether they are active or not.
Import: The user should be able to import a JSON file to restore their checklist. When imported, the checklist data should replace the current list and update the progress bar and numerical score.
Search Functionality:
The app should include a search bar that allows users to filter criteria by name. As the user types, the list should dynamically show or hide items that match the search term.
Real-Time Updates:
The UI should reflect changes immediately without requiring a page refresh. For example:
When a criterion is added, it should appear instantly.
When a checkbox is toggled, the progress bar and numerical score should update immediately.
When an item is deleted, it should be removed from the list, and the changes should be reflected in the stored data.
Additional Considerations:
The app should handle data validation during user input. For example, when adding a new criterion, ensure the user provides a valid name and priority (e.g., a number for the priority).
Ensure that items can be dragged and dropped smoothly, and that the reordering is reflected in the saved data.
When importing JSON data, handle any errors gracefully (e.g., if the file is not properly formatted, show an alert).
Here's a comprehensive JavaScript implementation for your Forex Checklist Analyzer web application. This script handles all the core functionalities you've outlined, ensuring a seamless and interactive experience for traders. Below is an overview of the key components and how they interact, followed by the complete JavaScript code.
Overview
Data Structure & Persistence:
Checklist Data: Each checklist is identified by a unique sectionId (in your case, '1'). The data is stored as an array of criteria objects, each containing id, name, priority, and active status.
LocalStorage: Data is saved and retrieved from localStorage using keys like 'checklistData-1'.
Initialization:
On page load, the script initializes the checklist by loading data from localStorage. If no data exists, it starts with an empty list.
Event listeners for drag-and-drop functionalities are attached to enable reordering of criteria.
Checklist Management:
Add Criteria: Prompts the user to enter a valid name and priority. Upon validation, a new criterion is added to the checklist.
Delete Criteria: Allows users to remove any criterion from the list.
Edit Priority: Users can update the priority of a criterion via a prompt.
Real-Time Progress Calculation:
The progress bar and numerical label update dynamically based on the sum of priorities of active criteria, capped at 100.
Checkbox Interaction:
Toggling a checkbox updates the active status of the criterion and recalculates the progress.
Drag-and-Drop Reordering:
Implements HTML5 Drag-and-Drop API to allow users to reorder criteria. The new order is saved to localStorage.
Search Functionality:
Filters the displayed criteria in real-time based on the user's search input.
Data Import/Export:
Export: Users can download their checklist data as a JSON file.
Import: Users can upload a JSON file to replace their current checklist. The script includes error handling to manage improperly formatted files.
Real-Time UI Updates:
All changes reflect immediately in the UI without requiring a page refresh.
Complete JavaScript Code
html
Usage Instructions
Integration:
Place the above <script> block just before the closing </body> tag in your HTML file to ensure all DOM elements are loaded before the script executes.
Functionality Usage:
Add: Click the "Add" button to insert a new criterion. You'll be prompted to enter the criterion's name and priority.
Delete: Each criterion has a "Delete" button to remove it from the list.
Edit Priority: Click on the priority label (e.g., "Priority: 5") to modify its value.
Drag-and-Drop: Click and hold on a criterion to drag it to a new position within the list.
Export: Click the "Export" button to download your checklist as a JSON file.
Import: Click the "Import" button to upload a previously exported JSON file, replacing the current checklist.
Search: Use the search bar to filter criteria by their names in real-time.
Data Persistence:
All your changes are automatically saved in the browser's LocalStorage. This ensures that your checklist persists across page reloads and browser sessions.
Error Handling:
The script includes validations for user inputs and handles potential errors during data import, ensuring robustness and reliability.
Additional Notes
Extensibility: While the current implementation is tailored for a single section with sectionId'1', the structure allows for multiple sections if needed in the future by initializing ChecklistManager with different sectionId values.
Styling Enhancements: For improved user experience, consider adding visual cues during drag-and-drop operations or when criteria are being edited.
Accessibility: Ensure that all interactive elements are accessible via keyboard navigation and are screen-reader friendly for enhanced accessibility.
Browser Compatibility: The script uses modern JavaScript features and the HTML5 Drag-and-Drop API. Ensure compatibility with the browsers your target audience uses.
This script should provide a solid foundation for your Forex Checklist Analyzer, offering a dynamic and user-friendly interface for traders to manage their strategies effectively.