BinaryFileController.coffee 1.7 KB

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