BinaryFileController.coffee 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. define [
  2. "base"
  3. ], (App) ->
  4. App.controller "BinaryFileController", ["$scope", "$rootScope", "$http", "$timeout", ($scope, $rootScope, $http, $timeout) ->
  5. TWO_MEGABYTES = 2 * 1024 * 1024
  6. $scope.bibtexPreview =
  7. loading: false
  8. shouldShowDots: false
  9. error: false
  10. data: null
  11. # Callback fired when the `img` tag fails to load,
  12. # `failedLoad` used to show the "No Preview" message
  13. $scope.failedLoad = false
  14. window.sl_binaryFilePreviewError = () =>
  15. $scope.failedLoad = true
  16. $scope.$apply()
  17. # Callback fired when the `img` tag is done loading,
  18. # `imgLoaded` is used to show the spinner gif while loading
  19. $scope.imgLoaded = false
  20. window.sl_binaryFilePreviewLoaded = () =>
  21. $scope.imgLoaded = true
  22. $scope.$apply()
  23. $scope.extension = (file) ->
  24. return file.name.split(".").pop()?.toLowerCase()
  25. $scope.loadBibtexFilePreview = () ->
  26. url = "/project/#{project_id}/file/#{$scope.openFile.id}?range=0-#{TWO_MEGABYTES}"
  27. $scope.bibtexPreview.loading = true
  28. $scope.bibtexPreview.shouldShowDots = false
  29. $scope.$apply()
  30. $http.get(url)
  31. .then (response) ->
  32. { data } = response
  33. $scope.bibtexPreview.loading = false
  34. $scope.bibtexPreview.error = false
  35. # show dots when payload is closs to cutoff
  36. if data.length >= (TWO_MEGABYTES - 200)
  37. $scope.bibtexPreview.shouldShowDots = true
  38. try
  39. # remove last partial line
  40. data = data.replace(/\n.*$/, '')
  41. finally
  42. $scope.bibtexPreview.data = data
  43. $timeout($scope.setHeight, 0)
  44. .catch () ->
  45. $scope.bibtexPreview.error = true
  46. $scope.bibtexPreview.loading = false
  47. $scope.setHeight = () ->
  48. # Behold, a ghastly hack
  49. guide = document.querySelector('.file-tree-inner')
  50. table_wrap = document.querySelector('.bib-preview .scroll-container')
  51. if table_wrap
  52. desired_height = guide.offsetHeight - 44
  53. if table_wrap.offsetHeight > desired_height
  54. table_wrap.style.height = desired_height + 'px'
  55. table_wrap.style['max-height'] = desired_height + 'px'
  56. $scope.loadBibtexIfRequired = () ->
  57. if $scope.extension($scope.openFile) == 'bib'
  58. $scope.bibtexPreview.data = null
  59. $scope.loadBibtexFilePreview()
  60. $scope.loadBibtexIfRequired()
  61. ]