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
|
2016-12-20 04:54:42 -05:00
|
|
|
errorReason: null
|
2014-07-08 07:02:26 -04:00
|
|
|
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-11-07 09:36:04 -05:00
|
|
|
|
|
|
|
$scope.refreshCanAddCollaborators = () ->
|
2014-07-08 07:02:26 -04:00
|
|
|
allowedNoOfMembers = $scope.project.features.collaborators
|
2016-11-07 09:36:04 -05:00
|
|
|
$scope.canAddCollaborators = (
|
|
|
|
($scope.project.members.length + $scope.project.invites.length) < allowedNoOfMembers or allowedNoOfMembers == INFINITE_COLLABORATORS
|
|
|
|
)
|
|
|
|
$scope.refreshCanAddCollaborators()
|
2014-07-08 07:02:26 -04:00
|
|
|
|
2016-11-07 09:36:04 -05:00
|
|
|
$scope.$watch "(project.members.length + project.invites.length)", (_noOfMembers) ->
|
|
|
|
$scope.refreshCanAddCollaborators()
|
2016-08-05 09:01:08 -04:00
|
|
|
|
2015-10-07 12:32:35 -04:00
|
|
|
$scope.autocompleteContacts = []
|
|
|
|
do loadAutocompleteUsers = () ->
|
|
|
|
$http.get "/user/contacts"
|
2017-06-20 11:04:06 -04:00
|
|
|
.then (response) ->
|
|
|
|
{ data } = response
|
2015-10-07 12:32:35 -04:00
|
|
|
$scope.autocompleteContacts = data.contacts or []
|
2015-10-08 13:17:53 -04:00
|
|
|
for contact in $scope.autocompleteContacts
|
|
|
|
if contact.type == "user"
|
2017-09-07 06:30:20 -04:00
|
|
|
if contact.last_name == "" and contact.first_name == contact.email.split("@")[0]
|
2015-10-08 13:17:53 -04:00
|
|
|
# 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 = []
|
2016-12-20 04:54:42 -05:00
|
|
|
$scope.state.error = false
|
|
|
|
$scope.state.errorReason = null
|
2015-10-07 12:32:35 -04:00
|
|
|
$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()
|
2016-09-21 05:48:04 -04:00
|
|
|
if member.type == "user"
|
|
|
|
email = member.email
|
|
|
|
else # Not an auto-complete object, so email == display
|
|
|
|
email = member.display
|
|
|
|
email = email.toLowerCase()
|
|
|
|
|
|
|
|
if email in currentMemberEmails
|
2015-11-02 10:21:41 -05:00
|
|
|
# Skip this existing member
|
|
|
|
return addNextMember()
|
2016-07-25 05:33:43 -04:00
|
|
|
|
2016-09-21 05:48:04 -04:00
|
|
|
if email in currentInviteEmails and inviteId = _.find(($scope.project.invites || []), (invite) -> invite.email == email)?._id
|
2016-08-03 11:30:34 -04:00
|
|
|
request = projectInvites.resendInvite(inviteId)
|
2016-09-21 05:48:04 -04:00
|
|
|
else
|
|
|
|
request = projectInvites.sendInvite(email, $scope.inputs.privileges)
|
2016-07-25 05:33:43 -04:00
|
|
|
|
2015-10-15 08:53:46 -04:00
|
|
|
request
|
2017-06-20 11:04:06 -04:00
|
|
|
.then (response) ->
|
|
|
|
{ data } = response
|
2016-12-20 04:54:42 -05:00
|
|
|
if data.error
|
|
|
|
$scope.state.error = true
|
|
|
|
$scope.state.errorReason = "#{data.error}"
|
|
|
|
$scope.state.inflight = false
|
2016-07-28 08:47:19 -04:00
|
|
|
else
|
2016-12-20 04:54:42 -05:00
|
|
|
if data.invite
|
|
|
|
invite = data.invite
|
|
|
|
$scope.project.invites.push invite
|
2016-07-28 08:47:19 -04:00
|
|
|
else
|
2016-12-20 04:54:42 -05:00
|
|
|
if data.users?
|
|
|
|
users = data.users
|
|
|
|
else if data.user?
|
|
|
|
users = [data.user]
|
|
|
|
else
|
|
|
|
users = []
|
|
|
|
$scope.project.members.push users...
|
2016-07-28 08:47:19 -04:00
|
|
|
|
2015-10-15 08:53:46 -04:00
|
|
|
setTimeout () ->
|
|
|
|
# Give $scope a chance to update $scope.canAddCollaborators
|
|
|
|
# with new collaborator information.
|
|
|
|
addNextMember()
|
|
|
|
, 0
|
2017-09-06 08:47:45 -04:00
|
|
|
.catch (err) ->
|
2015-10-07 12:32:35 -04:00
|
|
|
$scope.state.inflight = false
|
2015-11-04 09:33:27 -05:00
|
|
|
$scope.state.error = true
|
2017-09-07 06:30:42 -04:00
|
|
|
if err.status? and err.status == 400
|
2017-09-06 08:47:45 -04:00
|
|
|
$scope.state.errorReason = 'invalid_email'
|
|
|
|
else
|
|
|
|
$scope.state.errorReason = null
|
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)
|
2017-06-20 06:49:55 -04:00
|
|
|
.then () ->
|
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)
|
2017-06-20 06:49:55 -04:00
|
|
|
.catch () ->
|
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)
|
2017-06-20 06:49:55 -04:00
|
|
|
.then () ->
|
2016-07-25 09:27:02 -04:00
|
|
|
$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)
|
2017-06-20 06:49:55 -04:00
|
|
|
.catch () ->
|
2016-07-25 09:27:02 -04:00
|
|
|
$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)
|
2017-06-20 06:49:55 -04:00
|
|
|
.then () ->
|
2016-08-03 10:42:19 -04:00
|
|
|
$scope.state.inflight = false
|
2016-08-12 06:25:03 -04:00
|
|
|
event.target.blur()
|
2017-06-20 06:49:55 -04:00
|
|
|
.catch () ->
|
2016-08-03 10:42:19 -04:00
|
|
|
$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
|
|
|
|
}
|
|
|
|
|
2017-09-28 11:06:08 -04:00
|
|
|
$scope.openMakeTokenBasedModal = () ->
|
|
|
|
$modal.open {
|
|
|
|
templateUrl: "makeTokenBasedModalTemplate"
|
|
|
|
controller: "MakeTokenBasedModalController"
|
|
|
|
scope: $scope
|
|
|
|
}
|
|
|
|
|
2014-07-08 07:02:26 -04:00
|
|
|
$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()
|
|
|
|
]
|
|
|
|
|
2017-09-28 11:06:08 -04:00
|
|
|
App.controller "MakeTokenBasedModalController", ["$scope", "$modalInstance", "settings", ($scope, $modalInstance, settings) ->
|
|
|
|
|
|
|
|
$scope.makeTokenBased = () ->
|
|
|
|
$scope.project.publicAccesLevel = "tokenBased"
|
|
|
|
settings.saveProjectAdminSettings({publicAccessLevel: "tokenBased"})
|
|
|
|
$modalInstance.close()
|
|
|
|
|
|
|
|
$scope.cancel = () ->
|
|
|
|
$modalInstance.dismiss()
|
|
|
|
]
|
|
|
|
|
2014-07-08 07:02:26 -04:00
|
|
|
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
|
|
|
]
|