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

ENH Keep State and show Search field if any keywords are presented #1326

Merged
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
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

113 changes: 92 additions & 21 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') )
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
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,44 @@ $.entwine('ss', function($) {
if (hasLegacyFilterHeader) {
this.find('.sortable-header th:last').html(content);
}
},

keepStateInHistory: function() {
const newSchema = $(this).find('.gridfield-actionmenu__container').data('schema');
if (newSchema && newSchema.length > 0) {
newSchema.filter( e => {
if (e.type === 'link') {
const reqString = this.buildURLString(e.url)
const searchParam = reqString ? '?' + reqString.join('&') : '';
window.ss.router.replace(window.location.pathname + searchParam, undefined, undefined, false);
}
})
}
},

/**
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
* A params string can have duplicate keys.
* Function buildURLString splits string to "key => value" array
* and replaces values for existing keys with the new value
* and returns string with unique keys only
*
* @param {string} url
* @returns string
*/
buildURLString: function(url) {
const link = [window.location.origin, url].join('/');
const params = [window.location.search, (new URL(link)).searchParams.toString()].join('&').substring(1);
const newrequest = [];
const reqString = [];
params.split('&').forEach(param => {
const newVal = param.split('=');
newrequest[newVal[0]] = newVal[1] ? newVal[1] : '';
});
Object.keys(newrequest).forEach(param => {
reqString.push([param, newrequest[param]].join('='));
});
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved

return reqString;
}
});

Expand Down Expand Up @@ -370,17 +422,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 +439,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 @@ -619,7 +671,12 @@ $.entwine('ss', function($) {
});
}

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

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

search(data) {
Expand All @@ -646,7 +703,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 +791,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 +849,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
73 changes: 62 additions & 11 deletions tests/behat/features/gridfield-search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,72 @@ 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" "Vitol" with "Category"="Other"
And I am logged in with "ADMIN" permissions
And I go to "/admin/test"

Scenario: I can search and go to item
When I press the "Open search and filter" button
And I press the "Advanced" button
And I press the "Advanced" button
Then I should see a "#Form_CompaniesSearchForm_Search_Name.no-change-track" element
And I fill in "SearchBox__Name" with "Walmart"
And I press the "Enter" key in the "SearchBox__Name" field
And I fill in "SearchBox__Name" with "Walmart"
And I press the "Enter" key in the "SearchBox__Name" field
Then I should see "Walmart" in the "#Form_EditForm" element
But I should not see "ExxonMobil" in the ".col-Name" element
And I should not see "Vitol" in the ".col-Name" element
And I click "Walmart" in the "#Form_EditForm" element
Then I should see "Walmart"
And I should see "Walmart" in the ".breadcrumbs-wrapper" element

Scenario: I can navigate back and forward through the GridField
When I am on "/admin/pages"
Then I go to "/admin/test"
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 ".col-Name" element
And I should not see "Vitol" in the ".col-Name" element
When I move backward one page
And I should see "Pages"
When I move forward one page
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
But I should not see "ExxonMobil" in the ".col-Name" element
And I should not see "Vitol" in the ".col-Name" element
When I click "Walmart" in the "#Form_EditForm" element
Then I should see "Walmart"
And I should see "Walmart" in the ".breadcrumbs-wrapper" element
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" element
Then I click on the ".toolbar__back-button" element
And I should see "Walmart" in the "#Form_EditForm" element
But I should not see "ExxonMobil" in the ".col-Name" element
And I should not see "Vitol" in the ".col-Name" element
And I should see an "#SilverStripe-FrameworkTest-Model-CompanySearch_searchbox" element
When I move backward one page
Then I click "Employees" in the ".ui-tabs-nav" element
Then I should see "Alen"
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" element
When I move forward one page
And I should see "Walmart" in the "#Form_EditForm" element
But I should not see "ExxonMobil" in the ".col-Name" element
And I should not see "Vitol" in the ".col-Name" element
And I should see an "#SilverStripe-FrameworkTest-Model-CompanySearch_searchbox" element