-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathInstanceEditor.js
More file actions
467 lines (396 loc) · 20.8 KB
/
InstanceEditor.js
File metadata and controls
467 lines (396 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
define([
'css!vp_base/css/component/instanceEditor.css',
'vp_base/js/com/com_String',
'vp_base/js/com/com_util',
'vp_base/js/com/component/SuggestInput'
], function(insCss, com_String, com_util, SuggestInput) {
// temporary const
const VP_INS_BOX = 'vp-ins-box';
const VP_INS_SELECT_CONTAINER = 'vp-ins-select-container';
const VP_INS_SELECT_TITLE = 'vp-ins-select-title';
const VP_INS_SEARCH = 'vp-ins-search';
const VP_INS_TYPE = 'vp-ins-type';
const VP_INS_SELECT_BOX = 'vp-ins-select-box';
const VP_INS_SELECT_LIST = 'vp-ins-select-list';
const VP_INS_SELECT_ITEM = 'vp-ins-select-item';
const VP_INS_PARAMETER_BOX = 'vp-ins-parameter-box';
const VP_INS_PARAMETER = 'vp-ins-parameter';
const VP_CREATE_VAR_BOX = 'vp-create-var-box';
const VP_CREATE_VAR = 'vp-create-var';
const VP_CREATE_VAR_BTN = 'vp-create-var-btn';
// function/method types
var _METHOD_TYPES = ['function', 'method', 'type', 'builtin_function_or_method', 'PlotAccessor'];
/**
* @class InstanceEditor
* @param {object} pageThis
* @param {string} targetId
* @param {boolean} popup
* @constructor
*/
class InstanceEditor {
constructor(pageThis, targetId, containerId = 'vp_wrapper', popup = false) {
this.pageThis = pageThis;
this.targetId = targetId;
this.uuid = 'u' + com_util.getUUID();
this.containerId = containerId;
this.popup = popup;
this.state = {
code: '',
type: '',
list: [],
};
this.isFirstPage = false;
this.dataTypeInfo = [
{ label: 'DataFrame', type: 'DataFrame', assign: ' = pd.DataFrame()' },
{ label: 'Series', type: 'Series', assign: ' = pd.Series()' },
{ label: 'Dict', type: 'dict', assign: ' = {}' },
{ label: 'List', type: 'list', assign: ' = []' },
{ label: 'Integer', type: 'int', assign: ' = 0' },
{ label: 'Others', type: '' }
];
this.dataTypeList = ['DataFrame', 'Series', 'dict', 'list', 'int'];
this.bindEvent();
this.init();
}
getVarType() {
return this.state.type;
}
getVarList() {
return this.state.list;
}
init() {
this.reload();
}
wrapSelector(selector = '') {
return com_util.formatString('.{0} {1}', this.uuid, selector);
}
renderFirstPage() {
var tag = new com_String();
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_BOX, this.uuid); // vp-select-base
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_SELECT_CONTAINER, 'datatype');
tag.appendFormatLine('<div class="vp-multilang {0}">Data Type</div>', VP_INS_SELECT_TITLE);
tag.appendFormatLine('<input class="{0} {1}" type="hidden"/>', VP_INS_TYPE, 'datatype');
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_SELECT_BOX, 'datatype');
tag.appendFormatLine('<ul class="{0} {1}">', VP_INS_SELECT_LIST, 'datatype');
this.dataTypeInfo.forEach((obj, idx) => {
tag.appendFormatLine('<li class="{0}" data-var-name="{1}" data-var-type="{2}" data-var-assign="{3}">{4}</li>',
VP_INS_SELECT_ITEM + (idx == 0 ? ' selected' : ''), obj.label, obj.type, obj.assign, obj.label);
});
tag.appendLine('</ul>');
tag.appendLine('</div>'); // VP_INS_SELECT_BOX
tag.appendLine('</div>'); // VP_INS_SELECT_CONTAINER
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_SELECT_CONTAINER, 'variable');
tag.appendFormatLine('<div class="vp-multilang {0}">Variable</div>', VP_INS_SELECT_TITLE);
tag.appendFormatLine('<input class="{0} {1}" type="hidden"/>', VP_INS_TYPE, 'variable');
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_SELECT_BOX, 'variable');
tag.appendFormatLine('<ul class="{0} {1}">', VP_INS_SELECT_LIST, 'variable');
tag.appendLine('</ul>');
tag.appendLine('</div>'); // VP_INS_SELECT_BOX
// create variable input
tag.appendFormatLine('<div class="{0}">', VP_CREATE_VAR_BOX);
tag.appendFormatLine('<input class="vp-input {0}" type="text" placeholder="Create new variable" />', VP_CREATE_VAR);
tag.appendFormatLine('<div class="{0}"><img src="{1}"/></div>', VP_CREATE_VAR_BTN, '/nbextensions/visualpython/img/plus.svg');
tag.appendLine('</div>');
tag.appendLine('</div>'); // VP_INS_SELECT_CONTAINER
tag.appendLine('</div>'); // VP_INS_BOX END
// TODO: if this.popup == true
$(this.pageThis.wrapSelector('#' + this.containerId)).html(tag.toString());
return tag.toString();
}
renderVarList(varType, varList) {
var varListTag = new com_String();
varList != undefined && varList.forEach(obj => {
if ((varType == '' && !this.dataTypeList.includes(obj.type))
|| obj.type == varType) {
varListTag.appendFormatLine('<li class="{0}" data-var-name="{1}" data-var-type="{2}" title="{3}">{4}</li>',
VP_INS_SELECT_ITEM, obj.name, obj.type, obj.type, obj.name);
}
});
$(this.wrapSelector('.' + VP_INS_SELECT_LIST + '.variable')).html(varListTag.toString());
}
renderPage(replace = true) {
var tag = new com_String();
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_BOX, this.uuid); // vp-select-base
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_SELECT_CONTAINER, 'attr');
tag.appendFormatLine('<div class="vp-multilang {0}">Attribute</div>', VP_INS_SELECT_TITLE);
tag.appendFormatLine('<div style="{0}">', 'position: relative;');
tag.appendFormatLine('<input class="vp-input {0} {1}" type="text" placeholder="Search Attribute"/>', VP_INS_SEARCH, 'attr');
tag.appendFormatLine('<input class="{0} {1}" type="hidden"/>', VP_INS_TYPE, 'attr');
tag.appendLine('</div>');
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_SELECT_BOX, 'attr');
tag.appendFormatLine('<ul class="{0} {1}">', VP_INS_SELECT_LIST, 'attr');
tag.appendLine('</ul>');
tag.appendLine('</div>'); // VP_INS_SELECT_BOX
tag.appendLine('</div>'); // VP_INS_SELECT_CONTAINER
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_SELECT_CONTAINER, 'method');
tag.appendFormatLine('<div class="vp-multilang {0}">Method</div>', VP_INS_SELECT_TITLE);
tag.appendFormatLine('<div style="{0}">', 'position: relative;');
tag.appendFormatLine('<input class="vp-input {0} {1}" type="text" placeholder="Search Method"/>', VP_INS_SEARCH, 'method');
tag.appendFormatLine('<input class="{0} {1}" type="hidden"/>', VP_INS_TYPE, 'method');
tag.appendLine('</div>');
tag.appendFormatLine('<div class="{0} {1}">', VP_INS_SELECT_BOX, 'method');
tag.appendFormatLine('<ul class="{0} {1}">', VP_INS_SELECT_LIST, 'method');
tag.appendLine('</ul>');
tag.appendLine('</div>'); // VP_INS_SELECT_BOX
tag.appendLine('</div>'); // VP_INS_SELECT_CONTAINER
tag.appendFormatLine('<div class="{0}">', VP_INS_PARAMETER_BOX);
tag.appendFormatLine('<input type="text" class="{0}" placeholder="{1}"/>',
VP_INS_PARAMETER, 'input parameter');
tag.appendLine('</div>'); // VP_INS_PARAMETER
tag.appendLine('</div>'); // VP_INS_BOX END
// TODO: if this.popup == true
$(this.pageThis.wrapSelector('#' + this.containerId)).html(tag.toString());
return tag.toString();
}
bindEvent() {
var that = this;
// select datatype
$(document).on('click', this.wrapSelector('.' + VP_INS_SELECT_LIST + '.datatype .' + VP_INS_SELECT_ITEM), function (event) {
var varType = $(this).attr('data-var-type');
$(that.wrapSelector('.' + VP_INS_SELECT_LIST + '.datatype .' + VP_INS_SELECT_ITEM)).removeClass('selected');
$(this).addClass('selected');
// if others selected, cannot create variable
if (varType == '') {
$(that.wrapSelector('.' + VP_CREATE_VAR_BOX)).hide();
} else {
$(that.wrapSelector('.' + VP_CREATE_VAR_BOX)).show();
}
that.reload();
});
// select variable
$(document).on('click', this.wrapSelector('.' + VP_INS_SELECT_LIST + '.variable .' + VP_INS_SELECT_ITEM), function (event) {
var varName = $(this).attr('data-var-name');
var varType = $(this).attr('data-var-type');
// console.log('clicked', varName, varType, that.state);
$(that.pageThis.wrapSelector('#' + that.targetId)).trigger({
type: "instance_editor_selected",
varName: varName,
varType: varType,
isMethod: false
});
});
// create variable
$(document).on('click', this.wrapSelector('.' + VP_CREATE_VAR_BTN), function (event) {
var varName = $(that.wrapSelector('.' + VP_CREATE_VAR)).val();
var selectedType = $(that.wrapSelector('.' + VP_INS_SELECT_LIST + '.datatype .' + VP_INS_SELECT_ITEM + '.selected'));
var varType = selectedType.attr('data-var-type');
var assignCode = selectedType.attr('data-var-assign');
if (varName == '') {
; // no variable name entered
} else {
var code = com_util.formatString('{0}{1}\n{2}', varName, assignCode, 'type(' + varName + ').__name__');
vpKernel.execute(code).then(function (resultObj) {
let { result } = resultObj;
if (result.includes(varType)) {
com_util.renderSuccessMessage('Variable Created!');
that.reload();
}
$(that.wrapSelector('.' + VP_CREATE_VAR)).val('');
});
}
});
// select attribute
$(document).on('click', this.wrapSelector('.' + VP_INS_SELECT_LIST + '.attr .' + VP_INS_SELECT_ITEM), function (event) {
var varName = $(this).attr('data-var-name');
var varType = $(this).attr('data-var-type');
// console.log('clicked', varName, varType, that.state);
$(that.pageThis.wrapSelector('#' + that.targetId)).trigger({
type: "instance_editor_selected",
varName: varName,
varType: varType,
isMethod: false
});
});
// select method
$(document).on('click', this.wrapSelector('.' + VP_INS_SELECT_LIST + '.method .' + VP_INS_SELECT_ITEM), function (event) {
var varName = $(this).attr('data-var-name');
var varType = $(this).attr('data-var-type');
// console.log('clicked', varName, varType, that.state);
$(that.pageThis.wrapSelector('#' + that.targetId)).trigger({
type: "instance_editor_selected",
varName: varName,
varType: varType,
isMethod: true
});
});
// parameter input
$(document).on('change', this.wrapSelector('.' + VP_INS_PARAMETER), function (event) {
var parameter = $(this).val();
var variable = $(that.pageThis.wrapSelector('#' + that.targetId)).val();
var splitList = variable.split('.');
if (splitList && splitList.length > 0) {
var lastSplit = splitList[splitList.length - 1];
var matchList = lastSplit.match(/\(.*?\)$/gi);
if (matchList && matchList.length > 0) {
var lastBracket = matchList[matchList.length - 1];
splitList[splitList.length - 1] = lastSplit.replace(lastBracket, com_util.formatString('({0})', parameter));
var newCode = splitList.join('.');
$(that.pageThis.wrapSelector('#' + that.targetId)).trigger({
type: "instance_editor_replaced",
originCode: variable,
newCode: newCode
});
}
}
});
}
reload(callback = undefined) {
var that = this;
var variable = $(this.pageThis.wrapSelector('#' + this.targetId)).val();
if (variable == null) {
this.isFirstPage = false;
this.renderPage();
return;
}
this.state.code = variable;
if (variable == '') {
if (!this.isFirstPage) {
this.renderFirstPage();
this.isFirstPage = true;
}
} else {
this.isFirstPage = false;
this.renderPage();
}
var code = com_util.formatString('_vp_print(_vp_load_instance("{0}"))', variable);
vpKernel.execute(code).then(function (resultObj) {
let { result } = resultObj;
var varObj = {
type: 'None',
list: []
};
try {
varObj = JSON.parse(result);
} catch {
; // command error
}
var varType = varObj.type;
var varList = varObj.list;
that.state.type = varType;
that.state.list = varList;
// set variable type
// $(that.wrapSelector('#vp_instanceType')).text(varType);
// set dir list
if (that.isFirstPage) {
varType = $(that.wrapSelector('.' + VP_INS_SELECT_LIST + '.datatype .' + VP_INS_SELECT_ITEM + '.selected')).attr('data-var-type');
that.renderVarList(varType, varList);
} else {
var attrListTag = new com_String();
var methodListTag = new com_String();
var attrList = [];
var methodList = [];
varList != undefined && varList.forEach(obj => {
if (obj.type.includes('Indexer')) {
methodListTag.appendFormatLine('<li class="{0}" data-var-name="{1}" data-var-type="{2}" title="{3}">{4}</li>',
VP_INS_SELECT_ITEM, obj.name + '[]', obj.type, obj.type, obj.name);
methodList.push({
label: obj.name + '[]' + ' (' + obj.type + ')',
value: obj.name + '[]',
type: obj.type
});
}
// Method/Function... -> Method
else if (_METHOD_TYPES.includes(obj.type)) {
methodListTag.appendFormatLine('<li class="{0}" data-var-name="{1}" data-var-type="{2}" title="{3}">{4}</li>',
VP_INS_SELECT_ITEM, obj.name + '()', obj.type, obj.type, obj.name);
methodList.push({
label: obj.name + '()' + ' (' + obj.type + ')',
value: obj.name + '()',
type: obj.type
});
} else {
attrListTag.appendFormatLine('<li class="{0}" data-var-name="{1}" data-var-type="{2}" title="{3}">{4}</li>',
VP_INS_SELECT_ITEM, obj.name, obj.type, obj.type, obj.name);
attrList.push({
label: obj.name + ' (' + obj.type + ')',
value: obj.name,
type: obj.type
});
}
});
$(that.wrapSelector('.' + VP_INS_SELECT_LIST + '.attr')).html(attrListTag.toString());
$(that.wrapSelector('.' + VP_INS_SELECT_LIST + '.method')).html(methodListTag.toString());
// attribute search suggest
var suggestInput = new SuggestInput();
suggestInput.addClass('vp-input attr');
suggestInput.addClass(VP_INS_SEARCH);
suggestInput.setPlaceholder("Search Attribute");
suggestInput.setSuggestList(function () { return attrList; });
suggestInput.setSelectEvent(function (value, item) {
$(this.wrapSelector()).val(value);
$(that.wrapSelector('.' + VP_INS_TYPE + '.attr')).val(item.type);
$(that.pageThis.wrapSelector('#' + that.targetId)).trigger({
type: "instance_editor_selected",
varName: value,
varType: item.type,
isMethod: false
});
});
$(that.wrapSelector('.' + VP_INS_SEARCH + '.attr')).replaceWith(function () {
return suggestInput.toTagString();
});
// method search suggest
suggestInput = new SuggestInput();
suggestInput.addClass('vp-input method');
suggestInput.addClass(VP_INS_SEARCH);
suggestInput.setPlaceholder("Search Method");
suggestInput.setSuggestList(function () { return methodList; });
suggestInput.setSelectEvent(function (value, item) {
$(this.wrapSelector()).val(value);
$(that.wrapSelector('.' + VP_INS_TYPE + '.method')).val(item.type);
$(that.pageThis.wrapSelector('#' + that.targetId)).trigger({
type: "instance_editor_selected",
varName: value,
varType: item.type,
isMethod: true
});
});
$(that.wrapSelector('.' + VP_INS_SEARCH + '.method')).replaceWith(function () {
return suggestInput.toTagString();
});
// get parameter
var splitList = variable.split('.');
if (splitList && splitList.length > 0) {
var lastSplit = splitList[splitList.length - 1];
// if bracket is at the end of code
var matchList = lastSplit.match(/\(.*?\)$/gi);
if (matchList != null && matchList.length > 0) {
var lastBracket = matchList[matchList.length - 1];
// remove first/last brackets
var parameter = lastBracket.substr(1, lastBracket.length - 2);
$(that.wrapSelector('.' + VP_INS_PARAMETER)).val(parameter);
$(that.wrapSelector('.' + VP_INS_PARAMETER)).show();
} else {
$(that.wrapSelector('.' + VP_INS_PARAMETER)).val('');
$(that.wrapSelector('.' + VP_INS_PARAMETER)).hide();
}
} else {
$(that.wrapSelector('.' + VP_INS_PARAMETER)).hide();
}
}
// callback
if (callback) {
callback(varObj);
}
}).catch(function(resultObj) {
let { result } = resultObj;
// show alert if this is visible
if (that.pageThis.isHidden() == false) {
com_util.renderAlertModal(result.ename + ': ' + result.evalue);
}
// callback
if (callback) {
callback('');
}
});
}
show() {
$(this.wrapSelector()).show();
this.reload();
}
hide() {
$(this.wrapSelector()).hide();
}
}
return InstanceEditor;
})