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

Enable grid state, Fixes #314 #331

Merged
merged 41 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
45549a3
WIP GridStateModel
febbraiod May 23, 2018
c721606
Clean up debuggers
febbraiod May 23, 2018
7a93ffb
Linting
febbraiod May 23, 2018
098e808
Clean up comments and console.logs
febbraiod May 24, 2018
7e9dc9b
Reinstate check for xhIds, give config groupCol an xhId
febbraiod May 24, 2018
10a19a0
Stateful column ordering
febbraiod May 24, 2018
5e226cb
Deal with stale column state and new columns in code
febbraiod May 24, 2018
5e15cb4
Stateful sorting
febbraiod May 24, 2018
3a2f21c
Don't set stateful sortBy on a grid if grid doesn't have a correspond…
febbraiod May 24, 2018
b1435ac
Make stateful column ordering symmetrical with other stateful cmps
febbraiod May 24, 2018
bd6a2f0
Clean ups
febbraiod May 24, 2018
7b3a5de
Merge branches 'develop' and 'gridState' of https://github.com/exhi/h…
febbraiod May 25, 2018
804ea1f
Apply xhId to ag-grid's colId
febbraiod May 25, 2018
6b42ae7
Clean ups and tweaks based on CR
febbraiod May 25, 2018
0fdf3d8
Buffer calls to saveStateChange with lodash's debounce
febbraiod May 25, 2018
de67a89
Add todo comment
febbraiod May 25, 2018
790780b
Merge, and don't setColumns if order hasn't changed
febbraiod May 25, 2018
7f46dab
Don't setColumns if order hasn't changed
febbraiod May 25, 2018
ed67546
Merge branches 'develop' and 'gridState' of https://github.com/exhi/h…
febbraiod May 29, 2018
5ce5c10
Fix sync order columns bug
febbraiod May 29, 2018
efab1f1
Replace incorrectly removed import (fixes grouping)
febbraiod May 29, 2018
006f0e5
Store/Rehydrate column width, example on pref panel
febbraiod May 29, 2018
a3654bf
Changes from review
febbraiod May 30, 2018
ac184f9
Column chooser can reset state if grid has a stateModel
febbraiod May 30, 2018
f1f8988
Remove unneeded test code
febbraiod May 30, 2018
54ba016
Document stateModel in gridModel constructor
febbraiod May 30, 2018
254b0bc
Remove sencha specific column handling
febbraiod May 30, 2018
eee0971
Remove comments to move to PR
febbraiod May 30, 2018
9cf3097
Merge branches 'develop' and 'gridState' of https://github.com/exhi/h…
febbraiod Jun 4, 2018
e4adeb9
Use undo icon for resetting grid state
febbraiod Jun 4, 2018
7bcd09e
Merge with develop
febbraiod Jun 7, 2018
ed475c7
Correct comment
febbraiod Jun 7, 2018
494d95f
Merge branches 'develop' and 'gridState' of https://github.com/exhi/h…
febbraiod Jun 7, 2018
530e795
Improvements from CR. WIP, untested.
febbraiod Jun 8, 2018
983faef
Better commenting of updateGridColumns method
febbraiod Jun 8, 2018
ca44aa1
Remove unused import
febbraiod Jun 8, 2018
066a86b
Debug set sortBy
febbraiod Jun 8, 2018
cd774cf
Use fat arrow for debounced saveStateChange method
febbraiod Jun 8, 2018
09a43fc
Doc tweaks
febbraiod Jun 8, 2018
9c8690f
Minor clean ups
febbraiod Jun 8, 2018
567b484
Revert test code
febbraiod Jun 8, 2018
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
1 change: 0 additions & 1 deletion admin/columns/Columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import {fileColFactory} from '@xh/hoist/columns/Utils.js';


