Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-32248: Retain content tree state #1679

Merged
merged 3 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export default class ContentTree extends Component {
this.addWidthChangeListener = this.addWidthChangeListener.bind(this);
this.handleResizeEnd = this.handleResizeEnd.bind(this);
this._refTreeContainer = React.createRef();
this.scrollTimeout = null;
this.scrollPositionRestored = false;

this.state = {
resizeStartPositionX: 0,
containerWidth: 0,
containerWidth: this.getConfig('width'),
resizedContainerWidth: 0,
isResizing: false,
};
Expand All @@ -26,10 +28,52 @@ export default class ContentTree extends Component {
this.clearDocumentResizingListeners();
}

componentDidMount() {
this.containerScrollRef.addEventListener('scroll', (event) => {
window.clearTimeout(this.scrollTimeout);

this.scrollTimeout = window.setTimeout(
(scrollTop) => {
this.saveConfig('scrollTop', scrollTop);
},
50,
event.currentTarget.scrollTop
);
});
}

componentDidUpdate(prevState) {
if (this.state.containerWidth !== prevState.containerWidth) {
this.saveConfig('width', this.state.containerWidth);

document.body.dispatchEvent(new CustomEvent('ez-content-tree-resized'));
}

if (this.props.items && this.props.items.length && !this.scrollPositionRestored) {
this.scrollPositionRestored = true;

this.containerScrollRef.scrollTo(0, this.getConfig('scrollTop'));
}
}

saveConfig(id, value) {
const { userId } = this.props;
const data = JSON.parse(window.localStorage.getItem('ez-content-tree-state') || '{}');

if (!data[userId]) {
data[userId] = {};
}

data[userId][id] = value;

window.localStorage.setItem('ez-content-tree-state', JSON.stringify(data));
}

getConfig(id) {
const { userId } = this.props;
const data = JSON.parse(window.localStorage.getItem('ez-content-tree-state') || '{}');

return data[userId]?.[id];
}

changeContainerWidth({ clientX }) {
Expand Down Expand Up @@ -78,13 +122,8 @@ export default class ContentTree extends Component {
}

renderList() {
const { items } = this.props;

if (!items || !items.length) {
return;
}

const {
items,
loadMoreSubitems,
currentLocationId,
onClickItem,
Expand All @@ -108,8 +147,8 @@ export default class ContentTree extends Component {
};

return (
<div className="m-tree__scrollable-wrapper">
<List {...attrs} />
<div className="m-tree__scrollable-wrapper" ref={(ref) => (this.containerScrollRef = ref)}>
{!items || !items.length ? null : <List {...attrs} />}
</div>
);
}
Expand Down Expand Up @@ -158,4 +197,5 @@ ContentTree.propTypes = {
afterItemToggle: PropTypes.func.isRequired,
onCollapseAllItems: PropTypes.func.isRequired,
onClickItem: PropTypes.func,
userId: PropTypes.number.isRequired,
};
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default class ContentTreeModule extends Component {
}

render() {
const { onClickItem, subitemsLimit, subitemsLoadLimit, treeMaxDepth } = this.props;
const { onClickItem, subitemsLimit, subitemsLoadLimit, treeMaxDepth, userId } = this.props;
const attrs = {
items: this.items,
currentLocationId: this.getCurrentLocationId(),
Expand All @@ -363,6 +363,7 @@ export default class ContentTreeModule extends Component {
afterItemToggle: this.updateSubtreeAfterItemToggle,
onCollapseAllItems: this.handleCollapseAllItems,
onClickItem,
userId,
};

return <ContentTree {...attrs} />;
Expand Down Expand Up @@ -398,6 +399,6 @@ ContentTreeModule.defaultProps = {
subitemsLimit: window.eZ.adminUiConfig.contentTree.childrenLoadMaxLimit,
subitemsLoadLimit: window.eZ.adminUiConfig.contentTree.loadMoreLimit,
treeMaxDepth: window.eZ.adminUiConfig.contentTree.treeMaxDepth,
afterItemToggle: () => { },
afterItemToggle: () => {},
sort: {},
};