Fix: prevent slice error when OrgChart initializes without stored viewBox #897
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduction
During the integration of the OrgChart.js library into the Odoo portal module, I encountered an Odoo alert error and a TypeError in the console related to the .slice() method inside orgchart.js.
After investigating, I discovered that the issue originated from the _resizeHandler method being executed before the viewBox value was properly initialized.
This occurred when the browser’s localStorage was empty, causing getViewBox() to return null and leading to the error on the first chart load.
Description
When OrgChart initializes with an empty localStorage or no previous state, _resizeHandler may call getViewBox().slice(0) before the SVG viewBox attribute is defined, resulting in a TypeError: Cannot read properties of undefined (reading 'slice').
This patch introduces a null check for getViewBox() before calling .slice(), ensuring _resizeHandler executes only when a valid viewBox is available.
Technical Summary
Code Change