class PdfActionsManager { pageDirection; pagesContainer; constructor(id) { this.pagesContainer = document.getElementById(id); this.pageDirection = document.documentElement.getAttribute("lang-direction"); var styleElement = document.createElement('link'); styleElement.rel = 'stylesheet'; styleElement.href = 'css/pdfActions.css' document.head.appendChild(styleElement); } getPageContainer(element) { var container = element while (!container.classList.contains('page-container')) { container = container.parentNode; } return container; } moveUpButtonCallback(e) { var imgContainer = this.getPageContainer(e.target); const sibling = imgContainer.previousSibling; if (sibling) { this.movePageTo(imgContainer, sibling, true); } } moveDownButtonCallback(e) { var imgContainer = this.getPageContainer(e.target); const sibling = imgContainer.nextSibling; if (sibling) { this.movePageTo(imgContainer, sibling.nextSibling, true); } }; rotateCCWButtonCallback(e) { var imgContainer = this.getPageContainer(e.target); const img = imgContainer.querySelector("img"); this.rotateElement(img, -90) }; rotateCWButtonCallback(e) { var imgContainer = this.getPageContainer(e.target); const img = imgContainer.querySelector("img"); this.rotateElement(img, 90) }; deletePageButtonCallback(e) { var imgContainer = this.getPageContainer(e.target); this.pagesContainer.removeChild(imgContainer); }; insertFileButtonCallback(e) { var imgContainer = this.getPageContainer(e.target); this.addPdfs(imgContainer) }; setActions({ movePageTo, addPdfs, rotateElement }) { this.movePageTo = movePageTo; this.addPdfs = addPdfs; this.rotateElement = rotateElement; this.moveUpButtonCallback = this.moveUpButtonCallback.bind(this); this.moveDownButtonCallback = this.moveDownButtonCallback.bind(this); this.rotateCCWButtonCallback = this.rotateCCWButtonCallback.bind(this); this.rotateCWButtonCallback = this.rotateCWButtonCallback.bind(this); this.deletePageButtonCallback = this.deletePageButtonCallback.bind(this); this.insertFileButtonCallback = this.insertFileButtonCallback.bind(this); } adapt(div) { div.classList.add('pdf-actions_container'); const leftDirection = this.pageDirection === 'rtl' ? 'right' : 'left' const rightDirection = this.pageDirection === 'rtl' ? 'left' : 'right' const buttonContainer = document.createElement('div'); buttonContainer.classList.add("pdf-actions_button-container", "hide-on-drag"); const moveUp = document.createElement('button'); moveUp.classList.add("pdf-actions_move-left-button","btn", "btn-secondary"); moveUp.innerHTML = ``; moveUp.onclick = this.moveUpButtonCallback; buttonContainer.appendChild(moveUp); const moveDown = document.createElement('button'); moveDown.classList.add("pdf-actions_move-right-button","btn", "btn-secondary"); moveDown.innerHTML = ``; moveDown.onclick = this.moveDownButtonCallback; buttonContainer.appendChild(moveDown); const rotateCCW = document.createElement('button'); rotateCCW.classList.add("btn", "btn-secondary"); rotateCCW.innerHTML = ` `; rotateCCW.onclick = this.rotateCCWButtonCallback; buttonContainer.appendChild(rotateCCW); const rotateCW = document.createElement('button'); rotateCW.classList.add("btn", "btn-secondary"); rotateCW.innerHTML = ` `; rotateCW.onclick = this.rotateCWButtonCallback; buttonContainer.appendChild(rotateCW); const deletePage = document.createElement('button'); deletePage.classList.add("btn", "btn-danger"); deletePage.innerHTML = ` `; deletePage.onclick = this.deletePageButtonCallback; buttonContainer.appendChild(deletePage); div.appendChild(buttonContainer); const insertFileButtonContainer = document.createElement('div'); insertFileButtonContainer.classList.add( "pdf-actions_insert-file-button-container", leftDirection, `align-center-${leftDirection}`); const insertFileButton = document.createElement('button'); insertFileButton.classList.add("btn", "btn-primary", "pdf-actions_insert-file-button"); insertFileButton.innerHTML = ` `; insertFileButton.onclick = this.insertFileButtonCallback; insertFileButtonContainer.appendChild(insertFileButton); div.appendChild(insertFileButtonContainer); // add this button to every element, but only show it on the last one :D const insertFileButtonRightContainer = document.createElement('div'); insertFileButtonRightContainer.classList.add( "pdf-actions_insert-file-button-container", rightDirection, `align-center-${rightDirection}`); const insertFileButtonRight = document.createElement('button'); insertFileButtonRight.classList.add("btn", "btn-primary", "pdf-actions_insert-file-button"); insertFileButtonRight.innerHTML = ` insertFileButtonRight`; insertFileButtonRight.onclick = () => addPdfs(); insertFileButtonRightContainer.appendChild(insertFileButtonRight); div.appendChild(insertFileButtonRightContainer); return div; } } export default PdfActionsManager;