Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/ADF-1811/group-conditions-logic #605

Open
wants to merge 5 commits into
base: feat/HKD-6/integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/searchModal/advancedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export default function advancedSearchFactory(config) {
text: 'text',
list: 'list'
};
const criteriaLogic = {
and: 'LOGIC_AND',
or: 'LOGIC_OR',
not: 'LOGIC_NOT',
}

let isAdvancedSearchStatusEnabled;
let isCriteriaListUpdated = false;
Expand Down Expand Up @@ -127,6 +132,7 @@ export default function advancedSearchFactory(config) {
_.forEach(criteriaState, criterion => {
criterion.rendered = false;
criterion.value = null;
criterion.logic = null;
});
},
/**
Expand All @@ -147,8 +153,11 @@ export default function advancedSearchFactory(config) {
}
} else if (renderedCriterion.type === criteriaTypes.list) {
if (renderedCriterion.value && renderedCriterion.value.length > 0) {
/* Temp replaced OR with AND. See ADF-7 for details */
query += `${queryParam}:${renderedCriterion.value.join(' AND ')}`;
if(renderedCriterion.value.length === 1 && renderedCriterion.logic === criteriaLogic.not) {
//we have to pass NOT logic anyways, so add empty member to have NOT logic modifier in the query
renderedCriterion.value.push('');
}
query += renderedCriterion.value.map(value=>`${queryParam}:${value}`).join(` ${renderedCriterion.logic} `);
}
}
});
Expand Down Expand Up @@ -431,10 +440,16 @@ export default function advancedSearchFactory(config) {
if (criterion.value) {
$(`input[name=${criterion.id}-select]`, $criterionContainer).select2('data', initialCriterion);
}
criterion.logic = criterion.logic || criteriaLogic.and;
$(`input[name="${criterion.id}-logic"][value="${criterion.logic}"]`, $criterionContainer).prop('checked', true);
// set event to bind input value to critariaState
$(`input[name=${criterion.id}-select]`, $criterionContainer).on('change', event => {
criterion.value = event.val;
});
// set event to bind logic selector to critariaState
$(`input[name="${criterion.id}-logic"]`, $criterionContainer).on('change', event => {
criterion.logic = event.target.value;
});
} else {
// set initial value
if (criterion.value) {
Expand Down
8 changes: 7 additions & 1 deletion src/searchModal/scss/advancedSearch.scss
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ $invalidCriteriaBorder: #266d9c;
margin-left: 0;
}
}
label {
div.criterion-container {
width: 100%;
padding: 0;
margin: 0;
Expand Down Expand Up @@ -111,6 +111,12 @@ $invalidCriteriaBorder: #266d9c;
width: 100%;
}
}
.logic-radio-group {
display: flex;
justify-content: flex-start;
gap: 16px;
}

}
.invalid-criteria-warning-container {
background-color: $invalidCriteriaBackground;
Expand Down
15 changes: 13 additions & 2 deletions src/searchModal/tpl/list-select-criterion.tpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
<div class="filter-container {{criterion.id}}-filter" data-criteria="{{criterion.label}}" data-type="{{criterion.type}}">
<button class="icon-result-nok" aria-label="{{__ "Remove criteria"}}"></button>
<label>
<div class="criterion-container">
<span class="filter-label-text">{{criterion.label}}
{{#if criterion.isDuplicated}}
{{#if criterion.alias}}<span class="criteria-alias">({{criterion.alias}})</span>{{/if}}
{{#if criterion.class.label}}<span class="class-path">/ {{criterion.class.label}}</span>{{/if}}
{{/if}}
</span>
<div class="logic-radio-group">
<label class="logic-label">
<input type="radio" name="{{criterion.id}}-logic" value="LOGIC_AND"><span class="icon-radio"></span> {{__ "Exact match"}}
</label>
<label class="logic-label">
<input type="radio" name="{{criterion.id}}-logic" value="LOGIC_OR"><span class="icon-radio"></span> {{__ "In"}}
</label>
<label class="logic-label">
<input type="radio" name="{{criterion.id}}-logic" value="LOGIC_NOT"><span class="icon-radio"></span> {{__ "Not in"}}
</label>
</div>
<input type='text' name="{{criterion.id}}-select">
</label>
</div>
</div>
40 changes: 23 additions & 17 deletions test/searchModal/advancedSearch/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ define([
statusUrl: 'undefined/tao/AdvancedSearch/status'
});
const ready = assert.async();
assert.expect(9);
assert.expect(11);

instance.on('ready', function () {
assert.ok(instance.isEnabled(), 'the advanced search is enabled');
Expand All @@ -326,19 +326,12 @@ define([
const $criterionTextInput = $criteriaContainer.find('.inBothTextParentUri-filter input');
const $criterionSelectInput = $criteriaContainer.find('.inBothSelectParentUri-filter input');

// Checkboxes are temporary replaced with select2
// const $criterionListSelected = $criteriaContainer
// .find('.-filter input[type=checkbox]:checked')
// .get()
// .map(checkbox => {
// return checkbox.value;
// });

const $criterionListSelected = $criteriaContainer.find('.inBothListParentUri-filter input');

// check default value on each criterion type
await nextTick();
assert.equal($criterionTextInput.val(), 'default value0', 'text criterion correctly initialized');
await nextTick();
assert.deepEqual(
$criterionSelectInput.select2('val'),
['value0'],
Expand All @@ -354,11 +347,6 @@ define([
// update value on each criterion
$criterionTextInput.val('foo0').trigger('change');

// Checkboxes are temporary replaced with select2
// $criteriaContainer
// .find('.inBothListParentUri-filter input[type=checkbox][value=value2]')
// .prop('checked', true)
// .trigger('change');
$criteriaContainer.find('.inBothListParentUri-filter .select2-choices').click();
await nextTick(200);
$('.select2-results .select2-selected + * .select2-result-label').mouseup();
Expand All @@ -374,14 +362,32 @@ define([
['value1', 'value2'],
'list criteria correctly updated'
);

const query = instance.getAdvancedCriteriaQuery();
assert.equal(
query,
// TODO: change 2nd AND to OR when functionality is developed on BE
'inBothTextParentUri:foo0 AND inBothListParentUri:value1 AND value2 AND inBothSelectParentUri:value0',
'inBothTextParentUri:foo0 AND inBothListParentUri:value1 LOGIC_AND inBothListParentUri:value2 AND inBothSelectParentUri:value0',
'advanced search query is correctly built'
);

$criteriaContainer.find('input[name="inBothListParentUri-logic"][value="LOGIC_OR"]').prop('checked', true).change();
await nextTick(200);
const queryOr = instance.getAdvancedCriteriaQuery();
assert.equal(
queryOr,
'inBothTextParentUri:foo0 AND inBothListParentUri:value1 LOGIC_OR inBothListParentUri:value2 AND inBothSelectParentUri:value0',
'advanced search query with OR logic is correctly built'
);

$criteriaContainer.find('input[name="inBothListParentUri-logic"][value="LOGIC_NOT"]').prop('checked', true).change();
await nextTick(200);
const queryNot = instance.getAdvancedCriteriaQuery();
assert.equal(
queryNot,
'inBothTextParentUri:foo0 AND inBothListParentUri:value1 LOGIC_NOT inBothListParentUri:value2 AND inBothSelectParentUri:value0',
'advanced search query with NOT logic is correctly built'
);

instance.destroy();
ready();
});
Expand Down