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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/get_bots',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function (data) {
|
|
|
|
$('#bot_table_error').hide();
|
|
|
|
|
2013-06-27 23:05:36 +02:00
|
|
|
$.each(data.bots, function (idx, 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
|
|
|
|
|
|
|
function accept_bot_avatar_file_input(file) {
|
|
|
|
$('#bot_avatar_file').text(file.name);
|
|
|
|
$('#bot_avatar_file_input_error').hide();
|
|
|
|
$('#bot_avatar_clear_button').show();
|
|
|
|
$('#bot_avatar_upload_button').hide();
|
|
|
|
}
|
|
|
|
|
2013-06-14 20:03:54 +02:00
|
|
|
function clear_bot_avatar_file_input() {
|
|
|
|
var control = $('#bot_avatar_file_input');
|
|
|
|
var new_control = control.clone(true);
|
|
|
|
control.replaceWith(new_control);
|
2013-06-28 17:41:18 +02:00
|
|
|
$('#bot_avatar_file').text('');
|
|
|
|
$('#bot_avatar_clear_button').hide();
|
|
|
|
$('#bot_avatar_upload_button').show();
|
2013-06-14 20:03:54 +02:00
|
|
|
}
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
$('#bot_avatar_clear_button').click(function (e) {
|
2013-06-28 17:41:18 +02:00
|
|
|
clear_bot_avatar_file_input();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
$('#bot_avatar_upload_button').on('drop', function (e) {
|
2013-06-25 19:55:22 +02:00
|
|
|
var files = e.dataTransfer.files;
|
|
|
|
if (files === null || files === undefined || files.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-28 17:41:18 +02:00
|
|
|
$('#bot_avatar_file_input').get(0).files = files;
|
2013-06-25 19:55:22 +02:00
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2013-06-25 20:41:11 +02:00
|
|
|
function is_image_format(file) {
|
|
|
|
var type = file.type;
|
|
|
|
if (!type) return false;
|
|
|
|
|
|
|
|
var supported_types = [
|
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
|
|
|
'image/gif',
|
|
|
|
'image/svg+xml'
|
|
|
|
];
|
|
|
|
return $.inArray(type, supported_types) >= 0;
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
$('#bot_avatar_file_input').change(function (e) {
|
2013-06-14 20:03:54 +02:00
|
|
|
if (e.target.files.length === 0) {
|
|
|
|
$('#bot_avatar_file_input_error').hide();
|
|
|
|
} else if (e.target.files.length === 1) {
|
|
|
|
var file = e.target.files[0];
|
|
|
|
if (file.size > 5*1024*1024) {
|
|
|
|
$('#bot_avatar_file_input_error').text('File size must be < 5Mb.');
|
|
|
|
$('#bot_avatar_file_input_error').show();
|
|
|
|
clear_bot_avatar_file_input();
|
|
|
|
}
|
2013-06-25 20:41:11 +02:00
|
|
|
else if (!is_image_format(file)) {
|
|
|
|
$('#bot_avatar_file_input_error').text('File type is not supported.');
|
|
|
|
$('#bot_avatar_file_input_error').show();
|
|
|
|
clear_bot_avatar_file_input();
|
|
|
|
} else {
|
2013-06-28 17:41:18 +02:00
|
|
|
accept_bot_avatar_file_input(file);
|
2013-06-14 20:03:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('#bot_avatar_file_input_error').text('Please just upload one file.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
$('#bot_avatar_upload_button').click(function (e) {
|
2013-06-28 17:41:18 +02:00
|
|
|
$('#bot_avatar_file_input').trigger('click');
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
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-07-10 19:33:00 +02:00
|
|
|
util.make_loading_indicator($('#create_bot_spinner'), {text: 'Adding bot'});
|
2013-06-14 20:03:54 +02:00
|
|
|
$('#create_bot_button').hide();
|
2013-05-03 00:29:52 +02:00
|
|
|
$.ajax({
|
|
|
|
url: '/json/create_bot',
|
2013-06-14 20:03:54 +02:00
|
|
|
type: 'POST',
|
|
|
|
data: formData,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
2013-05-03 00:29:52 +02:00
|
|
|
success: function (data) {
|
2013-06-14 20:03:54 +02:00
|
|
|
util.destroy_loading_indicator($("#create_bot_spinner"));
|
2013-05-03 00:29:52 +02:00
|
|
|
$('#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();
|
|
|
|
clear_bot_avatar_file_input();
|
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) {
|
2013-06-14 20:03:54 +02:00
|
|
|
util.destroy_loading_indicator($("#create_bot_spinner"));
|
|
|
|
$('#create_bot_button').show();
|
2013-05-03 00:29:52 +02:00
|
|
|
$('#bot_table_error').text(JSON.parse(xhr.responseText).msg).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-07-10 17:26:30 +02:00
|
|
|
|
|
|
|
$("#bots_list").on("click", "button.delete_bot", function (e) {
|
|
|
|
var email = $(e.target).data('email');
|
|
|
|
$.ajax({
|
|
|
|
url: '/json/users/' + encodeURIComponent(email),
|
|
|
|
type: 'DELETE',
|
|
|
|
success: function () {
|
|
|
|
var row = $(e.target).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
|
|
|
|
|
|
|
$("#bots_list").on("click", "button.update_bot_api_key", function (e) {
|
|
|
|
var email = $(e.target).data('email');
|
|
|
|
$.ajax({
|
|
|
|
url: '/json/bots/' + encodeURIComponent(email) + '/api_key/regenerate',
|
|
|
|
type: 'POST',
|
|
|
|
success: function (data) {
|
|
|
|
var row = $(e.target).closest("li");
|
|
|
|
row.find(".api_key").find(".value").text(data.api_key);
|
|
|
|
row.find("api_key_error").hide();
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
var row = $(e.target).closest("li");
|
|
|
|
row.find(".api_key_error").text(JSON.parse(xhr.responseText).msg).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-05-03 00:29:52 +02:00
|
|
|
});
|
|
|
|
|
2013-06-27 23:05:36 +02:00
|
|
|
}());
|