Skip to content

Commit

Permalink
feat(grids): manually saving and restoring state
Browse files Browse the repository at this point in the history
This commit introduces a new feature to manually save and restore a
grid's state using `ui-grid-save-state`. The feature is prototyped in
the Journal module and adds an entry to the utils dropdown.

State is added as an additional Grid service that can be imported and
used by any module that uses a UI-Grid.
  • Loading branch information
sfount committed May 30, 2017
1 parent 36e72c9 commit 69eec2c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 8 deletions.
6 changes: 4 additions & 2 deletions client/src/i18n/en/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"RESET_DEFAULTS":"Reset Defaults",
"RETURN":"Return",
"SAVE":"Save",
"SAVE_GRID_CONFIGURATION" : "Save grid configuration",
"SEARCH":"Search",
"SELECT_INVOICES":"Select Invoices",
"SUBMIT":"Submit",
Expand Down Expand Up @@ -101,6 +102,7 @@
"ITEMS_FULL":"There are no additional inventory items that be assigned to this invoice.",
"INVOICES":"Invoices",
"IMPORT_SUCCESS":"Successfully Imported",
"GRID_STATE_SUCCESS":"Grid settings saved",
"LOADING":"Loading",
"MANY_TRYS":"Many tries",
"NOT_CONFIGURED":"The accounts for this currency have not been configured yet.",
Expand Down Expand Up @@ -139,7 +141,7 @@
"ACCOUNT_NUMBER":"Account Number",
"ACCOUNT_TITLE":"Account Text",
"ACCOUNT_TYPE":"Account Type",
"ACTIVATE":"Activate",
"ACTIVATE":"Activate",
"ACTIVE":"Active",
"ADD":"Add",
"ADDRESS":"Address",
Expand Down Expand Up @@ -207,7 +209,7 @@
"DATE_FROM":"Start",
"DATE_REGISTRATION":"Registration Date",
"DATE_TO":"End",
"DEACTIVATE":"Deactivate",
"DEACTIVATE":"Deactivate",
"DEBIT":"Debit",
"DEBTOR_GROUP":"Debtor Group",
"DEBTOR_GROUP_FORM":"Form debtor group record",
Expand Down
4 changes: 3 additions & 1 deletion client/src/i18n/fr/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"RESET_DEFAULTS":"Réinitialiser par défaut",
"RETURN":"Retourne",
"SAVE":"Sauver",
"SAVE_GRID_CONFIGURATION" : "Enregister la configuration",
"SEARCH":"Rechercher",
"SELECT_INVOICES":"Sélectionner la(les) Facture(s)",
"SUBMIT":"Soumettre",
Expand Down Expand Up @@ -99,6 +100,7 @@
"GROUPS_DEBTOR":"Attribuez un nouveau groupe de débiteur parmi les options ci-dessous. Appuyez sur 'confirmer' pour une mise à jour permanente du groupe de débiteur du patient",
"GROUPS_PATIENT":"Attribuer le patient à des groupes parmi les options ci-dessous. Appuyez sur 'confirmer' pour une mise à jour permanente des groupes souscrites du patient.",
"IMPORT_SUCCESS":"Importation avec succes",
"GRID_STATE_SUCCESS":"Enregistrement avec succès",
"INVOICES":"Factures",
"ITEMS_FULL":"Il n'y a pas d'articles supplémentaires qui seront affectés à cette facture.",
"LOADING":"Chargement en cours",
Expand Down Expand Up @@ -139,7 +141,7 @@
"ACCOUNT_NUMBER":"Numéro de Compte",
"ACCOUNT_TITLE":"Libelle de Compte",
"ACCOUNT_TYPE":"Type de Compte",
"ACTIVATE":"Activer",
"ACTIVATE":"Activer",
"ACTIVE":"Actif",
"ADD":"Ajouter",
"ADDRESS":"Adresse",
Expand Down
2 changes: 1 addition & 1 deletion client/src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var bhima = angular.module('bhima', [
'bhima.controllers', 'bhima.services', 'bhima.directives', 'bhima.filters',
'bhima.components', 'bhima.routes', 'ui.bootstrap',
'pascalprecht.translate', 'ngStorage', 'chart.js',
'tmh.dynamicLocale', 'ngFileUpload', 'ui.grid',
'tmh.dynamicLocale', 'ngFileUpload', 'ui.grid', 'ui.grid.saveState',
'ui.grid.selection', 'ui.grid.autoResize', 'ui.grid.resizeColumns',
'ui.grid.edit', 'ui.grid.grouping', 'ui.grid.treeView', 'ui.grid.cellNav',
'ui.grid.pagination', 'ui.grid.moveColumns', 'ui.grid.exporter',
Expand Down
45 changes: 45 additions & 0 deletions client/src/js/services/grid/GridState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
angular.module('bhima.services')
.service('GridStateService', GridStateService);

GridStateService.$inject = [
'util', 'appcache', 'NotifyService'
];

// responsible for -
// - caching grid state seperately for each grid
// - hooking into the grid api to apply the default size only when the grid is ready
// - exposing the methods to save and restore grid state
function GridStateService(util, AppCache, Notify) {
/* @const */
var stateCacheKey = 'gridState';

function StateInstance(gridOptions, moduleCacheKey) {
this._cacheKey = moduleCacheKey.concat(stateCacheKey);
this._cache = new AppCache(this._cacheKey);

util.after(gridOptions, 'onRegisterApi', function onRegisterApi(api) {
this._gridApi = api;

this._gridApi.core.on.rowsRendered(null, util.once(function () {
this.restoreGridState();
}.bind(this)));
}.bind(this));

this.saveGridState = saveGridState.bind(this);
this.restoreGridState = restoreGridState.bind(this);
}

function saveGridState() {
if (this._gridApi) {
this._cache.gridState = this._gridApi.saveState.save();
Notify.success('FORM.INFO.GRID_STATE_SUCCESS');
}
};

function restoreGridState() {
if (this._gridApi && this._cache.gridState) {
this._gridApi.saveState.restore(null, this._cache.gridState);
}
};
return StateInstance;
}
12 changes: 10 additions & 2 deletions client/src/modules/journal/journal.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@
<ul uib-dropdown-menu role="menu" class="dropdown-menu-right">
<li role="menuitem">
<a href data-method="configure" ng-click="JournalCtrl.openColumnConfigModal()">
<i class="fa fa-cogs"></i> <span translate>FORM.BUTTONS.CONFIGURE</span>
<i class="fa fa-columns"></i> <span translate>FORM.LABELS.COLUMNS</span>
</a>
</li>

<li role="menuitem">
<a href data-method="save-state" ng-click="JournalCtrl.saveGridState()">
<i class="fa fa-save"></i> <span translate>FORM.BUTTONS.SAVE_GRID_CONFIGURATION</span>
</a>
</li>

<li role="separator" class="divider"></li>
<li role="menuitem">
<a href ng-click="JournalCtrl.openTrialBalanceModal()" data-method="trial">
Expand Down Expand Up @@ -121,7 +128,8 @@
ui-grid-cellNav
ui-grid-edit
ui-grid-grouping
ui-grid-exporter>
ui-grid-exporter
ui-grid-save-state>

<bh-grid-loading-indicator
loading-state="JournalCtrl.loading"
Expand Down
9 changes: 7 additions & 2 deletions client/src/modules/journal/journal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ JournalController.$inject = [
'SessionService', 'NotifyService', 'TransactionService', 'GridEditorService',
'bhConstants', '$state', 'uiGridConstants', 'ModalService', 'LanguageService',
'AppCache', 'Store', 'uiGridGroupingConstants', 'ExportService', 'FindEntityService',
'FilterService', '$rootScope', '$filter', '$translate', 'GridExportService', 'TransactionTypeService', '$scope',
'FilterService', '$rootScope', '$filter', '$translate', 'GridExportService',
'TransactionTypeService', 'GridStateService', '$scope',
];

/**
Expand All @@ -34,14 +35,15 @@ function JournalController(Journal, Sorting, Grouping,
Filtering, Columns, Config, Session, Notify, Transactions, Editors,
bhConstants, $state, uiGridConstants, Modal, Languages, AppCache, Store,
uiGridGroupingConstants, Export, FindEntity, Filters, $rootScope, $filter,
$translate, GridExport, TransactionType, $scope) {
$translate, GridExport, TransactionType, GridState, $scope) {
// Journal utilities
var sorting;
var grouping;
var filtering;
var columnConfig;
var transactions;
var exportation;
var state;

var filter = new Filters();

Expand Down Expand Up @@ -97,6 +99,7 @@ function JournalController(Journal, Sorting, Grouping,
columnConfig = new Columns(vm.gridOptions, cacheKey);
transactions = new Transactions(vm.gridOptions);
exportation = new GridExport(vm.gridOptions, 'selected', 'visible');
state = new GridState(vm.gridOptions, cacheKey);

// attaching the filtering object to the view
vm.filtering = filtering;
Expand All @@ -122,6 +125,8 @@ function JournalController(Journal, Sorting, Grouping,
vm.loading = !vm.loading;
}

vm.saveGridState = state.saveGridState;

/**
* Column definitions; specify the configuration and behaviour for each column
* in the journal grid. Initialise each of the journal utilities,
Expand Down

0 comments on commit 69eec2c

Please sign in to comment.