-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathBlockMenu.js
More file actions
170 lines (153 loc) · 6.49 KB
/
BlockMenu.js
File metadata and controls
170 lines (153 loc) · 6.49 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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : BlockMenu.js
* Author : Black Logic
* Note : Render block menu
* License : GNU GPLv3 with Visual Python special exception
* Date : 2021. 12. 13
* Change Date :
*/
//============================================================================
// [CLASS] Block
//============================================================================
define([
'vp_base/js/com/com_util',
'vp_base/js/com/com_String',
'vp_base/js/com/component/Component',
'./CodeView'
], function (com_util, com_String, Component, CodeView) {
'use strict';
class BlockMenu extends Component {
constructor(boardFrame) {
super($('#vp_boardFrame'), {}, {boardFrame: boardFrame});
}
_init() {
this.boardFrame = this.prop.boardFrame;
this.block = undefined;
this.position = {
left: 0,
top: 0
};
this.prevBlockCodeview = null;
}
_bindEvent() {
var that = this;
/** edit block */
$(this.wrapSelector('#vp_block_menu_edit')).on('click', function () {
that.block.openPopup();
that.close();
});
/** run block */
$(this.wrapSelector('#vp_block_menu_run')).on('click', function () {
that.boardFrame.runBlock(that.block);
that.close();
});
/** add block */
$(this.wrapSelector('#vp_block_menu_add')).on('click', function () {
that.boardFrame.runBlock(that.block, true, false);
that.close();
});
/** duplicate block */
$(this.wrapSelector('#vp_block_menu_duplicate')).on('click', function () {
// duplicate block
that.boardFrame.copyBlock(that.block);
that.close();
});
/** delete block */
$(this.wrapSelector('#vp_block_menu_delete')).on('click', function () {
that.boardFrame.removeBlock(that.block);
that.close();
});
/** code view */
$(this.wrapSelector('#vp_block_menu_codeview')).on('click', function() {
let overallCode = new com_String();
let groupCode = that.boardFrame.runBlock(that.block, false, false);
if (that.block.id == 'apps_markdown') {
// if markdown, add #
groupCode = '#' + groupCode.replaceAll('\n', '\n# ');
}
overallCode.appendFormatLine('# Visual Python: {0} > {1}', that.block.name, that.block.name);
overallCode.append(groupCode);
// open codeview
let codeview = new CodeView({
codeview: overallCode.toString(),
config: {
id: 'blockCodeview',
name: 'Block Codeview',
path: ''
}
});
if (that.prevBlockCodeview != null) {
// remove prev code view
that.prevBlockCodeview.remove();
}
that.prevBlockCodeview = codeview;
codeview.open();
that.close();
});
}
template() {
var sbBlockMenu = new com_String();
sbBlockMenu.appendFormatLine('<div id="vp_block_menubox" style="0" class="vp-block-menu-box vp-close-on-blur">',
'display: none; position: fixed;');
// edit button
sbBlockMenu.appendLine('<div id="vp_block_menu_edit" class="vp-block-menu-item">Edit</div>');
sbBlockMenu.appendLine('<hr class="vp-extra-menu-line" id="vp_block_menu_line_1">');
// run button
sbBlockMenu.appendLine('<div id="vp_block_menu_run" class="vp-block-menu-item">Run</div>');
// add button
sbBlockMenu.appendLine('<div id="vp_block_menu_add" class="vp-block-menu-item">Code to cell</div>');
// duplicate button
sbBlockMenu.appendLine('<div id="vp_block_menu_duplicate" class="vp-block-menu-item">Duplicate</div>');
// delete button
sbBlockMenu.appendLine('<div id="vp_block_menu_delete" class="vp-block-menu-item">Delete</div>');
// codeview button
sbBlockMenu.appendLine('<hr class="vp-extra-menu-line" id="vp_block_menu_line_2">');
sbBlockMenu.appendLine('<div id="vp_block_menu_codeview" class="vp-block-menu-item">Code view</div>');
sbBlockMenu.appendLine('</div>');
return sbBlockMenu.toString();
}
open(block, left, top) {
this.block = block;
this.position = {
left: left,
top: top
};
// handling menu box to show inside visible area
let docWidth = $(document).width();
let docHeight = $(document).height();
let menuWidth = $(this.wrapSelector()).outerWidth();
let menuHeight = $(this.wrapSelector()).outerHeight();
if (docWidth < left + menuWidth) {
// horizontally out of view
this.position.left -= menuWidth;
}
if (docHeight < top + menuHeight) {
// vertically out of view
this.position.top -= menuHeight;
}
$(this.wrapSelector()).css(this.position);
// show items
$(this.wrapSelector('.vp-block-menu-item')).show();
$(this.wrapSelector('.vp-extra-menu-line')).show();
$(this.wrapSelector()).show();
// filter menu depends on block
let noContentBlocks = ['lgCtrl_try', 'lgCtrl_else', 'lgCtrl_finally'];
if (this.block.isSubBlock) {
// no duplicate
$(this.wrapSelector('#vp_block_menu_duplicate')).hide();
}
if (noContentBlocks.includes(this.block.id)) {
// no edit mode
$(this.wrapSelector('#vp_block_menu_edit')).hide();
$(this.wrapSelector('#vp_block_menu_line_1')).hide();
}
}
close() {
this.block = undefined;
$(this.wrapSelector()).hide();
}
}
return BlockMenu;
});