123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- const ComPDFKitViewer = {
- init (options, element, optionsUrl) {
- 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,
- Core: iframeWindow.instances.Core,
- UI: iframeWindow.instances.UI
- })
- })
- })
- const viewer = new Viewer(options, element, optionsUrl)
- })
- }
- }
- const docViewer = new Map()
- class Viewer {
- constructor (options, element, optionsUrl) {
- if (docViewer.get(element)) return
- docViewer.set(element, true)
- this.instance = null
- this.options = options
- this.initialDoc = options.pdfUrl || null
- this.element = element
- this.optionsUrl = optionsUrl
- 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;
- self.instance.initOptions(self.options)
- self._trigger("ready");
- },
- iframe.addEventListener("load", this.loadListener),
- this.element.appendChild(iframe)
- }
- }
- export default ComPDFKitViewer
|