On a webpage there are multiple select tags that hides or shows table rows.
The logic is implemented in javascript with multiple if/else if - conditionals and I'm looking for a way to simplify it:
if(
(validPublication == true)
&& (productPublication > 0)
&& (genre == -1 || $(this).data('genre') == genre)
&& identificator_shared == false
&& (format == 'foo' && $(this).data('foo') == true)
&& (identificator_version == -1 || $(this).data('topic_version') == identificator_version)
) {
$('#filter_topic_version_eq').removeAttr('disabled');
$('#filter_identificator_version_eq').removeAttr('disabled');
$('#filter_genre_eq').removeAttr('disabled');
$('#filter_format_eq').removeAttr('disabled');
$(this).show();
if ($(this).data('product')) {
products.push($(this).data('product'));
}
}
else if(
(validPublication == true)
&& (productPublication > 0)
&& (genre == -1 || $(this).data('genre') == genre)
// if format foo && drvt_shared=true dann alle Versionen anzeigen
&& (format == 'foo' && (identificator_shared == true))
&& ($(this).data('foo') == true)
) {
$('#filter_topic_version_eq').removeAttr('disabled');
$('#filter_identificator_version_eq').removeAttr('disabled');
$('#filter_format_eq').removeAttr('disabled');
$('#filter_genre_eq').removeAttr('disabled');
$(this).show();
if ($(this).data('product')) {
products.push($(this).data('product'));
}
}
else if(
(validPublication == true)
&& (productPublication > 0)
&& (genre == -1 || $(this).data('genre') == genre)
&& (format == 'bar' && (identificator_shared == false))
&& ($(this).data('bar') == true)
&& (identificator_version == -1 || $(this).data('topic_version') == identificator_version)
) {
$('#filter_format_eq').removeAttr('disabled');
$('#filter_genre_eq').removeAttr('disabled');
$('#filter_topic_version_eq').removeAttr('disabled');
$('#filter_identificator_version_eq').removeAttr('disabled');
$(this).show();
if ($(this).data('product')) {
products.push($(this).data('product'));
}
}
... and so on
I don't think using switch statements would be a huge improvement.
=== UPDATE: In the comments the advice to extract the if condition in a separate function was given.
Instead of
if (validPublication == true)
&& (productPublication > 0)
&& (genre == -1 || $(this).data('genre') == genre)
&& identificator_shared == false
&& (format == 'foo' && $(this).data('foo') == true)
&& (identificator_version == -1 || $(this).data('topic_version') == identificator_version)
)
I should use:
function foo(validPublication, productPublication, genre,identificator_shared,format,identificator_version) {
if(
(validPublication == true)
&& (productPublication > 0)
&& (genre == -1 || $(this).data('genre') == genre)
&& identificator_shared == false
&& (format == 'foo' && $(this).data('foo') == true)
&& (identificator_version == -1 || $(this).data('topic_version') == identificator_version)
) {
return true
}
}
so the main conditional looks like this:
if foo(validPublication, productPublication, genre,identificator_shared,format,identificator_version)
else if(bar())
else if(baz())
Do I understand it correctly?
ifbecomes thenif (somethingUnderstandable(x,y,z)) ... else if (somethingElse()) ....