overleaf/services/web/public/coffee/app/ide/online-users/OnlineUsersManager.coffee

72 lines
2 KiB
CoffeeScript
Raw Normal View History

2014-06-26 06:19:05 -04:00
define [
"../../../libs/md5"
], () ->
2014-06-25 13:17:17 -04:00
class OnlineUsersManager
constructor: (@ide, @$scope) ->
@$scope.onlineUsers = {}
@$scope.onlineUserCursorHighlights = {}
2014-06-25 13:17:17 -04:00
@$scope.$watch "editor.cursorPosition", (position) =>
if position?
@sendCursorPositionUpdate()
@ide.socket.on "clientTracking.clientUpdated", (client) =>
if client.id != @ide.socket.socket.sessionid # Check it's not me!
@$scope.$apply () =>
@$scope.onlineUsers[client.id] = client
@updateCursorHighlights()
@ide.socket.on "clientTracking.clientDisconnected", (client_id) =>
@$scope.$apply () =>
delete @$scope.onlineUsers[client_id]
@updateCursorHighlights()
updateCursorHighlights: () ->
console.log "UPDATING CURSOR HIGHLIGHTS"
@$scope.onlineUserCursorHighlights = {}
2014-06-25 13:17:17 -04:00
for client_id, client of @$scope.onlineUsers
doc_id = client.doc_id
2014-06-26 06:19:05 -04:00
continue if !doc_id?
@$scope.onlineUserCursorHighlights[doc_id] ||= []
@$scope.onlineUserCursorHighlights[doc_id].push {
2014-06-27 07:45:37 -04:00
label: client.name
2014-06-25 13:17:17 -04:00
cursor:
row: client.row
column: client.column
2014-06-26 06:19:05 -04:00
hue: @getHueForUserId(client.user_id)
2014-06-25 13:17:17 -04:00
}
UPDATE_INTERVAL: 500
sendCursorPositionUpdate: () ->
2014-06-30 09:01:39 -04:00
console.log "SENDING CURSOR POSITION UPDATE", @$scope.editor.cursorPosition
2014-06-25 13:17:17 -04:00
if !@cursorUpdateTimeout?
@cursorUpdateTimeout = setTimeout ()=>
position = @$scope.editor.cursorPosition
doc_id = @$scope.editor.open_doc_id
@ide.socket.emit "clientTracking.updatePosition", {
row: position.row
column: position.column
doc_id: doc_id
}
delete @cursorUpdateTimeout
, @UPDATE_INTERVAL
2014-06-26 06:19:05 -04:00
OWN_HUE: 200 # We will always appear as this color to ourselves
ANONYMOUS_HUE: 100
getHueForUserId: (user_id) ->
if !user_id?
return @ANONYMOUS_HUE
if window.user.id == user_id
return @OWN_HUE
hash = CryptoJS.MD5(user_id)
hue = parseInt(hash.toString().slice(0,8), 16) % 320
# Avoid 20 degrees either side of the personal hue
if hue > @OWNER_HUE - 20
hue = hue + 40
return hue
2014-06-25 13:17:17 -04:00