/**
* Shared columns for the admin client.
*/
Expand Down
17 changes: 16 additions & 1 deletion cmp/grid/ColChooser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {Icon} from '@xh/hoist/icon';
export class ColChooser extends Component {

render() {
const {isOpen, lrModel} = this.model;
const {isOpen, gridModel, lrModel} = this.model;

if (!isOpen) return null;

Expand All @@ -29,6 +29,12 @@ export class ColChooser extends Component {
items: [
leftRightChooser({model: lrModel, height: 300}),
toolbar(
button({
text: 'Reset',
icon: Icon.undo({cls: 'xh-red'}),
omit: !gridModel.stateModel,
onClick: this.restoreDefaults
}),
filler(),
button({
text: 'Cancel',
Expand All @@ -50,5 +56,14 @@ export class ColChooser extends Component {
this.onClose();
}

restoreDefaults = () => {
const {model} = this,
{stateModel} = model.gridModel;

stateModel.resetStateAsync().then(() => {
model.syncChooserData();
});
}

}
export const colChooser = elemFactory(ColChooser);
6 changes: 3 additions & 3 deletions cmp/grid/ColChooserModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {LeftRightChooserModel} from '@xh/hoist/cmp/leftrightchooser';
@HoistModel()
export class ColChooserModel {

gridModel = null
lrModel = null
gridModel = null;
lrModel = null;

@observable isOpen = false
@observable isOpen = false;

constructor(gridModel) {
this.gridModel = gridModel;
Expand Down
8 changes: 7 additions & 1 deletion cmp/grid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class Grid extends Component {
onSelectionChanged: this.onSelectionChanged,
onSortChanged: this.onSortChanged,
onGridSizeChanged: this.onGridSizeChanged,
onComponentStateChanged: this.onComponentStateChanged
onComponentStateChanged: this.onComponentStateChanged,
onDragStopped: this.onDragStopped
};

this.addAutorun(this.syncSelection);
Expand Down Expand Up @@ -271,6 +272,11 @@ class Grid extends Component {
this.model.setSortBy(ev.api.getSortModel());
}

onDragStopped = (ev) => {
const gridColumns = ev.api.columnController.gridColumns;
this.model.syncColumnOrder(gridColumns);
}

onGridSizeChanged = (ev) => {
ev.api.sizeColumnsToFit();
}
Expand Down
90 changes: 74 additions & 16 deletions cmp/grid/GridModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {XH, HoistModel} from '@xh/hoist/core';
import {action, observable} from '@xh/hoist/mobx';
import {StoreSelectionModel} from '@xh/hoist/data';
import {StoreContextMenu} from '@xh/hoist/cmp/contextmenu';
import {defaults, castArray, find, isString, isPlainObject, orderBy} from 'lodash';
import {defaults, castArray, find, isEqual, isString, isPlainObject, orderBy} from 'lodash';

import {ColChooserModel} from './ColChooserModel';
import {GridStateModel} from './GridStateModel';

/**
* Core Model for a Grid, specifying the grid's data store, column definitions,
Expand All @@ -24,6 +25,7 @@ export class GridModel {
selModel = null;
contextMenuFn = null;
colChooserModel = null;
stateModel = null;

@observable.ref agApi = null;
@observable.ref columns = [];
Expand All @@ -49,8 +51,6 @@ export class GridModel {
/**
* @param {BaseStore} store - store containing the data for the grid.
* @param {Object[]} columns - collection of column specifications.
* @param {(StoreSelectionModel|Object|String)} [selModel] - selection model to use,
* config to create one, or 'mode' property for a selection model.
* @param {string} [emptyText] - empty text to display if grid has no records. Can be valid HTML.
* Defaults to null, in which case no empty text will be shown.
* @param {Object[]} [sortBy] - one or more sorters to apply to store data.
Expand All @@ -59,6 +59,10 @@ export class GridModel {
* @param {string} [groupBy] - Column ID by which to group.
* @param {boolean} [enableColChooser] - true to setup support for column chooser UI and
* install a default context menu item to launch the chooser.
* @param {(StoreSelectionModel|Object|String)} [selModel] - selection model to use,
* config to create one, or 'mode' property for a selection model.
* @param {(GridStateModel|Object|String)} [stateModel] - state model to use,
* config to create one, or xhStateId property for a state model
* @param {function} [contextMenuFn] - closure returning a StoreContextMenu().
*/
constructor({
Expand All @@ -69,13 +73,12 @@ export class GridModel {
sortBy = [],
groupBy = null,
enableColChooser = false,
stateModel = null,
contextMenuFn = () => this.defaultContextMenu()
}) {
this.store = store;
this.columns = columns;
this.contextMenuFn = contextMenuFn;

this.selModel = this.parseSelModel(selModel, store);
this.emptyText = emptyText;

if (enableColChooser) {
Expand All @@ -84,6 +87,9 @@ export class GridModel {

this.setGroupBy(groupBy);
this.setSortBy(sortBy);

this.selModel = this.initSelModel(selModel, store);
this.stateModel = this.initStateModel(stateModel);
}

exportDataAsExcel(params) {
Expand Down Expand Up @@ -129,9 +135,18 @@ export class GridModel {
this.agApi = agApi;
}

/**
* Set row grouping
*
* This method is no-op if provided a field without a corresponding column.
* A falsey field argument will remove grouping entirely.
*/
@action
setGroupBy(field) {
const cols = this.columns;
const cols = this.columns,
groupCol = find(cols, {field});

if (field && !groupCol) return;

cols.forEach(it => {
if (it.rowGroup) {
Expand All @@ -140,17 +155,19 @@ export class GridModel {
}
});

if (field) {
const col = find(cols, {field});
if (col) {
col.rowGroup = true;
col.hide = true;
}
if (groupCol) {
groupCol.rowGroup = true;
groupCol.hide = true;
}

this.columns = [...cols];
}

/**
* Set sort by column
*
* This method is no-op if provided any sorters without a corresponding column
*/
@action
setSortBy(sortBy) {
// Normalize string, and partially specified values
Expand All @@ -161,9 +178,13 @@ export class GridModel {
return it;
});

const sortIsValid = sortBy.every(it => find(this.columns, {colId: it.colId}));
if (!sortIsValid) return;

this.sortBy = sortBy;
}


/** Load the underlying store. */
loadAsync(...args) {
return this.store.loadAsync(...args);
Expand All @@ -190,6 +211,24 @@ export class GridModel {
}
}

syncColumnOrder(agColumns) {
// Check for no-op
const xhColumns = this.columns,
newIdOrder = agColumns.map(it => it.colId),
oldIdOrder = xhColumns.map(it => it.colId),
orderChanged = !isEqual(newIdOrder, oldIdOrder);

if (!orderChanged) return;

const orderedCols = [];
agColumns.forEach(gridCol => {
const col = find(xhColumns, {colId: gridCol.colId});
orderedCols.push(col);
});

this.setColumns(orderedCols);
}

//-----------------------
// Implementation
//-----------------------
Expand All @@ -203,8 +242,7 @@ export class GridModel {
}
}


parseSelModel(selModel, store) {
initSelModel(selModel, store) {
if (selModel instanceof StoreSelectionModel) {
return selModel;
}
Expand All @@ -217,13 +255,33 @@ export class GridModel {
let mode = 'single';
if (isString(selModel)) {
mode = selModel;
} else if (selModel === null) {
} else if (selModel === null) {
mode = 'disabled';
}

return new StoreSelectionModel({mode, store});
}

initStateModel(stateModel) {
if (!stateModel) return;
let ret;

if (stateModel instanceof GridStateModel) {
ret = stateModel;
}

if (isPlainObject(stateModel)) {
ret = new GridStateModel(stateModel);
}

if (isString(stateModel)) {
ret = new GridStateModel({xhStateId: stateModel});
}

ret.init(this);
return ret;
}

destroy() {
XH.safeDestroy(this.colChooserModel);
// TODO: How are Stores destroyed?
Expand Down
Loading