BinaryFileController.coffee 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. $scope.failedLoad = false
  12. $rootScope.$on 'entity:selected', () ->
  13. $scope.failedLoad = false
  14. $scope.loadBibtexIfRequired()
  15. $scope.loadBibtexIfRequired = () ->
  16. if $scope.extension($scope.openFile) == 'bib'
  17. $scope.bibtexPreview.data = null
  18. $scope.loadBibtexFilePreview()
  19. window.sl_binaryFilePreviewError = () =>
  20. $scope.failedLoad = true
  21. $scope.$apply()
  22. $scope.extension = (file) ->
  23. return file.name.split(".").pop()?.toLowerCase()
  24. $scope.loadBibtexFilePreview = () ->
  25. url = "/project/#{project_id}/file/#{$scope.openFile.id}?range=0-#{TWO_MEGABYTES}"
  26. $scope.bibtexPreview.loading = true
  27. $scope.bibtexPreview.shouldShowDots = false
  28. $scope.$apply()
  29. $http.get(url)
  30. .success (data) ->
  31. $scope.bibtexPreview.loading = false
  32. $scope.bibtexPreview.error = false
  33. # show dots when payload is closs to cutoff
  34. if data.length >= (TWO_MEGABYTES - 200)
  35. $scope.bibtexPreview.shouldShowDots = true
  36. try
  37. # remove last partial line
  38. data = data.replace(/\n.*$/, '')
  39. finally
  40. $scope.bibtexPreview.data = data
  41. $timeout($scope.setHeight, 0)
  42. .error (err) ->
  43. $scope.bibtexPreview.error = true
  44. $scope.bibtexPreview.loading = false
  45. $scope.setHeight = () ->
  46. # Behold, a ghastly hack
  47. guide = document.querySelector('.file-tree-inner')
  48. table_wrap = document.querySelector('.bib-preview .scroll-container')
  49. if table_wrap
  50. desired_height = guide.offsetHeight - 44
  51. if table_wrap.offsetHeight > desired_height
  52. table_wrap.style.height = desired_height + 'px'
  53. table_wrap.style['max-height'] = desired_height + 'px'
  54. $scope.loadBibtexIfRequired()
  55. ]