2014-07-08 07:02:26 -04:00
|
|
|
define [
|
|
|
|
"base"
|
2018-02-20 05:37:55 -05:00
|
|
|
"moment"
|
|
|
|
], (App, moment) ->
|
2018-02-20 06:49:02 -05:00
|
|
|
App.controller "BinaryFileController", ["$scope", "$rootScope", "$http", "$timeout", "$element", "ide", ($scope, $rootScope, $http, $timeout, $element, ide) ->
|
2016-05-17 12:00:14 -04:00
|
|
|
|
2016-05-19 05:02:25 -04:00
|
|
|
TWO_MEGABYTES = 2 * 1024 * 1024
|
|
|
|
|
2018-02-20 05:37:55 -05:00
|
|
|
textExtensions = ['bib', 'tex', 'txt', 'cls', 'sty']
|
|
|
|
imageExtentions = ['png', 'jpg', 'jpeg', 'gif']
|
|
|
|
previewableExtensions = ['eps', 'pdf']
|
|
|
|
|
|
|
|
extension = (file) ->
|
|
|
|
return file.name.split(".").pop()?.toLowerCase()
|
|
|
|
|
|
|
|
$scope.isTextFile = () =>
|
|
|
|
textExtensions.indexOf(extension($scope.openFile)) > -1
|
|
|
|
$scope.isImageFile = () =>
|
|
|
|
imageExtentions.indexOf(extension($scope.openFile)) > -1
|
|
|
|
$scope.isPreviewableFile = () =>
|
|
|
|
previewableExtensions.indexOf(extension($scope.openFile)) > -1
|
|
|
|
$scope.isUnpreviewableFile = () ->
|
|
|
|
!$scope.isTextFile() and
|
|
|
|
!$scope.isImageFile() and
|
|
|
|
!$scope.isPreviewableFile()
|
|
|
|
|
|
|
|
$scope.textPreview =
|
2016-05-17 12:00:14 -04:00
|
|
|
loading: false
|
2016-05-20 04:17:25 -04:00
|
|
|
shouldShowDots: false
|
2016-05-17 12:00:14 -04:00
|
|
|
error: false
|
2016-05-19 06:11:23 -04:00
|
|
|
data: null
|
2016-05-04 04:32:59 -04:00
|
|
|
|
2018-02-20 06:49:02 -05:00
|
|
|
$scope.refreshing = false
|
2018-05-21 06:02:12 -04:00
|
|
|
$scope.refreshError = null
|
2018-02-20 06:49:02 -05:00
|
|
|
|
2018-02-20 05:37:55 -05:00
|
|
|
MAX_URL_LENGTH = 60
|
|
|
|
FRONT_OF_URL_LENGTH = 35
|
|
|
|
FILLER = '...'
|
|
|
|
TAIL_OF_URL_LENGTH = MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length
|
|
|
|
$scope.displayUrl = (url) ->
|
2018-03-05 06:21:31 -05:00
|
|
|
if !url?
|
|
|
|
return
|
2018-02-20 05:37:55 -05:00
|
|
|
if url.length > MAX_URL_LENGTH
|
|
|
|
front = url.slice(0, FRONT_OF_URL_LENGTH)
|
|
|
|
tail = url.slice(url.length - TAIL_OF_URL_LENGTH)
|
|
|
|
return front + FILLER + tail
|
|
|
|
else
|
|
|
|
return url
|
|
|
|
|
2018-05-24 06:31:50 -04:00
|
|
|
_tryOpenFile = (new_file_id) ->
|
|
|
|
iterations = 0
|
|
|
|
do tryOpen = () ->
|
|
|
|
if iterations > 10
|
|
|
|
return
|
|
|
|
iterations += 1
|
|
|
|
newFile = ide.fileTreeManager.findEntityById(new_file_id)
|
|
|
|
if newFile?
|
|
|
|
ide.binaryFilesManager.openFile(newFile)
|
|
|
|
else
|
|
|
|
setTimeout(tryOpen, 500)
|
|
|
|
|
2018-02-20 06:49:02 -05:00
|
|
|
$scope.refreshFile = (file) ->
|
|
|
|
$scope.refreshing = true
|
2018-05-21 06:02:12 -04:00
|
|
|
$scope.refreshError = null
|
2018-02-20 06:49:02 -05:00
|
|
|
ide.fileTreeManager.refreshLinkedFile(file)
|
2018-05-18 05:35:02 -04:00
|
|
|
.then (response) ->
|
|
|
|
{ data } = response
|
2018-05-18 06:07:59 -04:00
|
|
|
{ new_file_id } = data
|
2018-05-18 05:35:02 -04:00
|
|
|
$timeout(
|
|
|
|
() ->
|
2018-05-24 06:31:50 -04:00
|
|
|
_tryOpenFile(new_file_id)
|
|
|
|
, 0
|
2018-05-18 05:35:02 -04:00
|
|
|
)
|
2018-05-21 06:02:12 -04:00
|
|
|
$scope.refreshError = null
|
|
|
|
.catch (response) ->
|
|
|
|
$scope.refreshError = response.data
|
2018-02-20 06:49:02 -05:00
|
|
|
.finally () ->
|
|
|
|
$scope.refreshing = false
|
|
|
|
|
2016-05-20 09:28:51 -04:00
|
|
|
# Callback fired when the `img` tag fails to load,
|
|
|
|
# `failedLoad` used to show the "No Preview" message
|
2016-05-04 04:32:59 -04:00
|
|
|
$scope.failedLoad = false
|
|
|
|
window.sl_binaryFilePreviewError = () =>
|
|
|
|
$scope.failedLoad = true
|
|
|
|
$scope.$apply()
|
|
|
|
|
2016-05-20 09:28:51 -04:00
|
|
|
# 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()
|
|
|
|
|
2018-02-20 06:49:02 -05:00
|
|
|
do loadTextFileFilePreview = () ->
|
|
|
|
return unless $scope.isTextFile()
|
2016-05-19 05:02:25 -04:00
|
|
|
url = "/project/#{project_id}/file/#{$scope.openFile.id}?range=0-#{TWO_MEGABYTES}"
|
2018-02-20 06:49:02 -05:00
|
|
|
$scope.textPreview.data = null
|
2018-02-20 05:37:55 -05:00
|
|
|
$scope.textPreview.loading = true
|
|
|
|
$scope.textPreview.shouldShowDots = false
|
2016-05-19 06:11:23 -04:00
|
|
|
$scope.$apply()
|
2018-02-20 06:49:02 -05:00
|
|
|
$http({
|
|
|
|
url: url,
|
|
|
|
method: 'GET',
|
|
|
|
transformResponse: null # Don't parse JSON
|
|
|
|
})
|
2017-06-20 11:04:06 -04:00
|
|
|
.then (response) ->
|
|
|
|
{ data } = response
|
2018-02-20 05:37:55 -05:00
|
|
|
$scope.textPreview.error = false
|
2016-05-20 04:17:25 -04:00
|
|
|
# show dots when payload is closs to cutoff
|
|
|
|
if data.length >= (TWO_MEGABYTES - 200)
|
2018-02-20 05:37:55 -05:00
|
|
|
$scope.textPreview.shouldShowDots = true
|
2016-05-17 12:00:14 -04:00
|
|
|
# remove last partial line
|
2018-05-24 06:35:55 -04:00
|
|
|
data = data?.replace?(/\n.*$/, '')
|
2018-05-18 06:25:01 -04:00
|
|
|
$scope.textPreview.data = data
|
2018-02-20 05:37:55 -05:00
|
|
|
$timeout(setHeight, 0)
|
2018-02-20 06:49:02 -05:00
|
|
|
.catch (error) ->
|
|
|
|
console.error(error)
|
2018-02-20 05:37:55 -05:00
|
|
|
$scope.textPreview.error = true
|
|
|
|
$scope.textPreview.loading = false
|
2016-05-19 06:11:23 -04:00
|
|
|
|
2018-02-20 05:37:55 -05:00
|
|
|
setHeight = () ->
|
|
|
|
$preview = $element.find('.text-preview .scroll-container')
|
|
|
|
$footer = $element.find('.binary-file-footer')
|
|
|
|
maxHeight = $element.height() - $footer.height() - 14 # borders + margin
|
|
|
|
$preview.css('max-height': maxHeight)
|
|
|
|
# Don't show the preview until we've set the height, otherwise we jump around
|
|
|
|
$scope.textPreview.loading = false
|
2016-05-20 06:20:00 -04:00
|
|
|
|
2016-05-04 04:32:59 -04:00
|
|
|
]
|