1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- const ComPDFKitViewer = {
- init (options, element) {
- return new Promise((resolve) => {
- element.addEventListener("ready", function ready () {
- element.removeEventListener('ready', ready)
- const iframeWindow = element.querySelector('iframe').contentWindow
- Promise.resolve().then(function() {
- resolve({
- docViewer: iframeWindow.instance
- })
- })
- })
- const viewer = new Viewer(options, element)
- })
- }
- }
- class Viewer {
- constructor (options, element) {
- this.instance = null
- this.options = options
- this.initialDoc = options.pdfUrl || null
- this.element = element
- element.addEventListener("ready", (function ready() {
- element.removeEventListener("ready", ready)
- }))
- this.create()
- }
- _createEvent (e, t) {
- let n;
- try {
- n = new CustomEvent(e, {
- detail: t,
- bubbles: true,
- cancelable: true
- })
- } catch (o) {
- (n = document.createEvent("Event")).initEvent(e, true, true),
- n.detail = t
- }
- return n
- }
- create() {
- if (this.initialDoc) {
- this.initialDoc = encodeURIComponent(this.initialDoc)
- this._create()
- } else {
- this._create()
- }
- }
- _create () {
- if (!this._trigger) {
- this._trigger = function(e, t) {
- var n = this._createEvent(e, t);
- this.element.dispatchEvent(n)
- }
- }
- this.createViewer()
- }
- createViewer () {
- const self = this
- let webviewerUrl = './webviewer/index.html'
- if (this.initialDoc) {
- webviewerUrl += "#d=\"".concat(decodeURIComponent(this.initialDoc)).concat("\"")
- }
- const iframe = document.createElement("iframe")
- iframe.id = "webviewer-".concat(this.id)
- iframe.src = webviewerUrl
- iframe.title = "webviewer"
- iframe.frameBorder = 0
- iframe.width = "100%"
- iframe.height = "100%"
- iframe.setAttribute("allowfullscreen", true)
- iframe.setAttribute("webkitallowfullscreen", true)
- iframe.setAttribute("mozallowfullscreen", true)
- this.iframe = iframe
- this.options?.backgroundColor && iframe.setAttribute("data-bgcolor", this.options.backgroundColor),
- this.options?.assetPath && iframe.setAttribute("data-assetpath", encodeURIComponent(this.options.assetPath)),
- this.loadListener = function() {
- var $iframe = self.iframe;
- self.instance = $iframe.contentWindow.instance;
- $iframe.contentWindow.instance.initApiUrl(self.options)
- self._trigger("ready");
- },
- iframe.addEventListener("load", this.loadListener),
- this.element.appendChild(iframe)
- }
- }
- export default ComPDFKitViewer
|