Skip to content

Commit

Permalink
init column props from schema
Browse files Browse the repository at this point in the history
  • Loading branch information
joneit committed Mar 20, 2018
1 parent 30fcd0d commit 008adee
Show file tree
Hide file tree
Showing 21 changed files with 610 additions and 507 deletions.
10 changes: 9 additions & 1 deletion demo/js/demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ window.onload = function() {
// convert field names containing underscore to camel case by overriding column enum decorator
Hypergrid.behaviors.JSON.prototype.columnEnumKey = Hypergrid.behaviors.JSON.columnEnumDecorators.toCamelCase;

var schema = Hypergrid.lib.fields.getSchema(people1);

// as of v2.1.6, column properties can also be initialized from custom schema (as well as from a grid state object)
Object.assign(schema.find(function(columnSchema) { return columnSchema.name === 'height'; }), {
halign: 'right',
format: 'foot'
});

var gridOptions = {
data: people1,
margin: { bottom: '17px', right: '17px'},
schema: Hypergrid.lib.fields.getSchema(people1),
schema: schema,
plugins: require('fin-hypergrid-event-logger'),
state: { color: 'orange' }
},
Expand Down
8 changes: 4 additions & 4 deletions demo/js/demo/setState.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ module.exports = function(demo, grid) {
},

columns: {
height: {
halign: 'right',
format: 'foot'
},
// height: {
// halign: 'right',
// format: 'foot'
// },

/* eslint-disable camelcase */
last_name: {
Expand Down
11 changes: 4 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"dependencies": {
"chai": "^3.5.0",
"extend-me": "^2.3",
"extend-me": "^2.7.0",
"fin-hypergrid-data-source-base": "^0.4.10",
"fin-hypergrid-event-logger": "^1.0.3",
"finbars": "1.5.2",
Expand Down
54 changes: 43 additions & 11 deletions src/Hypergrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var SelectionModel = require('./lib/SelectionModel');
var Localization = require('./lib/Localization');
var Behavior = require('./behaviors/Behavior');
var behaviorJSON = require('./behaviors/JSON');
var CellRenderers = require('./cellRenderers');
var CellEditors = require('./cellEditors');
var cellRenderers = require('./cellRenderers');
var cellEditors = require('./cellEditors');

var EDGE_STYLES = ['top', 'bottom', 'left', 'right'],
RECT_STYLES = EDGE_STYLES.concat(['width', 'height', 'position']);
Expand Down Expand Up @@ -109,17 +109,19 @@ var Hypergrid = Base.extend('Hypergrid', {

/**
* @name cellRenderers
* @type {CellRenderer}
* @type {Registry}
* @memberOf Hypergrid#
*/
this.cellRenderers = new CellRenderers();
this.cellRenderers = cellRenderers;

/**
* Private version of cell editors registry with a bound `create` method for use by `getCellEditorAt`.
* @name cellEditors
* @type {CellEditor}
* @type {Registry}
* @memberOf Hypergrid#
*/
this.cellEditors = new CellEditors({ grid: this });
this.cellEditors = Object.create(cellEditors);
Object.defineProperty(this.cellEditors, 'create', { value: createCellEditor.bind(this) });

this.initCanvas();

Expand Down Expand Up @@ -302,7 +304,7 @@ var Hypergrid = Base.extend('Hypergrid', {
var: { value: new Var() }
});

// For all all default props of object type, if a dynamic prop, invoke setter; else deep clone it so changes
// For all default props of object type, if a dynamic prop, invoke setter; else deep clone it so changes
// made to inner props won't go to object on theme or defaults layers which are shared by other instances.
Object.keys(defaults).forEach(function(key) {
var value = defaults[key];
Expand Down Expand Up @@ -609,7 +611,16 @@ var Hypergrid = Base.extend('Hypergrid', {
* @see [Memento pattern](http://en.wikipedia.org/wiki/Memento_pattern)
*/
setState: function(state) {
this.behavior.setState(state);
this.addState(state, true);
},

/**
* @memberOf Hypergrid#
* @desc Add to the state object.
* @param {object} state
*/
addState: function(state, settingState) {
this.behavior.addState(state, settingState);
this.refreshProperties();
this.behaviorChanged();
},
Expand All @@ -636,7 +647,10 @@ var Hypergrid = Base.extend('Hypergrid', {

var space = options.space === undefined ? '\t' : options.space,
properties = this.properties,
calculators = properties.calculators;
calculators = properties.calculators,
blacklist = options.blacklist = options.blacklist || [];

blacklist.push('columnProperties'); // Never output this synonym of 'columns'

if (calculators) {
if (options.compact) {
Expand All @@ -654,8 +668,8 @@ var Hypergrid = Base.extend('Hypergrid', {
this.headerify = options.headerify;

var json = JSON.stringify(properties, function(key, value) {
if (options.blacklist && this === properties && options.blacklist.indexOf(key) >= 0) {
value = undefined;
if (this === properties && options.blacklist.indexOf(key) >= 0) {
value = undefined; // JSON.stringify ignores undefined props
} else if (key === 'calculator') {
if (calculators) {
// convert function reference to registry key
Expand Down Expand Up @@ -828,6 +842,13 @@ var Hypergrid = Base.extend('Hypergrid', {
this.renderer.setInfo(messages);
},

/**
* @memberOf Behavior#
*/
reindex: function() {
this.needsReindex = this.needsShapeChanged = true;
},

/**
* @memberOf Hypergrid#
* @summary _(See {@link Hypergrid.prototype#setData}.)_
Expand Down Expand Up @@ -889,6 +910,10 @@ var Hypergrid = Base.extend('Hypergrid', {
* Called from renderer/index.js
*/
deferredBehaviorChange: function() {
if (this.needsReindex) {
this.behavior.reindex();
}

if (this.needsShapeChanged) {
if (this.divCanvas) {
this.synchronizeScrollingBoundaries(); // calls computeCellsBounds and repaint (state change)
Expand Down Expand Up @@ -1946,6 +1971,13 @@ function deepClone(object) {
return result;
}

function createCellEditor(name, props) {
var CellEditor = cellEditors.get(name);
if (CellEditor) {
return new CellEditor(this, props);
}
}

/**
* @name plugins
* @memberOf Hypergrid
Expand Down
Loading

0 comments on commit 008adee

Please sign in to comment.