2017-11-13 05:20:42 +01:00
|
|
|
var settings_user_groups = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var meta = {
|
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.reset = function () {
|
|
|
|
meta.loaded = false;
|
|
|
|
};
|
|
|
|
|
2017-11-17 06:15:19 +01:00
|
|
|
exports.reload = function () {
|
2018-03-04 17:31:22 +01:00
|
|
|
if (!meta.loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-17 06:15:19 +01:00
|
|
|
var user_groups_section = $('#user-groups').expectOne();
|
|
|
|
user_groups_section.html('');
|
|
|
|
exports.populate_user_groups();
|
|
|
|
};
|
|
|
|
|
2018-03-13 10:46:17 +01:00
|
|
|
exports.can_edit = function (group_id) {
|
|
|
|
var me = people.get_person_from_user_id(people.my_current_user_id());
|
|
|
|
return (user_groups.is_member_of(group_id, people.my_current_user_id()) || me.is_admin);
|
|
|
|
};
|
|
|
|
|
2017-11-17 04:31:53 +01:00
|
|
|
exports.populate_user_groups = function () {
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2017-11-17 04:31:53 +01:00
|
|
|
var user_groups_section = $('#user-groups').expectOne();
|
|
|
|
var user_groups_array = user_groups.get_realm_user_groups();
|
|
|
|
_.each(user_groups_array, function (data) {
|
|
|
|
user_groups_section.append(templates.render('admin_user_group_list', {
|
|
|
|
user_group: {
|
|
|
|
name: data.name,
|
|
|
|
id: data.id,
|
|
|
|
description: data.description,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
var pill_container = $('.pill-container[data-group-pills="' + data.name + '"]');
|
2018-03-06 15:03:20 +01:00
|
|
|
var pills = input_pill.create({
|
|
|
|
container: pill_container,
|
|
|
|
create_item_from_text: user_pill.create_item_from_email,
|
|
|
|
get_text_from_item: user_pill.get_email_from_item,
|
|
|
|
});
|
|
|
|
|
|
|
|
function get_pill_user_ids() {
|
|
|
|
return user_pill.get_user_ids(pills);
|
|
|
|
}
|
|
|
|
|
|
|
|
function append_user(user) {
|
|
|
|
user_pill.append_person({
|
|
|
|
pill_widget: pills,
|
|
|
|
person: user,
|
|
|
|
});
|
|
|
|
}
|
2017-12-18 21:38:02 +01:00
|
|
|
|
2018-03-13 10:46:17 +01:00
|
|
|
var userg = $('div.user-group[id="' + data.id + '"]');
|
2018-03-15 12:56:31 +01:00
|
|
|
data.members.keys().forEach(function (user_id) {
|
2017-11-17 04:31:53 +01:00
|
|
|
var user = people.get_person_from_user_id(user_id);
|
|
|
|
|
|
|
|
if (user) {
|
2018-03-06 15:03:20 +01:00
|
|
|
append_user(user);
|
2017-11-17 04:31:53 +01:00
|
|
|
} else {
|
|
|
|
blueslip.warn('Unknown user ID ' + user_id + ' in members of user group ' + data.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-13 10:46:17 +01:00
|
|
|
function update_membership(group_id) {
|
|
|
|
if (exports.can_edit(group_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
userg.find('.name').attr('contenteditable','false');
|
|
|
|
userg.find('.description').attr('contenteditable','false');
|
|
|
|
userg.addClass('ntm');
|
|
|
|
pill_container.find('.input').attr('contenteditable','false');
|
|
|
|
pill_container.find('.input').css('display', 'none');
|
|
|
|
pill_container.addClass('notmem');
|
|
|
|
pill_container.off('keydown', '.pill');
|
|
|
|
pill_container.off('keydown', '.input');
|
|
|
|
pill_container.off('click');
|
|
|
|
pill_container.on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
pill_container.find('.pill').hover(function () {
|
|
|
|
pill_container.find('.pill').find('.exit').css('opacity', '0.5');
|
|
|
|
}, function () {}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
update_membership(data.id);
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
function is_user_group_changed() {
|
|
|
|
var draft_group = get_pill_user_ids();
|
|
|
|
var group_data = user_groups.get_user_group_from_id(data.id);
|
|
|
|
var original_group = group_data.members.keys();
|
|
|
|
var same_groups = _.isEqual(_.sortBy(draft_group), _.sortBy(original_group));
|
|
|
|
var description = $('#user-groups #' + data.id + ' .description').text().trim();
|
|
|
|
var name = $('#user-groups #' + data.id + ' .name').text().trim();
|
|
|
|
|
|
|
|
if ((group_data.description === description && group_data.name === name) &&
|
|
|
|
(!draft_group.length || same_groups)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_cancel_button() {
|
2018-03-13 10:46:17 +01:00
|
|
|
if (!exports.can_edit(data.id)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-15 12:10:07 +01:00
|
|
|
var cancel_button = $('#user-groups #' + data.id + ' .cancel');
|
|
|
|
var saved_button = $('#user-groups #' + data.id + ' .saved');
|
2018-03-15 12:56:31 +01:00
|
|
|
var save_instructions = $('#user-groups #' + data.id + ' .save-instructions');
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
if (is_user_group_changed() &&
|
|
|
|
!cancel_button.is(':visible')) {
|
|
|
|
saved_button.fadeOut(0);
|
|
|
|
cancel_button.css({display: 'inline-block', opacity: 0}).fadeTo(400, 1);
|
2018-03-15 12:56:31 +01:00
|
|
|
save_instructions.css({display: 'block', opacity: 0}).fadeTo(400, 1);
|
2018-03-15 12:10:07 +01:00
|
|
|
} else if (!is_user_group_changed() &&
|
|
|
|
cancel_button.is(':visible')) {
|
|
|
|
cancel_button.fadeOut();
|
2018-03-15 12:56:31 +01:00
|
|
|
save_instructions.fadeOut();
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function show_saved_button() {
|
|
|
|
var cancel_button = $('#user-groups #' + data.id + ' .cancel');
|
|
|
|
var saved_button = $('#user-groups #' + data.id + ' .saved');
|
2018-03-15 12:56:31 +01:00
|
|
|
var save_instructions = $('#user-groups #' + data.id + ' .save-instructions');
|
2018-03-15 12:10:07 +01:00
|
|
|
if (!saved_button.is(':visible')) {
|
|
|
|
cancel_button.fadeOut(0);
|
2018-03-15 12:56:31 +01:00
|
|
|
save_instructions.fadeOut(0);
|
2018-03-15 12:10:07 +01:00
|
|
|
saved_button.css({display: 'inline-block', opacity: 0}).fadeTo(400, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
function save_members() {
|
2018-03-06 15:03:20 +01:00
|
|
|
var draft_group = get_pill_user_ids();
|
2018-03-13 23:51:53 +01:00
|
|
|
var group_data = user_groups.get_user_group_from_id(data.id);
|
|
|
|
var original_group = group_data.members.keys();
|
2017-12-18 21:38:02 +01:00
|
|
|
var same_groups = _.isEqual(_.sortBy(draft_group), _.sortBy(original_group));
|
2018-03-13 23:51:53 +01:00
|
|
|
if (!draft_group.length || same_groups) {
|
|
|
|
return;
|
2017-12-18 21:38:02 +01:00
|
|
|
}
|
2018-03-13 23:51:53 +01:00
|
|
|
var added = _.difference(draft_group, original_group);
|
|
|
|
var removed = _.difference(original_group, draft_group);
|
|
|
|
channel.post({
|
|
|
|
url: "/json/user_groups/" + data.id + '/members',
|
|
|
|
data: {
|
|
|
|
add: JSON.stringify(added),
|
|
|
|
delete: JSON.stringify(removed),
|
|
|
|
},
|
2018-03-15 12:10:07 +01:00
|
|
|
success: function () {
|
|
|
|
show_saved_button();
|
|
|
|
},
|
2018-03-13 23:51:53 +01:00
|
|
|
});
|
2018-03-14 16:45:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function save_name_desc() {
|
|
|
|
var group_data = user_groups.get_user_group_from_id(data.id);
|
|
|
|
var description = $('#user-groups #' + data.id + ' .description').text().trim();
|
|
|
|
var name = $('#user-groups #' + data.id + ' .name').text().trim();
|
|
|
|
|
|
|
|
if (group_data.description === description && group_data.name === name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.patch({
|
|
|
|
url: "/json/user_groups/" + data.id,
|
|
|
|
data: {
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
},
|
|
|
|
success: function () {
|
2018-03-15 12:10:07 +01:00
|
|
|
setTimeout(show_saved_button, 100);
|
2018-03-14 16:45:37 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_not_blur(except_class, event) {
|
|
|
|
// Event generated from or inside the typeahead.
|
|
|
|
if ($(event.relatedTarget).closest(".typeahead").length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var blur_exceptions = _.without([".pill-container", ".name", ".description", ".input", ".delete"],
|
|
|
|
except_class);
|
2018-03-15 12:10:07 +01:00
|
|
|
if ($(event.relatedTarget).closest('#user-groups #' + data.id).length) {
|
2018-03-14 16:45:37 +01:00
|
|
|
return _.some(blur_exceptions, function (class_name) {
|
|
|
|
return $(event.relatedTarget).closest(class_name).length;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
function auto_save(class_name, event) {
|
2018-03-13 10:46:17 +01:00
|
|
|
if (!exports.can_edit(data.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
if (do_not_blur(class_name, event)) {
|
2018-03-14 16:45:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-03-15 12:10:07 +01:00
|
|
|
if ($(event.relatedTarget).closest('#user-groups #' + data.id) &&
|
|
|
|
$(event.relatedTarget).closest('.cancel').length) {
|
|
|
|
settings_user_groups.reload();
|
|
|
|
return;
|
|
|
|
}
|
2018-03-14 16:45:37 +01:00
|
|
|
save_name_desc();
|
|
|
|
save_members();
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$('#user-groups #' + data.id).on('blur', '.input', function (event) {
|
|
|
|
auto_save('.input', event);
|
2018-03-14 16:45:37 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$('#user-groups #' + data.id).on('blur', '.name', function (event) {
|
2018-03-15 12:10:07 +01:00
|
|
|
auto_save('.name', event);
|
|
|
|
});
|
|
|
|
$('#user-groups #' + data.id).on('input', '.name', function () {
|
|
|
|
update_cancel_button();
|
2018-03-14 16:45:37 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$('#user-groups #' + data.id).on('blur', '.description', function (event) {
|
2018-03-15 12:10:07 +01:00
|
|
|
auto_save('.description', event);
|
|
|
|
});
|
|
|
|
$('#user-groups #' + data.id).on('input', '.description', function () {
|
|
|
|
update_cancel_button();
|
2018-03-13 23:51:53 +01:00
|
|
|
});
|
2017-12-18 21:38:02 +01:00
|
|
|
|
2017-12-19 07:43:42 +01:00
|
|
|
var input = pill_container.children('.input');
|
2018-03-13 09:58:00 +01:00
|
|
|
(function set_up_typeahead() {
|
2018-03-13 10:46:17 +01:00
|
|
|
if (!exports.can_edit(data.id)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-13 09:58:00 +01:00
|
|
|
input.typeahead({
|
|
|
|
items: 5,
|
|
|
|
fixed: true,
|
|
|
|
dropup: true,
|
|
|
|
source: function () {
|
|
|
|
return user_pill.typeahead_source(pills);
|
|
|
|
},
|
|
|
|
highlighter: function (item) {
|
|
|
|
return typeahead_helper.render_person(item);
|
|
|
|
},
|
|
|
|
matcher: function (item) {
|
|
|
|
var query = this.query.toLowerCase();
|
|
|
|
return (item.email.toLowerCase().indexOf(query) !== -1
|
|
|
|
|| item.full_name.toLowerCase().indexOf(query) !== -1);
|
|
|
|
},
|
|
|
|
sorter: function (matches) {
|
|
|
|
return typeahead_helper.sort_recipientbox_typeahead(
|
|
|
|
this.query, matches, "");
|
|
|
|
},
|
|
|
|
updater: function (user) {
|
|
|
|
append_user(user);
|
|
|
|
input.focus();
|
|
|
|
update_cancel_button();
|
|
|
|
},
|
|
|
|
stopAdvance: true,
|
|
|
|
});
|
|
|
|
}());
|
2017-12-19 07:43:42 +01:00
|
|
|
|
2018-03-13 09:58:00 +01:00
|
|
|
(function pill_remove() {
|
2018-03-13 10:46:17 +01:00
|
|
|
if (!exports.can_edit(data.id)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-13 09:58:00 +01:00
|
|
|
pills.onPillRemove(function () {
|
|
|
|
// onPillRemove is fired before the pill is removed from
|
|
|
|
// the DOM.
|
2018-03-15 12:10:07 +01:00
|
|
|
update_cancel_button();
|
2018-03-13 09:58:00 +01:00
|
|
|
setTimeout(function () {
|
|
|
|
input.focus();
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
}());
|
2017-12-18 21:38:02 +01:00
|
|
|
});
|
2017-11-17 04:31:53 +01:00
|
|
|
};
|
|
|
|
|
2017-11-13 05:20:42 +01:00
|
|
|
exports.set_up = function () {
|
|
|
|
meta.loaded = true;
|
2017-11-17 04:31:53 +01:00
|
|
|
exports.populate_user_groups();
|
2017-11-17 06:15:19 +01:00
|
|
|
|
2018-03-25 15:46:34 +02:00
|
|
|
$(".organization form.admin-user-group-form").off("submit").on("submit", function (e) {
|
2017-11-17 06:15:19 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
var user_group_status = $('#admin-user-group-status');
|
|
|
|
|
|
|
|
var group = {
|
|
|
|
members: JSON.stringify([people.my_current_user_id()]),
|
|
|
|
};
|
|
|
|
_.each($(this).serializeArray(), function (obj) {
|
|
|
|
if (obj.value.trim() === "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
group[obj.name] = obj.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
channel.post({
|
|
|
|
url: "/json/user_groups/create",
|
|
|
|
data: group,
|
|
|
|
success: function () {
|
|
|
|
user_group_status.hide();
|
|
|
|
ui_report.success(i18n.t("User group added!"), user_group_status);
|
|
|
|
$("form.admin-user-group-form input[type='text']").val("");
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
user_group_status.hide();
|
|
|
|
var errors = JSON.parse(xhr.responseText).msg;
|
|
|
|
xhr.responseText = JSON.stringify({msg: errors});
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, user_group_status);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2017-12-06 20:00:23 +01:00
|
|
|
|
|
|
|
$('#user-groups').on('click', '.delete', function () {
|
2017-12-19 04:02:31 +01:00
|
|
|
var group_id = $(this).parents('.user-group').attr('id');
|
2018-03-13 10:46:17 +01:00
|
|
|
if (!exports.can_edit(group_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-06 20:00:23 +01:00
|
|
|
var user_group = user_groups.get_user_group_from_id(group_id);
|
|
|
|
var btn = $(this);
|
|
|
|
channel.del({
|
|
|
|
url: "/json/user_groups/" + group_id,
|
|
|
|
data: {
|
|
|
|
id: group_id,
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
user_groups.remove(user_group);
|
|
|
|
settings_user_groups.reload();
|
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
btn.text(i18n.t("Failed!"));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2017-12-19 04:02:31 +01:00
|
|
|
|
|
|
|
$('#user-groups').on('keypress', '.user-group h4 > span', function (e) {
|
|
|
|
if (e.which === 13) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2017-11-13 05:20:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = settings_user_groups;
|
|
|
|
}
|