-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathblock.js
More file actions
2571 lines (2198 loc) · 98 KB
/
block.js
File metadata and controls
2571 lines (2198 loc) · 98 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
define([
'nbextensions/visualpython/src/common/vpCommon'
, 'nbextensions/visualpython/src/container/vpContainer'
, 'nbextensions/visualpython/src/common/constant'
, './api.js'
, './constData.js'
, './component/option/class_option.js'
, './component/option/def_option.js'
, './component/option/if_option.js'
, './component/option/elif_option.js'
, './component/option/except_option.js'
, './component/option/for_option.js'
, './component/option/import_option.js'
, './component/option/code_option.js'
, './component/option/return_option.js'
, './component/option/while_option.js'
, './component/option/lambda_option.js'
, './component/option/api_option.js'
, './component/option/text_option.js'
, './component/option/node_option.js'
, './component/option/none_option.js'
], function ( vpCommon, vpContainer, vpConst
, api
, constData
, InitClassBlockOption
, InitDefBlockOption
, InitIfBlockOption
, InitElifBlockOption
, InitExceptBlockOption
, InitForBlockOption
, InitImportBlockOption
, InitCodeBlockOption
, InitReturnBlockOption
, InitWhileBlockOption
, InitLambdaBlockOption
, ApiBlockOption
, InitTextBlockOption
, InitNodeBlockOption
, InitNoneOption) {
const { InitAPIBlockOption, LoadAPIBlockOption } = ApiBlockOption;
const { SetChildBlockList_down_first_indent_last
, MapTypeToName
, MapNewLineStrToIndentString
, IsCanHaveIndentBlock
, IsElifElseExceptFinallyBlockType
, IsIfForTryBlockType
, GenerateApiCode
, GenerateClassInParamList
, GenerateDefCode
, GenerateReturnCode
, GenerateIfCode
, GenerateForCode
, GenerateLambdaCode
, GenerateExceptCode
, GenerateImportCode
, GenerateWhileCode
, IsCodeBlockType
, IsDefineBlockType
, IsNodeTextBlockType
, ShowCodeBlockCode } = api;
const { BLOCK_CODELINE_TYPE
, BLOCK_DIRECTION
, FOCUSED_PAGE_TYPE
, FOR_BLOCK_ARG3_TYPE
, IF_BLOCK_CONDITION_TYPE
, NUM_BLOCK_HEIGHT_PX
, NUM_INDENT_DEPTH_PX
, NUM_MAX_ITERATION
, NUM_NODE_OR_TEXT_BLOCK_MARGIN_TOP_PX
, NUM_EXCEED_DEPTH
, NUM_BLOCK_BOTTOM_HOLDER_HEIGHT
, STR_EMPTY
, STR_TOP
, STR_LEFT
, STR_BORDER
, STR_BORDER_LEFT
, STR_PX
, STR_OPACITY
, STR_MARGIN_TOP
, STR_MARGIN_LEFT
, STR_DISPLAY
, STR_BACKGROUND_COLOR
, STR_HEIGHT
, STR_NONE
, STR_BLOCK
, STR_FLEX
, STR_POSITION
, STR_ABSOLUTE
, STR_TRANSPARENT
, STR_WIDTH
, STR_TITLE
, STR_BREAK
, STR_CONTINUE
, STR_PASS
, STR_ONE_SPACE
, STR_ONE_INDENT
, STR_MSG_BLOCK_DEPTH_MUSH_NOT_EXCEED_6
, STR_CLICK
, STR_RIGHT_CLICK
, STR_KEYWORD_NEW_LINE
, STR_INPUT_YOUR_CODE
, STR_COLOR
, VP_BLOCK
, VP_BLOCK_BLOCKCODELINETYPE_CLASS_DEF
, VP_CLASS_PREFIX
, VP_CLASS_APIBLOCK_BOARD
, VP_CLASS_BLOCK_BOTTOM_HOLDER
, VP_CLASS_BLOCK_SHADOWBLOCK_CONTAINER
, VP_CLASS_APIBLOCK_BLOCK_HEADER
, VP_CLASS_SELECTED_SHADOWBLOCK
, VP_CLASS_STYLE_FLEX_ROW_BETWEEN
, VP_CLASS_STYLE_FLEX_ROW_END
, VP_CLASS_BLOCK_SUB_BTN_CONTAINER
, VP_CLASS_BLOCK_LEFT_HOLDER
, VP_CLASS_BLOCK_DEPTH_INFO
, VP_CLASS_BLOCK_NUM_INFO
, VP_CLASS_APIBLOCK_NODEBLOCK_INPUT
, VP_CLASS_APIBLOCK_NODEBLOCK_TEXT
, VP_CLASS_APIBLOCK_NODEBLOCK
, VP_CLASS_APIBLOCK_NODEBLOCK_TEXT_CONTAINER
, STR_CHANGE_KEYUP_PASTE
, STATE_className
, STATE_defName
, STATE_isIfElse
, STATE_isFinally
, STATE_codeLine
, COLOR_CLASS_DEF
, COLOR_CONTROL
, COLOR_API
, COLOR_CODE
, COLOR_CLASS_DEF_STRONG
, COLOR_CONTROL_STRONG
, COLOR_API_STRONG
, COLOR_CODE_STRONG
, COLOR_WHITE
, COLOR_BLACK
, COLOR_LINENUMBER
, COLOR_GRAY_input_your_code
, ERROR_AB0002_INFINITE_LOOP
, IMPORT_DEFAULT_DATA_LIST } = constData;
var Block = function(blockContainerThis, type , blockData, isGroupBlock=false) {
var codeStr = STR_EMPTY;
var blockType = type;
if ( blockType == BLOCK_CODELINE_TYPE.CLASS ) {
var classNum = blockContainerThis.getClassNum();
blockContainerThis.addClassNum();
} else if ( blockType == BLOCK_CODELINE_TYPE.DEF) {
var defNum = blockContainerThis.getDefNum();
blockContainerThis.addDefNum();
} else if (blockType == BLOCK_CODELINE_TYPE.BREAK) {
codeStr = STR_BREAK
} else if (blockType == BLOCK_CODELINE_TYPE.CONTINUE) {
codeStr = STR_CONTINUE
} else if (blockType == BLOCK_CODELINE_TYPE.PASS) {
codeStr = STR_PASS
} else if (blockType == BLOCK_CODELINE_TYPE.NODE) {
blockContainerThis.addNodeBlock(this);
} else if (blockType == BLOCK_CODELINE_TYPE.TEXT) {
blockContainerThis.addTextBlock(this);
}
/** state 데이터는 vpnote로 저장할 때, 저장 되는 데이터
* state가 아닌 데이터는 저장 할 수 없고, 주피터에서 블럭을 표현하는 용도로만 사용된다.
*/
this.state = {
className: 'vpClass' + classNum
, parentClassName: STR_EMPTY
, classInParamList: [STR_EMPTY]
, defName: 'vpFunc' + defNum
, defInParamList: []
, ifConditionList: [
{
arg1: STR_EMPTY
, arg2: STR_EMPTY
, arg3: STR_EMPTY
, arg4: STR_EMPTY
, arg5: STR_EMPTY
, arg6: STR_EMPTY
, codeLine: STR_EMPTY
, conditionType: IF_BLOCK_CONDITION_TYPE.ARG
}
]
, elifConditionList: [
{
arg1: STR_EMPTY
, arg2: STR_EMPTY
, arg3: STR_EMPTY
, arg4: STR_EMPTY
, arg5: STR_EMPTY
, arg6: STR_EMPTY
, codeLine: STR_EMPTY
, conditionType: IF_BLOCK_CONDITION_TYPE.ARG
}
]
, isIfElse: false
, forParam: {
arg1: STR_EMPTY
, arg2: STR_EMPTY
, arg3: FOR_BLOCK_ARG3_TYPE.INPUT_STR
, arg4: STR_EMPTY
, arg5: STR_EMPTY
, arg6: STR_EMPTY
, arg7: STR_EMPTY
, arg3InputStr: STR_EMPTY
, arg3Default: STR_EMPTY
}
, whileConditionList: [
{
arg1: STR_EMPTY
, arg2: STR_EMPTY
, arg3: STR_EMPTY
, arg4: 'and'
}
]
, exceptConditionList: [
{
arg1: STR_EMPTY
, arg2: `none`
, arg3: STR_EMPTY
, codeLine: STR_EMPTY
, conditionType: IF_BLOCK_CONDITION_TYPE.ARG
}
]
, isFinally: false
, baseImportList: IMPORT_DEFAULT_DATA_LIST
, customImportList: []
, isBaseImportPage: true
, lambdaArg1: STR_EMPTY
, lambdaArg2List: [ ]
, lambdaArg3: STR_EMPTY
, returnOutParamList: [ STR_EMPTY ]
, customCodeLine: codeStr
, metadata: null
, funcID: STR_EMPTY
}
this.state_backup = { ...this.state };
this.isGroupBlock = isGroupBlock;
/** string 데이터 */
this.blockName = STR_EMPTY;
this.codeLine = STR_EMPTY;
/** boolean 데이터 */
this.isClicked = false;
this.isCtrlPressed = false;
this.isNowMoved = false;
this.isIfElse = false;
this.isFinally = false;
/** number(숫자) 데이터 */
this.opacity = 1;
this.depth = 0;
this.blockLeftShadowHeight = 0;
this.blockNumber = 0;
this.width = blockContainerThis.getBlockMaxWidth();
/** this 블럭만이 가지는 특수 데이터
* 이 데이터들은 이동하거나 삭제 할 때 등 여러 용도로 사용 된다.
*/
this.type = type; // this 블럭의 타입 값 (type은 class : 1, def: 2, if: 3 ...)
this.childBlockUUIDList = []; // vpnote 저장용 데이터
this.uuid = vpCommon.getUUID(); // this 블럭의 고유 값
this.direction = BLOCK_DIRECTION.NONE; // this 블럭의 위치 값.
// 위치는 this 블럭의 부모로 부터 DOWN, INDENT인지 결정됨
/** this 데이터 */
this.blockContainerThis = blockContainerThis;
/** -------- 블럭을 표현하는 dom 데이터 ------------------------------- */
this.blockMainDom = null;
this.blockOptionPageDom = null;
/** -------- this 블럭을 중심으로 위치에 따른 block 데이터 ------------*/
this.prevBlock = null; // this 블럭의 부모 블럭
this.childBlock_down = null; // this 블럭의 자식 블럭 (DOWN 위치)
this.childBlock_indent = null; // this 블럭의 자식 블럭 (INDENT 위치)
this.childBlockList = []; // this 블럭의 자식 블럭 list
/** -------- this 블럭을 중심으로 특수 타입의 block 데이터 ---------*/
this.ifElseBlock = null; // if, for, try 블럭만 가질 수 있음
this.finallyBlock = null; // try 블럭만 가질 수 있음
this.lastElifBlock = null; // if 블럭만 가질 수 있음
this.lastChildBlock = null; // 블럭마다 클릭하면 고유 영역(this 블럭을 중심으로 자식 블럭 리스트)이 존재.
// 이 고유지역 중 가장 아래의 자식 블럭을 의미
this.propertyBlockFromDef = null; // def블럭만 가질 수 있음. def 블럭에서 decoration 블럭을 생성할 때의 decoration 블럭을 의미
/** ------- api list 블럭과 text 블럭만 가질 수 있는 데이터 ------------ */
this.optionPageLoadCallback = null;
this.loadOption = null;
this.importPakage = null;
/** -------- node 블럭만 가질 수 있는 데이터 --------------------------------------*/
this.isNodeBlockInput = false;
this.isNodeBlockToggled = false;
this.isNodeBlockTitleEmpty = true;
var name = MapTypeToName(type);
this.setBlockName(name);
/** vpnote를 open하고 블럭을 생성할 때 */
if (blockData) {
this.state = blockData.blockOptionState;
this.uuid = blockData.UUID;
/** Logic의 Define, Control, Execute을 드래그해서 블럭을 생성할 때 */
} else {
this.blockContainerThis.createBlock_fromLogic(this);
}
// if (!temporary) {
// this.blockContainerThis.addBlock(this);
// this.init();
// this.renderColor_thisBlockArea(true);
// }
}
Block.prototype.init = function() {
var blockContainerThis = this.getBlockContainerThis();
/** 기본 블럭의 dom 생성 */
var blockMainDom = blockContainerThis.makeBlockDom(this, true);
this.setBlockMainDom(blockMainDom);
/** node 블럭과 text 블럭일 경우의 만 LineNumberInfo Dom 생성 */
var blockType = this.getBlockType();
if ( blockType == BLOCK_CODELINE_TYPE.NODE
|| blockType == BLOCK_CODELINE_TYPE.TEXT
|| this.isGroupBlock) {
blockMainDom = blockContainerThis.makeBlockLineNumberInfoDom(this);
}
this.bindEventAll();
}
Block.prototype.apply = function() {
var blockContainerThis = this.blockContainerThis;
blockContainerThis.addBlock(this);
this.init();
this.renderColor_thisBlockArea(true);
this.getChildBlockList().forEach(childBlock => {
childBlock.apply();
});
}
Block.prototype.saveState = function() {
if (this.getBlockType() == BLOCK_CODELINE_TYPE.API
|| this.getBlockType() == BLOCK_CODELINE_TYPE.TEXT ) {
// set metadata
var importPackage = this.getImportPakage();
// get generatedCode and save as metadata
if (importPackage) {
var code = importPackage.generateCode(false, false);
importPackage.metaGenerate();
// importPackage.metadata.code = code;
importPackage['metadata'] = {
...importPackage['metadata'],
code : code
}
this.state.metadata = importPackage.metadata;
}
}
// save backup
this.state_backup = {
...this.state
};
this.isModified = false;
}
Block.prototype.loadState = function() {
this.state = {
...this.state_backup
};
// this.renderOptionPage(true); // Library 페이지는 로드가 늦게 되어서...
this.isModified = false;
}
/** 블럭에 표시할 데이터를 dom에 write함
* @param {string} textInfo 블럭의 text에 입력할 text string
*/
Block.prototype.writeCode = function(textInfo) {
var blockType = this.getBlockType();
var blockUUID = this.getUUID();
/** node 블럭일 경우 */
if (this.getBlockType() == BLOCK_CODELINE_TYPE.NODE) {
$(VP_CLASS_PREFIX + VP_CLASS_APIBLOCK_NODEBLOCK_TEXT + blockUUID).html(textInfo);
$(VP_CLASS_PREFIX + VP_CLASS_APIBLOCK_NODEBLOCK_INPUT + blockUUID).val(textInfo);
/** CODE 블럭일 경우 */
} else if ( IsCodeBlockType(blockType) == true ) {
var codeStr = ShowCodeBlockCode(this);
$(VP_CLASS_PREFIX + VP_CLASS_APIBLOCK_BLOCK_HEADER + blockUUID).html(codeStr);
$(VP_CLASS_PREFIX + VP_CLASS_APIBLOCK_BLOCK_HEADER + blockUUID).attr('title', codeStr);
/** node 블럭이 아닐 경우, CODE 블럭이 아닐 경우 */
} else {
if ( blockType == BLOCK_CODELINE_TYPE.CLASS) {
$(VP_CLASS_PREFIX + VP_CLASS_APIBLOCK_BLOCK_HEADER + 'class-name-' + blockUUID).html(this.getState('className'));
}
if ( blockType == BLOCK_CODELINE_TYPE.DEF) {
$(VP_CLASS_PREFIX + VP_CLASS_APIBLOCK_BLOCK_HEADER + 'def-name-' + blockUUID).html(this.getState('defName'));
}
$(VP_CLASS_PREFIX + VP_CLASS_APIBLOCK_BLOCK_HEADER + blockUUID).html(textInfo);
$(VP_CLASS_PREFIX + VP_CLASS_APIBLOCK_BLOCK_HEADER + blockUUID).attr('title', textInfo);
}
}
// ** --------------------------- Block을 삭제, 수정, 불러오기 혹은 주변 block과의 관계를 규정하는 메소드들 --------------------------- */
Block.prototype.isGroupBlock = function() {
return this.isGroupBlock;
}
Block.prototype.setGroupBlock = function(isGroupBlock) {
// if same state, do nothing
if (this.isGroupBlock == isGroupBlock) {
return;
}
// set groupblock
this.isGroupBlock = isGroupBlock;
// 1) remove group state
if (!isGroupBlock) {
this.blockContainerThis.removeNodeBlock(this);
$(this.blockMainDom).find(VP_CLASS_PREFIX + VP_CLASS_BLOCK_NUM_INFO).css('display','none');
$(this.blockMainDom).css(STR_MARGIN_TOP, 0);
}
// 2) add group state
else {
this.blockContainerThis.addNodeBlock(this);
if ($(this.blockMainDom).find(VP_CLASS_PREFIX + VP_CLASS_BLOCK_NUM_INFO).length <= 0) {
this.blockContainerThis.makeBlockLineNumberInfoDom(this);
}
$(this.blockMainDom).find(VP_CLASS_PREFIX + VP_CLASS_BLOCK_NUM_INFO).css('display','block');
$(this.blockMainDom).css(STR_MARGIN_TOP, NUM_NODE_OR_TEXT_BLOCK_MARGIN_TOP_PX);
}
}
Block.prototype.getPrevBlock = function() {
return this.prevBlock
}
Block.prototype.setPrevBlock = function(prevBlock) {
this.prevBlock = prevBlock;
}
Block.prototype.addChildBlockList = function(nextBlock) {
this.childBlockList = [ ...this.childBlockList, nextBlock]
}
Block.prototype.setChildBlockList = function(childBlockList) {
this.childBlockList = childBlockList;
}
Block.prototype.getChildBlockList = function() {
return this.childBlockList;
}
/** this 블럭의 자식 블럭 중에 DOWN 위치에 존재하는 블럭을 가져옴 */
Block.prototype.getChildBlock_down = function() {
var childBlock_down = null;
var childBlockList = this.getChildBlockList();
childBlockList.some(block => {
if ( block.getDirection() == BLOCK_DIRECTION.DOWN) {
childBlock_down = block;
return true;
}
});
return childBlock_down;
}
/** this 블럭의 자식 블럭 중에 INDENT 위치에 존재하는 블럭을 가져옴 */
Block.prototype.getChildBlock_indent = function() {
var childBlock_indent = null;
var childBlockList = this.getChildBlockList();
childBlockList.some(block => {
if ( block.getDirection() == BLOCK_DIRECTION.INDENT) {
childBlock_indent = block;
return true;
}
});
return childBlock_indent;
}
/** this 블럭의 자식 블럭(DOWN or INDENT 2개 까지 존재할 수 있음) 중에
* 인자로 들어온 deletedBlock과 일치하면 삭제
*/
Block.prototype.deleteChildBlock = function(deletedBlock) {
var childBlockList = this.getChildBlockList();
childBlockList.some((block, index) => {
if (block.getUUID() == deletedBlock.getUUID()) {
childBlockList.splice(index, 1);
}
});
}
Block.prototype.setPropertyBlockFromDef = function(propertyBlockFromDef) {
this.propertyBlockFromDef = propertyBlockFromDef;
}
Block.prototype.getPropertyBlockFromDef = function() {
return this.propertyBlockFromDef;
}
/**
* if 블럭이 생성한 elifList 중에 가장 아래에 위치한 elif block을 set, get
* @param {BLOCK} lastElifBlock
*/
Block.prototype.setLastElifBlock = function(lastElifBlock) {
this.lastElifBlock = lastElifBlock;
}
Block.prototype.getLastElifBlock = function() {
if (this.lastElifBlock) {
return this.lastElifBlock;
} else {
return this;
}
}
Block.prototype.getElseBlock = function() {
return this.ifElseBlock;
}
Block.prototype.setElseBlock = function(ifElseBlock) {
this.ifElseBlock = ifElseBlock;
}
/**
* ---------------thisBlockArea는 블럭을 클릭했을 때 진하게 색칠되는 영역을 말함--------------
* ---------------lastBlock_from_thisBlockArea은 thisBlockArea 영역의 맨 마지막 블럭을 의미함-----
*/
/**
* @param { Block } lastChildBlock
*/
Block.prototype.setLastBlock_from_thisBlockArea = function(lastChildBlock) {
this.lastChildBlock = lastChildBlock;
}
Block.prototype.getLastBlock_from_thisBlockArea = function() {
this.getBlockList_thisBlockArea();
return this.lastChildBlock;
}
/** 현재 root 블럭부터 모든 자식 블럭리스트 들을 board에 놓인 순서대로 전부 가져온다 */
Block.prototype.getRootToChildBlockList = function() {
var blockContainerThis = this.getBlockContainerThis();
var rootBlock = blockContainerThis.getRootBlock();
return rootBlock.getThisToLastBlockList();
}
/** 현재 this 블럭부터 모든 자식 블럭리스트 들을 board에 놓인 순서대로 가져온다
*/
Block.prototype.getThisToLastBlockList = function() {
var childBlockList = this.getChildBlockList();
var stack = [];
stack.push(childBlockList);
var thisToLastBlockList = [this];
thisToLastBlockList = this._getThisToLastBlockList(thisToLastBlockList, stack);
return thisToLastBlockList;
}
/**
* @private
* @param {Array<Block>} childBlockList
* @param {Array<Block>} stack
* @param {boolean} isNodeBlock node 블럭의 자식 블럭을 가져오냐 아니냐 true/false
* @param {boolean} isIfForTryBlock if, for, try 블럭의 자식 블럭을 가져오냐 아니냐 true/false
* @param {Block} ifForTryBlock if, for, try 블럭
*/
Block.prototype._getThisToLastBlockList = function(thisToLastBlockList, stack, isNodeBlock = false, isIfForTryBlock = false, ifForTryBlock) {
// console.log('isNodeBlock',isNodeBlock);
if (isIfForTryBlock == true) {
var _childBlockList = this.getChildBlockList();
stack.push(_childBlockList);
}
var iteration = 0;
var current;
while (stack.length != 0) {
current = stack.shift();
/** FIXME: 무한루프 체크 */
if (iteration > NUM_MAX_ITERATION) {
console.log(ERROR_AB0002_INFINITE_LOOP);
break;
}
iteration++;
/** 배열 일 때 */
if (Array.isArray(current)) {
var currBlockList = current;
stack = SetChildBlockList_down_first_indent_last(stack, currBlockList);
/** 배열 이 아닐 때 */
} else {
var currBlock = current;
var blockType = currBlock.getBlockType();
var blockDirection = currBlock.getDirection();
if (isNodeBlock == true) {
if ( (
IsNodeTextBlockType(currBlock.getBlockType()) == true
|| currBlock.isGroupBlock
)
&& currBlock.getUUID() != this.getUUID()) {
break;
}
this.setLastBlock_from_thisBlockArea(currBlock);
thisToLastBlockList.push(currBlock);
} else if (isIfForTryBlock == true) {
if ( blockDirection == BLOCK_DIRECTION.DOWN ) {
if (IsIfForTryBlockType(blockType) == true) {
break;
} else if (IsElifElseExceptFinallyBlockType(blockType) == true) {
var lastChildBlock = currBlock.getChildBlock_down();
if (lastChildBlock) {
thisToLastBlockList.push(lastChildBlock);
ifForTryBlock.setLastBlock_from_thisBlockArea(lastChildBlock);
}
var elifOrElseExceptFinallyBlockList = currBlock.getBlockList_thisBlockArea();
elifOrElseExceptFinallyBlockList.forEach(elifOrElseExceptFinallyBlock => {
thisToLastBlockList.push(elifOrElseExceptFinallyBlock);
});
}
}
} else {
thisToLastBlockList.push(currBlock);
}
var childBlockList = currBlock.getChildBlockList();
stack.unshift(childBlockList);
}
}
return thisToLastBlockList;
}
/** 현재 this 블럭부터 하위 depth 자식 블럭리스트(동일 depth 블럭 제거) 들을 전부 가져온다 */
Block.prototype.getBlockList_thisBlockArea = function() {
var stack = [];
var blockList_thisBlockArea = [];
var blockType = this.getBlockType();
if ( blockType == BLOCK_CODELINE_TYPE.NODE || this.isGroupBlock) {
stack = [this];
blockList_thisBlockArea = this._getThisToLastBlockList(blockList_thisBlockArea, stack, true);
return blockList_thisBlockArea;
}
/** this 블럭의 indent 위치에 자식 블럭이 있는지 확인
* this 블럭의 indent 위치에 자식 블럭이 있어야 하위 depth 자식 블럭들을 가져올 수 있다.
*/
var childBlock_indent = this.getChildBlock_indent();
if (childBlock_indent) {
stack.push(childBlock_indent);
}
blockList_thisBlockArea = [this];
/** this 블럭의 down 위치에 자식 블럭이 있는지 확인 */
var lastChildBlock = null;
if (IsCanHaveIndentBlock(blockType) == true) {
var childBlock_down = this.getChildBlock_down();
if (childBlock_down){
blockList_thisBlockArea.push(childBlock_down);
lastChildBlock = childBlock_down;
}
} else {
lastChildBlock = this;
}
/** if for try 일 경우 */
if ( IsIfForTryBlockType(blockType) == true
&& lastChildBlock != null) {
var ifForTryList = lastChildBlock._getThisToLastBlockList([], [], false, true, this);
blockList_thisBlockArea = this._getThisToLastBlockList(blockList_thisBlockArea, stack);
if (!this.lastChildBlock) {
this.setLastBlock_from_thisBlockArea(lastChildBlock);
}
ifForTryList.forEach(block => {
blockList_thisBlockArea.push(block);
});
return blockList_thisBlockArea;
/** if for try가 아닐 경우 */
} else {
blockList_thisBlockArea = this._getThisToLastBlockList(blockList_thisBlockArea, stack);
this.setLastBlock_from_thisBlockArea(lastChildBlock);
return blockList_thisBlockArea;
}
}
Block.prototype.getBlockList_thisBlockArea_noShadowBlock = function() {
var childBlockList = this.getBlockList_thisBlockArea();
childBlockList = childBlockList.filter(childBlock => {
if (childBlock.getBlockType() == BLOCK_CODELINE_TYPE.SHADOW) {
return false;
} else {
return true;
}
});
return childBlockList;
}
/** 생성할 블럭이 6뎁스를 초과 할 경우 alert창을 띄워 막음 */
Block.prototype.alertExceedDepth = function() {
vpCommon.renderAlertModal(STR_MSG_BLOCK_DEPTH_MUSH_NOT_EXCEED_6);
var blockContainerThis = this.getBlockContainerThis();
blockContainerThis.resetOptionPage();
}
/** param으로 받아온 block을 this블럭의 자식으로 append.
* 블럭을 생성할 때, 혹은 이동할 때 사용되는 메소드
* @param {BLOCK} appendedBlock 자식으로 append할 블럭
* @param {BLOCK_DIRECTION} direction 자식으로 append할 블럭의 위치 설정
*/
Block.prototype.appendBlock = function(appendedBlock, direction) {
var blockContainerThis = this.getBlockContainerThis();
var thisBlock = this;
/**
* depth가 6초과할 경우 alert
*/
var depth = thisBlock.calculateDepthAndGet();
if ( (depth >= NUM_EXCEED_DEPTH && direction == BLOCK_DIRECTION.INDENT)
|| depth > NUM_EXCEED_DEPTH) {
appendedBlock.alertExceedDepth();
return;
}
/** append할 block의 prev 블럭이 this 블럭일 때
* 이동한 block을 내 위치에 다시 놓을 경우
* */
var prevBlock = appendedBlock.getPrevBlock();
if ( prevBlock ) {
if (prevBlock.getUUID() == thisBlock.getUUID()) {
return;
}
}
appendedBlock.reConnectPrevBlock();
/** appendedBlock이 존재하는 영역의 가장 아래 블럭을 가져옴 */
var lastChildBlock = appendedBlock.getLastBlock_from_thisBlockArea();
// 새로 들어온 block의 이전 블럭을 현재 this블럭으로 정함
appendedBlock.setPrevBlock(thisBlock);
// 2번째 인자인 direction이 down 일 경우
if (direction == BLOCK_DIRECTION.DOWN) {
// console.log('BLOCK_DIRECTION.DOWN', appendedBlock.getDirection());
var blockType = appendedBlock.getBlockType();
if (IsElifElseExceptFinallyBlockType(blockType) == false) {
thisBlock.reConnectLastChildBlock(prevBlock, lastChildBlock, appendedBlock.getDirection());
}
var lastBlock = blockContainerThis.getLastBottomBlock(appendedBlock);
var childBlockList = thisBlock.getChildBlockList();
lastBlock.setChildBlockList(childBlockList);
childBlockList.forEach(block => {
block.setPrevBlock(lastBlock);
});
thisBlock.setChildBlockList([appendedBlock]);
// 2번째 인자인 direction이 indent일 경우
} else {
// console.log('BLOCK_DIRECTION.INDENT', appendedBlock.getDirection());
thisBlock.reConnectLastChildBlock(prevBlock, lastChildBlock, appendedBlock.getDirection());
var childBlock_indent = thisBlock.getChildBlock_indent();
if (childBlock_indent) {
childBlock_indent.setDirection(BLOCK_DIRECTION.DOWN);
childBlock_indent.setPrevBlock(lastChildBlock);
lastChildBlock.addChildBlockList(childBlock_indent);
thisBlock.deleteChildBlock(childBlock_indent);
}
thisBlock.addChildBlockList(appendedBlock);
}
appendedBlock.setDirection(direction);
}
/**
* 하위 depth block들을 지운다
*/
Block.prototype.deleteBlock_childBlockList = function() {
var blockContainerThis = this.getBlockContainerThis();
/**
* 만약 root 블럭일 경우
*/
if ( this.getPrevBlock() == null) {
this.setDirection(BLOCK_DIRECTION.NONE);
this.removeRootBlock();
/**
* 만약 root이 아닌 일반 블럭일 경우
*/
} else {
/** 부모 블럭과 연결 끊음 */
this.reConnectPrevBlock();
var prevBlock = this.getPrevBlock();
var deletedBlockDirection = this.getDirection();
var lastChildBlock = this.getLastBlock_from_thisBlockArea();
/** this 블럭이 존재하는 영역의 가장 아래 블럭과의 연결을 끊음*/
this.reConnectLastChildBlock(prevBlock, lastChildBlock, deletedBlockDirection);
}
var blockList_thisBlockArea = this.getBlockList_thisBlockArea();
blockList_thisBlockArea.forEach(block => {
block.deleteBlockDomAndData();
});
/** 현재 블럭 리스트가 다 제거되면 (blockList.length == 0)
* 이전 블럭 리스트도 제거함
*/
if (blockContainerThis.getBlockList().length == 0) {
blockContainerThis.setPrevBlockList([]);
};
/** 다시 렌더링 */
blockContainerThis.reRenderAllBlock_asc();
}
/** this 블럭을 이동하거나 삭제할 때,
* this 블럭과 prev 블럭과의 관계를 끊는 메소드
*/
Block.prototype.reConnectPrevBlock = function() {
var prevBlock = this.getPrevBlock();
var block = this;
if ( prevBlock ) {
prevBlock.deleteChildBlock(block);
}
}
/** this 블럭을 이동하거나 삭제할 때,
* this 블럭과 this 블럭영역의 가장 마지막 블럭(lastChildBlock)의 자식 블럭(down)과의 연결을 끊고,
* this 블럭의 prev 블럭과 this 블럭영역의 가장 마지막 블럭(lastChildBlock)의 자식 블럭(down)을 연결 시킴
*/
Block.prototype.reConnectLastChildBlock = function(prevBlock, lastChildBlock, direction) {
/** 블럭을 이동 시킬 때와 삭제할 때 childBlock_down가 존재
* 블럭을 새로 만들 때는 childBlock_down가 존재하지 않는다 x
*/
if (lastChildBlock) {
var childBlock_down = lastChildBlock.getChildBlock_down();
if (childBlock_down) {
childBlock_down.setDirection(direction);
childBlock_down.setPrevBlock(prevBlock);
prevBlock.addChildBlockList(childBlock_down);
lastChildBlock.deleteChildBlock(childBlock_down);
}
}
}
/**
* blockContainer의 blockList에서 block 삭제
*/
Block.prototype.deleteBlockDomAndData = function() {
/** board에 container dom에서
* this 블럭의 dom을 삭제 제거 */
const blockMainDom = this.getBlockMainDom();
$(blockMainDom).remove();
$(blockMainDom).empty();
/** blockContainer에서 block 데이터 삭제 제거 */
const blockContainerThis = this.getBlockContainerThis();
const blockUUID = this.getUUID();
blockContainerThis.deleteBlock(blockUUID);
blockContainerThis.deleteNodeBlock(blockUUID);
}
// ** --------------------------- Block dom 관련 메소드들 --------------------------- */
/** block dom을 가져옴 */
Block.prototype.getBlockMainDom = function() {
return this.blockMainDom;
}
/** block dom을 set */
Block.prototype.setBlockMainDom = function(blockMainDom) {
this.blockMainDom = blockMainDom;
}
/** Block Left Shadow dom을 가져옴 */
Block.prototype.getBlockLeftShadowDom = function() {
var blockMainDom = this.getBlockMainDom();
return $(blockMainDom).find(VP_CLASS_PREFIX + VP_CLASS_BLOCK_LEFT_HOLDER);
}
/** depth를 표시하는 dom을 가져옴 */
Block.prototype.getBlockDepthInfoDom = function() {
var blockMainDom = this.getBlockMainDom();
return $(blockMainDom).find(VP_CLASS_PREFIX + VP_CLASS_BLOCK_DEPTH_INFO);
}
/** LineNumber Info를 표시하는 dom을 가져옴 */
Block.prototype.getBlockLineNumberInfoDom = function() {
var blockMainDom = this.getBlockMainDom();
return $(blockMainDom).find(VP_CLASS_PREFIX + VP_CLASS_BLOCK_NUM_INFO);
}
/** Block Option Dom을 표시하는 dom */
Block.prototype.getBlockOptionPageDom = function() {
return this.blockOptionPageDom;
}
Block.prototype.setBlockOptionPageDom = function(blockOptionPageDom) {
this.blockOptionPageDom = blockOptionPageDom;
}
/** 현재 root 블럭부터 하위 depth 자식 블럭리스트(동일 depth 블럭 제거) 들을 전부 가져오고,
* 가져온 block들의 정보를 가지고 html dom을 만들어 return 한다.
* 블럭을 이동할 때 보여지는 dom을 생성
*/
Block.prototype.makeMovedBlockDom = function() {
var blockContainerThis = this.getBlockContainerThis();
var childBlockDomList = [];
var rootDepth = 0;
var $_boardPage = blockContainerThis.getBoardPage_$();
var $_blockNewMainDom = null;
var blockList_thisBlockArea = this.getBlockList_thisBlockArea();
var firstBlock = blockList_thisBlockArea[0];
/** 첫번째 블럭 dom 생성*/
if (firstBlock) {
rootDepth = firstBlock.calculateDepthAndGet();
var blockMainDom = blockContainerThis.makeBlockDom(firstBlock, false);
$_blockNewMainDom = $(blockMainDom);
$_blockNewMainDom.css(STR_POSITION, STR_ABSOLUTE);
$_boardPage.append($_blockNewMainDom);
}
/** 두번째 이후 블럭 dom 생성
* 첫번째 블럭이 node 블럭이고
* toggle 된 상태면 두번째 이후 블럭 생성 안함
*/
if (firstBlock.getBlockType() != BLOCK_CODELINE_TYPE.NODE) {
blockList_thisBlockArea.forEach((block, index) => {