overleaf/services/web/public/coffee/ide/history/HistoryManager.coffee

265 lines
7.1 KiB
CoffeeScript
Raw Normal View History

2014-07-08 07:02:26 -04:00
define [
2016-09-22 06:36:53 -04:00
"moment"
"ide/colors/ColorManager"
"ide/history/controllers/HistoryListController"
"ide/history/controllers/HistoryDiffController"
"ide/history/directives/infiniteScroll"
], (moment, ColorManager) ->
class HistoryManager
2014-07-08 07:02:26 -04:00
constructor: (@ide, @$scope) ->
@reset()
@$scope.toggleHistory = () =>
if @$scope.ui.view == "history"
2014-07-08 07:02:26 -04:00
@hide()
else
@show()
@$scope.$watch "history.selection.updates", (updates) =>
2014-07-08 07:02:26 -04:00
if updates? and updates.length > 0
@_selectDocFromUpdates()
@reloadDiff()
@$scope.$on "entity:selected", (event, entity) =>
if (@$scope.ui.view == "history") and (entity.type == "doc")
@$scope.history.selection.doc = entity
2014-07-08 07:02:26 -04:00
@reloadDiff()
show: () ->
@$scope.ui.view = "history"
2014-07-08 07:02:26 -04:00
@reset()
hide: () ->
@$scope.ui.view = "editor"
# Make sure we run the 'open' logic for whatever is currently selected
@$scope.$emit "entity:selected", @ide.fileTreeManager.findSelectedEntity()
reset: () ->
@$scope.history = {
2014-07-08 07:02:26 -04:00
updates: []
nextBeforeTimestamp: null
atEnd: false
selection: {
updates: []
doc: null
range: {
fromV: null
toV: null
start_ts: null
end_ts: null
2014-07-08 07:02:26 -04:00
}
}
diff: null
}
autoSelectRecentUpdates: () ->
return if @$scope.history.updates.length == 0
2014-07-08 07:02:26 -04:00
@$scope.history.updates[0].selectedTo = true
2014-07-08 07:02:26 -04:00
indexOfLastUpdateNotByMe = 0
for update, i in @$scope.history.updates
2014-07-08 07:02:26 -04:00
if @_updateContainsUserId(update, @$scope.user.id)
break
indexOfLastUpdateNotByMe = i
@$scope.history.updates[indexOfLastUpdateNotByMe].selectedFrom = true
2014-07-08 07:02:26 -04:00
2014-07-15 13:25:12 -04:00
BATCH_SIZE: 10
2014-07-08 07:02:26 -04:00
fetchNextBatchOfUpdates: () ->
url = "/project/#{@ide.project_id}/updates?min_count=#{@BATCH_SIZE}"
if @$scope.history.nextBeforeTimestamp?
url += "&before=#{@$scope.history.nextBeforeTimestamp}"
@$scope.history.loading = true
2014-07-08 07:02:26 -04:00
@ide.$http
.get(url)
.then (response) =>
{ data } = response
2014-07-08 07:02:26 -04:00
@_loadUpdates(data.updates)
@$scope.history.nextBeforeTimestamp = data.nextBeforeTimestamp
2014-07-08 07:02:26 -04:00
if !data.nextBeforeTimestamp?
@$scope.history.atEnd = true
@$scope.history.loading = false
2014-07-08 07:02:26 -04:00
reloadDiff: () ->
diff = @$scope.history.diff
{updates, doc} = @$scope.history.selection
{fromV, toV, start_ts, end_ts} = @_calculateRangeFromSelection()
2014-07-08 07:02:26 -04:00
return if !doc?
2014-07-08 07:02:26 -04:00
return if diff? and
diff.doc == doc and
diff.fromV == fromV and
diff.toV == toV
2014-07-08 07:02:26 -04:00
@$scope.history.diff = diff = {
2014-07-11 08:55:14 -04:00
fromV: fromV
toV: toV
start_ts: start_ts
end_ts: end_ts
doc: doc
2014-07-11 08:55:14 -04:00
error: false
2017-12-13 11:48:48 -05:00
pathname: doc.name
2014-07-08 07:02:26 -04:00
}
if !doc.deleted
2014-07-08 07:02:26 -04:00
diff.loading = true
url = "/project/#{@$scope.project_id}/doc/#{diff.doc.id}/diff"
2014-07-08 07:02:26 -04:00
if diff.fromV? and diff.toV?
url += "?from=#{diff.fromV}&to=#{diff.toV}"
2014-07-08 07:02:26 -04:00
@ide.$http
.get(url)
.then (response) =>
{ data } = response
2014-07-08 07:02:26 -04:00
diff.loading = false
{text, highlights} = @_parseDiff(data)
diff.text = text
diff.highlights = highlights
.catch () ->
2014-07-08 07:02:26 -04:00
diff.loading = false
diff.error = true
else
diff.deleted = true
diff.restoreInProgress = false
diff.restoreDeletedSuccess = false
diff.restoredDocNewId = null
2014-07-08 07:02:26 -04:00
restoreDeletedDoc: (doc) ->
2014-07-11 08:55:14 -04:00
url = "/project/#{@$scope.project_id}/doc/#{doc.id}/restore"
@ide.$http.post(url, name: doc.name, _csrf: window.csrfToken)
restoreDiff: (diff) ->
url = "/project/#{@$scope.project_id}/doc/#{diff.doc.id}/version/#{diff.fromV}/restore"
@ide.$http.post(url, _csrf: window.csrfToken)
2014-07-08 07:02:26 -04:00
_parseDiff: (diff) ->
row = 0
column = 0
highlights = []
text = ""
for entry, i in diff.diff or []
content = entry.u or entry.i or entry.d
content ||= ""
text += content
lines = content.split("\n")
startRow = row
startColumn = column
if lines.length > 1
endRow = startRow + lines.length - 1
endColumn = lines[lines.length - 1].length
else
endRow = startRow
endColumn = startColumn + lines[0].length
row = endRow
column = endColumn
range = {
start:
row: startRow
column: startColumn
end:
row: endRow
column: endColumn
}
if entry.i? or entry.d?
if entry.meta.user?
name = "#{entry.meta.user.first_name} #{entry.meta.user.last_name}"
else
name = "Anonymous"
if entry.meta.user?.id == @$scope.user.id
name = "you"
date = moment(entry.meta.end_ts).format("Do MMM YYYY, h:mm a")
if entry.i?
highlights.push {
label: "Added by #{name} on #{date}"
highlight: range
hue: ColorManager.getHueForUserId(entry.meta.user?.id)
2014-07-08 07:02:26 -04:00
}
else if entry.d?
highlights.push {
label: "Deleted by #{name} on #{date}"
strikeThrough: range
hue: ColorManager.getHueForUserId(entry.meta.user?.id)
2014-07-08 07:02:26 -04:00
}
return {text, highlights}
_loadUpdates: (updates = []) ->
previousUpdate = @$scope.history.updates[@$scope.history.updates.length - 1]
2014-07-08 07:02:26 -04:00
for update in updates
update.pathnames = [] # Used for display
for doc_id, doc of update.docs or {}
doc.entity = @ide.fileTreeManager.findEntityById(doc_id, includeDeleted: true)
update.pathnames.push doc.entity.name
2014-07-08 07:02:26 -04:00
for user in update.meta.users or []
if user?
user.hue = ColorManager.getHueForUserId(user.id)
2014-07-08 07:02:26 -04:00
if !previousUpdate? or !moment(previousUpdate.meta.end_ts).isSame(update.meta.end_ts, "day")
update.meta.first_in_day = true
update.selectedFrom = false
update.selectedTo = false
update.inSelection = false
previousUpdate = update
firstLoad = @$scope.history.updates.length == 0
2014-07-08 07:02:26 -04:00
@$scope.history.updates =
@$scope.history.updates.concat(updates)
2014-07-08 07:02:26 -04:00
@autoSelectRecentUpdates() if firstLoad
_calculateRangeFromSelection: () ->
fromV = toV = start_ts = end_ts = null
selected_doc_id = @$scope.history.selection.doc?.id
2017-12-07 05:13:46 -05:00
for update in @$scope.history.selection.updates or []
for doc_id, doc of update.docs
if doc_id == selected_doc_id
if fromV? and toV?
fromV = Math.min(fromV, doc.fromV)
toV = Math.max(toV, doc.toV)
start_ts = Math.min(start_ts, update.meta.start_ts)
end_ts = Math.max(end_ts, update.meta.end_ts)
else
fromV = doc.fromV
toV = doc.toV
start_ts = update.meta.start_ts
end_ts = update.meta.end_ts
break
2014-07-08 07:02:26 -04:00
return {fromV, toV, start_ts, end_ts}
2014-07-08 07:02:26 -04:00
# Set the track changes selected doc to one of the docs in the range
# of currently selected updates. If we already have a selected doc
# then prefer this one if present.
_selectDocFromUpdates: () ->
affected_docs = {}
for update in @$scope.history.selection.updates
for doc_id, doc of update.docs
affected_docs[doc_id] = doc.entity
2014-07-08 07:02:26 -04:00
selected_doc = @$scope.history.selection.doc
if selected_doc? and affected_docs[selected_doc.id]?
# Selected doc is already open
2014-07-08 07:02:26 -04:00
else
for doc_id, doc of affected_docs
selected_doc = doc
2014-07-08 07:02:26 -04:00
break
@$scope.history.selection.doc = selected_doc
@ide.fileTreeManager.selectEntity(selected_doc)
2014-07-08 07:02:26 -04:00
_updateContainsUserId: (update, user_id) ->
for user in update.meta.users
return true if user?.id == user_id
2014-07-08 07:02:26 -04:00
return false