-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathinstance.js
More file actions
429 lines (368 loc) · 15.7 KB
/
instance.js
File metadata and controls
429 lines (368 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
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
define([
'require'
, 'jquery'
, 'nbextensions/visualpython/src/common/vpCommon'
, 'nbextensions/visualpython/src/common/constant'
, 'nbextensions/visualpython/src/common/StringBuilder'
, 'nbextensions/visualpython/src/common/vpFuncJS'
, 'nbextensions/visualpython/src/pandas/common/commonPandas'
, 'nbextensions/visualpython/src/pandas/common/pandasGenerator'
, 'nbextensions/visualpython/src/common/component/vpSuggestInputText'
// subset editor
, 'nbextensions/visualpython/src/common/vpSubsetEditor'
// dir box
, 'nbextensions/visualpython/src/common/vpInstanceEditor'
// Code Mirror
, 'codemirror/lib/codemirror'
, 'codemirror/mode/python/python'
, 'notebook/js/codemirror-ipython'
, 'codemirror/addon/display/placeholder'
, 'codemirror/addon/display/autorefresh'
], function(requirejs, $, vpCommon, vpConst, sb, vpFuncJS, libPandas, pdGen, vpSuggestInputText
, vpSubsetEditor, vpInstanceEditor
, CodeMirror, cmpython, cmip) {
// 옵션 속성
const funcOptProp = {
stepCount : 1
, funcName : "Instance"
, funcID : "com_instance"
}
const MAX_STACK_SIZE = 20;
var _DATA_TYPES_OF_INDEX = [
// Index 하위 유형
'RangeIndex', 'CategoricalIndex', 'MultiIndex', 'IntervalIndex', 'DatetimeIndex', 'TimedeltaIndex', 'PeriodIndex', 'Int64Index', 'UInt64Index', 'Float64Index'
]
var _DATA_TYPES_OF_GROUPBY = [
// GroupBy 하위 유형
'DataFrameGroupBy', 'SeriesGroupBy'
]
var _SEARCHABLE_DATA_TYPES = [
// pandas 객체
'DataFrame', 'Series', 'Index', 'Period', 'GroupBy', 'Timestamp'
, ..._DATA_TYPES_OF_INDEX
, ..._DATA_TYPES_OF_GROUPBY
// Plot 관련 유형
//, 'Figure', 'AxesSubplot'
// Numpy
//, 'ndarray'
// Python 변수
//, 'str', 'int', 'float', 'bool', 'dict', 'list', 'tuple'
];
// function/method types
var _METHOD_TYPES = ['function', 'method', 'type', 'builtin_function_or_method', 'PlotAccessor'];
/**
* html load 콜백. 고유 id 생성하여 부과하며 js 객체 클래스 생성하여 컨테이너로 전달
* @param {function} callback 호출자(컨테이너) 의 콜백함수
*/
var optionLoadCallback = function(callback, meta) {
// document.getElementsByTagName("head")[0].appendChild(link);
// 컨테이너에서 전달된 callback 함수가 존재하면 실행.
if (typeof(callback) === 'function') {
var uuid = vpCommon.getUUID();
// 최대 10회 중복되지 않도록 체크
for (var idx = 0; idx < 10; idx++) {
// 이미 사용중인 uuid 인 경우 다시 생성
if ($(vpConst.VP_CONTAINER_ID).find("." + uuid).length > 0) {
uuid = vpCommon.getUUID();
}
}
$(vpCommon.wrapSelector(vpCommon.formatString("#{0}", vpConst.OPTION_GREEN_ROOM))).find(vpCommon.formatString(".{0}", vpConst.API_OPTION_PAGE)).addClass(uuid);
// 옵션 객체 생성
var varPackage = new VariablePackage(uuid);
varPackage.metadata = meta;
// 옵션 속성 할당.
varPackage.setOptionProp(funcOptProp);
// load metadata
// if (meta != undefined && meta.options != undefined) {
// try {
// var leftMeta = decodeURIComponent(meta.options[0].value);
// var rightMeta = decodeURIComponent(meta.options[1].value);
// var leftBlocks = JSON.parse(leftMeta);
// var rightBlocks = JSON.parse(rightMeta);
// varPackage.state.left.board.loadBoard(leftBlocks);
// varPackage.state.right.board.loadBoard(rightBlocks);
// } catch {
// ;
// }
// }
// html 설정.
varPackage.initHtml();
callback(varPackage); // 공통 객체를 callback 인자로 전달
}
}
/**
* html 로드.
* @param {function} callback 호출자(컨테이너) 의 콜백함수
*/
var initOption = function(callback, meta) {
vpCommon.loadHtml(vpCommon.wrapSelector(vpCommon.formatString("#{0}", vpConst.OPTION_GREEN_ROOM)), "file_io/instance.html", optionLoadCallback, callback, meta);
}
/**
* 본 옵션 처리 위한 클래스
* @param {String} uuid 고유 id
*/
var VariablePackage = function(uuid) {
this.uuid = uuid; // Load html 영역의 uuid.
this.package = {
input: [
{ name: 'vp_instanceVariable' },
{ name: 'vp_instanceAllocate' }
]
}
this.state = {
variable: {
subsetEditor: undefined,
codeEditor: undefined,
stack: []
}
, allocate: {
subsetEditor: undefined,
codeEditor: undefined,
stack: []
},
selectedBox: 'variable'
}
this.pointer = this.state.variable;
this.cmconfig = {
mode: {
name: 'python',
version: 3,
singleLineStringErrors: false
}, // text-cell(markdown cell) set to 'htmlmixed'
indentUnit: 4,
matchBrackets: true,
autoRefresh: true,
readOnly: true,
// lineWrapping: true, // text-cell(markdown cell) set to true
theme: "default",
extraKeys: {"Enter": "newlineAndIndentContinueMarkdownList"},
scrollbarStyle: "null"
}
}
/**
* vpFuncJS 에서 상속
*/
VariablePackage.prototype = Object.create(vpFuncJS.VpFuncJS.prototype);
/**
* 유효성 검사
* @returns 유효성 검사 결과. 적합시 true
*/
VariablePackage.prototype.optionValidation = function() {
return true;
// 부모 클래스 유효성 검사 호출.
// vpFuncJS.VpFuncJS.prototype.optionValidation.apply(this);
}
/**
* html 내부 binding 처리
*/
VariablePackage.prototype.initHtml = function() {
var that = this;
this.loadCss(Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.STYLE_PATH + "pandas/commonPandas.css");
this.loadCss(Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.STYLE_PATH + "file_io/instance.css");
this.bindEvent();
// variable - codemirror
this.state.variable.codeEditor = CodeMirror.fromTextArea($(this.wrapSelector('#vp_instanceVariable'))[0], this.cmconfig);
this.updateValue('variable', '');
// default select variable codemirror
$('.vp-instance-box.variable .CodeMirror').addClass('selected');
// allocate - codemirror
this.state.allocate.codeEditor = CodeMirror.fromTextArea($(this.wrapSelector('#vp_instanceAllocate'))[0], this.cmconfig);
this.updateValue('allocate', '');
// load metadata
var variable = this.getMetadata('vp_instanceVariable');
var allocate = this.getMetadata('vp_instanceAllocate');
this.updateValue('variable', variable);
this.updateValue('allocate', allocate);
// vpSubsetEditor
this.state.variable.subsetEditor = new vpSubsetEditor(this, "vp_instanceVariable", true);
this.state.allocate.subsetEditor = new vpSubsetEditor(this, "vp_instanceAllocate", true);
this.state.variable.subsetEditor.hideButton();
this.state.allocate.subsetEditor.hideButton();
this.ALLOW_SUBSET_TYPES = that.pointer.subsetEditor.getAllowSubsetTypes();
// vpInstanceEditor
this.state.variable.insEditor = new vpInstanceEditor(this, "vp_instanceVariable", 'vp_variableInsEditContainer');
// vpInstanceEditor
this.state.allocate.insEditor = new vpInstanceEditor(this, "vp_instanceAllocate", 'vp_allocateInsEditContainer');
that.state.variable.insEditor.show();
that.state.allocate.insEditor.hide();
// variable load
that.reloadInsEditor();
}
VariablePackage.prototype.bindEvent = function() {
var that = this;
// clear
$(this.wrapSelector('#vp_instanceClear')).click(function(event) {
that.addStack();
that.updateValue('', '');
that.reloadInsEditor();
});
// undo
$(this.wrapSelector('#vp_instanceUndo')).click(function(event) {
that.popStack();
that.reloadInsEditor();
});
// subset button clicked
$(document).on('click', this.wrapSelector('.vp-ds-button'), function(event) {
var insEditorType = $(this).closest('.vp-instance-box').hasClass('variable')? 'variable': 'allocate';
$(that.wrapSelector('.CodeMirror')).removeClass('selected');
if (insEditorType == 'variable') {
// variable
that.state.variable.insEditor.show();
that.state.allocate.insEditor.hide();
that.pointer = that.state.variable;
$(that.wrapSelector('.variable .CodeMirror')).addClass('selected');
} else if (insEditorType == 'allocate'){
// allocate
that.state.variable.insEditor.hide();
that.state.allocate.insEditor.show();
that.pointer = that.state.allocate;
$(that.wrapSelector('.allocate .CodeMirror')).addClass('selected');
} else {
that.state.variable.insEditor.hide();
that.state.allocate.insEditor.hide();
}
});
// subset applied - variable
$(document).on('change subset_apply', this.wrapSelector('#vp_instanceVariable'), function(event) {
var val = $(this).val();
that.addStack();
that.updateValue('variable', val);
});
// subset applied - allocate
$(document).on('change subset_apply', this.wrapSelector('#vp_instanceAllocate'), function(event) {
var val = $(this).val();
that.addStack();
that.updateValue('allocate', val);
});
// codemirror clicked
$(document).on('click', this.wrapSelector('.CodeMirror'), function(event) {
$(that.wrapSelector('.CodeMirror')).removeClass('selected');
$(this).addClass('selected');
// show/hide insEditor
var insEditorType = $(this).closest('.vp-instance-box').hasClass('variable')? 'variable': 'allocate';
if (insEditorType == 'variable') {
// variable
that.state.variable.insEditor.show();
that.state.allocate.insEditor.hide();
that.state.selectedBox = 'variable';
that.pointer = that.state.variable;
} else if (insEditorType == 'allocate'){
// allocate
that.state.variable.insEditor.hide();
that.state.allocate.insEditor.show();
that.state.selectedBox = 'allocate';
that.pointer = that.state.allocate;
} else {
that.state.selectedBox = '';
that.state.variable.insEditor.hide();
that.state.allocate.insEditor.hide();
}
});
$(document).on('focus', this.wrapSelector('.CodeMirror'), function(event) {
$(this).trigger('click');
});
// instance_editor_selected - variable
$(document).on('instance_editor_selected', this.wrapSelector('#vp_instanceVariable'), function(event) {
that.addStack();
var nowCode = that.state.variable.codeEditor.getValue();
if (nowCode != '') {
nowCode += '.'
}
var selectedVariable = event.varName;
that.updateValue('variable', nowCode + selectedVariable);
that.reloadInsEditor('variable');
});
// instance_editor_selected - allocate
$(document).on('instance_editor_selected', this.wrapSelector('#vp_instanceAllocate'), function(event) {
that.addStack();
var nowCode = that.state.allocate.codeEditor.getValue();
if (nowCode != '') {
nowCode += '.'
}
var selectedVariable = event.varName;
that.updateValue('allocate', nowCode + selectedVariable);
that.reloadInsEditor('allocate');
});
// instance_editor_replaced - variable
$(document).on('instance_editor_replaced', this.wrapSelector('#vp_instanceVariable'), function(event) {
that.addStack();
var newCode = event.newCode;
that.updateValue('variable', newCode);
that.reloadInsEditor('variable');
});
// instance_editor_replaced - allocate
$(document).on('instance_editor_replaced', this.wrapSelector('#vp_instanceAllocate'), function(event) {
that.addStack();
var newCode = event.newCode;
that.updateValue('allocate', newCode);
that.reloadInsEditor('allocate');
});
}
VariablePackage.prototype.updateValue = function(type, value) {
if (type == '') {
type = this.state.selectedBox;
}
this.state[type].codeEditor.setValue(value);
this.state[type].codeEditor.save();
this.state[type].codeEditor.focus();
this.state[type].codeEditor.setCursor({ line: 1, ch: value.length})
}
VariablePackage.prototype.addStack = function() {
var currentValue = this.pointer.codeEditor.getValue();
this.pointer.stack.push(currentValue);
// if stack over MAX_STACK_SIZE
if (this.pointer.stack.length > MAX_STACK_SIZE) {
this.pointer.stack.splice(0, 1);
}
console.log('add stack', currentValue, this.pointer.stack);
}
VariablePackage.prototype.popStack = function(replace=true) {
if (this.pointer.stack.length <= 0) {
return '';
}
var lastValue = this.pointer.stack.pop();
if (replace) {
this.updateValue('', lastValue);
}
console.log('pop stack', lastValue, this.pointer.stack);
return lastValue;
}
VariablePackage.prototype.reloadInsEditor = function(type='') {
var that = this;
var tempPointer = this.pointer;
var callbackFunction = function (varObj) {
var varType = varObj.type;
if (that.ALLOW_SUBSET_TYPES.includes(varType)) {
tempPointer.subsetEditor.showButton();
} else {
tempPointer.subsetEditor.hideButton();
}
};
if (type == '') {
this.pointer.insEditor.reload(callbackFunction);
} else {
tempPointer = this.state[type];
this.state[type].insEditor.reload(callbackFunction);
}
}
/**
* 코드 생성
* @param {boolean} exec 실행여부
*/
VariablePackage.prototype.generateCode = function(addCell, exec) {
var sbCode = new sb.StringBuilder();
// 변수 내용 조회
var leftCode = this.state.allocate.codeEditor.getValue();
var rightCode = this.state.variable.codeEditor.getValue();
if (leftCode && leftCode != '') {
sbCode.appendFormat('{0} = {1}', leftCode, rightCode);
} else {
sbCode.appendFormat('{0}', rightCode);
}
if (addCell) this.cellExecute(sbCode.toString(), exec);
return sbCode.toString();
}
return {
initOption: initOption
};
})