Skip to content

Commit

Permalink
bug 1749561: fix supersearch form and refining search
Browse files Browse the repository at this point in the history
This fixes the javascript parts of the supersearch form when you go to
refine the search.

It correctly removes the "exists" filters if they exist for the field.
It no longer messes up adding a filter for mac_crash_info or other
fields where the values get coerced into other things.

It still adds filters for product, product_type, platform, and version
rather than adding those to the simple form. I thought about trying to
figure that out and then decided it's not _critical_, so I'd leave it.
  • Loading branch information
willkg committed Jan 20, 2022
1 parent ba21c81 commit f19412a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
* Create a new dynamic form or run an action on an existing form.
*
* Actions:
* removeLine - Remove a line from the dynamic form that has a specified
* field, operator, and value
* newLine - Add a new line to this dynamic form
* getParams - Return an object with the content of this dynamic form
* setParams - Change the content of this dynamic form
Expand Down Expand Up @@ -75,12 +77,16 @@
return Object.keys(OPERATORS);
}

if (action === 'newLine' || action === 'getParams' || action === 'setParams') {
if (action === 'removeLine' || action === 'newLine' || action === 'getParams' || action === 'setParams') {
if (!dynamic) {
throw new Error('Impossible to call ' + action + ' on an object that was not initialized first');
}

if (action === 'newLine') {
if (action === 'removeLine') {
if (initialParams) {
return dynamic.removeLine(initialParams.field, initialParams.operator, initialParams.value);
}
} else if (action === 'newLine') {
if (initialParams) {
// there is some data, this should not be a blank line
return dynamic.createLine(initialParams.field, initialParams.operator, initialParams.value);
Expand Down Expand Up @@ -298,6 +304,29 @@
lines.push(line);
}

/**
* Remove a single line matching field/operator/value criteria from the
* form if it exists.
*/
function removeLine(field, operator, value) {
// Find the FormLine in question and remove it if it exists
for (var i in lines) {
var line = lines[i];
var filter = line.get();

// Skip lines that have a null filter
if (filter === null) {
continue;
}

// If everything matches, remove this one line and stop iterating
if (filter.field === field && filter.operator === operator && filter.value === value) {
line.remove();
return;
}
}
}

/**
* Reset this form by removing all lines.
*/
Expand Down Expand Up @@ -557,6 +586,7 @@
// Expose the public functions of this form so the context is kept.
form.data('dynamic', {
newLine: newLine,
removeLine: removeLine,
createLine: createLine,
getParams: getParams,
setParams: setParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ $(function () {
});
}

/**
* Remove any "exists" filters by field name.
*/
function removeExists(name) {
form.dynamicForm('removeLine', {
field: name,
operator: '!__null__',
value: '',
});
}

/**
* Handler for browser history state changes.
*/
Expand Down Expand Up @@ -503,7 +514,12 @@ $(function () {
contentElt.on('click', '.term', function (e) {
e.preventDefault();

addTerm($(this).data('field'), $(this).data('content'));
// First remove any existing filters for this field
removeExists($(this).data('field'));

// Using .attr() here prevents the value from being converted
var contentVal = $(this).attr('data-content');
addTerm($(this).data('field'), contentVal);

// And then run the new, corresponding search.
search();
Expand Down

0 comments on commit f19412a

Please sign in to comment.