BinaryFileController.coffee 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. .success (data) ->
  32. $scope.bibtexPreview.loading = false
  33. $scope.bibtexPreview.error = false
  34. # show dots when payload is closs to cutoff
  35. if data.length >= (TWO_MEGABYTES - 200)
  36. $scope.bibtexPreview.shouldShowDots = true
  37. try
  38. # remove last partial line
  39. data = data.replace(/\n.*$/, '')
  40. finally
  41. $scope.bibtexPreview.data = data
  42. $timeout($scope.setHeight, 0)
  43. .error (err) ->
  44. $scope.bibtexPreview.error = true
  45. $scope.bibtexPreview.loading = false
  46. $scope.setHeight = () ->
  47. # Behold, a ghastly hack
  48. guide = document.querySelector('.file-tree-inner')
  49. table_wrap = document.querySelector('.bib-preview .scroll-container')
  50. if table_wrap
  51. desired_height = guide.offsetHeight - 44
  52. if table_wrap.offsetHeight > desired_height
  53. table_wrap.style.height = desired_height + 'px'
  54. table_wrap.style['max-height'] = desired_height + 'px'
  55. $scope.loadBibtexIfRequired = () ->
  56. if $scope.extension($scope.openFile) == 'bib'
  57. $scope.bibtexPreview.data = null
  58. $scope.loadBibtexFilePreview()
  59. $scope.loadBibtexIfRequired()
  60. ]