-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathVarSelector.js
More file actions
349 lines (320 loc) · 14.4 KB
/
VarSelector.js
File metadata and controls
349 lines (320 loc) · 14.4 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
define([
'vp_base/js/com/com_Const',
'vp_base/js/com/com_String',
'vp_base/js/com/com_util',
], function(com_Const, com_String, com_util) {
const VP_VS_BOX = 'vp-vs-box';
const VP_VS_DATA_TYPE = 'vp-vs-data-type';
const VP_VS_VARIABLES = 'vp-vs-variables';
const VP_VS_TYPING_INPUT = 'vp-vs-typing-input';
const VP_VS_COLUMN_INPUT = 'vp-vs-column-input';
const VP_VS_REFRESH = 'vp-vs-refresh';
/**
* @class VarSelector
* @param {Array} dataTypes
* @param {String} defaultType
* @constructor
*
* using sample:
var varSelector = new VarSelector(['DataFrame', 'Series'], 'DataFrame');
$(this.wrapSelector('.vp-vs-tester')).html(varSelector.render());
*/
class VarSelector {
constructor(dataTypes, defaultType = '', showOthers = true, useTyping = true) {
this.uuid = 'u' + com_util.getUUID();
this.label = {
'others': 'Others',
'typing': 'Typing'
};
this.boxClass = [];
this.id = '';
this.class = [];
this.attributes = {};
this.typeClass = []; // type selector class
this.varClass = []; // variable selector class
this.colClass = []; // column selector class
this.dataTypes = dataTypes;
if (defaultType == '') {
if (dataTypes.length > 0) {
defaultType = dataTypes[0];
} else {
}
}
this.state = {
selectedType: defaultType,
varList: [],
column: ''
};
this.defaultType = defaultType;
this.defaultValue = '';
this.defaultColumn = '';
this.showOthers = showOthers;
this.useTyping = useTyping;
this.useColumn = false;
this.reload();
this.bindEvent();
}
setComponentId(id) {
this.id = id;
}
addBoxClass(classname) {
this.boxClass.push(classname);
}
addClass(classname) {
this.class.push(classname);
}
addTypeClass(classname) {
this.typeClass.push(classname);
}
addVarClass(classname) {
this.varClass.push(classname);
}
addColClass(classname) {
this.colClass.push(classname);
}
addAttribute(key, value) {
this.attributes.push({ [key]: value });
}
setValue(value) {
if (value == null || value == undefined) {
value = '';
}
this.defaultValue = value;
if (value.includes('[') && value.includes(']') ) {
// divide it to variable / column
let startIdx = value.indexOf('[');
let endIdx = value.indexOf(']');
this.defaultValue = value.substring(0, startIdx);
this.defaultColumn = value.substring(startIdx + 1, endIdx);
}
}
setState(newState) {
this.state = {
...this.state,
...newState
}
}
setUseColumn(useColumn) {
this.useColumn = useColumn;
}
wrapSelector(selector = '') {
return com_util.formatString('.{0} {1}', this.uuid, selector);
}
render(defaultType = this.defaultType, defaultValue = this.defaultValue) {
var tag = new com_String();
// var selector box
tag.appendFormatLine('<div class="{0} {1} {2}">', VP_VS_BOX, this.uuid, this.boxClass.join(' '));
// // hidden input value
// tag.appendFormatLine('<input type="hidden" {0} />',
// this.attributes.id? 'id="' + this.attributes.id + '"': '');
// data type selector
tag.appendFormatLine('<select class="{0} {1} {2}">', VP_VS_DATA_TYPE, 'vp-select m', this.typeClass.join(' '));
this.dataTypes.forEach((v, i) => {
tag.appendFormatLine('<option value="{0}" {1}>{2}</option>', v,
defaultType == v ? 'selected' : '', v);
});
if (this.showOthers) {
tag.appendFormatLine('<option value="{0}">{1}</option>', 'others', this.label.others);
}
if (this.useTyping) {
tag.appendFormatLine('<option value="{0}">{1}</option>', 'typing', this.label.typing);
}
tag.appendLine('</select>'); // VP_VS_DATA_TYPE
// variable selctor
tag.appendLine(this.renderVariableList(this.state.varList));
var attrStr = Object.keys(this.attributes).map(key => key + '="' + this.attributes[key] + '"').join(' ');
// typing
tag.appendFormatLine('<input type="text" class="{0} {1} {2}" placeholder="{3}" style="display: none;" {4} value="{5}" data-type="{6}" {7}/>',
VP_VS_TYPING_INPUT, 'vp-input m', this.class.join(' '),
'Type your code',
this.id ? 'id="' + this.id + '"' : '',
defaultValue,
defaultType,
attrStr);
// column for dataframe
tag.appendFormatLine('<select class="{0} {1} {2}" {3}></select>',
VP_VS_COLUMN_INPUT, 'vp-select m', this.colClass.join(' '),
(this.useColumn == true && defaultType == 'DataFrame'?'':'style="display: none;"'));
// reload
// LAB: img to url
// tag.appendFormatLine('<span class="{0} vp-cursor" title="{1}"><img src="{2}"></span>',
// VP_VS_REFRESH, 'Refresh variables', com_Const.IMAGE_PATH + 'refresh.svg');
tag.appendFormatLine('<span class="{0} vp-cursor vp-icon-refresh" title="{1}"></span>',
VP_VS_REFRESH, 'Refresh variables');
tag.appendLine('</div>'); // VP_VS_BOX
return tag.toString();
}
reload() {
var that = this;
// load using kernel
var dataTypes = this.showOthers ? [] : this.dataTypes;
vpKernel.getDataList(dataTypes).then(function (resultObj) {
try {
let { result, type, msg } = resultObj;
var varList = JSON.parse(result);
that.state.varList = varList;
// render variable list
that.loadVariableList(varList);
} catch (ex) {
// console.log(ex);
}
});
}
renderVariableList(varList) {
var tag = new com_String();
tag.appendFormatLine('<select class="{0} {1} {2}" {3}>', VP_VS_VARIABLES, 'vp-select m', this.varClass.join(' '),
this.state.selectedType == 'typing' ? 'style="display:none;"' : '');
varList.forEach(vObj => {
// varName, varType
var label = vObj.varName;
if (this.state.selectedType == 'others') {
label += com_util.formatString(' ({0})', vObj.varType);
}
tag.appendFormatLine('<option value="{0}" data-type="{1}" {2}>{3}</option>',
vObj.varName, vObj.varType,
this.defaultValue == vObj.varName ? 'selected' : '',
label);
});
tag.appendLine('</select>'); // VP_VS_VARIABLES
return tag.toString();
}
loadVariableList(varList) {
var filteredList = varList;
var that = this;
let dataTypes = this.dataTypes;
// Include various index types for Index type
var INDEX_TYPES = ['RangeIndex', 'CategoricalIndex', 'MultiIndex', 'IntervalIndex', 'DatetimeIndex', 'TimedeltaIndex', 'PeriodIndex', 'Int64Index', 'UInt64Index', 'Float64Index'];
// Include various groupby types for Groupby type
var GROUPBY_TYPES = ['DataFrameGroupBy', 'SeriesGroupBy']
if (dataTypes.indexOf('Index') >= 0) {
dataTypes = dataTypes.concat(INDEX_TYPES);
}
if (dataTypes.indexOf('GroupBy') >= 0) {
dataTypes = dataTypes.concat(GROUPBY_TYPES);
}
if (this.state.selectedType == 'others') {
filteredList = varList.filter(v => !dataTypes.includes(v.varType));
} else if (this.state.selectedType == 'typing') {
filteredList = [];
} else {
let filterDataTypes = [ this.state.selectedType ];
if (filterDataTypes.indexOf('Index') >= 0) {
filterDataTypes = filterDataTypes.concat(INDEX_TYPES);
}
if (filterDataTypes.indexOf('GroupBy') >= 0) {
filterDataTypes = filterDataTypes.concat(GROUPBY_TYPES);
}
filteredList = varList.filter(v => filterDataTypes.includes(v.varType));
}
// replace
$(this.wrapSelector('.' + VP_VS_VARIABLES)).replaceWith(function () {
return that.renderVariableList(filteredList);
});
$(this.wrapSelector('.' + VP_VS_VARIABLES)).trigger('change');
}
loadColumnList(varName) {
let that = this;
// get result and show on detail box
vpKernel.getColumnList(varName).then(function(resultObj) {
try {
let { result, type, msg } = resultObj;
var { list } = JSON.parse(result);
let newTag = new com_String();
newTag.appendFormatLine('<select class="{0} {1} {2}" {3}>',
VP_VS_COLUMN_INPUT, 'vp-select m', that.colClass.join(' '),
(that.useColumn == true && that.defaultType == 'DataFrame'?'':'style="display: none;"'));
newTag.appendFormatLine('<option value="{0}" data-dtype="{1}">{2}</option>',
'', '', '');
list && list.forEach(col => {
// label, value, dtype, array, location, category
newTag.appendFormatLine('<option value="{0}" data-dtype="{1}" {2}>{3}</option>',
col.value, col.dtype,
that.defaultColumn == col.value ? 'selected' : '',
col.label);
});
newTag.appendLine('</select>');
// replace
$(that.wrapSelector('.' + VP_VS_COLUMN_INPUT)).replaceWith(function() {
return newTag.toString();
});
} catch (e) {
vpLog.display(VP_LOG_TYPE.ERROR, 'varSelector - bindColumnSource: not supported data type. ', e);
}
});
}
bindEvent() {
var that = this;
// data type selection
$(document).on('change', this.wrapSelector('.' + VP_VS_DATA_TYPE), function (event) {
// re-renderVariableList
var dataType = $(this).val();
that.state.selectedType = dataType;
if (dataType == 'typing') {
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).val('');
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).attr('data-type', '');
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).show();
$(that.wrapSelector('.' + VP_VS_VARIABLES)).hide();
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).trigger({
type: 'var_changed',
value: '',
dataType: ''
});
} else {
$(that.wrapSelector('.' + VP_VS_VARIABLES)).show();
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).hide();
// 1) load variable once
that.loadVariableList(that.state.varList);
// 2) load on every selection of data types
// that.reload();
}
if (that.useColumn == true) {
if (dataType == 'DataFrame') {
$(that.wrapSelector('.' + VP_VS_COLUMN_INPUT)).show();
} else {
$(that.wrapSelector('.' + VP_VS_COLUMN_INPUT)).hide();
}
} else {
$(that.wrapSelector('.' + VP_VS_COLUMN_INPUT)).hide();
}
});
// variable selection
$(document).on('change', this.wrapSelector('.' + VP_VS_VARIABLES), function (event) {
var value = $(this).val();
var dataType = $(this).find('option:selected').attr('data-type');
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).val(value);
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).attr('data-type', dataType);
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).trigger({
type: 'var_changed',
value: value,
dataType: dataType
});
// if datatype == dataframe, change column list
if (that.useColumn == true && dataType == 'DataFrame') {
that.loadColumnList(value);
}
});
// column selection
$(document).on('change', this.wrapSelector('.' + VP_VS_COLUMN_INPUT), function(event) {
var value = $(that.wrapSelector('.' + VP_VS_VARIABLES)).val();
var colValue = $(this).val();
var newValue = value;
if (colValue != '') {
newValue += '[' + colValue + ']';
}
var dataType = $(this).find('option:selected').attr('data-type');
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).val(newValue);
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).attr('data-type', dataType);
$(that.wrapSelector('.' + VP_VS_TYPING_INPUT)).trigger({
type: 'var_changed',
value: newValue,
dataType: 'DataFrame'
});
});
// refresh
$(document).on('click', this.wrapSelector('.' + VP_VS_REFRESH), function() {
that.reload();
});
}
}
return VarSelector;
})