webviewer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const ComPDFKitViewer = {
  2. init (options, element, optionsUrl) {
  3. return new Promise((resolve) => {
  4. element.addEventListener("ready", function ready () {
  5. element.removeEventListener('ready', ready)
  6. const iframeWindow = element.querySelector('iframe').contentWindow
  7. Promise.resolve().then(function() {
  8. resolve({
  9. docViewer: iframeWindow.instance
  10. })
  11. })
  12. })
  13. const viewer = new Viewer(options, element, optionsUrl)
  14. })
  15. }
  16. }
  17. class Viewer {
  18. constructor (options, element, optionsUrl) {
  19. this.instance = null
  20. this.options = options
  21. this.initialDoc = options.pdfUrl || null
  22. this.element = element
  23. this.optionsUrl = optionsUrl
  24. element.addEventListener("ready", (function ready() {
  25. element.removeEventListener("ready", ready)
  26. }))
  27. this.create()
  28. }
  29. _createEvent (e, t) {
  30. let n;
  31. try {
  32. n = new CustomEvent(e, {
  33. detail: t,
  34. bubbles: true,
  35. cancelable: true
  36. })
  37. } catch (o) {
  38. (n = document.createEvent("Event")).initEvent(e, true, true),
  39. n.detail = t
  40. }
  41. return n
  42. }
  43. create() {
  44. if (this.initialDoc) {
  45. this.initialDoc = encodeURIComponent(this.initialDoc)
  46. this._create()
  47. } else {
  48. this._create()
  49. }
  50. }
  51. _create () {
  52. if (!this._trigger) {
  53. this._trigger = function(e, t) {
  54. var n = this._createEvent(e, t);
  55. this.element.dispatchEvent(n)
  56. }
  57. }
  58. this.createViewer()
  59. }
  60. createViewer () {
  61. const self = this
  62. let webviewerUrl = './webviewer/index.html'
  63. if (this.initialDoc) {
  64. webviewerUrl += "#d=\"".concat(decodeURIComponent(this.initialDoc)).concat("\"")
  65. }
  66. const iframe = document.createElement("iframe")
  67. iframe.id = "webviewer-".concat(this.id)
  68. iframe.src = webviewerUrl
  69. iframe.title = "webviewer"
  70. iframe.frameBorder = 0
  71. iframe.width = "100%"
  72. iframe.height = "100%"
  73. iframe.setAttribute("allowfullscreen", true)
  74. iframe.setAttribute("webkitallowfullscreen", true)
  75. iframe.setAttribute("mozallowfullscreen", true)
  76. this.iframe = iframe
  77. this.options?.backgroundColor && iframe.setAttribute("data-bgcolor", this.options.backgroundColor),
  78. this.options?.assetPath && iframe.setAttribute("data-assetpath", encodeURIComponent(this.options.assetPath)),
  79. this.loadListener = function() {
  80. var $iframe = self.iframe;
  81. self.instance = $iframe.contentWindow.instance;
  82. self.instance.initOptions(self.options)
  83. self._trigger("ready");
  84. },
  85. iframe.addEventListener("load", this.loadListener),
  86. this.element.appendChild(iframe)
  87. }
  88. }
  89. export default ComPDFKitViewer