Skip to content

Commit

Permalink
Improve Navigation script
Browse files Browse the repository at this point in the history
- remove unused paramaters
- decrease complexity of updateNavigationControls
  • Loading branch information
beatrycze-volk committed Mar 27, 2023
1 parent 6a932e8 commit 359b6a2
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions Resources/Public/JavaScript/PageView/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ class dlfNavigation {
this.docController.changePage(pageNo);
});

this.docController.eventTarget.addEventListener('tx-dlf-stateChanged', this.onStateChanged.bind(this));
this.docController.eventTarget.addEventListener('tx-dlf-stateChanged', () => {
this.onStateChanged();
});
}

/**
* @private
* @param {dlf.StateChangeEvent} e
*/
onStateChanged(e) {
onStateChanged() {
this.updateNavigationControls();
}

Expand All @@ -111,27 +112,16 @@ class dlfNavigation {
* Update DOM state of navigation buttons and dropdown. (For example,
* enable/disable the buttons depending on current page.)
*
* @param {number} pageNo
* @private
*/
updateNavigationControls() {
const currentPageNo = this.docController.currentPageNo;

for (const [key, value] of Object.entries(this.navigationButtons)) {
for (const value of Object.values(this.navigationButtons)) {
const btnPageNo = value.getPage(currentPageNo);
const isBtnPageVisible = this.docController.getVisiblePages(btnPageNo).some(page => page.pageNo === currentPageNo);
if (!isBtnPageVisible && 1 <= btnPageNo && btnPageNo <= this.docController.numPages) {
value.button.classList.remove('disabled');
} else {
value.button.classList.add('disabled');
}
// TODO(client-side): Check if it needs to be done always or only for not disabled buttons
this.toggleButtonDisabled(value.button, btnPageNo);
this.updateUrl(value.button, btnPageNo);

const textTemplate = value.button.getAttribute('data-text');
if (textTemplate) {
value.button.textContent = textTemplate.replace(/PAGE_STEPS/, this.getLongStep());
}
this.updateText(value.button);
}

if (this.pageSelect instanceof HTMLSelectElement) {
Expand All @@ -140,11 +130,44 @@ class dlfNavigation {
}

/**
* Update URLs of navigation buttons.
* Enable/disable the button depending on current page.
*
* @param {button} Element
* @param {pageNo}
*
* @private
*/
toggleButtonDisabled(button, pageNo) {
const isBtnPageVisible = this.docController.getVisiblePages(pageNo).some(page => page.pageNo === this.docController.currentPageNo);
if (!isBtnPageVisible && 1 <= pageNo && pageNo <= this.docController.numPages) {
button.classList.remove('disabled');
} else {
button.classList.add('disabled');
}
}

/**
* Update URLs of navigation button.
*
* @param {button} Element
*
* @private
*/
updateUrl(button, pageNo) {
button.setAttribute('href', this.docController.makePageUrl(pageNo));
}

/**
* Update text of navigation button.
*
* @param {button} Element
*
* @private
*/
updateUrl(button, page) {
button.setAttribute('href', this.docController.makePageUrl(page));
updateText(button) {
const textTemplate = button.getAttribute('data-text');
if (textTemplate) {
button.textContent = textTemplate.replace(/PAGE_STEPS/, this.getLongStep());
}
}
}

0 comments on commit 359b6a2

Please sign in to comment.