pdfJs.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(
  64. 'pdfDoubleClick',
  65. (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 (let 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 data-pdf-viewer class="pdfjs-viewer" pdf-src='pdfSrc' position='position' scale='scale' highlights='highlights' please-jump-to='pleaseJumpTo'></div>
  138. <div class="pdfjs-controls" ng-class="{'flash': flashControls }">
  139. <div class="btn-group">
  140. <a href
  141. class="btn btn-info btn-lg"
  142. ng-click="fitToWidth()"
  143. tooltip="Fit to Width"
  144. tooltip-append-to-body="true"
  145. tooltip-placement="bottom"
  146. >
  147. <i class="fa fa-fw fa-arrows-h"></i>
  148. </a>
  149. <a href
  150. class="btn btn-info btn-lg"
  151. ng-click="fitToHeight()"
  152. tooltip="Fit to Height"
  153. tooltip-append-to-body="true"
  154. tooltip-placement="bottom"
  155. >
  156. <i class="fa fa-fw fa-arrows-v"></i>
  157. </a>
  158. <a href
  159. class="btn btn-info btn-lg"
  160. ng-click="zoomIn()"
  161. tooltip="Zoom In"
  162. tooltip-append-to-body="true"
  163. tooltip-placement="bottom"
  164. >
  165. <i class="fa fa-fw fa-search-plus"></i>
  166. </a>
  167. <a href
  168. class="btn btn-info btn-lg"
  169. ng-click="zoomOut()"
  170. tooltip="Zoom Out"
  171. tooltip-append-to-body="true"
  172. tooltip-placement="bottom"
  173. >
  174. <i class="fa fa-fw fa-search-minus"></i>
  175. </a>
  176. </div>
  177. </div>
  178. <div class="progress-thin" ng-show="loading">
  179. <div class="progress-bar" ng-style="{ 'width': progress + '%' }"></div>
  180. </div>\
  181. `
  182. }))