define(function() { var Groups = {}; Groups.init = function() { var yourid = templates.get('yourid'), createEl = $('#create'), createModal = $('#create-modal'), createSubmitBtn = $('#create-modal-go'), createNameEl = $('#create-group-name'), detailsModal = $('#group-details-modal'), detailsSearch = detailsModal.find('#group-details-search'), searchResults = detailsModal.find('#group-details-search-results'), groupMembersEl = detailsModal.find('ul.current_members'), detailsModalSave = detailsModal.find('.btn-primary'), searchDelay = undefined, listEl = $('#groups-list'); createEl.on('click', function() { createModal.modal('show'); setTimeout(function() { createNameEl.focus(); }, 250); }); createSubmitBtn.on('click', function() { var submitObj = { name: createNameEl.val(), description: $('#create-group-desc').val() }, errorEl = $('#create-modal-error'), errorText; socket.emit('admin.groups.create', submitObj, function(err, data) { if (err) { switch (err) { case 'group-exists': errorText = 'Please choose another name

There seems to be a group with this name already.

'; break; case 'name-too-short': errorText = 'Please specify a group name

A group name is required for administrative purposes.

'; break; default: errorText = 'Uh-Oh

There was a problem creating your group. Please try again later!

'; break; } errorEl.html(errorText).removeClass('hide'); } else { errorEl.addClass('hide'); createNameEl.val(''); createModal.on('hidden.bs.modal', function() { ajaxify.go('admin/groups'); }); createModal.modal('hide'); } }); }); listEl.on('click', 'button[data-action]', function() { var el = $(this), action = el.attr('data-action'), gid = el.parents('li[data-gid]').attr('data-gid'); switch (action) { case 'delete': bootbox.confirm('Are you sure you wish to delete this group?', function(confirm) { if (confirm) { socket.emit('admin.groups.delete', gid, function(err, data) { console.log(err, data); if(err) { return app.alertError(err.message); } ajaxify.go('admin/groups'); }); } }); break; case 'members': socket.emit('admin.groups.get', gid, function(err, groupObj) { var formEl = detailsModal.find('form'), nameEl = formEl.find('#change-group-name'), descEl = formEl.find('#change-group-desc'), numMembers = groupObj.members.length, x; nameEl.val(groupObj.name); descEl.val(groupObj.description); if (numMembers > 0) { groupMembersEl.empty(); for (x = 0; x < numMembers; x++) { var memberIcon = $('
  • ') .attr('data-uid', groupObj.members[x].uid) .append($('').attr('src', groupObj.members[x].picture)) .append($('').html(groupObj.members[x].username)); groupMembersEl.append(memberIcon); } } detailsModal.attr('data-gid', groupObj.gid); detailsModal.modal('show'); }); break; } }); detailsSearch.on('keyup', function() { var searchEl = this; if (searchDelay) clearTimeout(searchDelay); searchDelay = setTimeout(function() { var searchText = searchEl.value, resultsEl = $('#group-details-search-results'), foundUser; socket.emit('admin.user.search', searchText, function(err, results) { if (!err && results && results.users.length > 0) { var numResults = results.users.length, x; if (numResults > 4) numResults = 4; resultsEl.empty(); for (x = 0; x < numResults; x++) { foundUser = $('
  • '); foundUser .attr({title: results.users[x].username, 'data-uid': results.users[x].uid}) .append($('').attr('src', results.users[x].picture)) .append($('').html(results.users[x].username)); resultsEl.append(foundUser); } } else { resultsEl.html('
  • No Users Found
  • '); } }); }, 200); }); searchResults.on('click', 'li[data-uid]', function() { var userLabel = $(this), uid = parseInt(userLabel.attr('data-uid')), gid = detailsModal.attr('data-gid'), members = []; groupMembersEl.find('li[data-uid]').each(function() { members.push(parseInt($(this).attr('data-uid'))); }); if (members.indexOf(uid) === -1) { socket.emit('admin.groups.join', { gid: gid, uid: uid }, function(err, data) { if (!err) { groupMembersEl.append(userLabel.clone(true)); } }); } }); groupMembersEl.on('click', 'li[data-uid]', function() { var uid = $(this).attr('data-uid'), gid = detailsModal.attr('data-gid'); socket.emit('admin.groups.get', gid, function(err, groupObj){ if (!err){ bootbox.confirm('Are you sure you want to remove this user?', function(confirm) { if (confirm){ socket.emit('admin.groups.leave', { gid: gid, uid: uid }, function(err, data) { if (!err) { groupMembersEl.find('li[data-uid="' + uid + '"]').remove(); } }); } }); } }); }); detailsModalSave.on('click', function() { var formEl = detailsModal.find('form'), nameEl = formEl.find('#change-group-name'), descEl = formEl.find('#change-group-desc'), gid = detailsModal.attr('data-gid'); socket.emit('admin.groups.update', { gid: gid, values: { name: nameEl.val(), description: descEl.val() } }, function(err) { if (!err) { detailsModal.on('hidden.bs.modal', function() { ajaxify.go('admin/groups'); }); detailsModal.modal('hide'); } }); }); }; return Groups; });