overleaf/services/web/public/coffee/ide/share/controllers/ShareProjectModalController.coffee

202 lines
6.2 KiB
CoffeeScript
Raw Normal View History

2014-07-08 07:02:26 -04:00
define [
"base"
], (App) ->
App.controller "ShareProjectModalController", ($scope, $modalInstance, $timeout, projectMembers, projectInvites, $modal, $http) ->
2014-07-08 07:02:26 -04:00
$scope.inputs = {
privileges: "readAndWrite"
contacts: []
2014-07-08 07:02:26 -04:00
}
$scope.state = {
error: null
inflight: false
startedFreeTrial: false
invites: []
2014-07-08 07:02:26 -04:00
}
$modalInstance.opened.then () ->
$timeout () ->
$scope.$broadcast "open"
, 200
INFINITE_COLLABORATORS = -1
$scope.$watch "project.members.length", (noOfMembers) ->
allowedNoOfMembers = $scope.project.features.collaborators
$scope.canAddCollaborators = noOfMembers < allowedNoOfMembers or allowedNoOfMembers == INFINITE_COLLABORATORS
window._m = projectMembers
$scope.autocompleteContacts = []
do loadAutocompleteUsers = () ->
$http.get "/user/contacts"
.success (data) ->
$scope.autocompleteContacts = data.contacts or []
for contact in $scope.autocompleteContacts
if contact.type == "user"
if contact.last_name == "" and contact.first_name = contact.email.split("@")[0]
# User has not set their proper name so use email as canonical display property
contact.display = contact.email
else
contact.name = "#{contact.first_name} #{contact.last_name}"
contact.display = "#{contact.name} <#{contact.email}>"
else
# Must be a group
contact.display = contact.name
2016-07-25 05:33:43 -04:00
getCurrentMemberEmails = () ->
$scope.project.members.map (u) -> u.email
getCurrentInviteEmails = () ->
$scope.project.invites.map (u) -> u.email
$scope.filterAutocompleteUsers = ($query) ->
currentMemberEmails = getCurrentMemberEmails()
return $scope.autocompleteContacts.filter (contact) ->
if contact.email? and contact.email in currentMemberEmails
return false
for text in [contact.name, contact.email]
if text?.toLowerCase().indexOf($query.toLowerCase()) > -1
return true
return false
$scope.addMembers = () ->
addMembers = () ->
return if $scope.inputs.contacts.length == 0
members = $scope.inputs.contacts
$scope.inputs.contacts = []
$scope.state.error = null
$scope.state.inflight = true
2016-07-25 05:33:43 -04:00
currentMemberEmails = getCurrentMemberEmails()
currentInviteEmails = getCurrentInviteEmails()
do addNextMember = () ->
if members.length == 0 or !$scope.canAddCollaborators
$scope.state.inflight = false
$scope.$apply()
return
2016-07-25 05:33:43 -04:00
member = members.shift()
if !member.type? and member.display in currentMemberEmails
# Skip this existing member
return addNextMember()
2016-07-25 05:33:43 -04:00
# NOTE: groups aren't really a thing in ShareLaTeX, partially inherited from DJ
if member.display in currentInviteEmails and inviteId = _.find($scope.project.invites, (invite) -> invite.email == member.display)?._id
request = projectInvites.resendInvite(inviteId)
else if member.type == "user"
request = projectInvites.sendInvite(member.email, $scope.inputs.privileges)
else if member.type == "group"
request = projectMembers.addGroup(member.id, $scope.inputs.privileges)
else # Not an auto-complete object, so email == display
request = projectInvites.sendInvite(member.display, $scope.inputs.privileges)
2016-07-25 05:33:43 -04:00
request
.success (data) ->
if data.invite
invite = data.invite
$scope.project.invites.push invite
else
if data.users?
users = data.users
else if data.user?
users = [data.user]
else
users = []
$scope.project.members.push users...
setTimeout () ->
# Give $scope a chance to update $scope.canAddCollaborators
# with new collaborator information.
addNextMember()
, 0
.error () ->
$scope.state.inflight = false
$scope.state.error = true
2016-07-25 05:33:43 -04:00
$timeout addMembers, 50 # Give email list a chance to update
2014-07-08 07:02:26 -04:00
$scope.removeMember = (member) ->
$scope.state.error = null
$scope.state.inflight = true
projectMembers
.removeMember(member)
.success () ->
2014-07-08 07:02:26 -04:00
$scope.state.inflight = false
index = $scope.project.members.indexOf(member)
return if index == -1
$scope.project.members.splice(index, 1)
.error () ->
2014-07-08 07:02:26 -04:00
$scope.state.inflight = false
$scope.state.error = "Sorry, something went wrong :("
$scope.revokeInvite = (invite) ->
$scope.state.error = null
$scope.state.inflight = true
projectInvites
.revokeInvite(invite._id)
.success () ->
$scope.state.inflight = false
index = $scope.project.invites.indexOf(invite)
return if index == -1
$scope.project.invites.splice(index, 1)
.error () ->
$scope.state.inflight = false
$scope.state.error = "Sorry, something went wrong :("
$scope.resendInvite = (invite, event) ->
2016-08-03 10:42:19 -04:00
$scope.state.error = null
$scope.state.inflight = true
projectInvites
.resendInvite(invite._id)
.success () ->
$scope.state.inflight = false
event.target.blur()
2016-08-03 10:42:19 -04:00
.error () ->
$scope.state.inflight = false
$scope.state.error = "Sorry, something went wrong resending the invite :("
event.target.blur()
2016-08-03 06:55:24 -04:00
2014-07-08 07:02:26 -04:00
$scope.openMakePublicModal = () ->
$modal.open {
templateUrl: "makePublicModalTemplate"
controller: "MakePublicModalController"
scope: $scope
}
$scope.openMakePrivateModal = () ->
$modal.open {
templateUrl: "makePrivateModalTemplate"
controller: "MakePrivateModalController"
scope: $scope
}
$scope.done = () ->
$modalInstance.close()
$scope.cancel = () ->
$modalInstance.dismiss()
App.controller "MakePublicModalController", ["$scope", "$modalInstance", "settings", ($scope, $modalInstance, settings) ->
$scope.inputs = {
privileges: "readAndWrite"
}
$scope.makePublic = () ->
$scope.project.publicAccesLevel = $scope.inputs.privileges
settings.saveProjectAdminSettings({publicAccessLevel: $scope.inputs.privileges})
2014-07-08 07:02:26 -04:00
$modalInstance.close()
$scope.cancel = () ->
$modalInstance.dismiss()
]
App.controller "MakePrivateModalController", ["$scope", "$modalInstance", "settings", ($scope, $modalInstance, settings) ->
$scope.makePrivate = () ->
$scope.project.publicAccesLevel = "private"
settings.saveProjectAdminSettings({publicAccessLevel: "private"})
2014-07-08 07:02:26 -04:00
$modalInstance.close()
$scope.cancel = () ->
$modalInstance.dismiss()
2016-07-25 05:33:43 -04:00
]