1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/public/js/clients/shared/pdf.js

184 lines
18 KiB
JavaScript
Raw Normal View History

/******/ (() => { // webpackBootstrap
var __webpack_exports__ = {};
/*!********************************************!*\
!*** ./resources/js/clients/shared/pdf.js ***!
\********************************************/
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resol
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
/**
* 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
*/
var PDF = /*#__PURE__*/function () {
function PDF(url, canvas) {
_classCallCheck(this, PDF);
this.url = url;
this.canvas = canvas;
this.context = canvas.getContext('2d');
this.currentPage = 1;
this.maxPages = 1;
this.currentScale = 1.25;
this.currentScaleText = document.getElementById('zoom-level');
if (matchMedia('only screen and (max-width: 480px)').matches) {
this.currentScale = 1.25;
}
this.currentScaleText.textContent = this.currentScale * 100 + '%';
}
_createClass(PDF, [{
key: "handlePreviousPage",
value: function handlePreviousPage() {
if (this.currentPage == 1) {
return;
}
this.currentPage -= 1;
this.handle();
}
}, {
key: "handleNextPage",
value: function handleNextPage() {
if (this.currentPage == this.maxPages) {
return;
}
this.currentPage += 1;
this.handle();
}
}, {
key: "handleZoomChange",
value: function handleZoomChange() {
var zoom = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
if (this.currentScale == 0.25 && !zoom) {
return;
}
if (zoom) {
this.currentScale += 0.25;
this.currentScaleText.textContent = this.currentScale * 100 + '%';
return this.handle();
}
this.currentScale -= 0.25;
this.currentScaleText.textContent = this.currentScale * 100 + '%';
return this.handle();
}
}, {
key: "prepare",
value: function prepare() {
var _this = this;
var previousPageButton = document.getElementById('previous-page-button').addEventListener('click', function () {
return _this.handlePreviousPage();
});
var nextPageButton = document.getElementById('next-page-button').addEventListener('click', function () {
return _this.handleNextPage();
});
var zoomInButton = document.getElementById('zoom-in').addEventListener('click', function () {
return _this.handleZoomChange(true);
});
var zoomOutButton = document.getElementById('zoom-out').addEventListener('click', function () {
return _this.handleZoomChange();
});
document.querySelector('meta[name=pdf-url]').addEventListener('change', function () {
_this.canvas.getContext('2d').clearRect(0, 0, _this.canvas.width, _this.canvas.height);
_this.url = document.querySelector("meta[name='pdf-url']").content;
_this.handle();
});
return this;
}
}, {
key: "setPagesInViewport",
value: function setPagesInViewport() {
var currentPageContainer = document.getElementById('current-page-container');
var totalPageContainer = document.getElementById('total-page-container');
var paginationButtonContainer = document.getElementById('pagination-button-container');
currentPageContainer.innerText = this.currentPage;
totalPageContainer.innerText = this.maxPages;
if (this.maxPages > 1) {
paginationButtonContainer.style.display = 'flex';
}
}
}, {
key: "handle",
value: function () {
var _handle = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
var pdf, page, viewport;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return pdfjsLib.getDocument(this.url).promise;
case 2:
pdf = _context.sent;
_context.next = 5;
return pdf.getPage(this.currentPage);
case 5:
page = _context.sent;
this.maxPages = pdf.numPages;
_context.next = 9;
return page.getViewport({
scale: this.currentScale
});
case 9:
viewport = _context.sent;
this.canvas.height = viewport.height;
this.canvas.width = viewport.width;
page.render({
canvasContext: this.context,
viewport: viewport
});
this.setPagesInViewport();
case 14:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function handle() {
return _handle.apply(this, arguments);
}
return handle;
}()
}]);
return PDF;
}();
var url = document.querySelector("meta[name='pdf-url']").content;
var canvas = document.getElementById('pdf-placeholder');
new PDF(url, canvas).prepare().handle();
/******/ })()
;