Adding obstacles one by one through the Add Obstacles dialog is useful for small experiments, but when you want to populate the AVL tree with a predefined set of road obstacles — for example, a saved test scenario — the Load JSON feature lets you do it in a single operation. You prepare aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/avl_tree_car_front/llms.txt
Use this file to discover all available pages before exploring further.
.json file that describes the road width and every obstacle, then point the dialog at that file. The controller validates the payload, POSTs it to the backend, syncs the returned IDs to the client-side road model, and redraws the D3.js tree automatically.
Prerequisites
No road needs to exist before using Load JSON. The button (#btn-load-json) is always enabled on the home screen. The controller creates RoadController and AVLController instances internally if they do not already exist.
JSON file format
Your.json file must follow this exact structure:
| Field | Type | Description |
|---|---|---|
configs.total_distance | number | Sets the road width client-side via road.setWidth(). Corresponds to the maximum X coordinate for the obstacle space. |
obstacles | array | List of obstacle objects to insert into the AVL tree. |
obstacles[].x | number | Horizontal pixel position of the obstacle. |
obstacles[].y | number | Vertical pixel position of the obstacle. |
obstacles[].type | string | Obstacle type name (see the obstacle types table). |
The controller normalizes only the first character of each obstacle type string to uppercase before creating each
Obstacle model instance (e.g. "cone" becomes "Cone"). All remaining characters are left unchanged — "ROCK" stays "ROCK", not "Rock". Use all-lowercase type strings to get clean Title Case values.Obstacle types
The following type strings are accepted. Any capitalisation variant is valid.| Type string | Description |
|---|---|
cone | Traffic cone |
rock | Rock |
tree | Tree |
tire | Tire |
nail | Nail |
trunk | Trunk |
person | Person |
car | Car |
bicycle | Bicycle |
chair | Chair |
Steps
Click Load JSON on the home screen
Click the Load JSON button (
#btn-load-json). HomeController lazily initialises RoadController, AVLController, and LoadJSONController if they are not yet present, then calls LoadJSONController.init(). A modal dialog titled Load Obstacles From JSON appears.Select your JSON file
Click the Load JSON file input and choose your
.json file from disk. The browser reads the file using the FileReader API and populates the read-only textarea (#load-json-text) with the raw file contents. You can review the JSON in the textarea before proceeding.Review the preview
The textarea shows the full JSON content. Verify that the obstacle list and
total_distance look correct before proceeding. You can also type or paste JSON directly into the textarea — the content of #load-json-text is read via .value when you click Accept, so manual edits are respected.Click Accept to send
Click Accept.
LoadJSONController.#sendJSON() executes the following sequence:- Resets the backend tree —
emit_reset_avl()is called via Socket.IO before anything is sent, so the backend starts from an empty tree. - Parses the textarea content —
JSON.parse()converts the raw string into a JavaScript object. - POSTs to
/avl/add/configs— the full JSON object is sent as the request body.
Backend response and client-side sync
On a
status 200 response the controller:- Shows a success toast with the backend’s response message.
- Calls
road.setWidth(data.configs.total_distance)to apply the road width client-side. - Maps each returned obstacle ID (from
req.data, an array in insertion order) to the corresponding obstacle entry in the JSON, uppercases the type’s first character, and constructs anObstaclemodel instance for each. - Stores the full list with
road.setObstacles(obstaclesList). - Calls
avlCtrl.getTree()which emitsget_tree_avlto the backend and triggers a full D3.js tree redraw when theavl_tree_balancedSocket.IO event arrives. - Enables both the Insert Obstacles (
#btn-insert-obstacles) and Remove & Watch Roads (#btn-watch-roads) buttons. - Closes the modal.
200, a warning toast is shown and the modal stays open.Error handling
| Condition | Behaviour |
|---|---|
Textarea is empty (!content) | Toast: No data to send! — the POST request is not made |
| Textarea content is fewer than 50 characters | Client-side obstacle sync is skipped silently after a successful POST — the road model will not have obstacle objects populated |
JSON.parse() fails | An exception is caught and logged; the modal stays open |
| Backend returns non-200 status | Warning toast with the backend’s message; no client state is changed |