define [ "base" ], (App) -> App.controller "ShareProjectModalController", ($scope, $modalInstance, $timeout, projectMembers, $modal, $http) -> $scope.inputs = { privileges: "readAndWrite" contacts: [] } $scope.state = { error: null inflight: false startedFreeTrial: false } $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 $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 $scope.filterAutocompleteUsers = ($query) -> return $scope.autocompleteContacts.filter (contact) -> for text in [contact.name, contact.email] if text?.toLowerCase().indexOf($query.toLowerCase()) > -1 return true return false $scope.addMembers = () -> $timeout -> # Give email list a chance to update return if $scope.inputs.contacts.length == 0 members = $scope.inputs.contacts $scope.inputs.contacts = [] $scope.state.error = null $scope.state.inflight = true do addNextMember = () -> if members.length == 0 or !$scope.canAddCollaborators $scope.state.inflight = false $scope.$apply() return member = members.shift() if member.type == "user" request = projectMembers.addMember(member.email, $scope.inputs.privileges) else if member.type == "group" request = projectMembers.addGroup(member.id, $scope.inputs.privileges) request .success (data) -> 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 = "Sorry, something went wrong :(" $scope.removeMember = (member) -> $scope.state.error = null $scope.state.inflight = true projectMembers .removeMember(member) .success () -> $scope.state.inflight = false index = $scope.project.members.indexOf(member) return if index == -1 $scope.project.members.splice(index, 1) .error () -> $scope.state.inflight = false $scope.state.error = "Sorry, something went wrong :(" $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.saveProjectSettings({publicAccessLevel: $scope.inputs.privileges}) $modalInstance.close() $scope.cancel = () -> $modalInstance.dismiss() ] App.controller "MakePrivateModalController", ["$scope", "$modalInstance", "settings", ($scope, $modalInstance, settings) -> $scope.makePrivate = () -> $scope.project.publicAccesLevel = "private" settings.saveProjectSettings({publicAccessLevel: "private"}) $modalInstance.close() $scope.cancel = () -> $modalInstance.dismiss() ]