2014-02-12 05:23:40 -05:00
|
|
|
require [
|
|
|
|
"libs/mustache"
|
|
|
|
"./main"
|
|
|
|
"libs/underscore"
|
|
|
|
], (m)->
|
|
|
|
$(document).ready ->
|
|
|
|
|
|
|
|
tableRowTemplate = '''
|
|
|
|
<tr>
|
2014-03-28 08:07:30 -04:00
|
|
|
<td> <input type="checkbox" class="select-one"></td>
|
2014-02-12 05:23:40 -05:00
|
|
|
<td> {{ email }} </td>
|
|
|
|
<td> {{ first_name }} {{ last_name }} </td>
|
|
|
|
<td> {{ !holdingAccount }} </td>
|
2014-03-03 12:48:48 -05:00
|
|
|
<td>
|
2014-03-28 08:07:30 -04:00
|
|
|
<input type="hidden" name="user_id" value="{{_id}}" class="user_id">
|
2014-03-03 12:48:48 -05:00
|
|
|
</td>
|
2014-02-12 05:23:40 -05:00
|
|
|
</tr>
|
|
|
|
'''
|
|
|
|
|
|
|
|
window.temp = tableRowTemplate
|
|
|
|
|
|
|
|
$form = $('form#addUserToGroup')
|
|
|
|
|
|
|
|
addUser = (e)->
|
|
|
|
|
|
|
|
parseEmails = (emailsString)->
|
|
|
|
regexBySpaceOrComma = /[\s,]+/
|
|
|
|
emails = emailsString.split(regexBySpaceOrComma)
|
|
|
|
emails = _.map emails, (email)->
|
|
|
|
email = email.trim()
|
|
|
|
emails = _.select emails, (email)->
|
|
|
|
email.indexOf("@") != -1
|
|
|
|
return emails
|
|
|
|
|
|
|
|
|
|
|
|
sendNewUserToServer = (email)->
|
|
|
|
$.ajax
|
|
|
|
url: "/subscription/group/user"
|
|
|
|
type: 'POST'
|
|
|
|
data:
|
|
|
|
email: email
|
2014-03-03 12:58:26 -05:00
|
|
|
_csrf: csrfToken
|
2014-02-12 05:23:40 -05:00
|
|
|
success: (data)->
|
|
|
|
if data.limitReached
|
|
|
|
alert("You have reached your maximum number of members")
|
|
|
|
else
|
|
|
|
renderNewUserInList data.user
|
|
|
|
|
|
|
|
renderNewUserInList = (user)->
|
|
|
|
html = Mustache.to_html(tableRowTemplate, user)
|
|
|
|
$('#userList').append(html)
|
|
|
|
|
|
|
|
e.preventDefault()
|
|
|
|
val = $form.find("input[name=email]").val()
|
|
|
|
emails = parseEmails(val)
|
|
|
|
emails.forEach (email)->
|
|
|
|
sendNewUserToServer(email)
|
|
|
|
$form.find("input").val('')
|
|
|
|
|
2014-03-03 12:48:48 -05:00
|
|
|
removeUsers = (e)->
|
2014-03-28 08:07:30 -04:00
|
|
|
selectedUserRows = $('td input.select-one:checked').closest('tr').find(".user_id").toArray()
|
|
|
|
do deleteNext = () ->
|
|
|
|
row = selectedUserRows.pop()
|
|
|
|
if row?
|
|
|
|
user_id = $(row).val()
|
|
|
|
$.ajax
|
|
|
|
url: "/subscription/group/user/#{user_id}"
|
|
|
|
type: 'DELETE'
|
|
|
|
data:
|
|
|
|
_csrf: csrfToken
|
|
|
|
success: ->
|
|
|
|
$(row).parents("tr").fadeOut(250)
|
|
|
|
deleteNext()
|
2014-03-03 12:48:48 -05:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
$form.on 'keypress', (e)->
|
|
|
|
if(e.keyCode == 13)
|
|
|
|
addUser(e)
|
|
|
|
|
|
|
|
$form.find(".addUser").on 'click', addUser
|
|
|
|
|
2014-03-03 12:48:48 -05:00
|
|
|
$('#deleteUsers').on 'click', removeUsers
|
2014-03-28 08:07:30 -04:00
|
|
|
|
|
|
|
$('input.select-all').on "change", () ->
|
|
|
|
if $(@).is(":checked")
|
|
|
|
$("input.select-one").prop( "checked", true )
|
|
|
|
else
|
|
|
|
$("input.select-one").prop( "checked", false )
|
|
|
|
|