2014-07-08 11:02:26 +00:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
2015-10-05 15:36:56 +00:00
|
|
|
App.controller "SubscriptionGroupMembersController", ($scope, queuedHttp) ->
|
2014-07-08 11:02:26 +00: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/group/user", {
|
|
|
|
email: email,
|
|
|
|
_csrf: window.csrfToken
|
|
|
|
})
|
2017-06-20 15:27:05 +00:00
|
|
|
.then (response) ->
|
|
|
|
{ data } = response
|
2014-07-08 11:02:26 +00:00
|
|
|
$scope.users.push data.user if data.user?
|
|
|
|
$scope.inputs.emails = ""
|
|
|
|
|
|
|
|
$scope.removeMembers = () ->
|
|
|
|
for user in $scope.selectedUsers
|
|
|
|
do (user) ->
|
2017-06-08 15:53:22 +00:00
|
|
|
if user.holdingAccount and !user._id?
|
2017-06-09 09:39:48 +00:00
|
|
|
url = "/subscription/group/email/#{encodeURIComponent(user.email)}"
|
2017-06-08 11:12:08 +00:00
|
|
|
else
|
|
|
|
url = "/subscription/group/user/#{user._id}"
|
2014-07-08 11:02:26 +00:00
|
|
|
queuedHttp({
|
|
|
|
method: "DELETE",
|
2017-06-08 11:12:08 +00:00
|
|
|
url: url
|
2014-07-08 11:02:26 +00:00
|
|
|
headers:
|
|
|
|
"X-Csrf-Token": window.csrfToken
|
|
|
|
})
|
2017-06-20 10:49:55 +00:00
|
|
|
.then () ->
|
2014-07-08 11:02:26 +00: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
|
|
|
|
|
2015-10-05 15:36:56 +00:00
|
|
|
App.controller "SubscriptionGroupMemberListItemController", ($scope) ->
|
2014-07-08 11:02:26 +00:00
|
|
|
$scope.$watch "user.selected", (value) ->
|
|
|
|
if value?
|
|
|
|
$scope.updateSelectedUsers()
|