@datagrok/compute 0.6.5 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,615 +0,0 @@
1
- import * as grok from 'datagrok-api/grok';
2
- import * as ui from 'datagrok-api/ui';
3
- import * as DG from 'datagrok-api/dg';
4
- import CodeMirror from 'codemirror';
5
- import 'codemirror/lib/codemirror.css';
6
- import {BehaviorSubject} from 'rxjs';
7
-
8
- const enum FUNC_PROPS_FIELDS {
9
- NAME = 'name',
10
- DESCRIPTION = 'description',
11
- LANGUAGE = 'language',
12
- HELP_URL = 'helpUrl',
13
- REFERENCE = 'reference',
14
- LOGIN = 'author?.login',
15
- SAMPLE = 'sample',
16
- ENVIRONMENT = 'environment',
17
- TAGS = 'tags'
18
- };
19
-
20
- const obligatoryFuncProps = ['name', 'description', 'helpUrl', 'language'];
21
-
22
- const functionPropsLabels = (key: FUNC_PROPS_FIELDS) => {
23
- switch (key) {
24
- case FUNC_PROPS_FIELDS.NAME: return 'Name';
25
- case FUNC_PROPS_FIELDS.DESCRIPTION: return 'Description';
26
- case FUNC_PROPS_FIELDS.LOGIN: return 'Author';
27
- case FUNC_PROPS_FIELDS.HELP_URL: return 'Help URL';
28
- case FUNC_PROPS_FIELDS.LANGUAGE: return 'Language';
29
- case FUNC_PROPS_FIELDS.REFERENCE: return 'Reference';
30
- case FUNC_PROPS_FIELDS.SAMPLE: return 'Sample';
31
- case FUNC_PROPS_FIELDS.ENVIRONMENT: return 'Environment';
32
- case FUNC_PROPS_FIELDS.TAGS: return 'Tags';
33
- }
34
- };
35
-
36
- const functionPropsCode = (key: string) => {
37
- switch (key) {
38
- case FUNC_PROPS_FIELDS.NAME: return 'name';
39
- case FUNC_PROPS_FIELDS.DESCRIPTION: return 'description';
40
- case FUNC_PROPS_FIELDS.LOGIN: return 'author';
41
- case FUNC_PROPS_FIELDS.HELP_URL: return 'helpUrl';
42
- case FUNC_PROPS_FIELDS.LANGUAGE: return 'language';
43
- case FUNC_PROPS_FIELDS.REFERENCE: return 'reference';
44
- case FUNC_PROPS_FIELDS.SAMPLE: return 'sample';
45
- case FUNC_PROPS_FIELDS.ENVIRONMENT: return 'environment';
46
- case FUNC_PROPS_FIELDS.TAGS: return 'tags';
47
- }
48
- }; ;
49
-
50
- const funcParamTypes = [
51
- DG.TYPE.BOOL,
52
- DG.TYPE.COLUMN_LIST,
53
- DG.TYPE.COLUMN,
54
- DG.TYPE.DATA_FRAME,
55
- DG.TYPE.DATE_TIME,
56
- DG.TYPE.FLOAT,
57
- DG.TYPE.GRAPHICS,
58
- DG.TYPE.INT,
59
- DG.TYPE.STRING,
60
- ];
61
-
62
- const enum FUNC_PARAM_FIELDS {
63
- DIRECTION = 'direction',
64
- TYPE = 'propertyType',
65
- NAME = 'name',
66
- DEFAULT_VALUE = 'defaultValue',
67
- DESCRIPTION = 'description',
68
- CATEGORY = 'category',
69
- };
70
-
71
- const enum DIRECTION {
72
- INPUT= 'input',
73
- OUTPUT= 'output',
74
- }
75
-
76
- const functionParamsMapping = {
77
- [FUNC_PARAM_FIELDS.DIRECTION]: 'Direction',
78
- [FUNC_PARAM_FIELDS.TYPE]: 'Type',
79
- [FUNC_PARAM_FIELDS.NAME]: 'Name',
80
- [FUNC_PARAM_FIELDS.DEFAULT_VALUE]: 'Default value',
81
- [FUNC_PARAM_FIELDS.DESCRIPTION]: 'Description',
82
- [FUNC_PARAM_FIELDS.CATEGORY]: 'Category',
83
- 'Direction': FUNC_PARAM_FIELDS.DIRECTION,
84
- 'Type': FUNC_PARAM_FIELDS.TYPE,
85
- 'Name': FUNC_PARAM_FIELDS.NAME,
86
- 'Default value': FUNC_PARAM_FIELDS.DEFAULT_VALUE,
87
- 'Description': FUNC_PARAM_FIELDS.DESCRIPTION,
88
- 'Category': FUNC_PARAM_FIELDS.CATEGORY,
89
- };
90
-
91
- enum COMMON_TAG_NAME {
92
- VALIDATORS = 'validators',
93
- CAPTION = 'caption',
94
- POSTFIX = 'postfix',
95
- UNITS = 'units',
96
- EDITOR = 'editor',
97
- }
98
-
99
- enum OPTIONAL_TAG_NAME {
100
- COLUMNS = 'columns',
101
- TYPE = 'type',
102
- FORMAT = 'format',
103
- ALLOW_NULLS = 'allowNulls',
104
- ACTION = 'action',
105
- CHOICES = 'choices',
106
- SUGGESTIONS = 'suggestions',
107
- SEM_TYPE = 'semType',
108
- MIN = 'min',
109
- MAX = 'max',
110
- }
111
-
112
- type DF_TAG_NAME = COMMON_TAG_NAME | OPTIONAL_TAG_NAME.COLUMNS;
113
- type COLUMN_TAG_NAME = COMMON_TAG_NAME |
114
- OPTIONAL_TAG_NAME.COLUMNS | OPTIONAL_TAG_NAME.FORMAT |
115
- OPTIONAL_TAG_NAME.ALLOW_NULLS | OPTIONAL_TAG_NAME.ACTION | OPTIONAL_TAG_NAME.SEM_TYPE
116
-
117
- type STRING_TAG_NAME = COMMON_TAG_NAME |
118
- OPTIONAL_TAG_NAME.CHOICES | OPTIONAL_TAG_NAME.SUGGESTIONS
119
-
120
- type INT_TAG_NAME = COMMON_TAG_NAME | OPTIONAL_TAG_NAME.MIN | OPTIONAL_TAG_NAME.MAX
121
-
122
- const COMMON_TAG_NAMES = [...Object.values(COMMON_TAG_NAME)];
123
- const DF_TAG_NAMES = [
124
- ...Object.values(COMMON_TAG_NAME),
125
- OPTIONAL_TAG_NAME.COLUMNS,
126
- OPTIONAL_TAG_NAME.ACTION];
127
- const COLUMN_TAG_NAMES = [...Object.values(COMMON_TAG_NAME), OPTIONAL_TAG_NAME.TYPE, OPTIONAL_TAG_NAME.FORMAT,
128
- OPTIONAL_TAG_NAME.ALLOW_NULLS, OPTIONAL_TAG_NAME.ACTION, OPTIONAL_TAG_NAME.SEM_TYPE];
129
- const STRING_TAG_NAMES = [...Object.values(COMMON_TAG_NAME), OPTIONAL_TAG_NAME.CHOICES, OPTIONAL_TAG_NAME.SUGGESTIONS];
130
- const INT_TAG_NAMES = [...Object.values(COMMON_TAG_NAME), OPTIONAL_TAG_NAME.MIN, OPTIONAL_TAG_NAME.MAX];
131
-
132
- type FuncParamBase = {
133
- direction: DIRECTION,
134
- name?: string,
135
- defaultValue?: string,
136
- description?: string
137
- }
138
-
139
- type FuncParam =
140
- FuncParamBase & {
141
- type: DG.TYPE.BOOL | DG.TYPE.DATE_TIME | DG.TYPE.FLOAT | DG.TYPE.GRAPHICS
142
- options?: {tag: COMMON_TAG_NAME, value: string}[]
143
- } | FuncParamBase & {
144
- type: DG.TYPE.INT
145
- options?: {tag: INT_TAG_NAME, value: string}[]
146
- } | FuncParamBase & {
147
- type: DG.TYPE.DATA_FRAME,
148
- options?: {tag: DF_TAG_NAME, value: string}[]
149
- } | FuncParamBase & {
150
- type: DG.TYPE.COLUMN_LIST | DG.TYPE.COLUMN,
151
- options?: {tag: COLUMN_TAG_NAME, value: string}[]
152
- } | FuncParamBase & {
153
- type: DG.TYPE.STRING
154
- options?: {tag: STRING_TAG_NAME, value: string}[]
155
- }
156
-
157
- const optionTags = ((param: DG.Property) => {
158
- switch (param.propertyType) {
159
- case DG.TYPE.INT:
160
- return INT_TAG_NAMES;
161
- case DG.TYPE.DATA_FRAME:
162
- return DF_TAG_NAMES;
163
- case DG.TYPE.COLUMN_LIST:
164
- case DG.TYPE.COLUMN:
165
- return COLUMN_TAG_NAMES;
166
- case DG.TYPE.STRING:
167
- return STRING_TAG_NAMES;
168
- default:
169
- return COMMON_TAG_NAMES;
170
- }
171
- });
172
-
173
- const generateParamLine = (param: DG.Property, direction: string) => {
174
- const optionTagsPreview = optionTags(param)
175
- .map((tag) => {
176
- return {tag: tag, val: param.options[tag]};
177
- })
178
- .filter((value) => !!value.val)
179
- .map(({tag, val}) => `${tag}: ${val}`)
180
- .concat(...param.category ? [`category: ${param.category}`]:[])
181
- .join('; ');
182
- const result =`#${direction}: ${param.propertyType} ${param.name? `${param.name} ` : ''}${param.defaultValue ? `= ${param.defaultValue} ` : ''}${!!optionTagsPreview.length ? `{${optionTagsPreview}} ` : ''}${param.description ? `[${param.description}]` : ''}\n`;
183
- return result;
184
- };
185
-
186
- export function _functionEditor(functionCode: string) {
187
- const inputScriptCopy = DG.Script.create(functionCode);
188
-
189
- const editorView = DG.View.create();
190
- editorView.name = 'Function Editor';
191
-
192
- const updateFuncPropValue = (param: DG.Property, propName: string, v: any) => {
193
- (inputScriptCopy as any)[propName] = v;
194
- refreshPreview();
195
- };
196
-
197
- const functionProps = {
198
- [functionPropsLabels(FUNC_PROPS_FIELDS.NAME)]: DG.Property.create(
199
- FUNC_PROPS_FIELDS.NAME, DG.TYPE.STRING, (x: any) => x[FUNC_PROPS_FIELDS.NAME],
200
- (x: any, v) => updateFuncPropValue(x, FUNC_PROPS_FIELDS.NAME, v),
201
- '',
202
- ),
203
- [functionPropsLabels(FUNC_PROPS_FIELDS.DESCRIPTION)]: DG.Property.create(
204
- FUNC_PROPS_FIELDS.DESCRIPTION, DG.TYPE.STRING, (x: any) => x[FUNC_PROPS_FIELDS.DESCRIPTION],
205
- (x: any, v) => updateFuncPropValue(x, FUNC_PROPS_FIELDS.DESCRIPTION, v),
206
- '',
207
- ),
208
- [functionPropsLabels(FUNC_PROPS_FIELDS.LANGUAGE)]: (() => {
209
- const temp = DG.Property.create(
210
- FUNC_PROPS_FIELDS.LANGUAGE, DG.TYPE.STRING, (x: any) => x[FUNC_PROPS_FIELDS.LANGUAGE],
211
- (x: any, v) => updateFuncPropValue(x, FUNC_PROPS_FIELDS.LANGUAGE, v),
212
- '');
213
- temp.fromOptions({choices: ['javascript', 'python', 'r', 'julia', 'octave', 'nodejs', 'grok']});
214
- return temp;
215
- })(),
216
- [functionPropsLabels(FUNC_PROPS_FIELDS.HELP_URL)]: DG.Property.create(
217
- FUNC_PROPS_FIELDS.HELP_URL, DG.TYPE.STRING, (x: any) => x[FUNC_PROPS_FIELDS.HELP_URL],
218
- (x: any, v) => updateFuncPropValue(x, FUNC_PROPS_FIELDS.HELP_URL, v),
219
- '',
220
- ),
221
- [functionPropsLabels(FUNC_PROPS_FIELDS.REFERENCE)]: DG.Property.create(
222
- FUNC_PROPS_FIELDS.REFERENCE, DG.TYPE.STRING, (x: any) => x[FUNC_PROPS_FIELDS.REFERENCE],
223
- (x: any, v) => updateFuncPropValue(x, FUNC_PROPS_FIELDS.REFERENCE, v),
224
- '',
225
- ),
226
- [functionPropsLabels(FUNC_PROPS_FIELDS.LOGIN)]: DG.Property.create(
227
- FUNC_PROPS_FIELDS.LOGIN, DG.TYPE.STRING, (x: any) => x[FUNC_PROPS_FIELDS.LOGIN],
228
- (x: any, v) => updateFuncPropValue(x, FUNC_PROPS_FIELDS.LOGIN, v),
229
- '',
230
- ),
231
- [functionPropsLabels(FUNC_PROPS_FIELDS.SAMPLE)]: DG.Property.create(
232
- FUNC_PROPS_FIELDS.SAMPLE, DG.TYPE.STRING, (x: any) => x[FUNC_PROPS_FIELDS.SAMPLE],
233
- (x: any, v) => updateFuncPropValue(x, FUNC_PROPS_FIELDS.SAMPLE, v),
234
- '',
235
- ),
236
- [functionPropsLabels(FUNC_PROPS_FIELDS.ENVIRONMENT)]: DG.Property.create(
237
- FUNC_PROPS_FIELDS.ENVIRONMENT, DG.TYPE.STRING, (x: any) => x[FUNC_PROPS_FIELDS.ENVIRONMENT],
238
- (x: any, v) => updateFuncPropValue(x, FUNC_PROPS_FIELDS.ENVIRONMENT, v),
239
- '',
240
- ),
241
- [functionPropsLabels(FUNC_PROPS_FIELDS.TAGS)]: DG.Property.create(
242
- FUNC_PROPS_FIELDS.TAGS, DG.TYPE.LIST,
243
- (x: any) => x[FUNC_PROPS_FIELDS.TAGS],
244
- (x: any, v) => updateFuncPropValue(x, FUNC_PROPS_FIELDS.TAGS, v),
245
- [],
246
- ),
247
- };
248
-
249
- const getNewProps = () => {
250
- const newProps = [];
251
- for (const prop of Object.values(functionProps)) {
252
- if (!prop.get(inputScriptCopy) && !(inputScriptCopy as any)[prop.name]) {
253
- newProps.push(prop);
254
- }
255
- }
256
- return newProps;
257
- };
258
-
259
- let addButton = ui.div();
260
- let propsForm = ui.div();
261
- let inputs: HTMLElement = ui.div();
262
-
263
-
264
- const functionPropsInput = (prop: DG.Property) => {
265
- switch (prop.name) {
266
- case FUNC_PROPS_FIELDS.LANGUAGE:
267
- return ui.choiceInput(
268
- functionPropsLabels(prop.name as FUNC_PROPS_FIELDS),
269
- prop.get(inputScriptCopy) || (inputScriptCopy as any)[prop.name],
270
- prop.choices,
271
- );
272
- // case FUNC_PROPS_FIELDS.TAGS:
273
- // return ui.multiChoiceInput(
274
- // functionPropsLabels(prop.name as FUNC_PROPS_FIELDS),
275
- // prop.get(inputScriptCopy) || (inputScriptCopy as any)[prop.name],
276
- // prop.choices,
277
- // );
278
- default:
279
- return ui.stringInput(functionPropsLabels(prop.name as FUNC_PROPS_FIELDS),
280
- prop.get(inputScriptCopy) || (inputScriptCopy as any)[prop.name]);
281
- }
282
- };
283
-
284
- const renderAddPropButton = () => {
285
- if (!getNewProps().length) return ui.div();
286
-
287
- const onItemClick = (item: DG.Property) => {
288
- item.set(inputScriptCopy, ' ');
289
- const newInputs = ui.inputs(
290
- Object.values(functionProps)
291
- .filter((prop) => !!prop.get(inputScriptCopy) || !!(inputScriptCopy as any)[prop.name])
292
- .map((prop) => addFullWidthInput(
293
- functionPropsInput(prop),
294
- prop,
295
- )),
296
- );
297
- inputs.replaceWith(newInputs);
298
- inputs = newInputs;
299
- refreshPreview();
300
- const newAddButton = renderAddPropButton();
301
- addButton.replaceWith(newAddButton);
302
- addButton = newAddButton;
303
- };
304
- const menu = DG.Menu.popup();
305
- getNewProps().forEach(
306
- (prop) => menu.item(functionPropsLabels(prop.name as FUNC_PROPS_FIELDS), () => onItemClick(prop)),
307
- );
308
-
309
- const shevron = ui.iconFA('chevron-down');
310
- const button = ui.button([ui.icons.add(()=>{}), shevron], () => {
311
- menu.show();
312
- });
313
-
314
- const div = ui.div([button]);
315
- div.style.cssText += 'margin-left: 152px';
316
- return div;
317
- };
318
-
319
- const addFullWidthInput = (input: DG.InputBase, prop: DG.Property) => {
320
- (input.root.lastChild as HTMLElement).style.cssText+='width: 400px; max-width: inherit;';
321
- input.onInput(() => {
322
- prop.set(inputScriptCopy, input.stringValue);
323
- refreshPreview();
324
- });
325
- (input.root as HTMLInputElement).placeholder = 'Enter your value...';
326
- input.root.append(ui.button(ui.icons.delete(()=>{}), () => {
327
- prop.set(inputScriptCopy, prop.defaultValue);
328
- refreshPreview();
329
- input.root.remove();
330
- const newAddButton = renderAddPropButton();
331
- addButton.replaceWith(newAddButton);
332
- addButton = newAddButton;
333
- }));
334
- (input.root.lastChild as HTMLElement).style.cssText+='display: inline-flex; justify-content: center; flex-direction: column';
335
- if (obligatoryFuncProps.includes(prop.name)) {
336
- input.root.style.cssText += 'padding-right: 35.375px';
337
- (input.root.lastChild as HTMLElement).style.cssText+='display: none;';
338
- }
339
- return input;
340
- };
341
-
342
- const functionPropsForm = () => {
343
- addButton = renderAddPropButton();
344
-
345
- inputs = ui.inputs(
346
- Object.values(functionProps)
347
- .filter((prop) => !!prop.get(inputScriptCopy) || !!(inputScriptCopy as any)[prop.name])
348
- .map((prop) => addFullWidthInput(
349
- functionPropsInput(prop),
350
- prop,
351
- )),
352
- );
353
- return ui.panel([
354
- ui.divV([
355
- inputs,
356
- addButton,
357
- ]),
358
- ]);
359
- };
360
-
361
- const functionInputParamsCopy = inputScriptCopy.inputs;
362
- const functionOutputParamsCopy = inputScriptCopy.outputs;
363
-
364
- const onFunctionParamClick = (paramName: string, direction: DIRECTION) => {
365
- let paramIndex: number;
366
- let param: DG.Property;
367
-
368
- if (direction === DIRECTION.INPUT) {
369
- paramIndex = functionInputParamsCopy.findIndex((input) => input.name === paramName);
370
- param = functionInputParamsCopy[paramIndex];
371
- } else {
372
- paramIndex= functionOutputParamsCopy.findIndex((input) => input.name === paramName);
373
- param = functionOutputParamsCopy[paramIndex];
374
- }
375
-
376
- if (!param) return ui.div('');
377
-
378
- const result = ui.input.form(param, optionalFuncParamsProps.filter(
379
- (prop) => optionTags(param).includes(prop.name as OPTIONAL_TAG_NAME)),
380
- );
381
-
382
- grok.shell.o = ui.divV([ui.h1(`Param: ${paramName}`), ui.block75([result])]);
383
- };
384
-
385
- const updateValue = (param: DG.Property, propName: string, v: any) => {
386
- (param as any)[propName] = v;
387
- const globalParam = functionOutputParamsCopy.find((input) => input.name === param.name) ||
388
- functionInputParamsCopy.find((input) => input.name === param.name);
389
- if (globalParam) globalParam.options[propName] = v;
390
- functionInputParamsState.next(functionInputParamsCopy);
391
- functionOutputParamsState.next(functionOutputParamsCopy);
392
- refreshPreview();
393
- };
394
-
395
- const obligatoryFuncParamsProps: DG.Property[] = [
396
- DG.Property.create(FUNC_PARAM_FIELDS.NAME, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.NAME],
397
- (x: any, v) => updateValue(x, FUNC_PARAM_FIELDS.NAME, v), ''),
398
- (() => {
399
- const temp = DG.Property.create(FUNC_PARAM_FIELDS.TYPE, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.TYPE], (x: any, v) => updateValue(x, FUNC_PARAM_FIELDS.TYPE, v), '');
400
- temp.fromOptions({choices: funcParamTypes});
401
- return temp;
402
- })(),
403
- DG.Property.create(FUNC_PARAM_FIELDS.DEFAULT_VALUE, DG.TYPE.OBJECT, (x: any) => x[FUNC_PARAM_FIELDS.DEFAULT_VALUE], (x: any, v) => updateValue(x, FUNC_PARAM_FIELDS.DEFAULT_VALUE, v), ''),
404
- (() => {
405
- const temp = DG.Property.create(FUNC_PARAM_FIELDS.DESCRIPTION, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.DESCRIPTION], (x: any, v) => updateValue(x, FUNC_PARAM_FIELDS.DESCRIPTION, v), '');
406
- temp.fromOptions({editor: 'textarea'});
407
- return temp;
408
- })(),
409
- DG.Property.create(FUNC_PARAM_FIELDS.CATEGORY, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.CATEGORY], (x: any, v) => updateValue(x, FUNC_PARAM_FIELDS.CATEGORY, v), ''),
410
- ];
411
-
412
- const optionalFuncParamsProps: DG.Property[] = [
413
- DG.Property.create(OPTIONAL_TAG_NAME.ACTION, DG.TYPE.STRING,
414
- (x: any) => x.options[OPTIONAL_TAG_NAME.ACTION],
415
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.ACTION, v), ''),
416
-
417
- DG.Property.create(OPTIONAL_TAG_NAME.ALLOW_NULLS, DG.TYPE.BOOL,
418
- (x: any) => x.options[OPTIONAL_TAG_NAME.ALLOW_NULLS],
419
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.ALLOW_NULLS, v), ''),
420
-
421
- DG.Property.create(OPTIONAL_TAG_NAME.CHOICES, DG.TYPE.LIST,
422
- (x: any) => x.options[OPTIONAL_TAG_NAME.CHOICES],
423
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.CHOICES, v), ''),
424
-
425
- (() => {
426
- const temp = DG.Property.create(OPTIONAL_TAG_NAME.COLUMNS, DG.TYPE.STRING,
427
- (x: any) => x.options[OPTIONAL_TAG_NAME.COLUMNS],
428
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.COLUMNS, v), '');
429
- temp.fromOptions({choices: ['numerical', 'categorical']});
430
- return temp;
431
- })(),
432
-
433
- DG.Property.create(OPTIONAL_TAG_NAME.FORMAT, DG.TYPE.STRING,
434
- (x: any) => x.options[OPTIONAL_TAG_NAME.FORMAT],
435
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.FORMAT, v), ''),
436
-
437
- DG.Property.create(OPTIONAL_TAG_NAME.MAX, DG.TYPE.INT,
438
- (x: any) => x.options[OPTIONAL_TAG_NAME.MAX],
439
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.MAX, v), ''),
440
-
441
- DG.Property.create(OPTIONAL_TAG_NAME.MIN, DG.TYPE.INT,
442
- (x: any) => x.options[OPTIONAL_TAG_NAME.MIN],
443
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.MIN, v), ''),
444
-
445
- DG.Property.create(OPTIONAL_TAG_NAME.SEM_TYPE, DG.TYPE.STRING,
446
- (x: any) => x.options[OPTIONAL_TAG_NAME.SEM_TYPE],
447
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.SEM_TYPE, v), ''),
448
-
449
- DG.Property.create(OPTIONAL_TAG_NAME.SUGGESTIONS, DG.TYPE.STRING,
450
- (x: any) => x.options[OPTIONAL_TAG_NAME.SUGGESTIONS],
451
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.SUGGESTIONS, v), ''),
452
-
453
- (() => {
454
- const temp = DG.Property.create(OPTIONAL_TAG_NAME.TYPE, DG.TYPE.STRING,
455
- (x: any) => x.options[OPTIONAL_TAG_NAME.TYPE],
456
- (x: any, v) => updateValue(x, OPTIONAL_TAG_NAME.TYPE, v), '');
457
- temp.fromOptions({choices: ['numerical', 'categorical', 'dateTime']});
458
- return temp;
459
- })(),
460
- ];
461
-
462
- const inputParamsDF = DG.DataFrame.create(functionInputParamsCopy.length);
463
- for (const p of obligatoryFuncParamsProps) {
464
- (inputParamsDF.columns as DG.ColumnList)
465
- .addNew(functionParamsMapping[p.name as keyof typeof functionParamsMapping], p.propertyType as DG.ColumnType)
466
- .init((i: number) => {
467
- return p.get(functionInputParamsCopy[i]) || (p as any)[functionInputParamsCopy[i].name];
468
- });
469
- }
470
- const inputParamGrid = DG.Grid.create(inputParamsDF);
471
- inputParamGrid.root.style.width = '100%';
472
- inputParamGrid.dataFrame?.getCol(functionParamsMapping[FUNC_PARAM_FIELDS.TYPE as keyof typeof functionParamsMapping])
473
- .setTag(DG.TAGS.CHOICES, `["${funcParamTypes.join(`", "`)}"]`);
474
- inputParamsDF.onCurrentRowChanged.subscribe(() => onFunctionParamClick((inputParamsDF.currentRow as any)['Name'], DIRECTION.INPUT));
475
-
476
- const outputParamsDF = DG.DataFrame.create(functionOutputParamsCopy.length);
477
- for (const p of obligatoryFuncParamsProps) {
478
- (outputParamsDF.columns as DG.ColumnList)
479
- .addNew(functionParamsMapping[p.name as keyof typeof functionParamsMapping], p.propertyType as DG.ColumnType)
480
- .init((i: number) => p.get(functionInputParamsCopy[i]) || (p as any)[functionInputParamsCopy[i].name]);
481
- }
482
- const outputParamGrid = DG.Grid.create(outputParamsDF);
483
- outputParamGrid.dataFrame?.getCol(functionParamsMapping[FUNC_PARAM_FIELDS.TYPE as keyof typeof functionParamsMapping])
484
- .setTag(DG.TAGS.CHOICES, `["${funcParamTypes.join(`", "`)}"]`);
485
- outputParamsDF.onCurrentRowChanged.subscribe(() => onFunctionParamClick((outputParamsDF.currentRow as any)['Name'], DIRECTION.OUTPUT));
486
- outputParamGrid.root.style.width = '100%';
487
-
488
- const area = ui.textInput('', '');
489
- const myCM = CodeMirror.fromTextArea((area.input as HTMLTextAreaElement));
490
-
491
- const previewDiv = ui.panel([ui.divV([ui.h1('Code preview'), area])]);
492
- previewDiv.style.flexGrow = '1';
493
-
494
- propsForm = functionPropsForm();
495
-
496
- const renderAddParamButton = () => {
497
- const paramProps = obligatoryFuncParamsProps
498
- .filter((param) => param.name !== FUNC_PARAM_FIELDS.DIRECTION);
499
-
500
- const newParam = {
501
- [FUNC_PARAM_FIELDS.NAME]: '',
502
- [FUNC_PARAM_FIELDS.TYPE]: DG.TYPE.BOOL,
503
- [FUNC_PARAM_FIELDS.DEFAULT_VALUE]: false,
504
- [FUNC_PARAM_FIELDS.DESCRIPTION]: '',
505
- [FUNC_PARAM_FIELDS.CATEGORY]: '',
506
- };
507
-
508
- const onItemClick = () => {
509
- ui.dialog('Add parameter')
510
- .add(ui.input.form(newParam, paramProps))
511
- .onOK(() => {
512
- inputParamsDF.rows.addNew(paramProps.map((prop) => prop.get(newParam)));
513
- refreshPreview();
514
- })
515
- .show();
516
- };
517
-
518
- const menu = DG.Menu.popup().items(funcParamTypes, onItemClick);
519
-
520
- const shevron = ui.iconFA('chevron-down');
521
- const button = ui.button([ui.icons.add(()=>{}), shevron], () => {
522
- menu.show();
523
- });
524
-
525
- const div = ui.div([button]);
526
- return div;
527
- };
528
- const addInputButton = renderAddParamButton();
529
- const addOutputButton = renderAddParamButton();
530
-
531
- const tabs = ui.tabControl({
532
- 'PROPERTIES': propsForm,
533
- 'PARAMETERS': ui.divV([
534
- ui.div(ui.h1('Input: '), {style: {'padding': '12px 12px 0px 12px'}}),
535
- inputParamGrid.root,
536
- addInputButton,
537
- ui.div(ui.h1('Output: '), {style: {'padding': '12px 12px 0px 12px'}}),
538
- outputParamGrid.root,
539
- addOutputButton,
540
- ]),
541
- });
542
- tabs.root.style.width = '100%';
543
- tabs.root.style.flexGrow = '3';
544
-
545
- editorView.append(
546
- ui.divV([
547
- tabs,
548
- previewDiv,
549
- ]),
550
- );
551
- editorView.box = true;
552
- editorView.setRibbonPanels([
553
- [
554
- ui.iconFA('eye', () => {
555
- previewDiv.hidden ? previewDiv.hidden = false : previewDiv.hidden = true;
556
- }),
557
- ui.bigButton('Save', () => grok.shell.info('plus')),
558
- ],
559
- ]);
560
-
561
- const refreshPreview = () => {
562
- let result = '';
563
- Object.values(functionProps).map((propField) => {
564
- const propValue = propField.get(inputScriptCopy) || (inputScriptCopy as any)[propField.name];
565
- if (!!propValue && !!propValue.length) {
566
- result += `#${functionPropsCode(propField.name as FUNC_PROPS_FIELDS)}: ${propValue}\n`;
567
- }
568
- });
569
- functionInputParamsCopy.map((param) => {
570
- result += generateParamLine(param, DIRECTION.INPUT);
571
- });
572
- functionOutputParamsCopy.map((param) => {
573
- result += generateParamLine(param, DIRECTION.OUTPUT);
574
- });
575
- myCM.setValue(result);
576
- };
577
-
578
- grok.shell.addView(editorView);
579
- refreshPreview();
580
-
581
- const functionInputParamsState = new BehaviorSubject(functionInputParamsCopy);
582
- functionInputParamsState.subscribe(() => {
583
- for (const p of obligatoryFuncParamsProps) {
584
- (inputParamsDF.columns as DG.ColumnList)
585
- .byName(functionParamsMapping[p.name as keyof typeof functionParamsMapping])
586
- .init((i: number) => {
587
- return p.get(functionInputParamsCopy[i]) || (functionInputParamsCopy[i] as any)[p.name];
588
- });
589
- }
590
- refreshPreview();
591
- });
592
-
593
- const functionOutputParamsState = new BehaviorSubject(functionOutputParamsCopy);
594
- functionOutputParamsState.subscribe(() => {
595
- for (const p of obligatoryFuncParamsProps) {
596
- (outputParamsDF.columns as DG.ColumnList)
597
- .byName(functionParamsMapping[p.name as keyof typeof functionParamsMapping])
598
- .init((i: number) => {
599
- return p.get(functionOutputParamsCopy[i]) || (functionOutputParamsCopy[i] as any)[p.name];
600
- });
601
- }
602
- refreshPreview();
603
- });
604
-
605
- inputParamGrid.onCellValueEdited.subscribe((editedCell)=> {
606
- (functionInputParamsCopy[editedCell.cell.rowIndex] as any)
607
- [functionParamsMapping[editedCell.cell.column.name as keyof typeof functionParamsMapping]] = editedCell.cell.value;
608
- functionInputParamsState.next(functionInputParamsCopy);
609
- });
610
- outputParamGrid.onCellValueEdited.subscribe((editedCell)=> {
611
- (functionOutputParamsCopy[editedCell.cell.rowIndex] as any)
612
- [functionParamsMapping[editedCell.cell.column.name as keyof typeof functionParamsMapping]] = editedCell.cell.value;
613
- functionOutputParamsState.next(functionOutputParamsCopy);
614
- });
615
- }