Skip to content

Commit

Permalink
added the possibility to store userData while saving grid state (ml…
Browse files Browse the repository at this point in the history
…eibman#627)

* added the possibility to store `userData` while saving grid state

* minor code readability enhancement

* resolved an issue with the `save` function being executed from `notify`ed events - in this case, userData is actually the triggering `jQuery.Event`

* improved userData storage in slick-grid state

* improved userData storage in slick-grid state

Co-authored-by: arash dalir <[email protected]>
  • Loading branch information
arashdalir and arash dalir authored Sep 8, 2021
1 parent d634221 commit 46d26d6
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions plugins/slick.state.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
_store = options.storage,
onStateChanged = new Slick.Event();

var userData = {
state: null,
current: null
};

function init(grid) {
_grid = grid;
_cid = grid.cid || options.cid;
Expand All @@ -75,8 +80,14 @@
var state = {
sortcols: getSortColumns(),
viewport: _grid.getViewport(),
columns: getColumns()
columns: getColumns(),
userData: null
};

state.userData = userData.current;

setUserDataFromState(state.userData);

onStateChanged.notify(state);
return _store.set(options.key_prefix + _cid, state);
}
Expand All @@ -89,7 +100,7 @@

_store.get(options.key_prefix + _cid)
.then(function success(state) {
if (state) {
if (state) {
if (state.sortcols) {
_grid.setSortColumns(state.sortcols);
}
Expand Down Expand Up @@ -119,12 +130,66 @@

_grid.setColumns(state.columns);
}
setUserDataFromState(state.userData);
}
dfd.resolve(state);
}, dfd.reject);
});
}

/**
* allows users to add their own data to the grid state
* this function does not trigger the save() function, so the actual act of writing the state happens in save()
* therefore, it's necessary to call save() function after setting user-data
*
* @param data
* @return {State}
*/
function setUserData(data){
userData.current = data;

return this;
}

/**
*
* @internal
* @param data
* @return {State}
*/
function setUserDataFromState(data){
userData.state = data;
return setUserData(data);
}

/**
* returns current value of user-data
* @return {Object}
*/
function getUserData(){
return userData.current;
}

/**
* returns user-data found in saved state
*
* @return {Object}
*/
function getStateUserData(){
return userData.state;
}

/**
* sets user-data to the value read from state
*
* @return {State}
*/
function resetUserData(){
userData.current = userData.state;

return this;
}

function getColumns() {
return $.map(_grid.getColumns(), function(col) {
return {
Expand All @@ -141,6 +206,7 @@

function reset(){
_store.set(options.key_prefix + _cid, {});
setUserDataFromState(null);
}
/*
* API
Expand All @@ -149,6 +215,10 @@
"init": init,
"destroy": destroy,
"save": save,
"setUserData": setUserData,
"resetUserData": resetUserData,
"getUserData": getUserData,
"getStateUserData": getStateUserData,
"restore": restore,
"onStateChanged": onStateChanged,
"reset": reset
Expand Down

0 comments on commit 46d26d6

Please sign in to comment.