| 1 |
// Copyright (c) NumFOCUS. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
const vegaExpressions: any = require('vega-expression'); |
| 5 |
const vegaFormat: any = require('vega-format'); |
| 6 |
const vegaFunctions: any = require('vega-functions'); |
| 7 |
|
| 8 |
import { WidgetModel, WidgetView } from '@jupyter-widgets/base'; |
| 9 |
import { CellRenderer } from '@lumino/datagrid'; |
| 10 |
import { MODULE_NAME, MODULE_VERSION } from './version'; |
| 11 |
|
| 12 |
// Newer versions of vega-functions require a locale property. |
| 13 |
// When using vega-functions from within Vega, this is provided, |
| 14 |
// but because we're using vega-functions separately, we need to |
| 15 |
// manually create and bind that locale context to the format function. |
| 16 |
const locale = vegaFormat.defaultLocale(); |
| 17 |
const dataflow = { context: { dataflow: { locale: () => locale } } }; |
| 18 |
for (const name in vegaFunctions.functionContext) { |
| 19 |
vegaFunctions.functionContext[name] = |
| 20 |
vegaFunctions.functionContext[name].bind(dataflow); |
| 21 |
} |
| 22 |
|
| 23 |
export class VegaExprModel extends WidgetModel { |
| 24 |
defaults() { |
| 25 |
return { |
| 26 |
...super.defaults(), |
| 27 |
_model_name: VegaExprModel.model_name, |
| 28 |
_model_module: VegaExprModel.model_module, |
| 29 |
_model_module_version: VegaExprModel.model_module_version, |
| 30 |
value: 'default_value', |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
initialize(attributes: any, options: any) { |
| 35 |
super.initialize(attributes, options); |
| 36 |
|
| 37 |
const codegen_params = { |
| 38 |
whitelist: ['cell', 'default_value', 'functions'], |
| 39 |
globalvar: 'cell', |
| 40 |
functions: (codegen: any) => { |
| 41 |
const fn = vegaExpressions.functions(codegen); |
| 42 |
for (const name in vegaFunctions.functionContext) { |
| 43 |
fn[name] = 'functions.' + name; |
| 44 |
} |
| 45 |
return fn; |
| 46 |
}, |
| 47 |
}; |
| 48 |
|
| 49 |
this._codegen = vegaExpressions.codegen(codegen_params); |
| 50 |
|
| 51 |
this.updateFunction(); |
| 52 |
this.on('change:value', this.updateFunction.bind(this)); |
| 53 |
} |
| 54 |
|
| 55 |
process(config: CellRenderer.CellConfig, defaultValue: any) { |
| 56 |
return this._function(config, defaultValue, vegaFunctions.functionContext); |
| 57 |
} |
| 58 |
|
| 59 |
_processRegex(match: string, data: string): string { |
| 60 |
const parsedMatch = data.match(/\[(.*?)\]/g)!; |
| 61 |
const column = parsedMatch[0]; |
| 62 |
const columnMatch = column.match(/\[(.*?)\]/)![1]; |
| 63 |
|
| 64 |
if (parsedMatch.length === 1) { |
| 65 |
// Column indexing for regular element. |
| 66 |
return `cell.metadata.data(cell.row, ${columnMatch})`; |
| 67 |
} else { |
| 68 |
// Column indexing for a compound element. |
| 69 |
const rest = parsedMatch.splice(1); |
| 70 |
return `cell.metadata.data(cell.row, ${columnMatch})${rest.join('')}`; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Augments transpiled JS code output from vega with |
| 76 |
* datamodel calls for condition validation. |
| 77 |
* @param parsedValue JS code (string) generated from a vega expression |
| 78 |
*/ |
| 79 |
private _augmentExpression(parsedValue: ParsedVegaExpr): ParsedVegaExpr { |
| 80 |
let codeToProcess = parsedValue.code; |
| 81 |
codeToProcess = codeToProcess.replace( |
| 82 |
// The original regex below is not compatible with safari (missing lookbehind) |
| 83 |
// /(?<=cell.metadata.data)(?<data>(\[(.*?)\])+)(?=)/g, |
| 84 |
/cell.metadata.data(?<data>(\[(.*?)\])+)/g, |
| 85 |
this._processRegex, |
| 86 |
); |
| 87 |
parsedValue.code = codeToProcess; |
| 88 |
|
| 89 |
return parsedValue; |
| 90 |
} |
| 91 |
|
| 92 |
private updateFunction() { |
| 93 |
let parsedValue: ParsedVegaExpr = this._codegen( |
| 94 |
vegaExpressions.parse(this.get('value')), |
| 95 |
); |
| 96 |
|
| 97 |
parsedValue = this._augmentExpression(parsedValue); |
| 98 |
|
| 99 |
this._function = Function( |
| 100 |
'cell', |
| 101 |
'default_value', |
| 102 |
'functions', |
| 103 |
'"use strict";return(' + parsedValue.code + ')', |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
static model_name = 'VegaExprModel'; |
| 108 |
static model_module = MODULE_NAME; |
| 109 |
static model_module_version = MODULE_VERSION; |
| 110 |
static view_name = 'VegaExprView'; |
| 111 |
static view_module = MODULE_NAME; |
| 112 |
static view_module_version = MODULE_VERSION; |
| 113 |
|
| 114 |
_codegen: any; |
| 115 |
_function: any; |
| 116 |
} |
| 117 |
|
| 118 |
export class VegaExprView extends WidgetView { |
| 119 |
process(config: CellRenderer.CellConfig, defaultValue: any) { |
| 120 |
return this.model.process(config, defaultValue); |
| 121 |
} |
| 122 |
|
| 123 |
model: VegaExprModel; |
| 124 |
} |
| 125 |
|
| 126 |
export interface ParsedVegaExpr { |
| 127 |
/** |
| 128 |
* A JavaScript storing literal describing |
| 129 |
* the converted vega expression |
| 130 |
*/ |
| 131 |
code: string; |
| 132 |
} |