forked from visualpython/visualpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcept_option.js
More file actions
405 lines (319 loc) · 20.2 KB
/
Copy pathexcept_option.js
File metadata and controls
405 lines (319 loc) · 20.2 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
define([
'jquery'
, 'nbextensions/visualpython/src/common/vpCommon'
, 'nbextensions/visualpython/src/common/StringBuilder'
, 'nbextensions/visualpython/src/common/component/vpTableLayoutVerticalSimple'
, '../../api.js'
, '../../constData.js'
, '../base/index.js'
], function ( $, vpCommon, sb, vpTableLayoutVerticalSimple
, api
, constData
, baseComponent ) {
const { CreateOneArrayValueAndGet
, UpdateOneArrayValueAndGet
, DeleteOneArrayValueAndGet
, GenerateExceptCode } = api;
const { BLOCK_CODELINE_TYPE
, IF_BLOCK_CONDITION_TYPE
, IF_BLOCK_SELECT_VALUE_ARG_TYPE
, STR_EMPTY
, STR_DISABLED
, STR_CLICK
, STR_CHANGE_KEYUP_PASTE
, STR_VARIABLE
, STR_OPERATOR
, STATE_exceptConditionList
, VP_CLASS_STYLE_FLEX_ROW
, VP_CLASS_STYLE_FLEX_ROW_BETWEEN
, VP_CLASS_STYLE_WIDTH_20PERCENT
, VP_CLASS_STYLE_WIDTH_35PERCENT
, VP_CLASS_STYLE_WIDTH_100PERCENT
, VP_CLASS_STYLE_BGCOLOR_C4C4C4
, VP_ID_PREFIX
, VP_ID_APIBLOCK_OPTION_IF_ARG
, VP_ID_APIBLOCK_OPTION_IF_ARG_1
, VP_ID_APIBLOCK_OPTION_IF_ARG_2
, VP_ID_APIBLOCK_OPTION_IF_ARG_3
, VP_ID_APIBLOCK_OPTION_IF_USER_INPUT
, VP_ID_APIBLOCK_OPTION_IF_PLUS
, VP_ID_APIBLOCK_OPTION_IF_PLUS_USER_INPUT
, VP_ID_APIBLOCK_OPTION_IF_DELETE
, COMPARISON_OPERATOR_IF_ARG2 } = constData;
const { MakeOptionContainer
, MakeOptionDeleteButton
, MakeVpSuggestInputText_apiblock
, MakeOptionInput } = baseComponent;
var InitIfBlockOption = function(thisBlock, optionPageSelector) {
var uuid = thisBlock.getUUID();
var blockContainerThis = thisBlock.getBlockContainerThis();
var ifConditionListState = thisBlock.getState(STATE_exceptConditionList);
/** --------------------------------- If Option 이벤트 함수 바인딩 ---------------------------------- */
/** if 변경 이벤트 함수 바인딩 */
ifConditionListState.forEach( ( condition, index ) => {
var uuid = thisBlock.getUUID();
const { conditionType } = condition;
// IF_BLOCK_CONDITION_TYPE
if (conditionType == IF_BLOCK_CONDITION_TYPE.ARG) {
for (var k = 1; k < 4; k++) {
((i) => {
/** arg1 arg2 arg3 arg4 arg5 arg6 */
/** If arg input 변경 이벤트 함수 바인딩 */
$(document).off(STR_CHANGE_KEYUP_PASTE, VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG +i+ index + uuid);
$(document).on(STR_CHANGE_KEYUP_PASTE, VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG +i+ index + uuid, function(event) {
var ifConditionListState = thisBlock.getState(STATE_exceptConditionList);
var ifConditionState = ifConditionListState[index];
var inputValue = $(this).val();
var updatedValue;
if (i == 1) {
updatedValue = {
...ifConditionState
, arg1: inputValue
}
} else if (i == 2) {
updatedValue = {
...ifConditionState
, arg2: inputValue
}
} else if (i == 3) {
updatedValue = {
...ifConditionState
, arg3: inputValue
}
}
ifConditionListState = UpdateOneArrayValueAndGet(ifConditionListState, index, updatedValue);
thisBlock.setState({
[STATE_exceptConditionList]: ifConditionListState
});
var ifConditionCode = GenerateExceptCode(thisBlock,BLOCK_CODELINE_TYPE.ELIF);
thisBlock.writeCode(ifConditionCode);
event.stopPropagation();
});
})(k);
}
} else {
/** codeline arg6 */
/** If USER input 변경 이벤트 함수 바인딩 */
$(document).off(STR_CHANGE_KEYUP_PASTE, VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_USER_INPUT + index + uuid);
$(document).on(STR_CHANGE_KEYUP_PASTE, VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_USER_INPUT + index + uuid, function(event) {
var ifConditionListState = thisBlock.getState(STATE_exceptConditionList);
var ifConditionState = ifConditionListState[index];
var inputValue = $(this).val();
var updatedValue= {
...ifConditionState
, codeLine: inputValue
}
ifConditionListState = UpdateOneArrayValueAndGet(ifConditionListState, index, updatedValue);
thisBlock.setState({
[STATE_exceptConditionList]: ifConditionListState
});
var ifConditionCode = GenerateExceptCode(thisBlock,BLOCK_CODELINE_TYPE.ELIF);
thisBlock.writeCode(ifConditionCode);
event.stopPropagation();
});
}
/** 삭제 if condition */
$(document).off(STR_CLICK, vpCommon.wrapSelector(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_DELETE + index + uuid));
$(document).on(STR_CLICK, vpCommon.wrapSelector(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_DELETE + index + uuid), function(event) {
var ifConditionListState = thisBlock.getState(STATE_exceptConditionList);
if (ifConditionListState.length == 1) {
return;
}
ifConditionListState = DeleteOneArrayValueAndGet(ifConditionListState, index);
thisBlock.setState({
[STATE_exceptConditionList]: ifConditionListState
});
var ifConditionCode = GenerateExceptCode(thisBlock, BLOCK_CODELINE_TYPE.ELIF);
thisBlock.writeCode(ifConditionCode);
blockContainerThis.renderBlockOptionTab();
event.stopPropagation();
});
});
/** 생성 if condition - arg1, arg2, arg3, arg4, arg5, arg6*/
$(document).off(STR_CLICK, vpCommon.wrapSelector(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_PLUS + uuid));
$(document).on(STR_CLICK, vpCommon.wrapSelector(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_PLUS + uuid), function(event) {
var ifConditionListState = thisBlock.getState(STATE_exceptConditionList);
var newCondition = {
arg1: ''
, arg2: 'none'
, arg3: ''
, conditionType: IF_BLOCK_CONDITION_TYPE.ARG
}
ifConditionListState = CreateOneArrayValueAndGet(ifConditionListState, ifConditionListState.length, newCondition);
thisBlock.setState({
[STATE_exceptConditionList]: ifConditionListState
});
var ifConditionCode = GenerateExceptCode(thisBlock, BLOCK_CODELINE_TYPE.ELIF);
thisBlock.writeCode(ifConditionCode);
blockContainerThis.renderBlockOptionTab();
event.stopPropagation();
});
/** 생성 if condition - User Input ,arg6*/
$(document).off(STR_CLICK, vpCommon.wrapSelector(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_PLUS_USER_INPUT + uuid));
$(document).on(STR_CLICK, vpCommon.wrapSelector(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_PLUS_USER_INPUT + uuid), function(event) {
var ifConditionListState = thisBlock.getState(STATE_exceptConditionList);
var newCondition = {
codeLine: ''
, arg6: ''
, conditionType: IF_BLOCK_CONDITION_TYPE.USER_INPUT
}
ifConditionListState = CreateOneArrayValueAndGet(ifConditionListState, ifConditionListState.length, newCondition);
thisBlock.setState({
[STATE_exceptConditionList]: ifConditionListState
});
var ifConditionCode = GenerateExceptCode(thisBlock, BLOCK_CODELINE_TYPE.ELIF);
thisBlock.writeCode(ifConditionCode);
blockContainerThis.renderBlockOptionTab();
event.stopPropagation();
});
/** arg1, arg2, arg3, arg4, arg5, arg6 select 변경 if condition */
var bindSelectValueEventFunc_if = function(selectedValue, index, type) {
var ifConditionListState = thisBlock.getState(STATE_exceptConditionList);
var ifConditionState = ifConditionListState[index];
var updatedValue;
if (type == IF_BLOCK_SELECT_VALUE_ARG_TYPE.ARG1) {
updatedValue = {
...ifConditionState
, arg1: selectedValue
}
} else if (type == IF_BLOCK_SELECT_VALUE_ARG_TYPE.ARG2) {
updatedValue = {
...ifConditionState
, arg2: selectedValue
}
if (selectedValue == 'none' || selectedValue == STR_EMPTY) {
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_2 + index + uuid).addClass(VP_CLASS_STYLE_BGCOLOR_C4C4C4);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid).attr(STR_DISABLED, true);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid).addClass(VP_CLASS_STYLE_BGCOLOR_C4C4C4);
} else {
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_2 + index + uuid).removeClass(VP_CLASS_STYLE_BGCOLOR_C4C4C4);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid).attr(STR_DISABLED, false);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid).removeClass(VP_CLASS_STYLE_BGCOLOR_C4C4C4);
}
} else if (type == IF_BLOCK_SELECT_VALUE_ARG_TYPE.ARG3) {
updatedValue = {
...ifConditionState
, arg3: selectedValue
}
}
ifConditionListState = UpdateOneArrayValueAndGet(ifConditionListState, index, updatedValue);
thisBlock.setState({
[STATE_exceptConditionList]: ifConditionListState
});
var ifConditionCode = GenerateExceptCode(thisBlock, BLOCK_CODELINE_TYPE.ELIF);
thisBlock.writeCode(ifConditionCode);
}
/** --------------------------------- Else Option 이벤트 함수 바인딩 ---------------------------------- */
/** If option 렌더링 */
var renderThisComponent = function() {
var optionContainerDom = MakeOptionContainer(thisBlock);
/* --------------------------- if ----------------------------- */
var loadedVariableNameList = blockContainerThis.getKernelLoadedVariableNameList();
var ifConditionListState = thisBlock.getState(STATE_exceptConditionList);
ifConditionListState.forEach( (condition, index) => {
const { conditionType } = condition;
var sbCondition = new sb.StringBuilder();
sbCondition.appendFormatLine("<div class='{0}' ", VP_CLASS_STYLE_FLEX_ROW_BETWEEN);
sbCondition.appendFormatLine(" style='{0}' >",'');
var sbConditionLeft = new sb.StringBuilder();
sbConditionLeft.appendFormatLine("<div class='{0}' ", VP_CLASS_STYLE_FLEX_ROW_BETWEEN);
sbConditionLeft.appendFormatLine(" style='{0}' >",'width:80%;');
// var sbConditionDom = $(sbCondition.toString());
if (conditionType == IF_BLOCK_CONDITION_TYPE.ARG) {
const { arg1, arg2, arg3 } = condition;
var loadedVariableNameList_arg1 = [ `AssertionError`, 'SystemError', 'TypeError', 'ModuleNotFoundError',
'BaseException', 'FileNotFoundError', 'ImportError',
'IndexError', 'MemoryError', 'LookupError', 'BufferError',
'EOFError'];
var loadedVariableNameList_arg3 = [ `e_${index + 1}`, ...loadedVariableNameList];
// var loadedVariableNameList_arg5 = [ `'k${index + 1}'`, ...loadedVariableNameList];
var suggestInputArg1 = MakeVpSuggestInputText_apiblock(VP_ID_APIBLOCK_OPTION_IF_ARG_1 + index + uuid
, arg1
, loadedVariableNameList_arg1
, VP_CLASS_STYLE_WIDTH_35PERCENT
, 'Error'
, function(selectedValue) {
bindSelectValueEventFunc_if(selectedValue,
index,
IF_BLOCK_SELECT_VALUE_ARG_TYPE.ARG1);
});
var suggestInputArg2 = MakeVpSuggestInputText_apiblock(VP_ID_APIBLOCK_OPTION_IF_ARG_2 + index + uuid
, arg2
, [ 'none', 'as', ...COMPARISON_OPERATOR_IF_ARG2]
, VP_CLASS_STYLE_WIDTH_20PERCENT
, STR_OPERATOR
, function(selectedValue) {
bindSelectValueEventFunc_if(selectedValue,
index,
IF_BLOCK_SELECT_VALUE_ARG_TYPE.ARG2);
});
var suggestInputArg3 = MakeVpSuggestInputText_apiblock(VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid
, arg3
, loadedVariableNameList_arg3
, VP_CLASS_STYLE_WIDTH_35PERCENT
, STR_VARIABLE
, function(selectedValue) {
bindSelectValueEventFunc_if(selectedValue,
index,
IF_BLOCK_SELECT_VALUE_ARG_TYPE.ARG3);
});
sbConditionLeft.appendLine(suggestInputArg1);
sbConditionLeft.appendLine(suggestInputArg2);
sbConditionLeft.appendLine(suggestInputArg3);
sbConditionLeft.appendLine("</div>");
sbCondition.appendLine(sbConditionLeft.toString());
var deleteConditionButton = MakeOptionDeleteButton(VP_ID_APIBLOCK_OPTION_IF_DELETE + index + uuid);
sbCondition.appendLine(deleteConditionButton);
sbCondition.appendLine("</div>");
} else {
const { codeLine } = condition;
var sbOptionInput = MakeOptionInput(VP_ID_APIBLOCK_OPTION_IF_USER_INPUT + index + uuid
, VP_CLASS_STYLE_WIDTH_100PERCENT
, codeLine
, 'Type user code');
sbConditionLeft.appendLine(sbOptionInput);
sbConditionLeft.appendLine("</div>");
sbCondition.appendLine(sbConditionLeft.toString());
sbCondition.appendLine("</div>");
}
var tblLayout = new vpTableLayoutVerticalSimple.vpTableLayoutVerticalSimple();
tblLayout.setTHWidth("5%");
tblLayout.addClass(VP_CLASS_STYLE_WIDTH_100PERCENT);
tblLayout.addRow('', sbCondition.toString());
optionContainerDom.append(tblLayout.toTagString());
});
var sbButtonContainer = new sb.StringBuilder();
sbButtonContainer.appendFormatLine("<div class='{0}' style='{1}' >", VP_CLASS_STYLE_FLEX_ROW, 'margin-top:15px;');
sbButtonContainer.appendLine("</div>");
var sbButtonContainerDom = $(sbButtonContainer.toString());
optionContainerDom.append(sbButtonContainerDom);
/** bottom block option 탭에 렌더링된 dom객체 생성 */
$(optionPageSelector).append(optionContainerDom);
/** 새로 IF OPTION을 열었을때,
*
* 1. arg value 값 넣기,
* 2. none disabled 설정,
* 3. none opacity 0 설정
* 4. 입력되지 않은 값 border alert 처리
* */
ifConditionListState.forEach( (condition,index) => {
const { arg1, arg2, arg3, arg4, arg5, arg6 } = condition;
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_1 + index + uuid).val(arg1);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_2 + index + uuid).val(arg2);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid).val(arg3);
if (arg2 == 'none' || arg2 == STR_EMPTY) {
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_2 + index + uuid).addClass(VP_CLASS_STYLE_BGCOLOR_C4C4C4);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid).attr(STR_DISABLED, true);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid).addClass(VP_CLASS_STYLE_BGCOLOR_C4C4C4);
} else {
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_2 + index + uuid).removeClass(VP_CLASS_STYLE_BGCOLOR_C4C4C4);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid).attr(STR_DISABLED, false);
$(VP_ID_PREFIX + VP_ID_APIBLOCK_OPTION_IF_ARG_3 + index + uuid).removeClass(VP_CLASS_STYLE_BGCOLOR_C4C4C4);
}
});
return optionContainerDom;
}
return renderThisComponent();
}
return InitIfBlockOption;
});