-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathloadVisualpython.js
More file actions
269 lines (239 loc) · 9.65 KB
/
loadVisualpython.js
File metadata and controls
269 lines (239 loc) · 9.65 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
/*
* 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 () {
// on clicking extension button
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();
});
$(document).on('focus', '.vp-dataselector input', function() {
com_interface.disableOtherShortcut();
});
$(document).on('blur', '.vp-dataselector 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();
});
};
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();
}
var _checkVersion = function() {
// check version timestamp
let nowDate = new Date();
vpConfig.getData('version_timestamp', 'vpcfg').then(function(data) {
let doCheckVersion = false;
if (data == undefined) {
// no timestamp, check version
doCheckVersion = true;
} else if (data != '') {
let lastCheck = new Date(parseInt(data));
let diffCheck_now = new Date(nowDate.getFullYear(), nowDate.getMonth() + 1, nowDate.getDate());
let diffCheck_last = new Date(lastCheck.getFullYear(), lastCheck.getMonth() + 1, lastCheck.getDate());
let diff = Math.abs(diffCheck_now.getTime() - diffCheck_last.getTime());
diff = Math.ceil(diff / (1000 * 3600 * 24));
if (diff >= 1) {
// if More than 1 day passed, check version
doCheckVersion = true;
}
}
// check version and update version_timestamp
if (doCheckVersion == true) {
vpConfig.checkVpVersion(true);
}
}).catch(function(err) {
vpLog.display(VP_LOG_TYPE.ERROR, err);
})
}
//========================================================================
// 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();
vpConfig.readKernelFunction();
_addToolBarVpButton();
_loadVpResource(cfg);
_checkVersion();
if (cfg.vp_section_display && vpFrame) {
vpFrame.openVp();
}
// Operations on kernel restart
events.on('kernel_ready.Kernel', function (evt, info) {
vpLog.display(VP_LOG_TYPE.LOG, 'vp operations for kernel ready...');
// read vp functions
vpConfig.readKernelFunction();
});
}
return { initVisualpython: initVisualpython, readConfig: readConfig };
}); /* function, define */
/* End of file */