2014-07-08 07:02:26 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
"libs/latex-log-parser"
|
2016-03-08 11:18:02 -05:00
|
|
|
"libs/bib-log-parser"
|
|
|
|
], (App, LogParser, BibLogParser) ->
|
2015-02-12 06:32:27 -05:00
|
|
|
App.controller "PdfController", ($scope, $http, ide, $modal, synctex, event_tracking, localStorage) ->
|
2016-03-22 06:24:58 -04:00
|
|
|
|
2014-07-08 07:02:26 -04:00
|
|
|
autoCompile = true
|
2016-03-22 10:38:48 -04:00
|
|
|
|
|
|
|
# pdf.view = uncompiled | pdf | errors
|
|
|
|
$scope.pdf.view = if $scope?.pdf?.url then 'pdf' else 'uncompiled'
|
2016-03-22 06:24:58 -04:00
|
|
|
$scope.shouldShowLogs = false
|
2016-03-22 05:39:33 -04:00
|
|
|
|
2014-07-22 08:38:34 -04:00
|
|
|
$scope.$on "project:joined", () ->
|
2014-07-08 07:02:26 -04:00
|
|
|
return if !autoCompile
|
|
|
|
autoCompile = false
|
|
|
|
$scope.recompile(isAutoCompile: true)
|
2014-10-20 07:21:40 -04:00
|
|
|
$scope.hasPremiumCompile = $scope.project.features.compileGroup == "priority"
|
2014-07-08 07:02:26 -04:00
|
|
|
|
2015-01-12 11:46:17 -05:00
|
|
|
$scope.$on "pdf:error:display", () ->
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.pdf.view = 'errors'
|
2016-03-21 09:20:03 -04:00
|
|
|
$scope.pdf.renderingError = true
|
2016-03-08 08:20:23 -05:00
|
|
|
|
2016-02-02 09:50:48 -05:00
|
|
|
$scope.draft = localStorage("draft:#{$scope.project_id}") or false
|
|
|
|
$scope.$watch "draft", (new_value, old_value) ->
|
|
|
|
if new_value? and old_value != new_value
|
|
|
|
localStorage("draft:#{$scope.project_id}", new_value)
|
2015-01-12 11:46:17 -05:00
|
|
|
|
2014-07-08 07:02:26 -04:00
|
|
|
sendCompileRequest = (options = {}) ->
|
|
|
|
url = "/project/#{$scope.project_id}/compile"
|
|
|
|
if options.isAutoCompile
|
|
|
|
url += "?auto_compile=true"
|
|
|
|
return $http.post url, {
|
2014-11-28 09:26:21 -05:00
|
|
|
rootDoc_id: options.rootDocOverride_id or null
|
2016-02-02 09:50:48 -05:00
|
|
|
draft: $scope.draft
|
2014-07-08 07:02:26 -04:00
|
|
|
_csrf: window.csrfToken
|
|
|
|
}
|
|
|
|
|
|
|
|
parseCompileResponse = (response) ->
|
|
|
|
# Reset everything
|
|
|
|
$scope.pdf.error = false
|
|
|
|
$scope.pdf.timedout = false
|
|
|
|
$scope.pdf.failure = false
|
|
|
|
$scope.pdf.uncompiled = false
|
2014-12-04 19:10:46 -05:00
|
|
|
$scope.pdf.projectTooLarge = false
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.pdf.url = null
|
2016-03-21 11:00:25 -04:00
|
|
|
$scope.pdf.clsiMaintenance = false
|
2016-03-21 11:16:17 -04:00
|
|
|
$scope.pdf.tooRecentlyCompiled = false
|
2016-04-25 07:42:03 -04:00
|
|
|
$scope.pdf.renderingError = false
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
if response.status == "timedout"
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.pdf.view = 'errors'
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.pdf.timedout = true
|
|
|
|
else if response.status == "autocompile-backoff"
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.pdf.view = 'errors'
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.pdf.uncompiled = true
|
2014-11-27 10:42:37 -05:00
|
|
|
else if response.status == "project-too-large"
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.pdf.view = 'errors'
|
2014-11-27 10:42:37 -05:00
|
|
|
$scope.pdf.projectTooLarge = true
|
2014-07-08 07:02:26 -04:00
|
|
|
else if response.status == "failure"
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.pdf.view = 'errors'
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.pdf.failure = true
|
2016-03-22 12:59:40 -04:00
|
|
|
$scope.shouldShowLogs = true
|
2014-07-08 07:02:26 -04:00
|
|
|
fetchLogs()
|
2016-03-21 11:00:25 -04:00
|
|
|
else if response.status == 'clsi-maintenance'
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.pdf.view = 'errors'
|
2016-03-21 11:00:25 -04:00
|
|
|
$scope.pdf.clsiMaintenance = true
|
2016-03-21 11:16:17 -04:00
|
|
|
else if response.status == "too-recently-compiled"
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.pdf.view = 'errors'
|
2016-03-21 11:16:17 -04:00
|
|
|
$scope.pdf.tooRecentlyCompiled = true
|
2016-03-22 06:32:44 -04:00
|
|
|
else if response.status == "success"
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.pdf.view = 'pdf'
|
2016-03-22 06:32:55 -04:00
|
|
|
$scope.shouldShowLogs = false
|
2015-02-26 11:47:47 -05:00
|
|
|
# define the base url
|
2015-02-25 12:06:27 -05:00
|
|
|
$scope.pdf.url = "/project/#{$scope.project_id}/output/output.pdf?cache_bust=#{Date.now()}"
|
2015-02-26 11:47:47 -05:00
|
|
|
# add a query string parameter for the compile group
|
2014-12-12 11:47:43 -05:00
|
|
|
if response.compileGroup?
|
|
|
|
$scope.pdf.compileGroup = response.compileGroup
|
2015-02-25 12:06:27 -05:00
|
|
|
$scope.pdf.url = $scope.pdf.url + "&compileGroup=#{$scope.pdf.compileGroup}"
|
2015-02-26 11:47:47 -05:00
|
|
|
# make a cache to look up files by name
|
2015-02-25 12:06:27 -05:00
|
|
|
fileByPath = {}
|
|
|
|
for file in response.outputFiles
|
|
|
|
fileByPath[file.path] = file
|
2015-02-26 11:47:47 -05:00
|
|
|
# if the pdf file has a build number, pass it to the clsi
|
2015-02-25 12:06:27 -05:00
|
|
|
if fileByPath['output.pdf']?.build?
|
|
|
|
build = fileByPath['output.pdf'].build
|
|
|
|
$scope.pdf.url = $scope.pdf.url + "&build=#{build}"
|
|
|
|
|
2015-02-26 11:47:47 -05:00
|
|
|
fetchLogs(fileByPath['output.log'])
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
IGNORE_FILES = ["output.fls", "output.fdb_latexmk"]
|
|
|
|
$scope.pdf.outputFiles = []
|
2015-02-23 12:43:22 -05:00
|
|
|
|
|
|
|
if !response.outputFiles?
|
|
|
|
return
|
2014-07-08 07:02:26 -04:00
|
|
|
for file in response.outputFiles
|
|
|
|
if IGNORE_FILES.indexOf(file.path) == -1
|
|
|
|
# Turn 'output.blg' into 'blg file'.
|
|
|
|
if file.path.match(/^output\./)
|
|
|
|
file.name = "#{file.path.replace(/^output\./, "")} file"
|
|
|
|
else
|
|
|
|
file.name = file.path
|
|
|
|
$scope.pdf.outputFiles.push file
|
|
|
|
|
2015-02-26 11:47:47 -05:00
|
|
|
fetchLogs = (outputFile) ->
|
|
|
|
qs = if outputFile?.build? then "?build=#{outputFile.build}" else ""
|
|
|
|
$http.get "/project/#{$scope.project_id}/output/output.log" + qs
|
2014-07-08 07:02:26 -04:00
|
|
|
.success (log) ->
|
2016-03-08 08:20:23 -05:00
|
|
|
#console.log ">>", log
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.pdf.rawLog = log
|
|
|
|
logEntries = LogParser.parse(log, ignoreDuplicates: true)
|
2016-03-08 08:20:23 -05:00
|
|
|
#console.log ">>", logEntries
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.pdf.logEntries = logEntries
|
|
|
|
$scope.pdf.logEntries.all = logEntries.errors.concat(logEntries.warnings).concat(logEntries.typesetting)
|
2016-03-08 08:20:23 -05:00
|
|
|
# # # #
|
2016-03-08 09:04:42 -05:00
|
|
|
proceed = () ->
|
2016-03-08 08:20:23 -05:00
|
|
|
$scope.pdf.logEntryAnnotations = {}
|
|
|
|
for entry in logEntries.all
|
|
|
|
if entry.file?
|
|
|
|
entry.file = normalizeFilePath(entry.file)
|
|
|
|
entity = ide.fileTreeManager.findEntityByPath(entry.file)
|
|
|
|
if entity?
|
|
|
|
$scope.pdf.logEntryAnnotations[entity.id] ||= []
|
|
|
|
$scope.pdf.logEntryAnnotations[entity.id].push {
|
|
|
|
row: entry.line - 1
|
|
|
|
type: if entry.level == "error" then "error" else "warning"
|
|
|
|
text: entry.message
|
|
|
|
}
|
2016-03-08 09:04:42 -05:00
|
|
|
# Get the biber log and parse it too
|
2016-03-08 08:20:23 -05:00
|
|
|
$http.get "/project/#{$scope.project_id}/output/output.blg" + qs
|
|
|
|
.success (log) ->
|
|
|
|
window._s = $scope
|
2016-03-08 11:18:02 -05:00
|
|
|
biberLogEntries = BibLogParser.parse(log, {})
|
2016-03-08 08:20:23 -05:00
|
|
|
if $scope.pdf.logEntries
|
|
|
|
entries = $scope.pdf.logEntries
|
|
|
|
all = biberLogEntries.errors.concat(biberLogEntries.warnings)
|
|
|
|
entries.all = entries.all.concat(all)
|
|
|
|
entries.errors = entries.errors.concat(biberLogEntries.errors)
|
|
|
|
entries.warnings = entries.warnings.concat(biberLogEntries.warnings)
|
2016-03-08 09:04:42 -05:00
|
|
|
proceed()
|
2016-03-08 08:20:23 -05:00
|
|
|
.error (e) ->
|
2016-03-22 10:42:00 -04:00
|
|
|
# it's not an error for the output.blg file to not be present
|
2016-03-08 09:04:42 -05:00
|
|
|
proceed()
|
2016-03-08 08:20:23 -05:00
|
|
|
# # # #
|
2014-07-08 07:02:26 -04:00
|
|
|
.error () ->
|
|
|
|
$scope.pdf.logEntries = []
|
|
|
|
$scope.pdf.rawLog = ""
|
|
|
|
|
|
|
|
getRootDocOverride_id = () ->
|
|
|
|
doc = ide.editorManager.getCurrentDocValue()
|
|
|
|
return null if !doc?
|
|
|
|
for line in doc.split("\n")
|
2016-02-03 05:18:34 -05:00
|
|
|
match = line.match /^[^%]*\\documentclass/
|
|
|
|
if match
|
2014-07-08 07:02:26 -04:00
|
|
|
return ide.editorManager.getCurrentDocId()
|
|
|
|
return null
|
|
|
|
|
|
|
|
normalizeFilePath = (path) ->
|
|
|
|
path = path.replace(/^(.*)\/compiles\/[0-9a-f]{24}\/(\.\/)?/, "")
|
|
|
|
path = path.replace(/^\/compile\//, "")
|
|
|
|
|
|
|
|
rootDocDirname = ide.fileTreeManager.getRootDocDirname()
|
|
|
|
if rootDocDirname?
|
|
|
|
path = path.replace(/^\.\//, rootDocDirname + "/")
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
$scope.recompile = (options = {}) ->
|
|
|
|
return if $scope.pdf.compiling
|
|
|
|
$scope.pdf.compiling = true
|
2016-03-08 08:20:23 -05:00
|
|
|
|
2015-04-17 06:22:26 -04:00
|
|
|
ide.$scope.$broadcast("flush-changes")
|
|
|
|
|
2014-07-08 07:02:26 -04:00
|
|
|
options.rootDocOverride_id = getRootDocOverride_id()
|
|
|
|
|
|
|
|
sendCompileRequest(options)
|
|
|
|
.success (data) ->
|
|
|
|
$scope.pdf.view = "pdf"
|
|
|
|
$scope.pdf.compiling = false
|
|
|
|
parseCompileResponse(data)
|
|
|
|
.error () ->
|
|
|
|
$scope.pdf.compiling = false
|
2016-04-25 07:42:03 -04:00
|
|
|
$scope.pdf.renderingError = false
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.pdf.error = true
|
2016-03-22 11:51:05 -04:00
|
|
|
$scope.pdf.view = 'errors'
|
2016-03-08 08:20:23 -05:00
|
|
|
|
2014-07-21 10:39:15 -04:00
|
|
|
# This needs to be public.
|
|
|
|
ide.$scope.recompile = $scope.recompile
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
$scope.clearCache = () ->
|
|
|
|
$http {
|
|
|
|
url: "/project/#{$scope.project_id}/output"
|
|
|
|
method: "DELETE"
|
|
|
|
headers:
|
|
|
|
"X-Csrf-Token": window.csrfToken
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.toggleLogs = () ->
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.shouldShowLogs = !$scope.shouldShowLogs
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
$scope.showPdf = () ->
|
|
|
|
$scope.pdf.view = "pdf"
|
2016-03-22 05:39:33 -04:00
|
|
|
$scope.shouldShowLogs = false
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
$scope.toggleRawLog = () ->
|
|
|
|
$scope.pdf.showRawLog = !$scope.pdf.showRawLog
|
|
|
|
|
|
|
|
$scope.openClearCacheModal = () ->
|
|
|
|
modalInstance = $modal.open(
|
|
|
|
templateUrl: "clearCacheModalTemplate"
|
|
|
|
controller: "ClearCacheModalController"
|
|
|
|
scope: $scope
|
|
|
|
)
|
|
|
|
|
|
|
|
$scope.syncToCode = (position) ->
|
|
|
|
synctex
|
|
|
|
.syncToCode(position)
|
|
|
|
.then (data) ->
|
|
|
|
{doc, line} = data
|
|
|
|
ide.editorManager.openDoc(doc, gotoLine: line)
|
2016-03-08 08:20:23 -05:00
|
|
|
|
2014-07-22 08:33:01 -04:00
|
|
|
$scope.switchToFlatLayout = () ->
|
|
|
|
$scope.ui.pdfLayout = 'flat'
|
|
|
|
$scope.ui.view = 'pdf'
|
2015-02-12 06:32:27 -05:00
|
|
|
ide.localStorage "pdf.layout", "flat"
|
2016-03-08 08:20:23 -05:00
|
|
|
|
2014-07-22 08:33:01 -04:00
|
|
|
$scope.switchToSideBySideLayout = () ->
|
|
|
|
$scope.ui.pdfLayout = 'sideBySide'
|
|
|
|
$scope.ui.view = 'editor'
|
2015-02-12 06:32:27 -05:00
|
|
|
localStorage "pdf.layout", "split"
|
2016-03-08 08:20:23 -05:00
|
|
|
|
2015-02-12 06:32:27 -05:00
|
|
|
if pdfLayout = localStorage("pdf.layout")
|
2014-07-22 08:33:01 -04:00
|
|
|
$scope.switchToSideBySideLayout() if pdfLayout == "split"
|
|
|
|
$scope.switchToFlatLayout() if pdfLayout == "flat"
|
|
|
|
else
|
|
|
|
$scope.switchToSideBySideLayout()
|
2014-07-08 07:02:26 -04:00
|
|
|
|
2015-10-15 06:38:01 -04:00
|
|
|
$scope.startFreeTrial = (source) ->
|
|
|
|
ga?('send', 'event', 'subscription-funnel', 'compile-timeout', source)
|
|
|
|
window.open("/user/subscription/new?planCode=student_free_trial_7_days")
|
|
|
|
$scope.startedFreeTrial = true
|
|
|
|
|
2014-07-08 07:02:26 -04:00
|
|
|
App.factory "synctex", ["ide", "$http", "$q", (ide, $http, $q) ->
|
|
|
|
synctex =
|
|
|
|
syncToPdf: (cursorPosition) ->
|
|
|
|
deferred = $q.defer()
|
|
|
|
|
|
|
|
doc_id = ide.editorManager.getCurrentDocId()
|
|
|
|
if !doc_id?
|
|
|
|
deferred.reject()
|
|
|
|
return deferred.promise
|
|
|
|
doc = ide.fileTreeManager.findEntityById(doc_id)
|
|
|
|
if !doc?
|
|
|
|
deferred.reject()
|
|
|
|
return deferred.promise
|
|
|
|
path = ide.fileTreeManager.getEntityPath(doc)
|
|
|
|
if !path?
|
|
|
|
deferred.reject()
|
|
|
|
return deferred.promise
|
2016-03-08 08:20:23 -05:00
|
|
|
|
2014-07-08 07:02:26 -04:00
|
|
|
# If the root file is folder/main.tex, then synctex sees the
|
|
|
|
# path as folder/./main.tex
|
|
|
|
rootDocDirname = ide.fileTreeManager.getRootDocDirname()
|
|
|
|
if rootDocDirname? and rootDocDirname != ""
|
|
|
|
path = path.replace(RegExp("^#{rootDocDirname}"), "#{rootDocDirname}/.")
|
|
|
|
|
|
|
|
{row, column} = cursorPosition
|
|
|
|
|
|
|
|
$http({
|
2016-03-08 08:20:23 -05:00
|
|
|
url: "/project/#{ide.project_id}/sync/code",
|
2014-07-08 07:02:26 -04:00
|
|
|
method: "GET",
|
|
|
|
params: {
|
|
|
|
file: path
|
|
|
|
line: row + 1
|
|
|
|
column: column
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.success (data) ->
|
|
|
|
deferred.resolve(data.pdf or [])
|
|
|
|
.error (error) ->
|
|
|
|
deferred.reject(error)
|
|
|
|
|
|
|
|
return deferred.promise
|
|
|
|
|
|
|
|
syncToCode: (position, options = {}) ->
|
|
|
|
deferred = $q.defer()
|
|
|
|
if !position?
|
|
|
|
deferred.reject()
|
|
|
|
return deferred.promise
|
|
|
|
|
|
|
|
# It's not clear exactly where we should sync to if it wasn't directly
|
|
|
|
# clicked on, but a little bit down from the very top seems best.
|
|
|
|
if options.includeVisualOffset
|
|
|
|
position.offset.top = position.offset.top + 80
|
|
|
|
|
|
|
|
$http({
|
2016-03-08 08:20:23 -05:00
|
|
|
url: "/project/#{ide.project_id}/sync/pdf",
|
2014-07-08 07:02:26 -04:00
|
|
|
method: "GET",
|
|
|
|
params: {
|
|
|
|
page: position.page + 1
|
|
|
|
h: position.offset.left.toFixed(2)
|
|
|
|
v: position.offset.top.toFixed(2)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.success (data) ->
|
|
|
|
if data.code? and data.code.length > 0
|
|
|
|
doc = ide.fileTreeManager.findEntityByPath(data.code[0].file)
|
|
|
|
return if !doc?
|
|
|
|
deferred.resolve({doc: doc, line: data.code[0].line})
|
|
|
|
.error (error) ->
|
|
|
|
deferred.reject(error)
|
|
|
|
|
|
|
|
return deferred.promise
|
|
|
|
|
|
|
|
return synctex
|
|
|
|
]
|
|
|
|
|
|
|
|
App.controller "PdfSynctexController", ["$scope", "synctex", "ide", ($scope, synctex, ide) ->
|
2014-07-10 09:36:04 -04:00
|
|
|
@cursorPosition = null
|
|
|
|
ide.$scope.$on "cursor:editor:update", (event, @cursorPosition) =>
|
|
|
|
|
|
|
|
$scope.syncToPdf = () =>
|
|
|
|
return if !@cursorPosition?
|
2014-07-08 07:02:26 -04:00
|
|
|
synctex
|
2014-07-10 09:36:04 -04:00
|
|
|
.syncToPdf(@cursorPosition)
|
2014-07-08 07:02:26 -04:00
|
|
|
.then (highlights) ->
|
|
|
|
$scope.pdf.highlights = highlights
|
|
|
|
|
|
|
|
$scope.syncToCode = () ->
|
|
|
|
synctex
|
|
|
|
.syncToCode($scope.pdf.position, includeVisualOffset: true)
|
|
|
|
.then (data) ->
|
|
|
|
{doc, line} = data
|
|
|
|
ide.editorManager.openDoc(doc, gotoLine: line)
|
|
|
|
]
|
|
|
|
|
|
|
|
App.controller "PdfLogEntryController", ["$scope", "ide", ($scope, ide) ->
|
|
|
|
$scope.openInEditor = (entry) ->
|
|
|
|
entity = ide.fileTreeManager.findEntityByPath(entry.file)
|
|
|
|
return if !entity? or entity.type != "doc"
|
|
|
|
if entry.line?
|
|
|
|
line = entry.line
|
|
|
|
ide.editorManager.openDoc(entity, gotoLine: line)
|
|
|
|
]
|
|
|
|
|
|
|
|
App.controller 'ClearCacheModalController', ["$scope", "$modalInstance", ($scope, $modalInstance) ->
|
|
|
|
$scope.state =
|
|
|
|
inflight: false
|
|
|
|
|
|
|
|
$scope.clear = () ->
|
|
|
|
$scope.state.inflight = true
|
|
|
|
$scope
|
|
|
|
.clearCache()
|
|
|
|
.then () ->
|
|
|
|
$scope.state.inflight = false
|
|
|
|
$modalInstance.close()
|
|
|
|
|
|
|
|
$scope.cancel = () ->
|
|
|
|
$modalInstance.dismiss('cancel')
|
2016-03-08 08:20:23 -05:00
|
|
|
]
|