Skip to content

Commit

Permalink
Merge pull request #580 from joneit/GRID-373_Grid-state_Part-3
Browse files Browse the repository at this point in the history
GRID-373: Grid state part 3 - calculators
  • Loading branch information
Jonathan Eiten authored Apr 11, 2017
2 parents f983efe + 7adb869 commit 36520cd
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 185 deletions.
38 changes: 18 additions & 20 deletions demo/js/demo/events.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/* globals vent */

'use strict';

module.exports = function(demo, grid) {

grid.addEventListener('fin-click', function(e) {
var cell = e.detail.gridCell;
if (vent) { console.log('fin-click cell:', cell); }
if (demo.vent) { console.log('fin-click cell:', cell); }
});

grid.addEventListener('fin-double-click', function(e) {
var rowContext = e.detail.dataRow;
if (vent) { console.log('fin-double-click row-context:', rowContext); }
if (demo.vent) { console.log('fin-double-click row-context:', rowContext); }
});

grid.addEventListener('fin-button-pressed', function(e) {
Expand All @@ -20,17 +18,17 @@ module.exports = function(demo, grid) {
});

grid.addEventListener('fin-scroll-x', function(e) {
if (vent) { console.log('fin-scroll-x ', e.detail.value); }
if (demo.vent) { console.log('fin-scroll-x ', e.detail.value); }
});

grid.addEventListener('fin-scroll-y', function(e) {
if (vent) { console.log('fin-scroll-y', e.detail.value); }
if (demo.vent) { console.log('fin-scroll-y', e.detail.value); }
});

grid.addEventListener('fin-cell-enter', function(e) {
var cellEvent = e.detail;

//if (vent) { console.log('fin-cell-enter', cell.x, cell.y); }
//if (demo.vent) { console.log('fin-cell-enter', cell.x, cell.y); }

//how to set the tooltip....
grid.setAttribute('title', 'event name: "fin-cell-enter"\n' +
Expand All @@ -56,7 +54,7 @@ module.exports = function(demo, grid) {
});

grid.addEventListener('fin-filter-applied', function(e) {
if (vent) { console.log('fin-filter-applied', e); }
if (demo.vent) { console.log('fin-filter-applied', e); }
});

/**
Expand Down Expand Up @@ -109,7 +107,7 @@ module.exports = function(demo, grid) {

grid.addEventListener('fin-selection-changed', function(e) {

if (vent) {
if (demo.vent) {
console.log('fin-selection-changed', grid.getSelectedRows(), grid.getSelectedColumns(), grid.getSelections());
}

Expand All @@ -127,7 +125,7 @@ module.exports = function(demo, grid) {
grid.addEventListener('fin-row-selection-changed', function(e) {
var detail = e.detail;

if (vent) { console.log('fin-row-selection-changed', detail); }
if (demo.vent) { console.log('fin-row-selection-changed', detail); }

// Move cell selection with row selection
var rows = detail.rows,
Expand Down Expand Up @@ -161,7 +159,7 @@ module.exports = function(demo, grid) {
});

grid.addEventListener('fin-column-selection-changed', function(e) {
if (vent) { console.log('fin-column-selection-changed', e.detail); }
if (demo.vent) { console.log('fin-column-selection-changed', e.detail); }

if (e.detail.columns.length === 0) {
console.log('no rows selected');
Expand All @@ -175,43 +173,43 @@ module.exports = function(demo, grid) {
});

grid.addEventListener('fin-editor-data-change', function(e) {
if (vent) { console.log('fin-editor-data-change', e.detail); }
if (demo.vent) { console.log('fin-editor-data-change', e.detail); }

});

grid.addEventListener('fin-request-cell-edit', function(e) {
if (vent) { console.log('fin-request-cell-edit', e); }
if (demo.vent) { console.log('fin-request-cell-edit', e); }
//e.preventDefault(); //uncomment to cancel editor popping up
});

grid.addEventListener('fin-before-cell-edit', function(e) {
if (vent) { console.log('fin-before-cell-edit', e); }
if (demo.vent) { console.log('fin-before-cell-edit', e); }
//e.preventDefault(); //uncomment to cancel updating the model with the new data
});

grid.addEventListener('fin-after-cell-edit', function(e) {
if (vent) { console.log('fin-after-cell-edit', e); }
if (demo.vent) { console.log('fin-after-cell-edit', e); }
});

grid.addEventListener('fin-editor-keyup', function(e) {
if (vent) { console.log('fin-editor-keyup', e.detail); }
if (demo.vent) { console.log('fin-editor-keyup', e.detail); }
});

grid.addEventListener('fin-editor-keypress', function(e) {
if (vent) { console.log('fin-editor-keypress', e.detail); }
if (demo.vent) { console.log('fin-editor-keypress', e.detail); }
});

grid.addEventListener('fin-editor-keydown', function(e) {
if (vent) { console.log('fin-editor-keydown', e.detail); }
if (demo.vent) { console.log('fin-editor-keydown', e.detail); }
});

grid.addEventListener('fin-groups-changed', function(e) {
if (vent) { console.log('fin-groups-changed', e.detail); }
if (demo.vent) { console.log('fin-groups-changed', e.detail); }
});

grid.addEventListener('fin-context-menu', function(e) {
var modelPoint = e.detail.gridCell;
if (vent) { console.log('fin-context-menu(' + modelPoint.x + ', ' + modelPoint.y + ')'); }
if (demo.vent) { console.log('fin-context-menu(' + modelPoint.x + ', ' + modelPoint.y + ')'); }
});

};
91 changes: 91 additions & 0 deletions src/Hypergrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,83 @@ var Hypergrid = Base.extend('Hypergrid', {
getState: function() {
return this.behavior.getState();
},

loadState: function(state) {
this.behavior.setState(state);
},

/**
* @param {object} [options]
* @param {object} [options.include] - Hash of special objects to exclude. To exclude one or more of `rows`, `columns`, `cells`, `subgrids`, `calculators`, `columnIndexes`, or `columnNames`, provide an object with those key(s) paired with value `false`.
* @param {boolean} [options.compact] - Run garbage collection first. The only property this current affects is `properties.calculators` (removes unused calculators from).
* @param {number|string} [options.space='\t'] - For no space, give `0`. (See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify|JSON.stringify}'s `space` param other options.)
* @param {function} [options.headerify] - If your headers were generated by a function (taking column name as a parameter), give a reference to that function here to avoid persisting headers that match the generated string.
* @memberOf Hypergrid#
*/
saveState: function(options) {
options = options || {};

var space = options.space === undefined ? '\t' : options.space,
include = options.include || {},
properties = this.properties,
calculators = properties.calculators;

if (calculators) {
if (options.compact) {
var columns = this.behavior.getColumns();
Object.keys(calculators).forEach(function(key) {
if (!columns.find(function(column) { return column.properties.calculator === calculators[key]; })) {
delete calculators[key];
}
});
}
calculators.toJSON = stringifyFunctions;
}

// Temporarily copy the given headerify function for access by columns getter
this.headerify = options.headerify;

// Temporarily copy values from dynamic properties layer as own members (stringify ignores prototype chain)
var dynaPropDescriptors = {};
['rows', 'columns', 'cells', 'subgrids', 'columnIndexes', 'columnNames'].forEach(function(key) {
if (!definedFalsy(include[key])) {
dynaPropDescriptors[key] = {
configurable: true,
enumerable: true,
value: properties[key]
};
}
});
Object.defineProperties(properties, dynaPropDescriptors); //must use descriptors because can't override a prop when it is a setter

var json = JSON.stringify(properties, function(key, value) {
switch (key) {
case 'calculator':
if (calculators) {
value = Object.keys(calculators).find(function(key) {
return calculators[key] === value;
});
} else {
value = value.toString();
}
break;
case 'calculators':
if (definedFalsy(include.calculators)) {
value = undefined;
}
}
return value;
}, space); //todo: still needs the cells array

// Remove the temporary copies
Object.keys(dynaPropDescriptors).forEach(function(key) {
delete properties[key];
});
delete this.headerify;

return json;
},

/**
* @memberOf Hypergrid#
* @returns {object} The initial mouse position on a mouse down event for cell editing or a drag operation.
Expand Down Expand Up @@ -2190,6 +2267,20 @@ function setStyles(el, style, keys) {
}
}

function stringifyFunctions() {
var self = this;
return Object.keys(this).reduce(function(obj, key) {
if (key !== 'toJSON') {
obj[key] = /^function /.test(key) ? null : self[key].toString();
}
return obj;
}, {});
}

// is it falsy (excluding undefined)?
function definedFalsy(b) {
return !b && b !== undefined;
}

/**
* @name plugins
Expand Down
Loading

0 comments on commit 36520cd

Please sign in to comment.