overleaf/services/web/public/coffee/ide/review-panel/controllers/ReviewPanelController.coffee

160 lines
4.9 KiB
CoffeeScript
Raw Normal View History

define [
"base",
"utils/EventEmitter"
2016-11-09 11:23:08 -05:00
"ide/colors/ColorManager"
], (App, EventEmitter, ColorManager) ->
2016-10-13 09:22:23 -04:00
App.controller "ReviewPanelController", ($scope, $element, ide) ->
$scope.reviewPanel =
entries: {}
trackNewChanges: false
2016-11-09 09:50:58 -05:00
$scope.commentState =
adding: false
content: ""
# TODO Just for prototyping purposes; remove afterwards.
mockedUserId = '12345abc'
scroller = $element.find(".review-panel-scroller")
2016-11-10 05:19:52 -05:00
list = $element.find(".rp-entry-list")
2016-10-13 05:09:59 -04:00
# Use these to avoid unnecessary updates. Scrolling one
# panel causes us to scroll the other panel, but there's no
# need to trigger the event back to the original panel.
ignoreNextPanelEvent = false
ignoreNextAceEvent = false
2016-10-13 05:09:59 -04:00
$scope.scrollEvents = new EventEmitter()
scrollPanel = (scrollTop, height) ->
if ignoreNextAceEvent
ignoreNextAceEvent = false
else
ignoreNextPanelEvent = true
list.height(height)
scroller.scrollTop(scrollTop)
scrollAce = (e) ->
if ignoreNextPanelEvent
ignoreNextPanelEvent = false
else
ignoreNextAceEvent = true
$scope.scrollEvents.emit "scroll", e.target.scrollTop
2016-10-13 09:22:23 -04:00
$scope.$watch "ui.reviewPanelOpen", (reviewPanelOpen) ->
return if !reviewPanelOpen?
if reviewPanelOpen
2016-11-09 10:46:47 -05:00
$scope.$broadcast "review-panel:layout"
2016-10-13 09:22:23 -04:00
scroller.on "scroll", scrollAce
$scope.onScroll = scrollPanel # Passed into the editor directive for it to call
else
scroller.off "scroll"
$scope.onScroll = null
2016-10-13 05:09:59 -04:00
# If we listen for scroll events in the review panel natively, then with a Mac trackpad
# the scroll is very smooth (natively done I'd guess), but we don't get polled regularly
# enough to keep Ace in step, and it noticeably lags. If instead, we borrow the manual
# mousewheel/trackpad scrolling behaviour from Ace, and turn mousewheel events into
# scroll events ourselves, then it makes the review panel slightly less smooth (barely)
# noticeable, but keeps it perfectly in step with Ace.
ace.require("ace/lib/event").addMouseWheelListener scroller[0], (e) ->
deltaY = e.wheelY
# console.log "mousewheel", deltaY
scroller.scrollTop(scroller.scrollTop() + deltaY * 4)
e.preventDefault()
2016-11-09 09:50:58 -05:00
2016-11-10 05:52:09 -05:00
$scope.acceptChange = (entry_id) ->
$scope.$broadcast "change:accept", entry_id
$scope.rejectChange = (entry_id) ->
$scope.$broadcast "change:reject", entry_id
2016-11-09 10:25:41 -05:00
$scope.startNewComment = () ->
2016-11-09 09:50:58 -05:00
$scope.commentState.adding = true
$scope.$broadcast "comment:select_line"
2016-11-09 10:46:47 -05:00
$scope.$broadcast "review-panel:layout"
2016-11-09 09:50:58 -05:00
2016-11-09 10:25:41 -05:00
$scope.submitNewComment = () ->
2016-11-09 09:50:58 -05:00
$scope.commentState.adding = false
2016-11-09 10:25:41 -05:00
$scope.$broadcast "comment:add", $scope.commentState.content
$scope.commentState.content = ""
2016-11-09 10:46:47 -05:00
$scope.$broadcast "review-panel:layout"
2016-11-09 10:25:41 -05:00
2016-11-09 10:51:40 -05:00
$scope.cancelNewComment = (entry) ->
$scope.commentState.adding = false
$scope.commentState.content = ""
$scope.$broadcast "review-panel:layout"
2016-11-09 10:25:41 -05:00
$scope.startReply = (entry) ->
entry.replying = true
2016-11-09 10:46:47 -05:00
$scope.$broadcast "review-panel:layout"
$scope.handleCommentReplyKeyPress = (ev, entry) ->
if ev.keyCode == 13 and !ev.shiftKey and !ev.ctrlKey and !ev.metaKey
ev.preventDefault()
ev.target.blur()
$scope.submitReply(entry)
2016-11-09 10:25:41 -05:00
$scope.submitReply = (entry) ->
entry.thread.push {
content: entry.replyContent
ts: new Date()
user_id: window.user_id
}
entry.replyContent = ""
2016-11-09 10:46:47 -05:00
entry.replying = false
2016-11-09 10:51:40 -05:00
$scope.$broadcast "review-panel:layout"
# TODO Just for prototyping purposes; remove afterwards
window.setTimeout((() ->
$scope.$applyAsync(() -> submitMockedReply(entry))
), 1000 * 2)
# TODO Just for prototyping purposes; remove afterwards.
submitMockedReply = (entry) ->
entry.thread.push {
content: 'Lorem ipsum dolor sit amet'
ts: new Date()
user_id: mockedUserId
}
entry.replyContent = ""
entry.replying = false
$scope.$broadcast "review-panel:layout"
2016-11-09 10:51:40 -05:00
$scope.cancelReply = (entry) ->
entry.replying = false
entry.replyContent = ""
2016-11-09 11:23:08 -05:00
$scope.$broadcast "review-panel:layout"
2016-11-09 11:34:23 -05:00
# TODO: Eventually we need to get this from the server, and update it
# when we get an id we don't know. This'll do for client side testing
2016-11-09 11:23:08 -05:00
refreshUsers = () ->
$scope.users = {}
# TODO Just for prototyping purposes; remove afterwards.
$scope.users[mockedUserId] = {
email: "gerald.butler@gmail.com"
name: "Gerald Butler"
isSelf: false
hue: 70
avatar_text: "G"
}
2016-11-09 11:23:08 -05:00
for member in $scope.project.members.concat($scope.project.owner)
2016-11-09 11:34:23 -05:00
if member._id == window.user_id
name = "You"
isSelf = true
2016-11-09 11:34:23 -05:00
else
name = "#{member.first_name} #{member.last_name}"
isSelf = false
2016-11-09 11:23:08 -05:00
$scope.users[member._id] = {
email: member.email
2016-11-09 11:34:23 -05:00
name: name
isSelf: isSelf
2016-11-09 11:23:08 -05:00
hue: ColorManager.getHueForUserId(member._id)
avatar_text: [member.first_name, member.last_name].filter((n) -> n?).map((n) -> n[0]).join ""
}
$scope.$watch "project.members", (members) ->
return if !members?
refreshUsers()