Extract settings_filters.js.

This commit is contained in:
Steve Howell 2017-04-08 14:24:03 -07:00 committed by Tim Abbott
parent 3e37f64f71
commit 6b549248e8
6 changed files with 116 additions and 94 deletions

View File

@ -43,6 +43,7 @@
"settings_org": false,
"settings_users": false,
"settings_streams": false,
"settings_filters": false,
"settings": false,
"resize": false,
"loading": false,

View File

@ -571,7 +571,7 @@ with_overrides(function (override) {
// realm_filters
var event = event_fixtures.realm_filters;
page_params.realm_filters = [];
override('admin.populate_filters', noop);
override('settings_filters.populate_filters', noop);
dispatch(event);
assert_same(page_params.realm_filters, event.realm_filters);

View File

@ -16,29 +16,6 @@ exports.show_or_hide_menu_item = function () {
}
};
exports.populate_filters = function (filters_data) {
if (!meta.loaded) {
return;
}
var filters_table = $("#admin_filters_table").expectOne();
filters_table.find("tr.filter_row").remove();
_.each(filters_data, function (filter) {
filters_table.append(
templates.render(
"admin_filter_list", {
filter: {
pattern: filter[0],
url_format_string: filter[1],
id: filter[2],
},
}
)
);
});
loading.destroy_indicator($('#admin_page_filters_loading_indicator'));
};
function _setup_page() {
var options = {
realm_name: page_params.realm_name,
@ -89,9 +66,6 @@ function _setup_page() {
$("#id_realm_default_language").val(page_params.realm_default_language);
// create loading indicators
loading.make_indicator($('#admin_page_filters_loading_indicator'));
// We set this flag before we're fully loaded so that the populate
// methods don't short-circuit.
meta.loaded = true;
@ -100,72 +74,7 @@ function _setup_page() {
settings_emoji.set_up();
settings_users.set_up();
settings_streams.set_up();
// Populate filters table
exports.populate_filters(page_params.realm_filters);
$('.admin_filters_table').on('click', '.delete', function (e) {
e.preventDefault();
e.stopPropagation();
var btn = $(this);
channel.del({
url: '/json/realm/filters/' + encodeURIComponent(btn.attr('data-filter-id')),
error: function (xhr) {
if (xhr.status.toString().charAt(0) === "4") {
btn.closest("td").html(
$("<p>").addClass("text-error").text($.parseJSON(xhr.responseText).msg)
);
} else {
btn.text(i18n.t("Failed!"));
}
},
success: function () {
var row = btn.parents('tr');
row.remove();
},
});
});
$(".organization").on("submit", "form.admin-filter-form", function (e) {
e.preventDefault();
e.stopPropagation();
var filter_status = $('#admin-filter-status');
var pattern_status = $('#admin-filter-pattern-status');
var format_status = $('#admin-filter-format-status');
filter_status.hide();
pattern_status.hide();
format_status.hide();
var filter = {};
_.each($(this).serializeArray(), function (obj) {
filter[obj.name] = obj.value;
});
channel.post({
url: "/json/realm/filters",
data: $(this).serialize(),
success: function (data) {
filter.id = data.id;
ui_report.success(i18n.t("Custom filter added!"), filter_status);
},
error: function (xhr) {
var errors = $.parseJSON(xhr.responseText).errors;
if (errors.pattern !== undefined) {
xhr.responseText = JSON.stringify({msg: errors.pattern});
ui_report.error(i18n.t("Failed"), xhr, pattern_status);
}
if (errors.url_format_string !== undefined) {
xhr.responseText = JSON.stringify({msg: errors.url_format_string});
ui_report.error(i18n.t("Failed"), xhr, format_status);
}
if (errors.__all__ !== undefined) {
xhr.responseText = JSON.stringify({msg: errors.__all__});
ui_report.error(i18n.t("Failed"), xhr, filter_status);
}
},
});
});
settings_filters.set_up();
}

View File

@ -134,7 +134,7 @@ function dispatch_normal_event(event) {
case 'realm_filters':
page_params.realm_filters = event.realm_filters;
echo.set_realm_filters(page_params.realm_filters);
admin.populate_filters(page_params.realm_filters);
settings_filters.populate_filters(page_params.realm_filters);
break;
case 'realm_domains':

View File

@ -0,0 +1,111 @@
var settings_filters = (function () {
var exports = {};
var meta = {
loaded: false,
};
exports.populate_filters = function (filters_data) {
if (!meta.loaded) {
return;
}
var filters_table = $("#admin_filters_table").expectOne();
filters_table.find("tr.filter_row").remove();
_.each(filters_data, function (filter) {
filters_table.append(
templates.render(
"admin_filter_list", {
filter: {
pattern: filter[0],
url_format_string: filter[1],
id: filter[2],
},
}
)
);
});
loading.destroy_indicator($('#admin_page_filters_loading_indicator'));
};
exports.set_up = function () {
meta.loaded = true;
// create loading indicators
loading.make_indicator($('#admin_page_filters_loading_indicator'));
// Populate filters table
exports.populate_filters(page_params.realm_filters);
$('.admin_filters_table').on('click', '.delete', function (e) {
e.preventDefault();
e.stopPropagation();
var btn = $(this);
channel.del({
url: '/json/realm/filters/' + encodeURIComponent(btn.attr('data-filter-id')),
error: function (xhr) {
if (xhr.status.toString().charAt(0) === "4") {
btn.closest("td").html(
$("<p>").addClass("text-error").text($.parseJSON(xhr.responseText).msg)
);
} else {
btn.text(i18n.t("Failed!"));
}
},
success: function () {
var row = btn.parents('tr');
row.remove();
},
});
});
$(".organization").on("submit", "form.admin-filter-form", function (e) {
e.preventDefault();
e.stopPropagation();
var filter_status = $('#admin-filter-status');
var pattern_status = $('#admin-filter-pattern-status');
var format_status = $('#admin-filter-format-status');
filter_status.hide();
pattern_status.hide();
format_status.hide();
var filter = {};
_.each($(this).serializeArray(), function (obj) {
filter[obj.name] = obj.value;
});
channel.post({
url: "/json/realm/filters",
data: $(this).serialize(),
success: function (data) {
filter.id = data.id;
ui_report.success(i18n.t("Custom filter added!"), filter_status);
},
error: function (xhr) {
var errors = $.parseJSON(xhr.responseText).errors;
if (errors.pattern !== undefined) {
xhr.responseText = JSON.stringify({msg: errors.pattern});
ui_report.error(i18n.t("Failed"), xhr, pattern_status);
}
if (errors.url_format_string !== undefined) {
xhr.responseText = JSON.stringify({msg: errors.url_format_string});
ui_report.error(i18n.t("Failed"), xhr, format_status);
}
if (errors.__all__ !== undefined) {
xhr.responseText = JSON.stringify({msg: errors.__all__});
ui_report.error(i18n.t("Failed"), xhr, filter_status);
}
},
});
});
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = settings_filters;
}

View File

@ -917,6 +917,7 @@ JS_SPECS = {
'js/settings_org.js',
'js/settings_users.js',
'js/settings_streams.js',
'js/settings_filters.js',
'js/settings.js',
'js/admin.js',
'js/tab_bar.js',