BinaryFileController.coffee 1.8 KB

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