list_render: Remove requirement of filter in opts.

The use case for this are small or fixed tables, which do not need
filtering support. Thus we are able to not include the unnecessary
search input inside the html parent container.
It is not used at present, but will be required when we refactor
the settings pages.

We also split out exports.validate_filter function for
unit testing the above condition.
This commit is contained in:
Ryan Rehman
2020-03-11 00:18:03 +05:30
committed by Tim Abbott
parent 0c821424cd
commit 1c605366ed
2 changed files with 37 additions and 20 deletions

View File

@@ -108,7 +108,7 @@ run_test('list_render', () => {
'fence',
'grape',
];
const opts = {
let opts = {
filter: {
element: search_input,
predicate: (item, value) => {
@@ -119,7 +119,7 @@ run_test('list_render', () => {
modifier: (item) => div(item),
};
const widget = list_render.create(container, list, opts);
let widget = list_render.create(container, list, opts);
widget.render();
@@ -160,6 +160,17 @@ run_test('list_render', () => {
widget.render();
expected_html = '<div>greta</div><div>gary</div>';
assert.deepEqual(container.appended_data.html(), expected_html);
// Opts does not require a filter key.
opts = {
modifier: (item) => div(item),
};
list_render.validate_filter(opts);
widget = list_render.create(container, ['apple', 'banana'], opts);
widget.render();
expected_html = '<div>apple</div><div>banana</div>';
assert.deepEqual(container.appended_data.html(), expected_html);
});
function sort_button(opts) {

View File

@@ -21,6 +21,27 @@ exports.filter = (value, list, opts) => {
});
};
exports.validate_filter = (opts) => {
if (!opts.filter) {
return;
}
if (opts.filter.predicate) {
if (typeof opts.filter.predicate !== 'function') {
blueslip.error('Filter predicate function is missing.');
return;
}
if (opts.filter.filterer) {
blueslip.error('Filterer and predicate are mutually exclusive.');
return;
}
} else {
if (typeof opts.filter.filterer !== 'function') {
blueslip.error('Filter filterer function is missing.');
return;
}
}
};
// @params
// container: jQuery object to append to.
// list: The list of items to progressively append.
@@ -60,22 +81,7 @@ exports.create = function ($container, list, opts) {
if (!opts) {
return;
}
if (opts.filter.predicate) {
if (typeof opts.filter.predicate !== 'function') {
blueslip.error('Filter predicate function is missing.');
return;
}
if (opts.filter.filterer) {
blueslip.error('Filterer and predicate are mutually exclusive.');
return;
}
} else {
if (typeof opts.filter.filterer !== 'function') {
blueslip.error('Filter filterer function is missing.');
return;
}
}
exports.validate_filter(opts);
const prototype = {
// Reads the provided list (in the scope directly above)
@@ -239,7 +245,7 @@ exports.create = function ($container, list, opts) {
// of items.
prototype.init();
if (opts.filter.onupdate) {
if (opts.filter && opts.filter.onupdate) {
opts.filter.onupdate();
}
}
@@ -288,7 +294,7 @@ exports.create = function ($container, list, opts) {
opts.parent_container.on("click", "[data-sort]", exports.handle_sort);
}
if (opts.filter.element) {
if (opts.filter && opts.filter.element) {
opts.filter.element.on(opts.filter.event || "input", function () {
const self = this;
const value = self.value.toLocaleLowerCase();