Skip to content

Commit

Permalink
fix(saveState): Fixes #4146 - Allow saving of pagination state
Browse files Browse the repository at this point in the history
  • Loading branch information
David Jellesma committed Feb 2, 2016
1 parent 47c77de commit c6d3b2a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/features/saveState/js/saveState.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@
savedState.selection = service.saveSelection( grid );
savedState.grouping = service.saveGrouping( grid );
savedState.treeView = service.saveTreeView( grid );
savedState.pagination = service.savePagination( grid );

return savedState;
},
Expand Down Expand Up @@ -323,6 +324,10 @@
service.restoreTreeView( grid, state.treeView );
}

if ( state.pagination ){
service.restorePagination( grid, state.pagination );
}

grid.refresh();
},

Expand Down Expand Up @@ -474,6 +479,26 @@
},


/**
* @ngdoc function
* @name savePagination
* @methodOf ui.grid.saveState.service:uiGridSaveStateService
* @description Saves the pagination state, if the pagination feature is enabled
* @param {Grid} grid the grid whose state we'd like to save
* @returns {object} the pagination state ready to be saved
*/
savePagination: function( grid ) {
if ( !grid.api.pagination || !grid.options.paginationPageSize ){
return {};
}

return {
paginationCurrentPage: grid.options.paginationCurrentPage,
paginationPageSize: grid.options.paginationPageSize
};
},


/**
* @ngdoc function
* @name saveTreeView
Expand Down Expand Up @@ -698,6 +723,25 @@
grid.api.treeView.setTreeView( treeViewState );
},

/**
* @ngdoc function
* @name restorePagination
* @methodOf ui.grid.saveState.service:uiGridSaveStateService
* @description Restores the pagination information, if pagination is enabled.
* @param {Grid} grid the grid whose state we'd like to restore
* @param {object} pagination the pagination object to be restored
* @param {number} pagination.paginationCurrentPage the page number to restore
* @param {number} pagination.paginationPageSize the number of items displayed per page
*/
restorePagination: function( grid, pagination ){
if ( !grid.api.pagination || !grid.options.paginationPageSize ){
return;
}

grid.options.paginationCurrentPage = pagination.paginationCurrentPage;
grid.options.paginationPageSize = pagination.paginationPageSize;
},

/**
* @ngdoc function
* @name findRowByIdentity
Expand Down
56 changes: 56 additions & 0 deletions src/features/saveState/test/saveState.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
});
});

describe('savePagination', function() {
beforeEach(function() {
grid.options.paginationPageSize = 25;
grid.options.paginationCurrentPage = 2;
grid.api.pagination = true;
});

it('saves paginationCurrentPage', function() {
expect(uiGridSaveStateService.savePagination( grid ) ).toEqual({
paginationCurrentPage: 2,
paginationPageSize: 25
});
});

});


describe('saveScrollFocus', function() {
it('does nothing when no cellNav module initialized', function() {
Expand Down Expand Up @@ -482,6 +498,46 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
});
});

describe('restorePagination', function() {
var pagination = {
paginationCurrentPage: 2,
paginationPageSize: 25
};

describe('when pagination is on', function() {
beforeEach(function() {
grid.options.paginationPageSize = 1;
grid.api.pagination = true;
uiGridSaveStateService.restorePagination( grid, pagination );
});

it('sets the paginationPageSize', function() {
expect(grid.options.paginationPageSize).toEqual(25);
});

it('sets the paginationCurrentPage', function() {
expect(grid.options.paginationCurrentPage).toEqual(2);
});
});

describe('when pagination is off', function() {
beforeEach(function() {
grid.api.pagination = false;
uiGridSaveStateService.restorePagination( grid, pagination );
});

it('does not modify paginationPageSize', function() {
expect(grid.options.paginationPageSize).toBeUndefined();
});

it('does not modify paginationCurrentPage', function() {
expect(grid.options.paginationCurrentPage).toBeUndefined();
});


});
});


describe('restoreScrollFocus', function() {
it('does nothing when no cellNav module initialized', function() {
Expand Down

0 comments on commit c6d3b2a

Please sign in to comment.