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. #1322

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c43cc48
MNT Refactor group tests into their own feature.
GuySartorelli Jun 13, 2022
829b17d
MNT add new test for Group title validation
GuySartorelli Jun 13, 2022
1f10403
extend lazy-loadable behavior
lekoala May 23, 2022
c691a26
fix selector
lekoala May 23, 2022
4127139
update doc
lekoala May 23, 2022
33a53e9
Update client/src/legacy/GridField.js
lekoala May 23, 2022
3419492
fix jquery + remove once
lekoala May 24, 2022
6efe864
refactor tabset
lekoala May 24, 2022
1029b6d
fix undefined el
lekoala May 24, 2022
386f4f2
consistent quotes
lekoala May 24, 2022
81ba88b
better var name
lekoala May 24, 2022
91e79d7
Run yarn build
GuySartorelli May 30, 2022
6ce078f
missing ,
lekoala May 30, 2022
b2f1d72
Update client/src/legacy/TabSet.js
lekoala May 31, 2022
0b1dc98
Update client/src/legacy/TabSet.js
lekoala May 31, 2022
3a9b2be
Update client/src/legacy/TabSet.js
lekoala May 31, 2022
6874b11
simplify PR
lekoala May 31, 2022
3c87c2a
Update client/src/legacy/TabSet.js
lekoala Jun 21, 2022
2df3a1e
Update client/src/legacy/TabSet.js
lekoala Jun 21, 2022
d2e2c1b
Update client/src/legacy/TabSet.js
lekoala Jun 21, 2022
2ca3648
Update client/src/legacy/TabSet.js
lekoala Jun 21, 2022
3914334
Update client/src/legacy/TabSet.js
lekoala Jun 21, 2022
ff9248b
Update client/src/legacy/TabSet.js
lekoala Jun 21, 2022
c570ecc
move lazyLoadGridFields
lekoala Jun 21, 2022
e7023e3
MNT Build bundle
GuySartorelli Jun 21, 2022
19e1ea4
LeftAndMain - Extended batch actions form
mooror Jul 5, 2022
5d32154
Built css and javascript bundles
nccomput Jul 5, 2022
27568eb
LeftAndMain - Added missing use statement
Jul 6, 2022
5df3748
JS - Fixed variable naming issues and rebuilt
Jul 6, 2022
7641a6f
Javascript - Code quality changes
mooror Jul 7, 2022
decddc6
Javascript - Reset fields before displaying them
mooror Jul 7, 2022
c795d9b
styles - Replaced ID selector + Changed nesting
mooror Jul 7, 2022
71d1d39
LeftAndMain - Removed changes to BatchActionsForm
mooror Jul 7, 2022
8f07875
Built css and javascript bundles
mooror Jul 7, 2022
f135f89
ENH Show Search field if any keywords are presented
Jul 7, 2022
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.

2 changes: 1 addition & 1 deletion client/dist/styles/bundle.css

Large diffs are not rendered by default.

83 changes: 62 additions & 21 deletions client/src/legacy/GridField.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ $.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').length === 0) || (this.data('gridfield-lazy-load-state') === 'force') )
(this.closest('.ss-tabset, .cms-tabset').length === 0) || (this.data('gridfield-lazy-load-state') === 'force') )
) {
// If our GridField is not inside a tabset for an immidiate reload
this.data('gridfield-lazy-load-state', 'ready');
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);
Comment on lines +205 to +206
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure we're not just throwing away other GET vars that were already present

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I'll check parameters that we pass in GridField.php class. Since, here I just grab all search params from scheme, that we also pass to Edit button and as URL for click. So, would be better to be sure that we have a correct value in the scheme.
What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the URL for the gridfield we're currently looking at - which needs to include any GET parameters that are already in the URL.
So for example if I'm on some page /admin/pages/edit/show/2?some-var=some-value and I click paginate on a gridfield, the some-var=some-value shouldn't be removed. These may be here for any reason beyond gridfield, and removing them may cause problems in some project-specific code that relies on them for whatever reason.

