Skip to content

Commit

Permalink
ENH Show Search field if any keywords are presented
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Jul 19, 2022
1 parent 3baacfb commit c768e97
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 24 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

81 changes: 61 additions & 20 deletions client/src/legacy/GridField.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ $.entwine('ss', function($) {
this.fixColumns();
this.injectSearchButton(false);
}

if (this.hasFilters()) {
this.injectSearchButton(true);
};

if (this.is('.grid-field--lazy-loadable') && (
(this.closest('.ss-tabset, .cms-tabset').length === 0) || (this.data('gridfield-lazy-load-state') === 'force') )
Expand Down Expand Up @@ -142,6 +146,16 @@ $.entwine('ss', function($) {
return JSON.parse(this.find(':input[name="' + this.data('name') + '[GridState]"]').val());
},

/**
* @returns {Boolean}
*/
hasFilters: function() {
if (this.getState().GridFieldFilterHeader) {
return true;
}
return false;
},

needsColumnFix: function() {
return (
this.find('.grid-field__filter-header, .grid-field__search-holder').length &&
Expand Down Expand Up @@ -180,6 +194,19 @@ $.entwine('ss', function($) {
if (hasLegacyFilterHeader) {
this.find('.sortable-header th:last').html(content);
}
},

keepStateInHistory: function() {
const newURLSceme = $(this).find('.gridfield-actionmenu__container').data('schema');
if (newURLSceme && newURLSceme.length > 0) {
newURLSceme.filter( e => {
if (e.type === 'link') {
const searchParam = e.url.includes('?') ? '?' + e.url.split('?')[1] : '';
const historyState = $.extend({}, {path: window.location.pathname + searchParam}, this.getState());
history.replaceState(historyState, '', window.location.pathname + searchParam);
}
})
}
}
});

Expand Down Expand Up @@ -370,17 +397,6 @@ $.entwine('ss', function($) {
triggerChange = false;
}

const successCallback = function(data, status, response) {
const messageText = response.getResponseHeader('X-Message-Text');
const messageType = response.getResponseHeader('X-Message-Type');
if (messageText && messageType) {
var formEditError = $("#Form_EditForm_error");
formEditError.addClass(messageType);
formEditError.html(messageText);
formEditError.show();
}
};

var data = [
{
name: this.attr('name'),
Expand All @@ -398,10 +414,21 @@ $.entwine('ss', function($) {
});
}

this.getGridField().reload(
{ data },
successCallback
);
const gridField = $(this).getGridField();
const successCallback = function(data, status, response) {
gridField.keepStateInHistory();

const messageText = response.getResponseHeader('X-Message-Text');
const messageType = response.getResponseHeader('X-Message-Type');
if (messageText && messageType) {
var formEditError = $("#Form_EditForm_error");
formEditError.addClass(messageType);
formEditError.html(messageText);
formEditError.show();
}
};

gridField.reload({ data }, successCallback );

e.preventDefault();
},
Expand Down Expand Up @@ -646,7 +673,12 @@ $.entwine('ss', function($) {
}
}

this.getGridField().reload({ data: ajaxData });
const gridField = $(this).getGridField();
const successCallback = function() {
gridField.keepStateInHistory();
};

gridField.reload({ data: ajaxData }, successCallback);
},

refresh() {
Expand Down Expand Up @@ -729,9 +761,13 @@ $.entwine('ss', function($) {
});
}

this.getGridField().reload({
data: ajaxData
});
const gridField = $(this).getGridField();
const successCallback = function() {
gridField.keepStateInHistory();
};

gridField.reload({ data: ajaxData }, successCallback);

return false;
}else{
filterbtn.addClass('hover-alike');
Expand Down Expand Up @@ -783,7 +819,12 @@ $.entwine('ss', function($) {

var gridfield = $(this).getGridField();
gridfield.setState('GridFieldPaginator', {currentPage: newpage});
gridfield.reload();

const successCallback = function() {
gridfield.keepStateInHistory();
};

gridfield.reload({}, successCallback);

return false;
}
Expand Down
39 changes: 36 additions & 3 deletions tests/behat/features/gridfield-search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ Feature: Search in GridField
So that I see proper result and don't see warning

Background:
Given the "Company" "Walmart"
And the "Company" "ExxonMobil"
And the "Company" "Test"
Given the "Company" "Walmart" with "Category"="Retail"
And the "Employee" "Alen" with "Company"="1"
And the "Employee" "Bill" with "Company"="1"
And the "Employee" "Ford" with "Company"="1"
And the "Company" "ExxonMobil" with "Category"="Oil"
And the "Company" "Test" with "Category"="Other"
And I am logged in with "ADMIN" permissions
And I go to "/admin/test"

Expand All @@ -22,3 +25,33 @@ Feature: Search in GridField
And I click "Walmart" in the "#Form_EditForm" element
Then I should see "Walmart"
And I should see "Walmart" in the ".breadcrumbs-wrapper" element

@run
Scenario: I can navigate back and forward through the GridField
When I press the "Open search and filter" button
And I press the "Advanced" button
And I fill in "Search__Category" with "Retail"
And I press the "Enter" key in the "Search__Category" field
Then I should see "Walmart" in the "#Form_EditForm" element
But I should not see "ExxonMobil" in the "#Form_EditForm" element
And I should not see "Test" in the "#Form_EditForm" element
And I click "Walmart" in the "#Form_EditForm" element
Then I should see "Walmart"
Then I click "Employees" in the ".ui-tabs-nav" element
And I press the "Open search and filter" button
And I press the "Advanced" button
And I fill in "Search__Name" with "Alen"
And I press the "Enter" key in the "Search__Category" field
Then I should see "Alen" in the "#Form_ItemEditForm_Employees" element
And I click "Alen" in the "#Form_ItemEditForm_Employees" element
And I click "Walmart" in the ".breadcrumbs-wrapper" element
And I click "Employees" in the ".ui-tabs-nav" element
Then I should see "Alen" in the "#Form_ItemEditForm_Employees" element
But I should not see "Bill" in the ".col-Name" element
And I should not see "Ford" in the ".col-Name" element
And I should see an "#EmployeesSearch_searchbox[value='Alen']" element
Then I click "Companies" in the ".breadcrumbs-wrapper" element
And I should see "Walmart" in the "#Form_EditForm" element
But I should not see "ExxonMobil" in the "#Form_EditForm" element
And I should not see "Test" in the "#Form_EditForm" element
And I should see an "#SilverStripe-FrameworkTest-Model-CompanySearch_searchbox" element

0 comments on commit c768e97

Please sign in to comment.