overleaf/services/web/public/coffee/ide/binary-files/controllers/BinaryFileController.coffee

71 lines
2.2 KiB
CoffeeScript
Raw Normal View History

2014-07-08 07:02:26 -04:00
define [
"base"
], (App) ->
2016-05-17 12:00:14 -04:00
App.controller "BinaryFileController", ["$scope", "$rootScope", "$http", "$timeout", ($scope, $rootScope, $http, $timeout) ->
TWO_MEGABYTES = 2 * 1024 * 1024
2016-05-17 12:00:14 -04:00
$scope.bibtexPreview =
loading: false
shouldShowDots: false
2016-05-17 12:00:14 -04:00
error: false
2016-05-19 06:11:23 -04:00
data: null
# Callback fired when the `img` tag fails to load,
# `failedLoad` used to show the "No Preview" message
$scope.failedLoad = false
window.sl_binaryFilePreviewError = () =>
$scope.failedLoad = true
$scope.$apply()
# Callback fired when the `img` tag is done loading,
# `imgLoaded` is used to show the spinner gif while loading
$scope.imgLoaded = false
window.sl_binaryFilePreviewLoaded = () =>
$scope.imgLoaded = true
$scope.$apply()
2014-07-08 07:02:26 -04:00
$scope.extension = (file) ->
return file.name.split(".").pop()?.toLowerCase()
2016-05-17 12:00:14 -04:00
$scope.loadBibtexFilePreview = () ->
url = "/project/#{project_id}/file/#{$scope.openFile.id}?range=0-#{TWO_MEGABYTES}"
2016-05-17 12:00:14 -04:00
$scope.bibtexPreview.loading = true
$scope.bibtexPreview.shouldShowDots = false
2016-05-19 06:11:23 -04:00
$scope.$apply()
2016-05-17 12:00:14 -04:00
$http.get(url)
.then (data) ->
2016-05-17 12:00:14 -04:00
$scope.bibtexPreview.loading = false
$scope.bibtexPreview.error = false
# show dots when payload is closs to cutoff
if data.length >= (TWO_MEGABYTES - 200)
$scope.bibtexPreview.shouldShowDots = true
2016-05-17 12:00:14 -04:00
try
# remove last partial line
data = data.replace(/\n.*$/, '')
finally
$scope.bibtexPreview.data = data
$timeout($scope.setHeight, 0)
.catch (err) ->
2016-05-17 12:00:14 -04:00
$scope.bibtexPreview.error = true
$scope.bibtexPreview.loading = false
$scope.setHeight = () ->
# Behold, a ghastly hack
guide = document.querySelector('.file-tree-inner')
table_wrap = document.querySelector('.bib-preview .scroll-container')
if table_wrap
desired_height = guide.offsetHeight - 44
2016-05-17 12:00:14 -04:00
if table_wrap.offsetHeight > desired_height
table_wrap.style.height = desired_height + 'px'
table_wrap.style['max-height'] = desired_height + 'px'
2016-05-19 06:11:23 -04:00
$scope.loadBibtexIfRequired = () ->
if $scope.extension($scope.openFile) == 'bib'
$scope.bibtexPreview.data = null
$scope.loadBibtexFilePreview()
2016-05-19 06:11:23 -04:00
$scope.loadBibtexIfRequired()
]