The edit and view buttons don't need these, because they're relevant only while you're on the page that holds the gridfield, not for the item edit form.

}
})
}
}
});

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
30 changes: 28 additions & 2 deletions client/src/legacy/LeftAndMain.BatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,39 @@ $.entwine('ss.tree', function($){
onchange: function(e) {
var form = $(e.target.form),
btn = form.find(':submit'),
selected = $(e.target).val();
selected = $(e.target).val(),
actionUrlParts = selected.split('/'),
actionName = actionUrlParts[actionUrlParts.length - 1];

// Refresh selected / enabled nodes
$('#Form_BatchActionsForm').refreshSelected();

// Process action parameter fields
var parameterFields = $('#BatchActionParameters_' + actionName);
if (parameterFields.length) {
// Reset fields to default values before displaying them
parameterFields.find(':input').each(function () {
var input = $(this)[0];
if (input.tagName === 'SELECT') {
input.selectedIndex = -1;
$(this).trigger('chosen:updated');
} else if (input.type === 'checkbox') {
input.checked = input.defaultChecked;
} else {
input.value = input.defaultValue;
}
});

// Hide / display action parameter fields
parameterFields.siblings().hide();
parameterFields.show();
$('#BatchActionParameters').slideDown();
} else {
$('#BatchActionParameters').slideUp();
}

// TODO Should work by triggering change() along, but doesn't - entwine event bubbling?
this.trigger("chosen:updated");
this.trigger('chosen:updated');

this._super(e);
}
Expand Down
76 changes: 52 additions & 24 deletions client/src/legacy/TabSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,56 @@ $.entwine('ss', function($){
this._super();
},

onadd: function () {
this.on(
'tabsactivate',
function (event, { newPanel }) {
this.lazyLoadGridFields(newPanel);
this.triggerLazyLoad(newPanel);
}.bind(this)
);
this.on(
'tabscreate',
function (event, { panel }) {
this.lazyLoadGridFields(panel);
this.triggerLazyLoad(panel);
}.bind(this)
);
this._super();
},

/**
* @func triggerLazyLoad
* @desc Find all the lazy loadable fields in the panel and trigger their reload.
* @param {Object} panel
* @param {string} selector
*/
triggerLazyLoad: function (panel, selector = '.lazy-loadable') {
panel.find(selector).each((idx, el) => {
var $el = $(el);
var lazyEvent = el.dataset.lazyEvent || 'lazyload';
if ($el.closest('.ss-tabset, .cms-tabset').is(this)) {
// This should be listened only once
el.dispatchEvent(new Event(lazyEvent));
}
});
},

/**
* @func lazyLoadGridFields
* @desc Find all the lazy loadable gridfield in the panel and trigger their reload.
* @param {Object} panel
*/
lazyLoadGridFields: function(panel) {
panel.find('.grid-field--lazy-loadable').each((i, el) => {
const gridfield = $(el);
// Avoid triggering all gridfields when using nested tabs
if (gridfield.closest('.ss-tabset, .cms-tabset').is(this)) {
$(el).lazyload();
}
});
},

/**
* @func openTabFromURL
* @param {string} hash
Expand All @@ -44,24 +94,17 @@ $.entwine('ss', function($){
$(() => {
$trigger.click();
});
},
}

}),

/**
/**
* Lightweight wrapper around jQuery UI tabs for generic tab set-up
*/
$('.ss-tabset').entwine({
IgnoreTabState: false,

onadd: function() {
this.on("tabsactivate", (function (event, {newPanel}) {
this.lazyLoadGridFields(newPanel);
}).bind(this));
this.on("tabscreate", (function(event, {panel}) {
this.lazyLoadGridFields(panel);
}).bind(this));

// Can't name redraw() as it clashes with other CMS entwine classes
this.redrawTabs();
this._super();
Expand Down Expand Up @@ -95,21 +138,6 @@ $.entwine('ss', function($){
if(!matches) return;
$(this).attr('href', document.location.href.replace(/#.*/, '') + matches[0]);
});
},

/**
* @func lazyLoadGridFields
* @desc Find all the lazy loadable gridfield in the panel and trigger their reload.
* @param {Object} panel
*/
lazyLoadGridFields: function(panel) {
panel.find('.grid-field--lazy-loadable').each((i, el) => {
const gridfield = $(el);
// Avoid triggering all gridfields when using nested tabs
if (gridfield.closest('.ss-tabset, .cms-tabset').is(this)) {
$(el).lazyload();
}
});
}

});
Expand Down
19 changes: 19 additions & 0 deletions client/src/styles/legacy/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,25 @@ html, body {
border-bottom-left-radius: 0;
width: calc(#{$grid-gutter-width} + 1px); // gutters + 1px border
}

// Action parameter fields
.action-parameters {
.form__field-description {
margin-bottom: 0;
}

.field {
height: auto;
}

.field:last-child {
margin-bottom: 0;
}

.form__field-holder--no-label {
padding-right: 0;
}
}
}

/** --------------------------------------------
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
Loading