pdfJs.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* eslint-disable
  2. max-len,
  3. no-cond-assign,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. import App from '../../../base'
  16. import './pdfViewer'
  17. export default App.directive('pdfng', ($timeout, localStorage) => ({
  18. scope: {
  19. pdfSrc: '=',
  20. highlights: '=',
  21. position: '=',
  22. dblClickCallback: '=',
  23. firstRenderDone: '=',
  24. updateConsumedBandwidth: '=',
  25. },
  26. link(scope, element, attrs) {
  27. scope.loading = false
  28. scope.pleaseJumpTo = null
  29. scope.scale = null
  30. let initializedPosition = false
  31. const initializePosition = function () {
  32. let position, scale
  33. if (initializedPosition) {
  34. return
  35. }
  36. initializedPosition = true
  37. if ((scale = localStorage('pdf.scale')) != null) {
  38. scope.scale = { scaleMode: scale.scaleMode, scale: +scale.scale }
  39. } else {
  40. scope.scale = { scaleMode: 'scale_mode_fit_width' }
  41. }
  42. if ((position = localStorage(`pdf.position.${attrs.key}`))) {
  43. scope.position = {
  44. page: +position.page,
  45. offset: {
  46. top: +position.offset.top,
  47. left: +position.offset.left,
  48. },
  49. }
  50. }
  51. scope.$on('$destroy', () => {
  52. localStorage('pdf.scale', scope.scale)
  53. return localStorage(`pdf.position.${attrs.key}`, scope.position)
  54. })
  55. return $(window).unload(() => {
  56. localStorage('pdf.scale', scope.scale)
  57. return localStorage(`pdf.position.${attrs.key}`, scope.position)
  58. })
  59. }
  60. const flashControls = () =>
  61. scope.$evalAsync(function () {
  62. scope.flashControls = true
  63. return $timeout(() => (scope.flashControls = false), 1000)
  64. })
  65. scope.$on('pdfDoubleClick', (event, e) =>
  66. typeof scope.dblClickCallback === 'function'
  67. ? scope.dblClickCallback({
  68. page: e.page - 1,
  69. offset: { top: e.y, left: e.x },
  70. })
  71. : undefined
  72. )
  73. scope.$on('flash-controls', () => flashControls())
  74. scope.$watch('pdfSrc', function (url) {
  75. if (url) {
  76. scope.loading = true
  77. scope.loaded = false
  78. scope.progress = 1
  79. initializePosition()
  80. return flashControls()
  81. }
  82. })
  83. scope.$on('loaded', function () {
  84. scope.loaded = true
  85. scope.progress = 100
  86. return $timeout(function () {
  87. scope.loading = false
  88. return delete scope.progress
  89. }, 500)
  90. })
  91. scope.fitToHeight = function () {
  92. const scale = angular.copy(scope.scale)
  93. scale.scaleMode = 'scale_mode_fit_height'
  94. return (scope.scale = scale)
  95. }
  96. scope.fitToWidth = function () {
  97. const scale = angular.copy(scope.scale)
  98. scale.scaleMode = 'scale_mode_fit_width'
  99. return (scope.scale = scale)
  100. }
  101. scope.zoomIn = function () {
  102. const scale = angular.copy(scope.scale)
  103. scale.scaleMode = 'scale_mode_value'
  104. scale.scale = scale.scale * 1.2
  105. return (scope.scale = scale)
  106. }
  107. scope.zoomOut = function () {
  108. const scale = angular.copy(scope.scale)
  109. scale.scaleMode = 'scale_mode_value'
  110. scale.scale = scale.scale / 1.2
  111. return (scope.scale = scale)
  112. }
  113. if (attrs.resizeOn != null) {
  114. for (const event of Array.from(attrs.resizeOn.split(','))) {
  115. scope.$on(event, function (e) {})
  116. }
  117. }
  118. // console.log 'got a resize event', event, e
  119. scope.$on('progress', (event, progress) =>
  120. scope.$apply(function () {
  121. if (scope.loaded) {
  122. return
  123. }
  124. scope.progress = Math.floor((progress.loaded / progress.total) * 100)
  125. if (scope.progress > 100) {
  126. scope.progress = 100
  127. }
  128. if (scope.progress < 0) {
  129. return (scope.progress = 0)
  130. }
  131. })
  132. )
  133. return scope.$on('$destroy', function () {})
  134. },
  135. // console.log 'pdfjs destroy event'
  136. template: `\
  137. <div
  138. data-pdf-viewer
  139. class="pdfjs-viewer"
  140. pdf-src='pdfSrc'
  141. position='position'
  142. scale='scale'
  143. highlights='highlights'
  144. please-jump-to='pleaseJumpTo'
  145. first-render-done="firstRenderDone"
  146. update-consumed-bandwidth="updateConsumedBandwidth"
  147. >
  148. </div>
  149. <div class="pdfjs-controls" ng-class="{'flash': flashControls }">
  150. <div class="btn-group">
  151. <a href
  152. class="btn btn-info btn-lg"
  153. ng-click="fitToWidth()"
  154. tooltip="Fit to Width"
  155. tooltip-append-to-body="true"
  156. tooltip-placement="bottom"
  157. >
  158. <i class="fa fa-fw fa-arrows-h"></i>
  159. </a>
  160. <a href
  161. class="btn btn-info btn-lg"
  162. ng-click="fitToHeight()"
  163. tooltip="Fit to Height"
  164. tooltip-append-to-body="true"
  165. tooltip-placement="bottom"
  166. >
  167. <i class="fa fa-fw fa-arrows-v"></i>
  168. </a>
  169. <a href
  170. class="btn btn-info btn-lg"
  171. ng-click="zoomIn()"
  172. tooltip="Zoom In"
  173. tooltip-append-to-body="true"
  174. tooltip-placement="bottom"
  175. >
  176. <i class="fa fa-fw fa-search-plus"></i>
  177. </a>
  178. <a href
  179. class="btn btn-info btn-lg"
  180. ng-click="zoomOut()"
  181. tooltip="Zoom Out"
  182. tooltip-append-to-body="true"
  183. tooltip-placement="bottom"
  184. >
  185. <i class="fa fa-fw fa-search-minus"></i>
  186. </a>
  187. </div>
  188. </div>
  189. <div class="progress-thin" ng-show="loading">
  190. <div class="progress-bar" ng-style="{ 'width': progress + '%' }"></div>
  191. </div>\
  192. `,
  193. }))