mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-15 07:33:04 +01:00
10 lines
2.4 KiB
JavaScript
10 lines
2.4 KiB
JavaScript
|
/**
|
||
|
* Invoice Ninja (https://invoiceninja.com)
|
||
|
*
|
||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||
|
*
|
||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||
|
*
|
||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||
|
*/class a{constructor(e,t){this.url=e,this.canvas=t,this.context=t.getContext("2d"),this.currentPage=1,this.maxPages=1,this.currentScale=1,this.currentScaleText=document.getElementById("zoom-level"),matchMedia("only screen and (max-width: 480px)").matches&&(this.currentScale=1),this.currentScaleText.textContent=this.currentScale*100+"%"}handlePreviousPage(){this.currentPage!=1&&(this.currentPage-=1,this.handle())}handleNextPage(){this.currentPage!=this.maxPages&&(this.currentPage+=1,this.handle())}handleZoomChange(e=null){if(!(this.currentScale==.25&&!e))return e?(this.currentScale+=.25,this.currentScaleText.textContent=this.currentScale*100+"%",this.handle()):(this.currentScale-=.25,this.currentScaleText.textContent=this.currentScale*100+"%",this.handle())}prepare(){return document.getElementById("previous-page-button").addEventListener("click",()=>this.handlePreviousPage()),document.getElementById("next-page-button").addEventListener("click",()=>this.handleNextPage()),document.getElementById("zoom-in").addEventListener("click",()=>this.handleZoomChange(!0)),document.getElementById("zoom-out").addEventListener("click",()=>this.handleZoomChange()),document.querySelector("meta[name=pdf-url]").addEventListener("change",()=>{this.canvas.getContext("2d").clearRect(0,0,this.canvas.width,this.canvas.height),this.url=document.querySelector("meta[name='pdf-url']").content,this.handle()}),this}setPagesInViewport(){let e=document.getElementById("current-page-container"),t=document.getElementById("total-page-container"),n=document.getElementById("pagination-button-container");e.innerText=this.currentPage,t.innerText=this.maxPages,this.maxPages>1&&(n.style.display="flex")}async handle(){let e=await pdfjsLib.getDocument(this.url).promise,t=await e.getPage(this.currentPage);this.maxPages=e.numPages;let n=await t.getViewport({scale:this.currentScale});this.canvas.height=n.height,this.canvas.width=n.width,t.render({canvasContext:this.context,viewport:n}),this.setPagesInViewport()}}const r=document.querySelector("meta[name='pdf-url']").content,i=document.getElementById("pdf-placeholder");new a(r,i).prepare().handle();
|