Skip to content

Commit

Permalink
Fix issue #79: Remember which folders have been collapsed in the Read…
Browse files Browse the repository at this point in the history
…ing Stack
  • Loading branch information
codedread committed Dec 9, 2024
1 parent 79a797b commit bcd054c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions code/reading-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export class ReadingStack {
*/
this.currentBookLoadedCallbacks_ = [];

/**
* Holds whether this folder is collapsed or not.
* @type {Map<BookContainer, boolean>}
* @private
*/
this.folderCollapsedMap_ = new Map();

getElem('readingStackButton').addEventListener('click', () => this.toggleOpen());
getElem('readingStackOverlay').addEventListener('click', () => this.toggleOpen());
}
Expand Down Expand Up @@ -129,6 +136,7 @@ export class ReadingStack {
book.removeEventListener(BookEventType.LOADING_STARTED, this);
}
this.books_ = [];
this.folderCollapsedMap_.clear();
this.currentBookNum_ = -1;
this.renderStack_();
}
Expand Down Expand Up @@ -286,9 +294,16 @@ export class ReadingStack {
}
}

/** Toggles an individual folder expanded or collapsed. */
toggleFolderExpandCollapse_(folderDiv) {
/**
* Toggles an individual folder expanded or collapsed. Updates the collapse
* map to remember which folders have been collapsed.
* @param {HTMLDivElement} folderDiv
* @param {BookContainer} folder
* @private
*/
toggleFolderExpandCollapse_(folderDiv, folder) {
folderDiv.classList.toggle('collapsed');
this.folderCollapsedMap_.set(folder, folderDiv.classList.contains('collapsed'));
}

// TODO: Do this better so that each change of state doesn't require a complete re-render?
Expand Down Expand Up @@ -334,7 +349,10 @@ export class ReadingStack {
folderNameSpan.textContent = ancestor.getName();
folderDiv.appendChild(folderNameSpan);
const zippyButtonEl = folderDiv.querySelector('span.zippyButton');
zippyButtonEl.addEventListener('click', (evt) => this.toggleFolderExpandCollapse_(folderDiv));
zippyButtonEl.addEventListener('click', (evt) => this.toggleFolderExpandCollapse_(folderDiv, ancestor));
if (this.folderCollapsedMap_.get(ancestor) === true) {
this.toggleFolderExpandCollapse_(folderDiv, ancestor);
}
curDiv.appendChild(folderDiv);
renderedContainerMap.set(ancestor, folderDiv);
}
Expand Down

0 comments on commit bcd054c

Please sign in to comment.