Skip to content

Commit

Permalink
Merge pull request #644 from finos/perspective-config
Browse files Browse the repository at this point in the history
Perspective config
  • Loading branch information
texodus authored Jul 8, 2019
2 parents 3b1c177 + f3926cf commit 36473b7
Show file tree
Hide file tree
Showing 25 changed files with 287 additions and 112 deletions.
32 changes: 32 additions & 0 deletions docs/md/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,38 @@ view.delete();
// on this `table()`!
table.delete();
```
### Customizing behavior with `perspective.config.js`

For ease of configu synchronization between the Node.js, WebWorker and Browser,
Perspective supports configuration statically. You may override Perspective's
[default settings](https://github.com/finos/perspective/blob/master/packages/perspective/src/js/config/settings.js)
via a `perspective.config.js` or `perspective.config.json`
file in your project's root or parent path, or via the `"perspective"` key in
your project's `package.json`.

Note that, while in Node.js this config file is read at runtime, for the browser
this file must be read at compile time (handled automatically via
[`@finos/perspective-webpack-plugin`](https://github.com/finos/perspective/tree/master/packages/perspective-webpack-plugin)).
Thus, to update it you must either
rebuild your code, or supply the JSON configuration object to the `worker()`
constructor on initialization.

```javascript
module.exports = {
types: {
string: {
aggregate: "dominant"
},
float: {
format: {
style: "decimal",
minimumFractionDigits: 2,
maximumFractionDigits: 2
}
}
}
};
```

## `perspective-viewer` web component

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
import {select} from "d3";
import {getGroupValues, getSplitValues, getDataValues} from "./selectionData";
import {get_type_config} from "@finos/perspective/dist/esm/config";

export function generateHtml(tooltipDiv, data, settings) {
const tooltipValues = getGroupValues(data, settings)
Expand All @@ -30,4 +31,9 @@ function addDataValues(tooltipDiv, values) {
});
}

const formatNumber = value => value.toLocaleString(undefined, {style: "decimal"});
const formatNumber = value =>
value.toLocaleString(undefined, {
style: "decimal",
minimumFractionDigits: get_type_config("float").precision,
maximumFractionDigits: get_type_config("float").precision
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*
*/

import {get_type_config} from "@finos/perspective/dist/esm/config";

export function format_tooltip(context, type, schema, axis_titles, pivot_titles) {
const row_pivots_titles = pivot_titles.row,
column_pivot_titles = pivot_titles.column;
Expand Down Expand Up @@ -145,9 +147,9 @@ function value_exists(value) {

function format_value(value, type) {
if (type === "datetime") {
return new Date(value).toLocaleString();
return new Date(value).toLocaleString("en-us", get_type_config("datetime").format);
} else if (type === "date") {
return new Date(value).toLocaleString("en-us", {year: "numeric", month: "numeric", day: "numeric"});
return new Date(value).toLocaleString("en-us", get_type_config("date").format);
} else if (type === "float" || type === "integer") {
return format_number(value, type);
} else {
Expand All @@ -157,8 +159,8 @@ function format_value(value, type) {

function format_number(num, format) {
if (format === "float") {
return Number.parseFloat(num).toLocaleString();
return Number.parseFloat(num).toLocaleString(get_type_config("float").format);
} else {
return Number.parseInt(num).toLocaleString();
return Number.parseInt(num).toLocaleString(get_type_config("float").format);
}
}
45 changes: 10 additions & 35 deletions packages/perspective-viewer-hypergrid/src/js/hypergrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {bindTemplate} from "@finos/perspective-viewer/dist/esm/utils.js";
const TEMPLATE = require("../html/hypergrid.html");

import style from "../less/hypergrid.less";
import {get_type_config} from "@finos/perspective/src/js/config";

const COLUMN_HEADER_FONT = "12px Helvetica, sans-serif";
const GROUP_LABEL_FONT = "12px Open Sans, sans-serif"; // overrides COLUMN_HEADER_FONT for group labels
Expand Down Expand Up @@ -245,41 +246,15 @@ bindTemplate(TEMPLATE, style)(
// Add tree cell renderer
this.grid.cellRenderers.add("TreeCell", Base.extend({paint: treeLineRendererPaint}));

const float_formatter = null_formatter(
new this.grid.localization.NumberFormatter("en-US", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
})
);
this.grid.localization.add("FinanceFloat", float_formatter);

const integer_formatter = null_formatter(new this.grid.localization.NumberFormatter("en-us", {}));
this.grid.localization.add("FinanceInteger", integer_formatter);

const datetime_formatter = null_formatter(
new this.grid.localization.DateFormatter("en-us", {
week: "numeric",
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric"
}),
-1
);

const date_formatter = null_formatter(
new this.grid.localization.DateFormatter("en-us", {
week: "numeric",
year: "numeric",
month: "numeric",
day: "numeric"
}),
-1
);
this.grid.localization.add("FinanceDatetime", datetime_formatter);
this.grid.localization.add("FinanceDate", date_formatter);
const float_formatter = new this.grid.localization.NumberFormatter("en-US", get_type_config("float").format);
const integer_formatter = new this.grid.localization.NumberFormatter("en-us", get_type_config("integer").format);
this.grid.localization.add("FinanceFloat", null_formatter(float_formatter));
this.grid.localization.add("FinanceInteger", null_formatter(integer_formatter));

const datetime_formatter = new this.grid.localization.DateFormatter("en-us", get_type_config("datetime").format);
const date_formatter = new this.grid.localization.DateFormatter("en-us", get_type_config("date").format);
this.grid.localization.add("FinanceDatetime", null_formatter(datetime_formatter, -1));
this.grid.localization.add("FinanceDate", null_formatter(date_formatter, -1));

this.grid.localization.add("FinanceTree", {
format: function(val, type) {
Expand Down
2 changes: 1 addition & 1 deletion packages/perspective-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"clean:screenshots": "rimraf \"screenshots/**/*.@(failed|diff).png\"",
"docs": "npm-run-all docs:jsdoc docs:deploy",
"docs:jsdoc": "jsdoc2md src/js/viewer.js -p list --separators --no-gfm > README.md",
"docs:deploy": "(echo \"---\nid: perspective-viewer\ntitle: \\`perspective-viewer\\` API\n---\n\n\"; cat README.md) > ../../docs/obj/perspective-viewer-site.md"
"docs:deploy": "(echo \"---\nid: perspective-viewer\ntitle: \\`perspective-viewer\\` API\n---\n\n\"; cat README.md) > ../../docs/obj/perspective-viewer.md"
},
"publishConfig": {
"access": "public"
Expand Down
9 changes: 5 additions & 4 deletions packages/perspective-viewer/src/js/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import awesomplete_style from "!!css-loader!awesomplete/awesomplete.css";
import {bindTemplate} from "./utils.js";

import perspective from "@finos/perspective";
import {get_type_config} from "@finos/perspective/dist/esm/config";
import template from "../html/row.html";

import style from "../less/row.less";
Expand Down Expand Up @@ -73,7 +74,7 @@ class Row extends HTMLElement {
default:
}
if (!this.hasAttribute("aggregate")) {
this.setAttribute("aggregate", perspective.AGGREGATE_DEFAULTS[type]);
this.setAttribute("aggregate", get_type_config(type).aggregate);
}
let filter_operand = this.shadowRoot.querySelector("#filter_operand");
this._callback = event => this._update_filter(event);
Expand Down Expand Up @@ -119,7 +120,7 @@ class Row extends HTMLElement {
const filter_dropdown = this.shadowRoot.querySelector("#filter_operator");
const filter = JSON.parse(this.getAttribute("filter"));
if (filter_dropdown.value !== filter.operator) {
filter_dropdown.value = filter.operator || perspective.FILTER_DEFAULTS[this.getAttribute("type")];
filter_dropdown.value = filter.operator || get_type_config(this.getAttribute("type")).filter_operator;
}
filter_dropdown.style.width = get_text_width(filter_dropdown.value);
const filter_input = this.shadowRoot.querySelector("#filter_operand");
Expand All @@ -135,7 +136,7 @@ class Row extends HTMLElement {
let aggregate = this.getAttribute("aggregate");
if (agg_dropdown.value !== aggregate && this.hasAttribute("type")) {
let type = this.getAttribute("type");
agg_dropdown.value = aggregate || perspective.AGGREGATE_DEFAULTS[type];
agg_dropdown.value = aggregate || get_type_config(type).aggregate;
}
}

Expand Down Expand Up @@ -190,7 +191,7 @@ class Row extends HTMLElement {
} else {
event.dataTransfer.setData(
"text",
JSON.stringify([this.getAttribute("name"), perspective.FILTER_DEFAULTS[this.getAttribute("type")], undefined, this.getAttribute("type"), this.getAttribute("aggregate")])
JSON.stringify([this.getAttribute("name"), get_type_config(this.getAttribute("type")).filter_operator, undefined, this.getAttribute("type"), this.getAttribute("aggregate")])
);
}
this.dispatchEvent(new CustomEvent("row-drag"));
Expand Down
2 changes: 1 addition & 1 deletion packages/perspective-viewer/src/js/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class PerspectiveViewer extends ActionElement {
* config object is an array of three elements:
* * The column name.
* * The filter operation as a string. See
* {@link perspective/src/js/defaults.js}
* {@link perspective/src/js/config/constants.js}
* * The filter argument, as a string, float or Array<string> as the
* filter operation demands.
* @fires PerspectiveViewer#perspective-config-update
Expand Down
6 changes: 3 additions & 3 deletions packages/perspective-viewer/src/js/viewer/dom_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

import perspective from "@finos/perspective";
import {get_type_config} from "@finos/perspective/dist/esm/config";
import {dragend} from "./dragdrop.js";
import {renderers} from "./renderers.js";

Expand Down Expand Up @@ -56,10 +56,10 @@ export class DomElement extends PerspectiveElement {
if (aggregate) {
aggregate = aggregate.op;
} else {
aggregate = perspective.AGGREGATE_DEFAULTS[type];
aggregate = get_type_config(type).aggregate;
}
} else {
aggregate = perspective.AGGREGATE_DEFAULTS[type];
aggregate = get_type_config(type).aggregate;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import _ from "lodash";

import perspective from "@finos/perspective";
import {get_type_config} from "@finos/perspective/dist/esm/config";
import {CancelTask} from "./cancel_task.js";
import {COMPUTATIONS} from "../computed_column.js";

Expand Down Expand Up @@ -47,7 +48,7 @@ function get_aggregates_with_defaults(aggregate_attribute, schema, cols) {
found.add(col.column);
if (type) {
if (col.op === "" || perspective.TYPE_AGGREGATES[type].indexOf(col.op) === -1) {
col.op = perspective.AGGREGATE_DEFAULTS[type];
col.op = get_type_config(type).aggregate;
}
aggregates.push(col);
} else {
Expand All @@ -60,7 +61,7 @@ function get_aggregates_with_defaults(aggregate_attribute, schema, cols) {
if (!found.has(col)) {
aggregates.push({
column: col,
op: perspective.AGGREGATE_DEFAULTS[schema[col]]
op: get_type_config(schema[col]).aggregate
});
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/perspective-webpack-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const PSP_WORKER_LOADER = require.resolve("./src/js/psp-worker-loader");
const WASM_LOADER = require.resolve("./src/js/wasm-loader.js");
const PSP_WORKER_COMPILER_LOADER = require.resolve("./src/js/psp-worker-compiler-loader.js");
const {get_config} = require("@finos/perspective/dist/esm/config");

class PerspectiveWebpackPlugin {
constructor(options = {}) {
Expand Down Expand Up @@ -78,6 +79,23 @@ class PerspectiveWebpackPlugin {
]
});

const perspective_config = get_config();
if (perspective_config) {
rules.push({
test: /\.js$/,
include: /perspective[\\/].+?[\\/]config[\\/]index\.js$/,
use: [
{
loader: "string-replace-loader",
options: {
search: "global.__TEMPLATE_CONFIG__",
replace: JSON.stringify(perspective_config, null, 4)
}
}
]
});
}

rules.push({
test: /psp\.async\.wasm\.js$/,
include: this.options.load_path,
Expand Down
3 changes: 2 additions & 1 deletion packages/perspective-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"bufferutil": "~3.0.0"
},
"peerDependencies": {
"webpack": ">=3.5 <5"
"webpack": ">=3.5 <5",
"@finos/perspective": "0.3.1"
}
}
2 changes: 0 additions & 2 deletions packages/perspective/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ declare module '@finos/perspective' {
type perspective = {
TYPE_AGGREGATES: ValuesByType,
TYPE_FILTERS: ValuesByType,
AGGREGATE_DEFAULTS: ValueByType,
FILTER_DEFAULTS: ValueByType,
SORT_ORDERS: SortOrders,
table(): Table,
worker(): PerspectiveWorker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ Client.prototype._handle = function(e) {
}

const msgs = this._worker.messages;

this._worker.initialized.value = true;
this._worker.messages = [];

if (msgs) {
for (const m in msgs) {
if (msgs.hasOwnProperty(m)) {
setTimeout(msgs[m]);
msgs[m]();
}
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*
*/

import {override_config} from "../config";

function error_to_json(error) {
const obj = {};
if (typeof error !== "string") {
Expand Down Expand Up @@ -38,6 +40,9 @@ export class Server {
* `Server` must be extended and the `post` method implemented before it can be initialized.
*/
init(msg) {
if (msg.config) {
override_config(msg.config);
}
this.post(msg);
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ export const TYPE_AGGREGATES = {
date: STRING_AGGREGATES
};

export const AGGREGATE_DEFAULTS = {
string: "count",
float: "sum",
integer: "sum",
boolean: "count",
datetime: "count",
date: "count"
};

const BOOLEAN_FILTERS = ["&", "|", "==", "!=", "or", "and"];

const NUMBER_FILTERS = ["<", ">", "==", "<=", ">=", "!=", "is nan", "is not nan"];
Expand All @@ -88,18 +79,3 @@ export const TYPE_FILTERS = {
datetime: DATETIME_FILTERS,
date: DATETIME_FILTERS
};

export const FILTER_DEFAULTS = {
string: "==",
float: "==",
integer: "==",
boolean: "==",
datetime: "==",
date: "=="
};

export const TYPED_ARRAY_SENTINEL_VALUE_INT8 = 255;
export const TYPED_ARRAY_SENTINEL_VALUE_INT16 = 32767;
export const TYPED_ARRAY_SENTINEL_VALUE_INT32 = 2147483647;
export const TYPED_ARRAY_SENTINEL_VALUE_FLOAT32 = 3.40282e38;
export const TYPED_ARRAY_SENTINEL_VALUE_FLOAT64 = Number.MAX_VALUE;
Loading

0 comments on commit 36473b7

Please sign in to comment.