-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathVarSelector2.js
More file actions
381 lines (352 loc) · 15.7 KB
/
VarSelector2.js
File metadata and controls
381 lines (352 loc) · 15.7 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
define([
'vp_base/js/com/com_Const',
'vp_base/js/com/com_String',
'vp_base/js/com/com_util',
'vp_base/js/com/component/Component',
], function(com_Const, com_String, com_util, Component) {
/**
* @class VarSelector
* @constructor
*/
class VarSelector extends Component{
constructor(parentTag, dataTypes=['DataFrame', 'Series', 'ndarray', 'list', 'dict'], defaultType='', showOthers=true, showFilterbox=true) {
super(null, {parentTag: parentTag, dataTypes: dataTypes, defaultType: defaultType, showOthers: showOthers, showFilterbox: showFilterbox});
}
_init() {
this._value = "";
this._placeholder = "Select variable";
this._compID = "";
this._additionalClass = "";
this._normalFilter = true;
this._suggestList = new Array();
this._selectEvent = undefined;
this._attributes = {};
this._minLength = 0;
this._parentTag = this.state.parentTag;
this._dataTypes = this.state.dataTypes;
this._defaultType = this.state.defaultType;
this._showOthers = this.state.showOthers;
this._showFilterbox = this.state.showFilterbox;
if (this._defaultType == '') {
if (this._dataTypes.length > 0) {
this._defaultType = this._dataTypes[0];
} else {
}
}
this.state = {
selectedType: this._defaultType,
varList: [],
column: ''
}
this.reload();
this.bindEvent();
}
render() {
;
}
bindEvent() {
let that = this;
// bind Event on focus/click box
$(document).on(com_util.formatString("focus.init-{0} click.init-{1}", that.uuid, that.uuid), com_util.formatString(".{0}.{1}", that.uuid, 'vp-vs-uninit'), function () {
// unbind initial event
$(document).unbind(com_util.formatString(".init-{0}", that.uuid));
$(com_util.formatString(".{0}.{1}", that.uuid, 'vp-vs-uninit')).removeClass('vp-vs-uninit').addClass('vp-vs-init');
// bind autocomplete
that.bindAutocomplete();
// bind Event
$(that._parentTag).on('click', that.wrapSelector('.vp-vs-filter'), function(evt) {
// check disabled
if (!$(this).parent().find('input').is(':disabled')) {
// toggle filter box
let isOpen = $(that.wrapSelector('.vp-vs-filter-box')).hasClass('vp-inline-block');
$('.vp-vs-filter-box').removeClass('vp-inline-block');
if (!isOpen) {
// open filter box
$(that.wrapSelector('.vp-vs-filter-box')).addClass('vp-inline-block');
}
}
evt.stopPropagation();
});
$(that._parentTag).on('click', function(evt) {
let target = evt.target;
if ($(that.wrapSelector('.vp-vs-filter-box')).find(target).length > 0
|| $(target).hasClass('vp-vs-filter-box')) {
// trigger focus
$(that.wrapSelector('.vp-vs-input')).trigger('focus');
}
});
$(that._parentTag).on('change', that.wrapSelector('.vp-vs-filter-select-all'), function() {
let checked = $(this).prop('checked');
// check all
$(that.wrapSelector('.vp-vs-filter-type')).prop('checked', checked);
// reload
that.reload().then(function() {
$(that.wrapSelector('.vp-vs-input')).trigger('focus');
});
});
$(that._parentTag).on('change', that.wrapSelector('.vp-vs-filter-type'), function() {
// if checked all
let allLength = $(that.wrapSelector('.vp-vs-filter-type')).length;
let checkedLength = $(that.wrapSelector('.vp-vs-filter-type:checked')).length;
if (allLength == checkedLength) {
$(that.wrapSelector('.vp-vs-filter-select-all')).prop('checked', true);
} else {
$(that.wrapSelector('.vp-vs-filter-select-all')).prop('checked', false);
}
that.reload().then(function() {
$(that.wrapSelector('.vp-vs-input')).trigger('focus');
});
});
});
}
/**
* value
* @param {String} value value
*/
setValue(value = "") {
this._value = value;
}
/**
* placeholder
* @param {String} placeholder placeholder
*/
setPlaceholder(placeholder = "") {
this._placeholder = placeholder;
}
/**
* Component id
* @param {String} compID
*/
setComponentID(compID = "") {
this._compID = compID;
}
/**
* normal filter usage
* @param {String} normalFilter
*/
setNormalFilter(normalFilter = true) {
this._normalFilter = normalFilter;
}
/**
* suggest list
* @param {Array or Function} suggestList
*/
setSuggestList(suggestList = new Array()) {
this._suggestList = suggestList;
}
/**
* show default list
* - true: autocomplete.minLength to 0
* - false: autocomplete.minLength to 1
* @param {bool} showDefaultList
*/
setMinSearchLength(minLength) {
this._minLength = minLength;
}
/**
* Additional Class
* @param {String} additionalClass
*/
addClass(additionalClass = "") {
if (additionalClass == "")
return;
this._additionalClass = com_util.formatString("{0} {1}", this._additionalClass, additionalClass);
}
addAttribute(key, value) {
this._attributes = {
...this._attributes,
[key]: value
};
}
/**
* selection event
* @param {function} selectEvent
*/
setSelectEvent(selectEvent) {
if (typeof selectEvent != "function") {
selectEvent = undefined;
}
this._selectEvent = selectEvent;
}
reload() {
var that = this;
// load using kernel
let dataTypes = this._dataTypes;
let excludeTypes = [];
let othersChecked = this._showOthers;
// check filter
if ($(this.wrapSelector('.vp-vs-filter-box')).length > 0) {
let filterTypes = [];
let filterTypeTags = $(this.wrapSelector('.vp-vs-filter-type'));
filterTypeTags.each((i, tag) => {
let checked = $(tag).prop('checked');
let dtype = $(tag).data('dtype');
if (checked) {
if (dtype == 'others') {
othersChecked = true;
} else {
filterTypes.push(dtype);
}
} else {
if (dtype == 'others') {
othersChecked = false;
} else {
excludeTypes.push(dtype);
}
}
});
let allChecked = $(this.wrapSelector('.vp-vs-filter-select-all')).prop('checked');
if (allChecked) {
if (othersChecked) {
dataTypes = []; // load all types of variables
excludeTypes = [];
} else {
dataTypes = filterTypes;
}
} else {
if (othersChecked) {
dataTypes = [];
} else {
if (filterTypes.length == 0) {
// nothing checked
// no variable list
this.state.varList = [];
this._suggestList = [];
return new Promise(function(resolve, reject) {
resolve([]);
});
}
dataTypes = filterTypes;
}
}
} else {
dataTypes = [];
}
vpLog.display(VP_LOG_TYPE.DEVELOP, 'VarSelector2 - reload ', dataTypes, excludeTypes);
return new Promise(function(resolve, reject) {
vpKernel.getDataList(dataTypes, excludeTypes).then(function (resultObj) {
try {
let { result, type, msg } = resultObj;
var varList = JSON.parse(result);
// re-mapping variable list
varList = varList.map(obj => {
return {
label: obj.varName,
value: obj.varName,
dtype: obj.varType
};
});
// save variable list as state
that.state.varList = varList;
that._suggestList = varList;
resolve(varList);
} catch (ex) {
reject(ex);
}
});
});
}
bindAutocomplete() {
let that = this;
let minLength = this._minLength;
$(com_util.formatString(".{0} input", that.uuid)).autocomplete({
autoFocus: true,
minLength: minLength,
source: function (req, res) {
var srcList = typeof that._suggestList == "function" ? that._suggestList() : that._suggestList;
var returlList = new Array();
if (that._normalFilter) {
for (var idx = 0; idx < srcList.length; idx++) {
// srcList as object array
if (typeof srcList[idx] == "object") {
// { label: string, value: string } format
if (srcList[idx].label.toString().toLowerCase().includes(req.term.trim().toLowerCase())) {
returlList.push(srcList[idx]);
}
} else if (srcList[idx].toString().toLowerCase().includes(req.term.trim().toLowerCase()))
returlList.push(srcList[idx]);
}
} else {
returlList = srcList;
}
res(returlList);
},
select: function (evt, ui) {
let result = true;
// trigger change
$(this).val(ui.item.value);
$(this).trigger('change');
// select event
if (typeof that._selectEvent == "function")
result = that._selectEvent(ui.item.value, ui.item);
if (result != undefined) {
return result;
}
return true;
},
search: function(evt, ui) {
return true;
},
open: function(evt, ui) {
if (!$(that.wrapSelector('.vp-vs-filter-box')).hasClass('vp-inline-block')) {
$(that.wrapSelector('.vp-vs-filter-box')).addClass('vp-inline-block');
}
},
close: function(evt, ui) {
// $(that.wrapSelector('.vp-vs-filter-box')).removeClass('vp-inline-block');
evt.preventDefault();
return false;
}
}).focus(function () {
$(this).select();
$(this).autocomplete('search', $(this).val());
}).click(function () {
$(this).select();
$(this).autocomplete('search', $(this).val());
}).autocomplete('instance')._renderItem = function(ul, item) {
return $('<li>').attr('data-value', item.value)
.append(`<div class="vp-vs-item">${item.label}<label class="vp-gray-text vp-cursor"> | ${item.dtype}</label></div>`)
.appendTo(ul);
};
}
/**
* icon input box tag
* @returns html icon input text tag string
*/
toTagString() {
var sbTagString = new com_String();
var that = this;
// make attributes
var attributes = Object.keys(this._attributes).map(key => key + '="' + this._attributes[key] + '"').join(" ");
sbTagString.appendFormatLine('<div class="{0} {1}">', this.uuid, 'vp-vs-box vp-vs-uninit');
sbTagString.appendFormatLine(`<input type="text" class="vp-vs-blur-btn {0} {1}" {2} placeholder="{3}" value="{4}" {5}/>`,
'vp-vs-input', that._additionalClass, that._compID == "" ? "" : com_util.formatString('id="{0}"', that._compID), that._placeholder, that._value, attributes);
if (this._showFilterbox) {
// filter icon
// LAB: img to url
// sbTagString.appendFormatLine('<span class="vp-vs-blur-btn {0}"><img src="{1}"/></span>', 'vp-vs-filter', com_Const.IMAGE_PATH + 'filter.svg');
sbTagString.appendFormatLine('<span class="vp-vs-blur-btn {0} {1}"></span>', 'vp-vs-filter', 'vp-icon-filter');
// filter box
sbTagString.appendFormatLine('<div class="vp-vs-blur-btn vp-vs-blur {0}">', 'vp-vs-filter-box');
sbTagString.appendLine('<div class="vp-grid-box">');
sbTagString.appendFormatLine('<input type="checkbox" id="{0}" class="{1}" checked><label for="{2}">{3}</label>',
this.uuid + '_vsSelectAll', 'vp-vs-filter-select-all', this.uuid + '_vsSelectAll', 'Select All');
this._dataTypes && this._dataTypes.forEach(dt => {
let tmpId = that.uuid + '_' + dt;
sbTagString.appendFormatLine('<input type="checkbox" id="{0}" class="{1}" data-dtype="{2}" checked><label for="{3}">{4}</label>',
tmpId, 'vp-vs-filter-type', dt, tmpId, dt);
});
if (this._showOthers) {
let tmpId = that.uuid + '_others';
sbTagString.appendFormatLine('<input type="checkbox" id="{0}" class="{1}" data-dtype="{2}" checked><label for="{3}">{4}</label>',
tmpId, 'vp-vs-filter-type', 'others', tmpId, 'Others');
}
sbTagString.appendLine('</div>'); // end of vp-grid-box
sbTagString.appendLine('</div>'); // end of vp-vs-filter-box
}
sbTagString.appendLine('</div>'); // end of vp-vs-box
return sbTagString.toString();
}
}
return VarSelector;
});