2013-05-03 00:29:52 +02:00
|
|
|
var settings = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-06-14 20:03:54 +02:00
|
|
|
function add_bot_row(name, email, avatar_url, api_key) {
|
2013-07-05 21:52:38 +02:00
|
|
|
var info = {
|
|
|
|
name: name,
|
|
|
|
email: email,
|
|
|
|
avatar_url: avatar_url,
|
|
|
|
api_key: api_key
|
|
|
|
};
|
2013-06-14 20:03:54 +02:00
|
|
|
|
2013-07-05 21:52:38 +02:00
|
|
|
var row = $(templates.render('bot_avatar_row', info));
|
2013-07-05 20:59:25 +02:00
|
|
|
$('#bots_list').append(row);
|
|
|
|
$('#bots_list').show();
|
2013-05-03 00:29:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function is_local_part(value, element) {
|
|
|
|
// Adapted from Django's EmailValidator
|
|
|
|
return this.optional(element) || /^[\-!#$%&'*+\/=?\^_`{}|~0-9A-Z]+(\.[\-!#$%&'*+\/=?\^_`{}|~0-9A-Z]+)*$/i.test(value);
|
|
|
|
}
|
|
|
|
|
2013-11-19 17:15:48 +01:00
|
|
|
// Choose avatar stamp fairly randomly, to help get old avatars out of cache.
|
|
|
|
exports.avatar_stamp = Math.floor(Math.random()*100);
|
|
|
|
|
2013-05-03 00:29:52 +02:00
|
|
|
$(function () {
|
2013-10-28 15:49:38 +01:00
|
|
|
|
2013-12-02 03:01:09 +01:00
|
|
|
if (!feature_flags.show_digest_email_setting) {
|
|
|
|
$("#digest_container").hide();
|
|
|
|
}
|
2013-10-28 15:49:38 +01:00
|
|
|
|
|
|
|
function upload_avatar(file_input) {
|
|
|
|
var form_data = new FormData();
|
|
|
|
|
|
|
|
form_data.append('csrfmiddlewaretoken', csrf_token);
|
|
|
|
jQuery.each(file_input[0].files, function (i, file) {
|
|
|
|
form_data.append('file-'+i, file);
|
|
|
|
});
|
|
|
|
|
|
|
|
var spinner = $("#upload_avatar_spinner").expectOne();
|
|
|
|
util.make_loading_indicator(spinner, {text: 'Uploading avatar.'});
|
|
|
|
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2013-10-28 15:49:38 +01:00
|
|
|
url: '/json/set_avatar',
|
|
|
|
data: form_data,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
success: function (data) {
|
|
|
|
util.destroy_loading_indicator($("#upload_avatar_spinner"));
|
2013-11-19 17:15:48 +01:00
|
|
|
var url = data.avatar_url + '&stamp=' + exports.avatar_stamp;
|
2013-10-28 15:49:38 +01:00
|
|
|
$("#user-settings-avatar").expectOne().attr("src", url);
|
2013-11-19 17:15:48 +01:00
|
|
|
exports.avatar_stamp += 1;
|
2013-10-28 15:49:38 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
avatar.build_user_avatar_widget(upload_avatar);
|
|
|
|
|
2013-11-15 16:52:31 +01:00
|
|
|
if (page_params.domain === "users.customer4.invalid" ||
|
|
|
|
page_params.name_changes_disabled) {
|
2013-08-05 02:29:01 +02:00
|
|
|
// At the request of the facilitators, CUSTOMER4 users
|
|
|
|
// can't change their names, so don't show that as a settings
|
|
|
|
// option. This is also disabled through the JSON UI. Once we
|
|
|
|
// have the infrastructure for administrative policies, we can
|
|
|
|
// handle this more gracefully.
|
2013-11-15 16:52:31 +01:00
|
|
|
//
|
|
|
|
// Additionally, if this install has disabled name changes, hide the
|
|
|
|
// container
|
2013-08-05 02:29:01 +02:00
|
|
|
$("#name_change_container").hide();
|
|
|
|
}
|
|
|
|
|
2014-02-11 16:31:22 +01:00
|
|
|
channel.get({
|
|
|
|
url: '/json/bots',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-05-03 00:29:52 +02:00
|
|
|
success: function (data) {
|
|
|
|
$('#bot_table_error').hide();
|
|
|
|
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(data.bots, function (elem) {
|
2013-06-14 20:03:54 +02:00
|
|
|
add_bot_row(elem.full_name, elem.username, elem.avatar_url, elem.api_key);
|
2013-05-03 00:29:52 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
error: function (xhr, error_type, xhn) {
|
|
|
|
$('#bot_table_error').text("Could not fetch bots list").show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.validator.addMethod("bot_local_part",
|
|
|
|
function (value, element) {
|
|
|
|
return is_local_part.call(this, value + "-bot", element);
|
|
|
|
},
|
|
|
|
"Please only use characters that are valid in an email address");
|
|
|
|
|
2013-06-28 17:41:18 +02:00
|
|
|
|
2013-08-01 15:43:48 +02:00
|
|
|
var create_avatar_widget = avatar.build_bot_create_widget();
|
2013-06-28 17:41:18 +02:00
|
|
|
|
2013-05-03 00:29:52 +02:00
|
|
|
$('#create_bot_form').validate({
|
|
|
|
errorClass: 'text-error',
|
|
|
|
success: function () {
|
|
|
|
$('#bot_table_error').hide();
|
|
|
|
},
|
|
|
|
submitHandler: function () {
|
2013-06-14 20:03:54 +02:00
|
|
|
var full_name = $('#create_bot_name').val();
|
2013-05-03 00:29:52 +02:00
|
|
|
var short_name = $('#create_bot_short_name').val();
|
2013-06-14 20:03:54 +02:00
|
|
|
var formData = new FormData();
|
|
|
|
formData.append('csrfmiddlewaretoken', csrf_token);
|
|
|
|
formData.append('full_name', full_name);
|
|
|
|
formData.append('short_name', short_name);
|
2013-07-05 17:43:56 +02:00
|
|
|
jQuery.each($('#bot_avatar_file_input')[0].files, function (i, file) {
|
2013-06-14 20:03:54 +02:00
|
|
|
formData.append('file-'+i, file);
|
|
|
|
});
|
2013-11-16 20:03:56 +01:00
|
|
|
$('#create_bot_button').val('Adding bot...').prop('disabled', true);
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2014-02-11 16:42:19 +01:00
|
|
|
url: '/json/bots',
|
2013-06-14 20:03:54 +02:00
|
|
|
data: formData,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
2013-05-03 00:29:52 +02:00
|
|
|
success: function (data) {
|
|
|
|
$('#bot_table_error').hide();
|
|
|
|
$('#create_bot_name').val('');
|
|
|
|
$('#create_bot_short_name').val('');
|
2013-06-14 20:03:54 +02:00
|
|
|
$('#create_bot_button').show();
|
2013-08-01 15:43:48 +02:00
|
|
|
create_avatar_widget.clear();
|
2013-05-03 00:29:52 +02:00
|
|
|
|
2013-06-14 20:03:54 +02:00
|
|
|
add_bot_row(
|
|
|
|
full_name,
|
|
|
|
short_name + "-bot@" + page_params.domain,
|
|
|
|
data.avatar_url,
|
|
|
|
data.api_key
|
|
|
|
);
|
2013-05-03 00:29:52 +02:00
|
|
|
},
|
|
|
|
error: function (xhr, error_type, exn) {
|
|
|
|
$('#bot_table_error').text(JSON.parse(xhr.responseText).msg).show();
|
2013-11-16 20:03:56 +01:00
|
|
|
},
|
|
|
|
complete: function (xhr, status) {
|
|
|
|
$('#create_bot_button').val('Create bot').prop('disabled', false);
|
2013-05-03 00:29:52 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-07-10 17:26:30 +02:00
|
|
|
|
|
|
|
$("#bots_list").on("click", "button.delete_bot", function (e) {
|
2013-10-21 01:04:25 +02:00
|
|
|
var email = $(e.currentTarget).data('email');
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.del({
|
2014-02-11 17:14:33 +01:00
|
|
|
url: '/json/bots/' + encodeURIComponent(email),
|
2013-07-10 17:26:30 +02:00
|
|
|
success: function () {
|
2013-10-21 01:04:25 +02:00
|
|
|
var row = $(e.currentTarget).closest("li");
|
2013-07-10 21:40:01 +02:00
|
|
|
row.hide('slow', function () { row.remove(); });
|
2013-07-10 22:30:03 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
$('#bot_delete_error').text(JSON.parse(xhr.responseText).msg).show();
|
2013-07-10 17:26:30 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 17:12:35 +02:00
|
|
|
|
2013-07-26 05:07:59 +02:00
|
|
|
$("#bots_list").on("click", "button.regenerate_bot_api_key", function (e) {
|
2013-10-21 01:04:25 +02:00
|
|
|
var email = $(e.currentTarget).data('email');
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2013-07-22 17:12:35 +02:00
|
|
|
url: '/json/bots/' + encodeURIComponent(email) + '/api_key/regenerate',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-07-22 17:12:35 +02:00
|
|
|
success: function (data) {
|
2013-10-21 01:04:25 +02:00
|
|
|
var row = $(e.currentTarget).closest("li");
|
2013-07-22 17:12:35 +02:00
|
|
|
row.find(".api_key").find(".value").text(data.api_key);
|
|
|
|
row.find("api_key_error").hide();
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2013-10-21 01:04:25 +02:00
|
|
|
var row = $(e.currentTarget).closest("li");
|
2013-07-22 17:12:35 +02:00
|
|
|
row.find(".api_key_error").text(JSON.parse(xhr.responseText).msg).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 20:09:34 +02:00
|
|
|
|
2013-07-29 16:27:18 +02:00
|
|
|
var image_version = 0;
|
2013-07-22 20:09:34 +02:00
|
|
|
|
|
|
|
$("#bots_list").on("click", "button.open_edit_bot_form", function (e) {
|
2013-10-21 01:04:25 +02:00
|
|
|
var li = $(e.currentTarget).closest('li');
|
2013-07-22 20:09:34 +02:00
|
|
|
var edit_div = li.find('div.edit_bot');
|
|
|
|
var form = li.find('.edit_bot_form');
|
|
|
|
var image = li.find(".image");
|
|
|
|
var bot_info = li.find(".bot_info");
|
|
|
|
var reset_edit_bot = li.find(".reset_edit_bot");
|
|
|
|
|
|
|
|
var old_full_name = bot_info.find(".name").text();
|
|
|
|
form.find(".edit_bot_name").attr('value', old_full_name);
|
|
|
|
|
|
|
|
image.hide();
|
|
|
|
bot_info.hide();
|
|
|
|
edit_div.show();
|
|
|
|
|
2013-08-01 15:43:48 +02:00
|
|
|
var avatar_widget = avatar.build_bot_edit_widget(li);
|
2013-07-29 16:27:18 +02:00
|
|
|
|
2013-07-22 20:09:34 +02:00
|
|
|
function show_row_again() {
|
|
|
|
image.show();
|
|
|
|
bot_info.show();
|
|
|
|
edit_div.hide();
|
2013-08-01 15:43:48 +02:00
|
|
|
avatar_widget.close();
|
2013-07-22 20:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reset_edit_bot.click(function (event) {
|
|
|
|
show_row_again();
|
|
|
|
$(this).off(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
var errors = form.find('.bot_edit_errors');
|
|
|
|
|
|
|
|
form.validate({
|
|
|
|
errorClass: 'text-error',
|
|
|
|
success: function (label) {
|
|
|
|
errors.hide();
|
|
|
|
},
|
|
|
|
submitHandler: function () {
|
|
|
|
var email = form.data('email');
|
|
|
|
var full_name = form.find('.edit_bot_name').val();
|
2013-07-29 16:27:18 +02:00
|
|
|
var file_input = li.find('.edit_bot_avatar_file_input');
|
2013-07-22 20:09:34 +02:00
|
|
|
var spinner = form.find('.edit_bot_spinner');
|
|
|
|
var edit_button = form.find('.edit_bot_button');
|
|
|
|
var formData = new FormData();
|
|
|
|
formData.append('full_name', full_name);
|
|
|
|
formData.append('csrfmiddlewaretoken', csrf_token);
|
2013-07-29 16:27:18 +02:00
|
|
|
jQuery.each(file_input[0].files, function (i, file) {
|
|
|
|
formData.append('file-'+i, file);
|
|
|
|
});
|
2013-07-22 20:09:34 +02:00
|
|
|
util.make_loading_indicator(spinner, {text: 'Editing bot'});
|
|
|
|
edit_button.hide();
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.patch({
|
2013-07-22 20:09:34 +02:00
|
|
|
url: '/json/bots/' + encodeURIComponent(email),
|
|
|
|
data: formData,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
success: function (data) {
|
|
|
|
util.destroy_loading_indicator(spinner);
|
|
|
|
errors.hide();
|
|
|
|
edit_button.show();
|
|
|
|
show_row_again();
|
|
|
|
bot_info.find('.name').text(full_name);
|
2013-07-29 16:27:18 +02:00
|
|
|
if (data.avatar_url) {
|
|
|
|
// Note that the avatar_url won't actually change on the back end
|
|
|
|
// when the user had a previous uploaded avatar. Only the content
|
|
|
|
// changes, so we version it to get an uncached copy.
|
|
|
|
image_version += 1;
|
|
|
|
image.find('img').attr('src', data.avatar_url+'&v='+image_version.toString());
|
|
|
|
}
|
2013-07-22 20:09:34 +02:00
|
|
|
},
|
|
|
|
error: function (xhr, error_type, exn) {
|
|
|
|
util.destroy_loading_indicator(spinner);
|
|
|
|
edit_button.show();
|
|
|
|
errors.text(JSON.parse(xhr.responseText).msg).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-08-08 18:08:30 +02:00
|
|
|
$("#show_api_key_box").on("click", "button.regenerate_api_key", function (e) {
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2013-08-08 18:08:30 +02:00
|
|
|
url: '/json/users/me/api_key/regenerate',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-08-08 18:08:30 +02:00
|
|
|
success: function (data) {
|
|
|
|
$('#api_key_value').text(data.api_key);
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
$('#user_api_key_error').text(JSON.parse(xhr.responseText).msg).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 20:09:34 +02:00
|
|
|
|
2013-05-03 00:29:52 +02:00
|
|
|
});
|
|
|
|
|
2013-11-19 17:15:48 +01:00
|
|
|
return exports;
|
2013-06-27 23:05:36 +02:00
|
|
|
}());
|