pdfJs.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. },
  24. link(scope, element, attrs) {
  25. scope.loading = false
  26. scope.pleaseJumpTo = null
  27. scope.scale = null
  28. let initializedPosition = false
  29. const initializePosition = function() {
  30. let position, scale
  31. if (initializedPosition) {
  32. return
  33. }
  34. initializedPosition = true
  35. if ((scale = localStorage('pdf.scale')) != null) {
  36. scope.scale = { scaleMode: scale.scaleMode, scale: +scale.scale }
  37. } else {
  38. scope.scale = { scaleMode: 'scale_mode_fit_width' }
  39. }
  40. if ((position = localStorage(`pdf.position.${attrs.key}`))) {
  41. scope.position = {
  42. page: +position.page,
  43. offset: {
  44. top: +position.offset.top,
  45. left: +position.offset.left
  46. }
  47. }
  48. }
  49. scope.$on('$destroy', () => {
  50. localStorage('pdf.scale', scope.scale)
  51. return localStorage(`pdf.position.${attrs.key}`, scope.position)
  52. })
  53. return $(window).unload(() => {
  54. localStorage('pdf.scale', scope.scale)
  55. return localStorage(`pdf.position.${attrs.key}`, scope.position)
  56. })
  57. }
  58. const flashControls = () =>
  59. scope.$evalAsync(function() {
  60. scope.flashControls = true
  61. return $timeout(() => (scope.flashControls = false), 1000)
  62. })
  63. scope.$on('pdfDoubleClick', (event, e) =>
  64. typeof scope.dblClickCallback === 'function'
  65. ? scope.dblClickCallback({
  66. page: e.page - 1,
  67. offset: { top: e.y, left: e.x }
  68. })
  69. : undefined
  70. )
  71. scope.$on('flash-controls', () => flashControls())
  72. scope.$watch('pdfSrc', function(url) {
  73. if (url) {
  74. scope.loading = true
  75. scope.loaded = false
  76. scope.progress = 1
  77. initializePosition()
  78. return flashControls()
  79. }
  80. })
  81. scope.$on('loaded', function() {
  82. scope.loaded = true
  83. scope.progress = 100
  84. return $timeout(function() {
  85. scope.loading = false
  86. return delete scope.progress
  87. }, 500)
  88. })
  89. scope.fitToHeight = function() {
  90. const scale = angular.copy(scope.scale)
  91. scale.scaleMode = 'scale_mode_fit_height'
  92. return (scope.scale = scale)
  93. }
  94. scope.fitToWidth = function() {
  95. const scale = angular.copy(scope.scale)
  96. scale.scaleMode = 'scale_mode_fit_width'
  97. return (scope.scale = scale)
  98. }
  99. scope.zoomIn = function() {
  100. const scale = angular.copy(scope.scale)
  101. scale.scaleMode = 'scale_mode_value'
  102. scale.scale = scale.scale * 1.2
  103. return (scope.scale = scale)
  104. }
  105. scope.zoomOut = function() {
  106. const scale = angular.copy(scope.scale)
  107. scale.scaleMode = 'scale_mode_value'
  108. scale.scale = scale.scale / 1.2
  109. return (scope.scale = scale)
  110. }
  111. if (attrs.resizeOn != null) {
  112. for (let event of Array.from(attrs.resizeOn.split(','))) {
  113. scope.$on(event, function(e) {})
  114. }
  115. }
  116. // console.log 'got a resize event', event, e
  117. scope.$on('progress', (event, progress) =>
  118. scope.$apply(function() {
  119. if (scope.loaded) {
  120. return
  121. }
  122. scope.progress = Math.floor((progress.loaded / progress.total) * 100)
  123. if (scope.progress > 100) {
  124. scope.progress = 100
  125. }
  126. if (scope.progress < 0) {
  127. return (scope.progress = 0)
  128. }
  129. })
  130. )
  131. return scope.$on('$destroy', function() {})
  132. },
  133. // console.log 'pdfjs destroy event'
  134. template: `\
  135. <div data-pdf-viewer class="pdfjs-viewer" pdf-src='pdfSrc' position='position' scale='scale' highlights='highlights' please-jump-to='pleaseJumpTo'></div>
  136. <div class="pdfjs-controls" ng-class="{'flash': flashControls }">
  137. <div class="btn-group">
  138. <a href
  139. class="btn btn-info btn-lg"
  140. ng-click="fitToWidth()"
  141. tooltip="Fit to Width"
  142. tooltip-append-to-body="true"
  143. tooltip-placement="bottom"
  144. >
  145. <i class="fa fa-fw fa-arrows-h"></i>
  146. </a>
  147. <a href
  148. class="btn btn-info btn-lg"
  149. ng-click="fitToHeight()"
  150. tooltip="Fit to Height"
  151. tooltip-append-to-body="true"
  152. tooltip-placement="bottom"
  153. >
  154. <i class="fa fa-fw fa-arrows-v"></i>
  155. </a>
  156. <a href
  157. class="btn btn-info btn-lg"
  158. ng-click="zoomIn()"
  159. tooltip="Zoom In"
  160. tooltip-append-to-body="true"
  161. tooltip-placement="bottom"
  162. >
  163. <i class="fa fa-fw fa-search-plus"></i>
  164. </a>
  165. <a href
  166. class="btn btn-info btn-lg"
  167. ng-click="zoomOut()"
  168. tooltip="Zoom Out"
  169. tooltip-append-to-body="true"
  170. tooltip-placement="bottom"
  171. >
  172. <i class="fa fa-fw fa-search-minus"></i>
  173. </a>
  174. </div>
  175. </div>
  176. <div class="progress-thin" ng-show="loading">
  177. <div class="progress-bar" ng-style="{ 'width': progress + '%' }"></div>
  178. </div>\
  179. `
  180. }))