Extract settings_emoji.js.

This commit is contained in:
Steve Howell 2017-04-08 08:24:07 -07:00 committed by Tim Abbott
parent 76ec9cf60b
commit a3b44148af
6 changed files with 98 additions and 74 deletions

View File

@ -39,6 +39,7 @@
"settings_lab": false, "settings_lab": false,
"settings_bots": false, "settings_bots": false,
"settings_sections": false, "settings_sections": false,
"settings_emoji": false,
"settings": false, "settings": false,
"resize": false, "resize": false,
"loading": false, "loading": false,

View File

@ -560,7 +560,7 @@ with_overrides(function (override) {
global.with_stub(function (stub) { global.with_stub(function (stub) {
override('emoji.update_emojis', stub.f); override('emoji.update_emojis', stub.f);
override('admin.populate_emoji', noop); override('settings_emoji.populate_emoji', noop);
dispatch(event); dispatch(event);
var args = stub.get_args('realm_emoji'); var args = stub.get_args('realm_emoji');
assert_same(args.realm_emoji, event.realm_emoji); assert_same(args.realm_emoji, event.realm_emoji);

View File

@ -291,26 +291,6 @@ function make_stream_default(stream_name) {
}); });
} }
exports.populate_emoji = function (emoji_data) {
if (!meta.loaded) {
return;
}
var emoji_table = $('#admin_emoji_table').expectOne();
emoji_table.find('tr.emoji_row').remove();
_.each(emoji_data, function (data, name) {
emoji_table.append(templates.render('admin_emoji_list', {
emoji: {
name: name, source_url: data.source_url,
display_url: data.display_url,
author: data.author,
is_admin: page_params.is_admin,
},
}));
});
loading.destroy_indicator($('#admin_page_emoji_loading_indicator'));
};
exports.populate_filters = function (filters_data) { exports.populate_filters = function (filters_data) {
if (!meta.loaded) { if (!meta.loaded) {
return; return;
@ -443,7 +423,6 @@ function _setup_page() {
loading.make_indicator($('#admin_page_bots_loading_indicator')); loading.make_indicator($('#admin_page_bots_loading_indicator'));
loading.make_indicator($('#admin_page_streams_loading_indicator')); loading.make_indicator($('#admin_page_streams_loading_indicator'));
loading.make_indicator($('#admin_page_deactivated_users_loading_indicator')); loading.make_indicator($('#admin_page_deactivated_users_loading_indicator'));
loading.make_indicator($('#admin_page_emoji_loading_indicator'));
loading.make_indicator($('#admin_page_auth_methods_loading_indicator')); loading.make_indicator($('#admin_page_auth_methods_loading_indicator'));
loading.make_indicator($('#admin_page_filters_loading_indicator')); loading.make_indicator($('#admin_page_filters_loading_indicator'));
@ -469,11 +448,11 @@ function _setup_page() {
// methods don't short-circuit. // methods don't short-circuit.
meta.loaded = true; meta.loaded = true;
settings_emoji.set_up();
// Populate authentication methods table // Populate authentication methods table
exports.populate_auth_methods(page_params.realm_authentication_methods); exports.populate_auth_methods(page_params.realm_authentication_methods);
// Populate emoji table
exports.populate_emoji(page_params.realm_emoji);
exports.update_default_streams_table(); exports.update_default_streams_table();
// Populate filters table // Populate filters table
@ -1011,55 +990,6 @@ function _setup_page() {
}); });
}); });
$('.admin_emoji_table').on('click', '.delete', function (e) {
e.preventDefault();
e.stopPropagation();
var btn = $(this);
channel.del({
url: '/json/realm/emoji/' + encodeURIComponent(btn.attr('data-emoji-name')),
error: function (xhr) {
if (xhr.status.toString().charAt(0) === "4") {
btn.closest("td").html(
$("<p>").addClass("text-error").text(JSON.parse(xhr.responseText).msg)
);
} else {
btn.text(i18n.t("Failed!"));
}
},
success: function () {
var row = btn.parents('tr');
row.remove();
},
});
});
$(".organization").on("submit", "form.admin-emoji-form", function (e) {
e.preventDefault();
e.stopPropagation();
var emoji_status = $('#admin-emoji-status');
var emoji = {};
_.each($(this).serializeArray(), function (obj) {
emoji[obj.name] = obj.value;
});
channel.put({
url: "/json/realm/emoji/" + encodeURIComponent(emoji.name),
data: $(this).serialize(),
success: function () {
$('#admin-emoji-status').hide();
ui_report.success(i18n.t("Custom emoji added!"), emoji_status);
$("form.admin-emoji-form input[type='text']").val("");
},
error: function (xhr) {
$('#admin-emoji-status').hide();
var errors = JSON.parse(xhr.responseText).msg;
xhr.responseText = JSON.stringify({msg: errors});
ui_report.error(i18n.t("Failed!"), xhr, emoji_status);
},
});
});
$('.admin_filters_table').on('click', '.delete', function (e) { $('.admin_filters_table').on('click', '.delete', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();

View File

@ -128,7 +128,7 @@ function dispatch_normal_event(event) {
case 'realm_emoji': case 'realm_emoji':
emoji.update_emojis(event.realm_emoji); emoji.update_emojis(event.realm_emoji);
admin.populate_emoji(event.realm_emoji); settings_emoji.populate_emoji(event.realm_emoji);
break; break;
case 'realm_filters': case 'realm_filters':

View File

@ -0,0 +1,92 @@
var settings_emoji = (function () {
var exports = {};
var meta = {
loaded: false,
};
exports.populate_emoji = function (emoji_data) {
if (!meta.loaded) {
return;
}
var emoji_table = $('#admin_emoji_table').expectOne();
emoji_table.find('tr.emoji_row').remove();
_.each(emoji_data, function (data, name) {
emoji_table.append(templates.render('admin_emoji_list', {
emoji: {
name: name, source_url: data.source_url,
display_url: data.display_url,
author: data.author,
is_admin: page_params.is_admin,
},
}));
});
loading.destroy_indicator($('#admin_page_emoji_loading_indicator'));
};
exports.set_up = function () {
meta.loaded = true;
loading.make_indicator($('#admin_page_emoji_loading_indicator'));
// Populate emoji table
exports.populate_emoji(page_params.realm_emoji);
$('.admin_emoji_table').on('click', '.delete', function (e) {
e.preventDefault();
e.stopPropagation();
var btn = $(this);
channel.del({
url: '/json/realm/emoji/' + encodeURIComponent(btn.attr('data-emoji-name')),
error: function (xhr) {
if (xhr.status.toString().charAt(0) === "4") {
btn.closest("td").html(
$("<p>").addClass("text-error").text(JSON.parse(xhr.responseText).msg)
);
} else {
btn.text(i18n.t("Failed!"));
}
},
success: function () {
var row = btn.parents('tr');
row.remove();
},
});
});
$(".organization").on("submit", "form.admin-emoji-form", function (e) {
e.preventDefault();
e.stopPropagation();
var emoji_status = $('#admin-emoji-status');
var emoji = {};
_.each($(this).serializeArray(), function (obj) {
emoji[obj.name] = obj.value;
});
channel.put({
url: "/json/realm/emoji/" + encodeURIComponent(emoji.name),
data: $(this).serialize(),
success: function () {
$('#admin-emoji-status').hide();
ui_report.success(i18n.t("Custom emoji added!"), emoji_status);
$("form.admin-emoji-form input[type='text']").val("");
},
error: function (xhr) {
$('#admin-emoji-status').hide();
var errors = JSON.parse(xhr.responseText).msg;
xhr.responseText = JSON.stringify({msg: errors});
ui_report.error(i18n.t("Failed!"), xhr, emoji_status);
},
});
});
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = settings_emoji;
}

View File

@ -913,6 +913,7 @@ JS_SPECS = {
'js/settings_muting.js', 'js/settings_muting.js',
'js/settings_lab.js', 'js/settings_lab.js',
'js/settings_sections.js', 'js/settings_sections.js',
'js/settings_emoji.js',
'js/settings.js', 'js/settings.js',
'js/admin.js', 'js/admin.js',
'js/tab_bar.js', 'js/tab_bar.js',