Skip to content

Commit

Permalink
Merge pull request #604 from marmelab/debounce_filter
Browse files Browse the repository at this point in the history
[RFR] Add debounce to the filters
  • Loading branch information
fzaninotto committed Aug 22, 2015
2 parents eb30bc1 + a10a843 commit 6a5eb30
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/javascripts/ng-admin/Crud/filter/maFilterController.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from 'underscore';

/**
*
* @param {$scope} $scope
Expand All @@ -14,11 +16,11 @@ function maFilterController($scope, $state, $stateParams) {
this.$state = $state;
this.$stateParams = $stateParams;
this.$scope.values = this.$scope.values() || {};
$scope.$watch('values', (newValues, oldValues) => {
$scope.$watch('values', _.debounce((newValues, oldValues) => {
if (newValues != oldValues) {
this.filter(); // FIXME use debounce
this.filter();
}
}, true)
}, 500), true);
this.$scope.filters = this.$scope.filters;
this.$scope.datastore = this.$scope.datastore();
this.isFilterEmpty = isEmpty(this.$scope.values);
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function routing($stateProvider) {
filterData: ['ReadQueries', 'view', function (ReadQueries, view) {
return ReadQueries.getAllReferencedData(view.getFilterReferences(false));
}],
filterEntries: ['dataStore', 'view', 'filterData', function (dataStore, view, filterData) {
filterEntries: ['dataStore', 'view', 'filterData', 'Entry', function (dataStore, view, filterData, Entry) {
var filters = view.getFilterReferences(false);
var filterEntries;

Expand Down
23 changes: 19 additions & 4 deletions src/javascripts/test/e2e/filterViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ describe('List filter', function () {
describe('text filter', function() {
it('should filter globally', function () {
// Filter globally for 'rabbit'
$$('.filters .filter:nth-child(1) input').sendKeys('rabbit');
$$('.grid tr td:nth-child(4)').then(function (tdElements) {
$$('.filters .filter:nth-child(1) input').sendKeys('rabbit')
.then(function() {
return browser.driver.sleep(600); // debounce delay
})
.then(function() {
return $$('.grid tr td:nth-child(4)');
})
.then(function (tdElements) {
expect(tdElements.length).toBe(1);
expect(tdElements[0].getText()).toBe('White Rabbit: it was indeed: she was out of the gr...');
});
Expand All @@ -105,8 +111,14 @@ describe('List filter', function () {
});

it('should not filter when empty', function () {
$$('.filters .filter:nth-child(1) input').clear();
$$('.grid tr td:nth-child(4)').then(function (tdElements) {
$$('.filters .filter:nth-child(1) input').clear()
.then(function() {
return browser.driver.sleep(600); // debounce delay
})
.then(function() {
return $$('.grid tr td:nth-child(4)');
})
.then(function (tdElements) {
expect(tdElements.length).toBe(10);
});
});
Expand All @@ -120,6 +132,7 @@ describe('List filter', function () {
element(by.css('.filters .ui-select-placeholder')).click();
element(by.css('.filters .ui-select-search')).sendKeys('Perspi');
element(by.css('#ui-select-choices-row-0-0')).click();
browser.driver.sleep(600); // debounce delay
$$('.grid tr td:nth-child(4)').then(function (tdElements) {
expect(tdElements.length).toBe(2);
expect(tdElements[0].getText()).toBe('I\'d been the whiting,\' said the Hatter, it woke up...');
Expand All @@ -139,6 +152,7 @@ describe('List filter', function () {
it('should reset page number', function () {
// Filter globally for 'I'
$$('.filters .filter:nth-child(1) input').sendKeys('I');
browser.driver.sleep(600); // debounce delay
$$('ma-datagrid-pagination .total').then(function (totalElements) {
expect(totalElements[0].getText()).toBe('1 - 10 on 11');
});
Expand All @@ -149,6 +163,7 @@ describe('List filter', function () {
// Filter globally for 'be'
$$('.filters .filter:nth-child(1) input').clear();
$$('.filters .filter:nth-child(1) input').sendKeys('be');
browser.driver.sleep(600); // debounce delay
$$('ma-datagrid-pagination .total').then(function (totalElements) {
expect(totalElements[0].getText()).toBe('1 - 5 on 5');
});
Expand Down

0 comments on commit 6a5eb30

Please sign in to comment.