Debian Codegraph @

js/vegaexpr.ts

raw ◀ ▶
1 Debian patch touch this file
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 // @ts-ignore
53 this.on('change:value', this.updateFunction.bind(this));
54 }
55
56 process(config: CellRenderer.CellConfig, defaultValue: any) {
57 return this._function(config, defaultValue, vegaFunctions.functionContext);
58 }
59
60 _processRegex(match: string, data: string): string {
61 const parsedMatch = data.match(/\[(.*?)\]/g)!;
62 const column = parsedMatch[0];
63 const columnMatch = column.match(/\[(.*?)\]/)![1];
64
65 if (parsedMatch.length === 1) {
66 // Column indexing for regular element.
67 return `cell.metadata.data(cell.row, ${columnMatch})`;
68 } else {
69 // Column indexing for a compound element.
70 const rest = parsedMatch.splice(1);
71 return `cell.metadata.data(cell.row, ${columnMatch})${rest.join('')}`;
72 }
73 }
74
75 /**
76 * Augments transpiled JS code output from vega with
77 * datamodel calls for condition validation.
78 * @param parsedValue JS code (string) generated from a vega expression
79 */
80 private _augmentExpression(parsedValue: ParsedVegaExpr): ParsedVegaExpr {
81 let codeToProcess = parsedValue.code;
82 codeToProcess = codeToProcess.replace(
83 // The original regex below is not compatible with safari (missing lookbehind)
84 // /(?<=cell.metadata.data)(?<data>(\[(.*?)\])+)(?=)/g,
85 /cell.metadata.data(?<data>(\[(.*?)\])+)/g,
86 this._processRegex,
87 );
88 parsedValue.code = codeToProcess;
89
90 return parsedValue;
91 }
92
93 private updateFunction() {
94 let parsedValue: ParsedVegaExpr = this._codegen(
•95 // @ts-ignore
96 vegaExpressions.parse(this.get('value')),
97 );
98
99 parsedValue = this._augmentExpression(parsedValue);
100
101 this._function = Function(
102 'cell',
103 'default_value',
104 'functions',
105 '"use strict";return(' + parsedValue.code + ')',
106 );
107 }
108
109 static model_name = 'VegaExprModel';
110 static model_module = MODULE_NAME;
111 static model_module_version = MODULE_VERSION;
112 static view_name = 'VegaExprView';
113 static view_module = MODULE_NAME;
114 static view_module_version = MODULE_VERSION;
115
116 _codegen: any;
117 _function: any;
118 }
119
120 export class VegaExprView extends WidgetView {
121 process(config: CellRenderer.CellConfig, defaultValue: any) {
122 return this.model.process(config, defaultValue);
123 }
124
125 model: VegaExprModel;
126 }
127
128 export interface ParsedVegaExpr {
129 /**
130 * A JavaScript storing literal describing
131 * the converted vega expression
132 */
133 code: string;
134 }