@datagrok/compute 0.6.1 → 0.6.5

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.
@@ -3,7 +3,7 @@ import * as ui from 'datagrok-api/ui';
3
3
  import * as DG from 'datagrok-api/dg';
4
4
  import CodeMirror from 'codemirror';
5
5
  import 'codemirror/lib/codemirror.css';
6
- import {_package} from '../package';
6
+ import {BehaviorSubject} from 'rxjs';
7
7
 
8
8
  const enum FUNC_PROPS_FIELDS {
9
9
  NAME = 'name',
@@ -33,7 +33,7 @@ const functionPropsLabels = (key: FUNC_PROPS_FIELDS) => {
33
33
  }
34
34
  };
35
35
 
36
- const functionPropsCode = (key: FUNC_PROPS_FIELDS) => {
36
+ const functionPropsCode = (key: string) => {
37
37
  switch (key) {
38
38
  case FUNC_PROPS_FIELDS.NAME: return 'name';
39
39
  case FUNC_PROPS_FIELDS.DESCRIPTION: return 'description';
@@ -45,19 +45,7 @@ const functionPropsCode = (key: FUNC_PROPS_FIELDS) => {
45
45
  case FUNC_PROPS_FIELDS.ENVIRONMENT: return 'environment';
46
46
  case FUNC_PROPS_FIELDS.TAGS: return 'tags';
47
47
  }
48
- };
49
-
50
- type FuncPropsState = {
51
- [FUNC_PROPS_FIELDS.NAME]: string | null,
52
- [FUNC_PROPS_FIELDS.DESCRIPTION]: string | null,
53
- [FUNC_PROPS_FIELDS.LANGUAGE]: string | null,
54
- [FUNC_PROPS_FIELDS.HELP_URL]: string | null,
55
- [FUNC_PROPS_FIELDS.REFERENCE]: string | null,
56
- [FUNC_PROPS_FIELDS.LOGIN]: string | null,
57
- [FUNC_PROPS_FIELDS.SAMPLE]: string | null,
58
- [FUNC_PROPS_FIELDS.ENVIRONMENT]: string | null,
59
- [FUNC_PROPS_FIELDS.TAGS]: string | null,
60
- }
48
+ }; ;
61
49
 
62
50
  const funcParamTypes = [
63
51
  DG.TYPE.BOOL,
@@ -85,15 +73,19 @@ const enum DIRECTION {
85
73
  OUTPUT= 'output',
86
74
  }
87
75
 
88
- const functionParamsLabels = (key: FUNC_PARAM_FIELDS) => {
89
- switch (key) {
90
- case FUNC_PARAM_FIELDS.DIRECTION: return 'Direction';
91
- case FUNC_PARAM_FIELDS.TYPE: return 'Type';
92
- case FUNC_PARAM_FIELDS.NAME: return 'Name';
93
- case FUNC_PARAM_FIELDS.DEFAULT_VALUE: return 'Default value';
94
- case FUNC_PARAM_FIELDS.DESCRIPTION: return 'Description';
95
- case FUNC_PARAM_FIELDS.CATEGORY: return 'Category';
96
- }
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,
97
89
  };
98
90
 
99
91
  enum COMMON_TAG_NAME {
@@ -106,7 +98,6 @@ enum COMMON_TAG_NAME {
106
98
 
107
99
  enum OPTIONAL_TAG_NAME {
108
100
  COLUMNS = 'columns',
109
- CATEGORICAL = 'categorical',
110
101
  TYPE = 'type',
111
102
  FORMAT = 'format',
112
103
  ALLOW_NULLS = 'allowNulls',
@@ -118,7 +109,7 @@ enum OPTIONAL_TAG_NAME {
118
109
  MAX = 'max',
119
110
  }
120
111
 
121
- type DF_TAG_NAME = COMMON_TAG_NAME | OPTIONAL_TAG_NAME.COLUMNS | OPTIONAL_TAG_NAME.CATEGORICAL;
112
+ type DF_TAG_NAME = COMMON_TAG_NAME | OPTIONAL_TAG_NAME.COLUMNS;
122
113
  type COLUMN_TAG_NAME = COMMON_TAG_NAME |
123
114
  OPTIONAL_TAG_NAME.COLUMNS | OPTIONAL_TAG_NAME.FORMAT |
124
115
  OPTIONAL_TAG_NAME.ALLOW_NULLS | OPTIONAL_TAG_NAME.ACTION | OPTIONAL_TAG_NAME.SEM_TYPE
@@ -132,9 +123,8 @@ const COMMON_TAG_NAMES = [...Object.values(COMMON_TAG_NAME)];
132
123
  const DF_TAG_NAMES = [
133
124
  ...Object.values(COMMON_TAG_NAME),
134
125
  OPTIONAL_TAG_NAME.COLUMNS,
135
- OPTIONAL_TAG_NAME.CATEGORICAL,
136
126
  OPTIONAL_TAG_NAME.ACTION];
137
- const COLUMN_TAG_NAMES = [...Object.values(COMMON_TAG_NAME), OPTIONAL_TAG_NAME.COLUMNS, OPTIONAL_TAG_NAME.FORMAT,
127
+ const COLUMN_TAG_NAMES = [...Object.values(COMMON_TAG_NAME), OPTIONAL_TAG_NAME.TYPE, OPTIONAL_TAG_NAME.FORMAT,
138
128
  OPTIONAL_TAG_NAME.ALLOW_NULLS, OPTIONAL_TAG_NAME.ACTION, OPTIONAL_TAG_NAME.SEM_TYPE];
139
129
  const STRING_TAG_NAMES = [...Object.values(COMMON_TAG_NAME), OPTIONAL_TAG_NAME.CHOICES, OPTIONAL_TAG_NAME.SUGGESTIONS];
140
130
  const INT_TAG_NAMES = [...Object.values(COMMON_TAG_NAME), OPTIONAL_TAG_NAME.MIN, OPTIONAL_TAG_NAME.MAX];
@@ -148,11 +138,11 @@ type FuncParamBase = {
148
138
 
149
139
  type FuncParam =
150
140
  FuncParamBase & {
151
- type: DG.TYPE.BOOL | DG.TYPE.DATE_TIME | DG.TYPE.FLOAT | DG.TYPE.GRAPHICS | DG.TYPE.INT
141
+ type: DG.TYPE.BOOL | DG.TYPE.DATE_TIME | DG.TYPE.FLOAT | DG.TYPE.GRAPHICS
152
142
  options?: {tag: COMMON_TAG_NAME, value: string}[]
153
143
  } | FuncParamBase & {
154
144
  type: DG.TYPE.INT
155
- options?: {tag: COMMON_TAG_NAME, value: string}[]
145
+ options?: {tag: INT_TAG_NAME, value: string}[]
156
146
  } | FuncParamBase & {
157
147
  type: DG.TYPE.DATA_FRAME,
158
148
  options?: {tag: DF_TAG_NAME, value: string}[]
@@ -164,57 +154,103 @@ type FuncParam =
164
154
  options?: {tag: STRING_TAG_NAME, value: string}[]
165
155
  }
166
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
+
167
173
  const generateParamLine = (param: DG.Property, direction: string) => {
168
- const optionTags = (() => {
169
- switch (param.propertyType) {
170
- case DG.TYPE.INT:
171
- return INT_TAG_NAMES;
172
- case DG.TYPE.DATA_FRAME:
173
- return DF_TAG_NAMES;
174
- case DG.TYPE.COLUMN_LIST:
175
- case DG.TYPE.COLUMN:
176
- return COLUMN_TAG_NAMES;
177
- case DG.TYPE.STRING:
178
- return STRING_TAG_NAMES;
179
- default:
180
- return COMMON_TAG_NAMES;
181
- }
182
- })();
183
- const optionTagsPreview = optionTags
174
+ const optionTagsPreview = optionTags(param)
184
175
  .map((tag) => {
185
- // console.log(param.name, tag, JSON.stringify(param.options), param.options[tag]);
186
176
  return {tag: tag, val: param.options[tag]};
187
177
  })
188
178
  .filter((value) => !!value.val)
189
- .map(({tag, val}) => `${tag}:${val}`)
179
+ .map(({tag, val}) => `${tag}: ${val}`)
180
+ .concat(...param.category ? [`category: ${param.category}`]:[])
190
181
  .join('; ');
191
182
  const result =`#${direction}: ${param.propertyType} ${param.name? `${param.name} ` : ''}${param.defaultValue ? `= ${param.defaultValue} ` : ''}${!!optionTagsPreview.length ? `{${optionTagsPreview}} ` : ''}${param.description ? `[${param.description}]` : ''}\n`;
192
183
  return result;
193
184
  };
194
185
 
195
186
  export function _functionEditor(functionCode: string) {
196
- const inputScript = DG.Script.create(functionCode);
187
+ const inputScriptCopy = DG.Script.create(functionCode);
197
188
 
198
189
  const editorView = DG.View.create();
199
190
  editorView.name = 'Function Editor';
200
191
 
201
- const functionPropsCopy: FuncPropsState = {
202
- [FUNC_PROPS_FIELDS.NAME]: inputScript.name || null,
203
- [FUNC_PROPS_FIELDS.DESCRIPTION]: inputScript.description || null,
204
- [FUNC_PROPS_FIELDS.LANGUAGE]: inputScript.language || null,
205
- [FUNC_PROPS_FIELDS.HELP_URL]: inputScript.helpUrl || null,
206
- [FUNC_PROPS_FIELDS.REFERENCE]: inputScript.reference || null,
207
- [FUNC_PROPS_FIELDS.LOGIN]: inputScript.author?.login || null,
208
- [FUNC_PROPS_FIELDS.SAMPLE]: inputScript.sample || null,
209
- [FUNC_PROPS_FIELDS.ENVIRONMENT]: inputScript.environment || null,
210
- [FUNC_PROPS_FIELDS.TAGS]: inputScript.tags?.join(' ') || null,
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
+ ),
211
247
  };
212
248
 
213
249
  const getNewProps = () => {
214
250
  const newProps = [];
215
- for (const key of Object.keys(functionPropsCopy)) {
216
- if (functionPropsCopy[key as keyof FuncPropsState] == null) {
217
- newProps.push(key);
251
+ for (const prop of Object.values(functionProps)) {
252
+ if (!prop.get(inputScriptCopy) && !(inputScriptCopy as any)[prop.name]) {
253
+ newProps.push(prop);
218
254
  }
219
255
  }
220
256
  return newProps;
@@ -222,20 +258,40 @@ export function _functionEditor(functionCode: string) {
222
258
 
223
259
  let addButton = ui.div();
224
260
  let propsForm = ui.div();
225
- let inputs = 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
+ };
226
283
 
227
284
  const renderAddPropButton = () => {
228
285
  if (!getNewProps().length) return ui.div();
229
286
 
230
- const onItemClick = (item: string) => {
231
- functionPropsCopy[item as keyof FuncPropsState] = '';
287
+ const onItemClick = (item: DG.Property) => {
288
+ item.set(inputScriptCopy, ' ');
232
289
  const newInputs = ui.inputs(
233
- Object.keys(functionPropsCopy)
234
- .filter((prop) => functionPropsCopy[prop as keyof FuncPropsState] !== null)
290
+ Object.values(functionProps)
291
+ .filter((prop) => !!prop.get(inputScriptCopy) || !!(inputScriptCopy as any)[prop.name])
235
292
  .map((prop) => addFullWidthInput(
236
- ui.stringInput(
237
- functionPropsLabels(prop as keyof FuncPropsState),
238
- functionPropsCopy[prop as keyof FuncPropsState] || ''), prop,
293
+ functionPropsInput(prop),
294
+ prop,
239
295
  )),
240
296
  );
241
297
  inputs.replaceWith(newInputs);
@@ -245,9 +301,10 @@ export function _functionEditor(functionCode: string) {
245
301
  addButton.replaceWith(newAddButton);
246
302
  addButton = newAddButton;
247
303
  };
248
- const menu = DG.Menu.popup().items(getNewProps().map(
249
- (propName) => functionPropsLabels(propName as FUNC_PROPS_FIELDS),
250
- ), onItemClick);
304
+ const menu = DG.Menu.popup();
305
+ getNewProps().forEach(
306
+ (prop) => menu.item(functionPropsLabels(prop.name as FUNC_PROPS_FIELDS), () => onItemClick(prop)),
307
+ );
251
308
 
252
309
  const shevron = ui.iconFA('chevron-down');
253
310
  const button = ui.button([ui.icons.add(()=>{}), shevron], () => {
@@ -259,15 +316,15 @@ export function _functionEditor(functionCode: string) {
259
316
  return div;
260
317
  };
261
318
 
262
- const addFullWidthInput = (input: DG.InputBase, propName: string) => {
319
+ const addFullWidthInput = (input: DG.InputBase, prop: DG.Property) => {
263
320
  (input.root.lastChild as HTMLElement).style.cssText+='width: 400px; max-width: inherit;';
264
321
  input.onInput(() => {
265
- functionPropsCopy[propName as keyof typeof functionPropsCopy] = input.stringValue;
322
+ prop.set(inputScriptCopy, input.stringValue);
266
323
  refreshPreview();
267
324
  });
268
325
  (input.root as HTMLInputElement).placeholder = 'Enter your value...';
269
326
  input.root.append(ui.button(ui.icons.delete(()=>{}), () => {
270
- functionPropsCopy[propName as keyof typeof functionPropsCopy] = null;
327
+ prop.set(inputScriptCopy, prop.defaultValue);
271
328
  refreshPreview();
272
329
  input.root.remove();
273
330
  const newAddButton = renderAddPropButton();
@@ -275,7 +332,7 @@ export function _functionEditor(functionCode: string) {
275
332
  addButton = newAddButton;
276
333
  }));
277
334
  (input.root.lastChild as HTMLElement).style.cssText+='display: inline-flex; justify-content: center; flex-direction: column';
278
- if (obligatoryFuncProps.includes(propName)) {
335
+ if (obligatoryFuncProps.includes(prop.name)) {
279
336
  input.root.style.cssText += 'padding-right: 35.375px';
280
337
  (input.root.lastChild as HTMLElement).style.cssText+='display: none;';
281
338
  }
@@ -286,12 +343,11 @@ export function _functionEditor(functionCode: string) {
286
343
  addButton = renderAddPropButton();
287
344
 
288
345
  inputs = ui.inputs(
289
- Object.keys(functionPropsCopy)
290
- .filter((prop) => !!functionPropsCopy[prop as keyof FuncPropsState])
346
+ Object.values(functionProps)
347
+ .filter((prop) => !!prop.get(inputScriptCopy) || !!(inputScriptCopy as any)[prop.name])
291
348
  .map((prop) => addFullWidthInput(
292
- ui.stringInput(
293
- functionPropsLabels(prop as keyof FuncPropsState),
294
- functionPropsCopy[prop as keyof FuncPropsState] || ''), prop,
349
+ functionPropsInput(prop),
350
+ prop,
295
351
  )),
296
352
  );
297
353
  return ui.panel([
@@ -302,147 +358,132 @@ export function _functionEditor(functionCode: string) {
302
358
  ]);
303
359
  };
304
360
 
305
- let functionInputParamsCopy = inputScript.inputs;
306
- let functionOutputParamsCopy = inputScript.outputs;
307
-
308
- const iconsByTypes = (type: DG.TYPE) => {
309
- switch (type) {
310
- case DG.TYPE.BOOL: return ui.iconImage(DG.TYPE.BOOL, `${_package.webRoot}/icons/Bool.svg`);
311
- case DG.TYPE.COLUMN_LIST: return ui.iconImage(DG.TYPE.BOOL, `${_package.webRoot}/icons/Column-list.svg`);
312
- case DG.TYPE.COLUMN: return ui.iconImage(DG.TYPE.COLUMN, `${_package.webRoot}/icons/Column.svg`);
313
- case DG.TYPE.DATA_FRAME: return ui.iconImage(DG.TYPE.DATA_FRAME, `${_package.webRoot}/icons/Dataframe.svg`);
314
- case DG.TYPE.DATE_TIME: return ui.iconImage(DG.TYPE.COLUMN, `${_package.webRoot}/icons/Datetime.svg`);
315
- case DG.TYPE.FLOAT: return ui.iconImage(DG.TYPE.FLOAT, `${_package.webRoot}/icons/Double.svg`);
316
- case DG.TYPE.GRAPHICS: return ui.iconImage(DG.TYPE.GRAPHICS, `${_package.webRoot}/icons/Graphics.svg`);
317
- case DG.TYPE.INT: return ui.iconImage(DG.TYPE.FLOAT, `${_package.webRoot}/icons/Int.svg`);
318
- case DG.TYPE.STRING: return ui.iconImage(DG.TYPE.STRING, `${_package.webRoot}/icons/String.svg`);
319
- default: return ui.iconImage('Blob', `${_package.webRoot}/icons/Blob.svg`);
320
- }
321
- };
361
+ const functionInputParamsCopy = inputScriptCopy.inputs;
362
+ const functionOutputParamsCopy = inputScriptCopy.outputs;
322
363
 
323
- const onFunctionParamClick = (paramName: string, card: HTMLElement, direction: DIRECTION) => {
324
- const acc = ui.accordion();
325
- acc.root.style.cssText += 'min-width: 400px';
364
+ const onFunctionParamClick = (paramName: string, direction: DIRECTION) => {
365
+ let paramIndex: number;
366
+ let param: DG.Property;
326
367
 
327
- acc.addPane('General', ()=> {
328
- let paramIndex: number;
329
- let param: DG.Property;
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
+ }
330
375
 
331
- if (direction === DIRECTION.INPUT) {
332
- paramIndex = functionInputParamsCopy.findIndex((input) => input.name === paramName);
333
- param = functionInputParamsCopy[paramIndex];
334
- } else {
335
- paramIndex= functionOutputParamsCopy.findIndex((input) => input.name === paramName);
336
- param = functionOutputParamsCopy[paramIndex];
337
- }
376
+ if (!param) return ui.div('');
338
377
 
339
- if (!param) return ui.div('');
340
-
341
- const updateCard = (propName: string, v: any) => {
342
- (param as any)[propName] = v;
343
- const newCard = renderCard(param, direction);
344
- card.replaceWith(newCard);
345
- card = newCard;
346
- refreshPreview();
347
- };
348
-
349
- const funcParams: DG.Property[] = [
350
- (() => {
351
- const temp = DG.Property.create(FUNC_PARAM_FIELDS.DIRECTION, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.DIRECTION], (x: any, v) => updateCard(FUNC_PARAM_FIELDS.DIRECTION, v), '');
352
- temp.options({choices: ['input', 'output']});
353
- return temp;
354
- })(),
355
- (() => {
356
- const temp = DG.Property.create(FUNC_PARAM_FIELDS.TYPE, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.TYPE], (x: any, v) => updateCard(FUNC_PARAM_FIELDS.TYPE, v), '');
357
- temp.options({choices: funcParamTypes});
358
- return temp;
359
- })(),
360
- DG.Property.create(FUNC_PARAM_FIELDS.NAME, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.NAME], (x: any, v) => updateCard(FUNC_PARAM_FIELDS.NAME, v), ''),
361
- DG.Property.create(FUNC_PARAM_FIELDS.DEFAULT_VALUE, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.DEFAULT_VALUE], (x: any, v) => updateCard(FUNC_PARAM_FIELDS.DEFAULT_VALUE, v), ''),
362
- (() => {
363
- const temp = DG.Property.create(FUNC_PARAM_FIELDS.DESCRIPTION, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.DESCRIPTION], (x: any, v) => updateCard(FUNC_PARAM_FIELDS.DESCRIPTION, v), '');
364
- temp.options({editor: 'textarea'});
365
- return temp;
366
- })(),
367
- DG.Property.create(FUNC_PARAM_FIELDS.CATEGORY, DG.TYPE.STRING, (x: any) => x[FUNC_PARAM_FIELDS.CATEGORY], (x: any, v) => updateCard(FUNC_PARAM_FIELDS.CATEGORY, v), ''),
368
- ];
369
-
370
- const paramProps = funcParams
371
- .filter((param) => param.name !== FUNC_PARAM_FIELDS.DIRECTION);
372
-
373
- const result = ui.input.form(param, paramProps);
374
-
375
- return ui.div(result);
376
- });
378
+ const result = ui.input.form(param, optionalFuncParamsProps.filter(
379
+ (prop) => optionTags(param).includes(prop.name as OPTIONAL_TAG_NAME)),
380
+ );
377
381
 
378
- acc.addPane('Tags', ()=> {
379
- return ui.div();
380
- });
382
+ grok.shell.o = ui.divV([ui.h1(`Param: ${paramName}`), ui.block75([result])]);
383
+ };
381
384
 
382
- grok.shell.o = ui.divV([ui.h1(`Param: ${paramName}`), acc.root]);
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();
383
393
  };
384
394
 
385
- const funcParams: DG.Property[] = [
386
- DG.Property.jsString(FUNC_PARAM_FIELDS.DIRECTION),
387
- DG.Property.jsString(FUNC_PARAM_FIELDS.TYPE),
388
- DG.Property.jsString(FUNC_PARAM_FIELDS.NAME),
389
- DG.Property.jsString(FUNC_PARAM_FIELDS.DEFAULT_VALUE),
390
- DG.Property.jsString(FUNC_PARAM_FIELDS.DESCRIPTION),
391
- DG.Property.jsString(FUNC_PARAM_FIELDS.CATEGORY),
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), ''),
392
410
  ];
393
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
+ ];
394
461
 
395
- const renderCard = (functionParam: DG.Property, direction: DIRECTION) => {
396
- const iconStyle = {style: {
397
- 'display': 'flex',
398
- 'flex-direction': 'column',
399
- 'justify-content': 'center',
400
- 'width': '32.5px',
401
- 'align-items': 'center',
402
- }};
403
-
404
- const editButtonDiv = ui.div(ui.button(ui.icons.edit(()=>{}), () =>{
405
- onFunctionParamClick(functionParam.name, tempCard, direction);
406
- }), {
407
- style: {
408
- ...iconStyle.style,
409
- 'visibility': 'hidden',
410
- },
411
- });
412
-
413
- const tempCard = ui.divH([
414
- ui.div(iconsByTypes(functionParam.propertyType), iconStyle),
415
- ui.divV([
416
- ui.span([functionParam.name], {style: {'font-weight': 'bold'}}),
417
- ui.span([funcParams
418
- .filter((param) => param.name !== FUNC_PARAM_FIELDS.NAME)
419
- .map((param) => (functionParam as any)[param.name])
420
- .filter((paramValue) => !!paramValue).join(' | '),
421
- ]),
422
- ], {style: {'width': '100%', 'padding': '5px'}}),
423
- editButtonDiv,
424
- ui.div(ui.button(ui.icons.delete(()=>{}), () =>{
425
- if (direction === DIRECTION.INPUT) {
426
- functionInputParamsCopy = functionInputParamsCopy.filter((param) => param !== functionParam);
427
- } else {
428
- functionOutputParamsCopy = functionOutputParamsCopy.filter((param) => param !== functionParam);
429
- }
430
- refreshPreview();
431
- tempCard.remove();
432
- }), iconStyle),
433
- ], {style: {'border-bottom': 'thin solid #ECEFF2'}});
434
- tempCard.addEventListener('click', () => onFunctionParamClick(functionParam.name, tempCard, direction));
435
- tempCard.addEventListener('mouseover', () => editButtonDiv.style.visibility = 'visible');
436
- tempCard.addEventListener('mouseout', () => editButtonDiv.style.visibility = 'hidden');
437
- return tempCard;
438
- };
439
-
440
- let inputParamCards = ui.divV(
441
- functionInputParamsCopy.map((functionParam) => renderCard(functionParam, DIRECTION.INPUT)),
442
- );
443
- const outputParamCards = ui.divV(
444
- functionOutputParamsCopy.map((functionParam) => renderCard(functionParam, DIRECTION.OUTPUT)),
445
- );
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%';
446
487
 
447
488
  const area = ui.textInput('', '');
448
489
  const myCM = CodeMirror.fromTextArea((area.input as HTMLTextAreaElement));
@@ -453,21 +494,22 @@ export function _functionEditor(functionCode: string) {
453
494
  propsForm = functionPropsForm();
454
495
 
455
496
  const renderAddParamButton = () => {
456
- const paramProps = funcParams
497
+ const paramProps = obligatoryFuncParamsProps
457
498
  .filter((param) => param.name !== FUNC_PARAM_FIELDS.DIRECTION);
458
499
 
459
- const newParam: DG.PropertyOptions = {};
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
+ };
460
507
 
461
508
  const onItemClick = () => {
462
509
  ui.dialog('Add parameter')
463
510
  .add(ui.input.form(newParam, paramProps))
464
511
  .onOK(() => {
465
- functionInputParamsCopy.push(DG.Property.fromOptions(newParam));
466
- const newInputParamsCards = ui.divV(
467
- functionInputParamsCopy.map((functionParam) => renderCard(functionParam, DIRECTION.INPUT)),
468
- );
469
- inputParamCards.replaceWith(newInputParamsCards);
470
- inputParamCards = newInputParamsCards;
512
+ inputParamsDF.rows.addNew(paramProps.map((prop) => prop.get(newParam)));
471
513
  refreshPreview();
472
514
  })
473
515
  .show();
@@ -490,10 +532,10 @@ export function _functionEditor(functionCode: string) {
490
532
  'PROPERTIES': propsForm,
491
533
  'PARAMETERS': ui.divV([
492
534
  ui.div(ui.h1('Input: '), {style: {'padding': '12px 12px 0px 12px'}}),
493
- inputParamCards,
535
+ inputParamGrid.root,
494
536
  addInputButton,
495
537
  ui.div(ui.h1('Output: '), {style: {'padding': '12px 12px 0px 12px'}}),
496
- outputParamCards,
538
+ outputParamGrid.root,
497
539
  addOutputButton,
498
540
  ]),
499
541
  });
@@ -518,9 +560,11 @@ export function _functionEditor(functionCode: string) {
518
560
 
519
561
  const refreshPreview = () => {
520
562
  let result = '';
521
- Object.keys(functionPropsCopy).map((propField) => {
522
- const propValue = functionPropsCopy[propField as keyof FuncPropsState];
523
- if (propValue) result += `#${functionPropsCode(propField as keyof FuncPropsState)}: ${propValue}\n`;
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
+ }
524
568
  });
525
569
  functionInputParamsCopy.map((param) => {
526
570
  result += generateParamLine(param, DIRECTION.INPUT);
@@ -533,4 +577,39 @@ export function _functionEditor(functionCode: string) {
533
577
 
534
578
  grok.shell.addView(editorView);
535
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
+ });
536
615
  }