-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathloadVisualpython.js
More file actions
247 lines (222 loc) · 8.7 KB
/
loadVisualpython.js
File metadata and controls
247 lines (222 loc) · 8.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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : loadVisualpython.js
* Author : Black Logic
* Note : Load Visual Python
* License : GPLv3 (GNU General Public License v3.0)
* Date : 2021. 08. 14
* Change Date :
*/
//============================================================================
// Load Visual Python
//============================================================================
(
require.specified('base/js/namespace')
? define
: function (deps, callback) {
'use strict';
// if here, the Jupyter namespace hasn't been specified to be loaded.
// This means that we're probably embedded in a page,
// so we need to make our definition with a specific module name
return define('vp_base/js/loadVisualpython', deps, callback);
}
)([
'css!vp_base/css/root.css',
'vp_base/js/com/com_Const',
'vp_base/js/com/com_util',
'vp_base/js/com/com_Config',
'vp_base/js/com/com_Log',
'vp_base/js/com/com_Kernel',
'vp_base/js/com/com_interface',
'vp_base/js/MainFrame'
], function (rootCss, com_Const, com_util, com_Config, com_Log, com_Kernel, com_interface, MainFrame) {
'use strict';
//========================================================================
// Define variable
//========================================================================
var Jupyter;
var events;
var liveNotebook = false;
var metadataSettings;
var vpPosition;
var vpFrame;
//========================================================================
// Require: Jupyter & events
//========================================================================
try {
// namespace's specified checking. events exception can occur
// this will work in a live notebook because nbextensions & custom.js are loaded
// by/after notebook.js, which requires base/js/namespace
Jupyter = require('base/js/namespace');
events = require('base/js/events');
liveNotebook = true;
} catch (err) {
// We *are* theoretically in a non-live notebook
console.log('[vp] working in non-live notebook'); //, err);
// in non-live notebook, there's no event structure, so we make our own
if (window.events === undefined) {
var Events = function () { };
window.events = $([new Events()]);
}
events = window.events;
}
//========================================================================
// Internal call function
//========================================================================
/**
* Add toolbar button
*/
var _addToolBarVpButton = function () {
// Call notebookApp initialize event, if toolbar is not yet ready
if (!Jupyter.toolbar) {
events.on('app_initialized.NotebookApp', function (evt) {
_addToolBarVpButton();
});
return;
}
// Add toolbar button, if it's not existing
if ($('#' + com_Const.TOOLBAR_BTN_INFO.ID).length === 0) {
$(Jupyter.toolbar.add_buttons_group([
Jupyter.keyboard_manager.actions.register({
'help': com_Const.TOOLBAR_BTN_INFO.HELP
, 'icon': com_Const.TOOLBAR_BTN_INFO.ICON
, 'handler': function () {
// Extension 버튼 클릭 시 실행
// _toggleVp(cfg);
vpFrame.toggleVp();
}
}, com_Const.TOOLBAR_BTN_INFO.NAME, com_Const.TOOLBAR_BTN_INFO.PREFIX)
])).find('.btn').attr('id', com_Const.TOOLBAR_BTN_INFO.ID).addClass(com_Const.TOOLBAR_BTN_INFO.ICON_CONTAINER);
}
};
/**
* Create vp
* @param {Object} cfg configuration
* @param {*} st
*/
var _loadVpResource = function (cfg, st) {
if (!liveNotebook)
cfg = $.extend(true, {}, vpConfig.defaultConfig, cfg);
vpFrame = new MainFrame();
vpFrame.loadMainFrame();
// TODO: hotkey control -> Implement under InputComponent or Event class
// input:text - hotkey control
$(document).on('focus', com_util.wrapSelector('input'), function() {
com_interface.disableOtherShortcut();
});
$(document).on('blur', com_util.wrapSelector('input'), function() {
com_interface.enableOtherShortcut();
});
$(document).on('focus', '.vp-popup-frame input', function() {
com_interface.disableOtherShortcut();
});
$(document).on('blur', '.vp-popup-frame input', function() {
com_interface.enableOtherShortcut();
});
$(document).on('focus', '#vp_fileNavigation input', function() {
com_interface.disableOtherShortcut();
});
$(document).on('blur', '#vp_fileNavigation input', function() {
com_interface.enableOtherShortcut();
});
// textarea - hotkey control
$(document).on('focus', com_util.wrapSelector('.vp-popup-frame textarea'), function() {
com_interface.disableOtherShortcut();
});
$(document).on('blur', com_util.wrapSelector('.vp-popup-frame textarea'), function() {
com_interface.enableOtherShortcut();
});
};
/**
* Declare background vp functions
*/
var _readKernelFunction = function() {
var libraryList = [
'printCommand.py',
'fileNaviCommand.py',
'pandasCommand.py',
'variableCommand.py'
];
libraryList.forEach(libName => {
var libPath = com_Const.PYTHON_PATH + libName
$.get(libPath).done(function(data) {
var code_init = data;
Jupyter.notebook.kernel.execute(code_init, { iopub: { output: function(data) {
console.log('visualpython - loaded library', data);
} } }, { silent: false });
}).fail(function() {
console.log('visualpython - failed to load getPath library');
});
})
}
var _setGlobalVariables = function() {
/**
* visualpython log util
* - use it instead of console.log
* ex) vpLog.display(VP_LOG_TYPE.LOG, 'log text');
*/
window.vpLog = new com_Log();
/**
* visualpython log util types
* DEVELOP, LOG, ERROR
*/
window.VP_LOG_TYPE = com_Log.LOG_TYPE;
/**
* visualpython config util
*/
window.vpConfig = new com_Config();
window.VP_MODE_TYPE = com_Config.MODE_TYPE;
/**
* visualpython kernel
*/
window.vpKernel = new com_Kernel();
}
//========================================================================
// External call function
//========================================================================
/**
* Read our config from server config & notebook metadata
* This function should only be called when both:
* 1. the notebook (and its metadata) has fully loaded
* AND
* 2. Jupyter.notebook.config.loaded has resolved
*/
var readConfig = function () {
var cfg = vpConfig.defaultConfig;
if (!liveNotebook) {
return cfg;
}
// config may be specified at system level or at document level. first, update
// defaults with config loaded from server
let defaultMetadata = JSON.parse(JSON.stringify(vpConfig.metadataSettings));
let metadata = vpConfig.getMetadata();
vpConfig.resetMetadata();
if (metadata && defaultMetadata.vp_config_version == metadata.vp_config_version) {
Object.keys(defaultMetadata).forEach(key => {
let value = (metadata && metadata.hasOwnProperty(key) ? metadata : defaultMetadata)[key];
vpConfig.setMetadata({ [key]: value });
cfg[key] = value;
});
} else {
// if config version is different, overwrite config
vpConfig.setMetadata(defaultMetadata);
}
return cfg;
};
/**
* Initialize Visual Python
*/
var initVisualpython = function () {
_setGlobalVariables();
let cfg = readConfig();
_readKernelFunction();
_addToolBarVpButton();
_loadVpResource(cfg);
if (cfg.vp_section_display && vpFrame) {
vpFrame.openVp();
}
}
return { initVisualpython: initVisualpython, readConfig: readConfig };
}); /* function, define */
/* End of file */