overleaf/services/web/public/coffee/main/group-members.coffee

60 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2014-07-08 07:02:26 -04:00
define [
"base"
], (App) ->
App.controller "SubscriptionGroupMembersController", ($scope, queuedHttp) ->
2014-07-08 07:02:26 -04:00
$scope.users = window.users
$scope.groupSize = window.groupSize
$scope.selectedUsers = []
$scope.inputs =
emails: ""
parseEmails = (emailsString)->
regexBySpaceOrComma = /[\s,]+/
emails = emailsString.split(regexBySpaceOrComma)
emails = _.map emails, (email)->
email = email.trim()
emails = _.select emails, (email)->
email.indexOf("@") != -1
return emails
$scope.addMembers = () ->
emails = parseEmails($scope.inputs.emails)
for email in emails
queuedHttp
.post("/subscription/invites", {
2014-07-08 07:02:26 -04:00
email: email,
_csrf: window.csrfToken
})
.then (response) ->
{ data } = response
2014-07-08 07:02:26 -04:00
$scope.users.push data.user if data.user?
$scope.inputs.emails = ""
$scope.removeMembers = () ->
for user in $scope.selectedUsers
do (user) ->
if user.holdingAccount and !user._id?
url = "/subscription/group/email/#{encodeURIComponent(user.email)}"
else
url = "/subscription/group/user/#{user._id}"
2014-07-08 07:02:26 -04:00
queuedHttp({
method: "DELETE",
url: url
2014-07-08 07:02:26 -04:00
headers:
"X-Csrf-Token": window.csrfToken
})
.then () ->
2014-07-08 07:02:26 -04:00
index = $scope.users.indexOf(user)
return if index == -1
$scope.users.splice(index, 1)
$scope.selectedUsers = []
$scope.updateSelectedUsers = () ->
$scope.selectedUsers = $scope.users.filter (user) -> user.selected
App.controller "SubscriptionGroupMemberListItemController", ($scope) ->
2014-07-08 07:02:26 -04:00
$scope.$watch "user.selected", (value) ->
if value?
$scope.updateSelectedUsers()