-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathMenuFrame.js
More file actions
347 lines (310 loc) · 13.1 KB
/
MenuFrame.js
File metadata and controls
347 lines (310 loc) · 13.1 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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : MenuFrame.js
* Author : Black Logic
* Note : Render and load menu frame
* License : GNU GPLv3 with Visual Python special exception
* Date : 2021. 09. 13
* Change Date :
*/
//============================================================================
// [CLASS] MenuFrame
//============================================================================
define([
__VP_TEXT_LOADER__('../../html/menuFrame.html'), // INTEGRATION: unified version of text loader
__VP_CSS_LOADER__('vp_base/css/menuFrame'), // INTEGRATION: unified version of css loader
'../com/com_Config',
'../com/com_Const',
'../com/com_util',
'../com/com_interface',
'../com/component/Component',
'../com/component/SuggestInput',
'../com/component/InnerFuncViewer',
'../com/component/PackageManager',
__VP_RAW_LOADER__('../../data/libraries.json'), // INTEGRATION: text! to raw-loader
'./MenuGroup',
'./MenuItem',
'./TaskBar'
], function(menuFrameHtml, menuFrameCss, com_Config, com_Const, com_util, com_interface, Component, SuggestInput, InnerFuncViewer, PackageManager,
librariesJson,
MenuGroup, MenuItem, TaskBar) {
'use strict';
//========================================================================
// Define Variable
//========================================================================
const {
VP_MIN_WIDTH,
MENU_MIN_WIDTH,
BOARD_MIN_WIDTH,
MENU_BOARD_SPACING
} = com_Config;
//========================================================================
// Declare class
//========================================================================
/**
* MenuFrame
*/
class MenuFrame extends Component {
//========================================================================
// Constructor
//========================================================================
constructor($target, state, prop={}) {
super($target, state, prop);
/**
* state.vp_menu_width : menu width (metadata)
* prop.parent : MainFrame
*/
}
//========================================================================
// Internal call function
//========================================================================
_init() {
// get json library list
this.menuLibrariesFlatten = []; // use it for searching
this.menuLibraries = this.getMenuLibraries();
}
/**
* Bind events on menuFrame
*/
_bindEvent() {
var that = this;
// Click extra menu button
$(this.wrapSelector('#vp_headerExtraMenuBtn')).on('click', function(evt) {
$('#vp_headerExtraMenu').toggle();
evt.stopPropagation();
});
// Click toggle board icon
$(this.wrapSelector('#vp_toggleBoard')).on('click', function() {
that.prop.parent.toggleNote();
});
// Click extra menu item
$(this.wrapSelector('#vp_headerExtraMenu li')).on('click', function() {
let menu = $(this).data('menu');
switch(menu) {
case 'check-version':
// check vp version
vpConfig.checkVpVersion();
break;
case 'view-inner-func':
let viewer = new InnerFuncViewer();
viewer.open();
break;
case 'restart':
// restart vp
vpConfig.readKernelFunction().then(function() {
// successfully restarted
com_util.renderSuccessMessage('Successfully loaded inner functions for Visual Python');
}).catch(function() {
com_util.renderAlertModal('No connected runtime is detected. Please connect to runtime...');
});
break;
case 'about':
case 'vpnote':
break;
}
});
// Click version updater
$(this.wrapSelector('#vp_versionUpdater')).on('click', function() {
vpConfig.checkVpVersion();
});
// Click package manager
$(this.wrapSelector('#vp_packageManager')).on('click', function() {
let packageManager = new PackageManager();
packageManager.open();
});
}
_unbindResizable() {
$('#vp_menuFrame').resizable('destroy');
}
/**
* Bind resizable(jquery.ui)
*/
_bindResizable() {
// resizable
$('#vp_menuFrame').resizable({
// containment: 'parent',
helper: 'vp-menuframe-resizer',
handles: 'e',
// resizeHeight: false,
minWidth: MENU_MIN_WIDTH,
// maxWidth: 0,
start: function(event, ui) {
},
resize: function(event, ui) {
var parentWidth = $('#vp_wrapper')[0].getBoundingClientRect().width;
var currentWidth = ui.size.width;
var newBoardWidth = 0;
var showBoard = $('#vp_boardFrame').is(':visible');
if (showBoard) {
newBoardWidth = parentWidth - currentWidth - MENU_BOARD_SPACING;
// check board minimum width
if (newBoardWidth < BOARD_MIN_WIDTH + MENU_BOARD_SPACING) {
newBoardWidth = BOARD_MIN_WIDTH;
currentWidth = parentWidth - (BOARD_MIN_WIDTH + MENU_BOARD_SPACING);
// change current width
ui.size.width = currentWidth;
}
}
// resize menu frame with current resized width
$('#vp_menuFrame').width(currentWidth);
// resize board frame with left space
$('#vp_boardFrame').width(newBoardWidth);
vpConfig.setMetadata({
vp_position: { width: parentWidth },
vp_menu_width: currentWidth,
vp_note_width: newBoardWidth
});
vpLog.display(VP_LOG_TYPE.DEVELOP, 'resizing menuFrame');
},
stop: function(event, ui) {
},
});
}
//========================================================================
// External call function
//========================================================================
/**
* Get menu object
* @returns library object
*/
getMenuLibraries() {
var libraries = {};
// LAB: webpack5 load json object by default
if (vpConfig.extensionType === 'lab' || vpConfig.extensionType === 'lite') {
libraries = librariesJson;
} else {
libraries = JSON.parse(librariesJson);
}
if (!libraries || !libraries.library) {
vpLog.display(VP_LOG_TYPE.ERROR, 'vp menus are not avilable!');
return {};
}
vpLog.display(VP_LOG_TYPE.LOG, 'vp menus version : ', libraries.library.version);
if (libraries && libraries.library) {
return libraries.library.item;
}
return {};
}
getMenuLibrary(menuId, libraries=this.menuLibrariesFlatten) {
for (var i=0; i < libraries.length; i++) {
var item = libraries[i];
if (item) {
if (item.id === menuId) {
return item;
}
// if (item.type === 'package') {
// var result = this.getMenuLibrary(menuId, item.item);
// if (result) {
// return result;
// }
// }
}
}
return null;
}
template() {
this.$pageDom = $(menuFrameHtml.replaceAll('${vp_base}', com_Const.BASE_PATH));
// LAB: vp_base replacing test
// const regexp = /"\$\{vp_base\}(.+?)"/g;
// var replaceHtml = menuFrameHtml;
// const rep_list = [...replaceHtml.matchAll(regexp)];
// for (var i=0; i<rep_list.length; i++) {
// try {
// var img_url = require(`..${rep_list[i][1]}`);
// // var img_url = '/lib/visualpython' + rep_list[i][1];
// replaceHtml = replaceHtml.replace(rep_list[i][0], `"${img_url}"`)
// } catch (ex) {
// ; // ignore error
// console.log(ex);
// }
// }
// this.$pageDom = $(replaceHtml);
// add hover title for logo
this.$pageDom.find('.vp-logo').prop('title', com_Const.TOOLBAR_BTN_INFO.HELP);
return this.$pageDom;
}
renderMenuItem(group) {
var that = this;
var body = group.getBody();
var item = group.getItem();
var state = group.getState();
item && item.forEach(child => {
// remember parent to navigate its parent menu
var category = state.category?(state.category + ' > ' + state.name):state.name;
child['category'] = category;
if (child.type == 'package') {
// packages : MenuGroup
var menuGroup = new MenuGroup($(body), child);
if (child.item) {
that.renderMenuItem(menuGroup);
}
} else {
// functions : MenuItem
that.menuLibrariesFlatten.push(child);
if (!child.hide) {
var menuItem = new MenuItem($(body), child);
}
}
});
}
renderTaskBar(taskList) {
this.taskBar = new TaskBar($(this.wrapSelector('#vp_menuFooter')), { taskList: taskList });
}
render() {
super.render(true);
var that = this;
this._bindResizable();
// set width using metadata
$(this.wrapSelector()).width(this.state.vp_menu_width);
// render menuItem
var menuLibraries = this.menuLibraries;
vpLog.display(VP_LOG_TYPE.DEVELOP, menuLibraries);
this.menuLibrariesFlatten = [];
menuLibraries && menuLibraries.forEach(item => {
if (item.type == 'package') {
// packages : MenuGroup
var menuGroup = new MenuGroup($('.vp-menugroup-list'), item);
if (item.item) {
that.renderMenuItem(menuGroup);
}
}
});
let functionList = this.menuLibrariesFlatten.map(menu => {
return { label: menu.name, value: menu.name, dtype: menu.category, ...menu }
});
// render searchbox
let searchBox = new SuggestInput();
searchBox.setComponentID('vp_menuSearchBox');
searchBox.addClass('vp-input vp-menu-search-box');
searchBox.setPlaceholder('Search libraries');
searchBox.setMinSearchLength(2);
searchBox.setSuggestList(function () { return functionList; });
searchBox.setSelectEvent(function (value) {
$(this.wrapSelector()).val(value);
$(this.wrapSelector()).trigger('change');
});
searchBox.setSelectEvent(function(value, item) {
$(this.wrapSelector()).val(value);
$('#vp_wrapper').trigger({
type:"create_option_page",
blockType: 'task',
menuId: item.id,
menuState: {},
afterAction: 'open'
});
$(this.wrapSelector()).trigger('change');
// clear search box
$(that.wrapSelector('#vp_menuSearchBox')).val('');
return false;
});
searchBox.setNormalFilter(true);
// replace searchbox
$(this.wrapSelector('#vp_menuSearchBox')).replaceWith(searchBox.toTagString());
// render taskBar
this.renderTaskBar([]);
}
}
return MenuFrame;
});
/* End of file */