2014-07-08 07:02:26 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
2016-07-25 09:27:02 -04:00
|
|
|
App.controller "ShareProjectModalController", ($scope, $modalInstance, $timeout, projectMembers, projectInvites, $modal, $http) ->
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.inputs = {
|
|
|
|
privileges: "readAndWrite"
|
2015-10-15 08:53:46 -04:00
|
|
|
contacts: []
|
2014-07-08 07:02:26 -04:00
|
|
|
}
|
|
|
|
$scope.state = {
|
|
|
|
error: null
|
|
|
|
inflight: false
|
|
|
|
startedFreeTrial: false
|
2016-07-25 09:27:02 -04:00
|
|
|
invites: []
|
2014-07-08 07:02:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$modalInstance.opened.then () ->
|
|
|
|
$timeout () ->
|
|
|
|
$scope.$broadcast "open"
|
|
|
|
, 200
|
|
|
|
|
|
|
|
INFINITE_COLLABORATORS = -1
|
2016-09-06 04:37:53 -04:00
|
|
|
$scope.$watch "(project.members.length + project.invites.length)", (noOfMembers) ->
|
2014-07-08 07:02:26 -04:00
|
|
|
allowedNoOfMembers = $scope.project.features.collaborators
|
|
|
|
$scope.canAddCollaborators = noOfMembers < allowedNoOfMembers or allowedNoOfMembers == INFINITE_COLLABORATORS
|
|
|
|
|
2016-08-05 09:01:08 -04:00
|
|
|
window._m = projectMembers
|
|
|
|
|
2015-10-07 12:32:35 -04:00
|
|
|
$scope.autocompleteContacts = []
|
|
|
|
do loadAutocompleteUsers = () ->
|
|
|
|
$http.get "/user/contacts"
|
|
|
|
.success (data) ->
|
|
|
|
$scope.autocompleteContacts = data.contacts or []
|
2015-10-08 13:17:53 -04:00
|
|
|
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
|
|
|
|
2015-11-02 10:21:41 -05:00
|
|
|
getCurrentMemberEmails = () ->
|
2016-08-15 09:56:02 -04:00
|
|
|
($scope.project.members || []).map (u) -> u.email
|
2015-10-08 13:17:53 -04:00
|
|
|
|
2016-08-03 11:30:34 -04:00
|
|
|
getCurrentInviteEmails = () ->
|
2016-08-15 09:56:02 -04:00
|
|
|
($scope.project.invites || []).map (u) -> u.email
|
2016-08-03 11:30:34 -04:00
|
|
|
|
2015-10-07 12:32:35 -04:00
|
|
|
$scope.filterAutocompleteUsers = ($query) ->
|
2015-11-02 10:21:41 -05:00
|
|
|
currentMemberEmails = getCurrentMemberEmails()
|
2015-10-08 13:17:53 -04:00
|
|
|
return $scope.autocompleteContacts.filter (contact) ->
|
2015-11-02 10:21:41 -05:00
|
|
|
if contact.email? and contact.email in currentMemberEmails
|
|
|
|
return false
|
2015-10-08 13:17:53 -04:00
|
|
|
for text in [contact.name, contact.email]
|
2015-10-07 12:32:35 -04:00
|
|
|
if text?.toLowerCase().indexOf($query.toLowerCase()) > -1
|
|
|
|
return true
|
|
|
|
return false
|
2015-09-02 07:20:37 -04:00
|
|
|
|
2015-10-07 12:32:35 -04:00
|
|
|
$scope.addMembers = () ->
|
2015-11-04 09:33:27 -05:00
|
|
|
addMembers = () ->
|
2015-10-07 12:32:35 -04:00
|
|
|
return if $scope.inputs.contacts.length == 0
|
2015-09-02 07:20:37 -04:00
|
|
|
|
2015-10-15 08:53:46 -04:00
|
|
|
members = $scope.inputs.contacts
|
2015-10-07 12:32:35 -04:00
|
|
|
$scope.inputs.contacts = []
|
|
|
|
$scope.state.error = null
|
|
|
|
$scope.state.inflight = true
|
2016-07-25 05:33:43 -04:00
|
|
|
|
2016-08-15 10:40:16 -04:00
|
|
|
if !$scope.project.invites?
|
|
|
|
$scope.project.invites = []
|
|
|
|
|
2015-11-02 10:21:41 -05:00
|
|
|
currentMemberEmails = getCurrentMemberEmails()
|
2016-08-03 11:30:34 -04:00
|
|
|
currentInviteEmails = getCurrentInviteEmails()
|
2015-10-07 12:32:35 -04:00
|
|
|
do addNextMember = () ->
|
2015-10-15 08:53:46 -04:00
|
|
|
if members.length == 0 or !$scope.canAddCollaborators
|
2015-09-02 07:20:37 -04:00
|
|
|
$scope.state.inflight = false
|
2015-10-07 12:32:35 -04:00
|
|
|
$scope.$apply()
|
|
|
|
return
|
2016-07-25 05:33:43 -04:00
|
|
|
|
2015-10-15 08:53:46 -04:00
|
|
|
member = members.shift()
|
2015-11-02 10:21:41 -05:00
|
|
|
if !member.type? and member.display in currentMemberEmails
|
|
|
|
# Skip this existing member
|
|
|
|
return addNextMember()
|
2016-07-25 05:33:43 -04:00
|
|
|
|
2016-07-28 08:47:19 -04:00
|
|
|
# NOTE: groups aren't really a thing in ShareLaTeX, partially inherited from DJ
|
2016-08-15 10:19:16 -04:00
|
|
|
if member.display in currentInviteEmails and inviteId = _.find(($scope.project.invites || []), (invite) -> invite.email == member.display)?._id
|
2016-08-03 11:30:34 -04:00
|
|
|
request = projectInvites.resendInvite(inviteId)
|
|
|
|
else if member.type == "user"
|
2016-07-25 10:07:14 -04:00
|
|
|
request = projectInvites.sendInvite(member.email, $scope.inputs.privileges)
|
2015-10-15 08:53:46 -04:00
|
|
|
else if member.type == "group"
|
|
|
|
request = projectMembers.addGroup(member.id, $scope.inputs.privileges)
|
2015-11-02 10:21:41 -05:00
|
|
|
else # Not an auto-complete object, so email == display
|
2016-07-25 09:27:02 -04:00
|
|
|
request = projectInvites.sendInvite(member.display, $scope.inputs.privileges)
|
2016-07-25 05:33:43 -04:00
|
|
|
|
2015-10-15 08:53:46 -04:00
|
|
|
request
|
2015-10-07 12:32:35 -04:00
|
|
|
.success (data) ->
|
2016-07-25 10:07:14 -04:00
|
|
|
if data.invite
|
|
|
|
invite = data.invite
|
2016-08-01 12:05:37 -04:00
|
|
|
$scope.project.invites.push invite
|
2016-07-28 08:47:19 -04:00
|
|
|
else
|
|
|
|
if data.users?
|
|
|
|
users = data.users
|
|
|
|
else if data.user?
|
|
|
|
users = [data.user]
|
|
|
|
else
|
|
|
|
users = []
|
|
|
|
$scope.project.members.push users...
|
|
|
|
|
2015-10-15 08:53:46 -04:00
|
|
|
setTimeout () ->
|
|
|
|
# Give $scope a chance to update $scope.canAddCollaborators
|
|
|
|
# with new collaborator information.
|
|
|
|
addNextMember()
|
|
|
|
, 0
|
2015-10-07 12:32:35 -04:00
|
|
|
.error () ->
|
|
|
|
$scope.state.inflight = false
|
2015-11-04 09:33:27 -05:00
|
|
|
$scope.state.error = true
|
2016-07-25 05:33:43 -04:00
|
|
|
|
2015-11-04 09:33:27 -05: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)
|
2014-11-06 06:53:59 -05:00
|
|
|
.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)
|
2014-11-06 06:53:59 -05:00
|
|
|
.error () ->
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.state.inflight = false
|
|
|
|
$scope.state.error = "Sorry, something went wrong :("
|
|
|
|
|
2016-07-25 09:27:02 -04:00
|
|
|
$scope.revokeInvite = (invite) ->
|
|
|
|
$scope.state.error = null
|
|
|
|
$scope.state.inflight = true
|
|
|
|
projectInvites
|
|
|
|
.revokeInvite(invite._id)
|
|
|
|
.success () ->
|
|
|
|
$scope.state.inflight = false
|
2016-08-01 12:05:37 -04:00
|
|
|
index = $scope.project.invites.indexOf(invite)
|
2016-07-25 09:27:02 -04:00
|
|
|
return if index == -1
|
2016-08-01 12:05:37 -04:00
|
|
|
$scope.project.invites.splice(index, 1)
|
2016-07-25 09:27:02 -04:00
|
|
|
.error () ->
|
|
|
|
$scope.state.inflight = false
|
|
|
|
$scope.state.error = "Sorry, something went wrong :("
|
|
|
|
|
2016-08-12 06:25:03 -04:00
|
|
|
$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
|
2016-08-12 06:25:03 -04:00
|
|
|
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 :("
|
2016-08-12 06:25:03 -04:00
|
|
|
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
|
2016-03-10 06:13:57 -05:00
|
|
|
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"
|
2016-03-10 06:13:57 -05:00
|
|
|
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
|
|
|
]
|