-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathcom_Popup.js
More file actions
353 lines (308 loc) · 12.6 KB
/
com_Popup.js
File metadata and controls
353 lines (308 loc) · 12.6 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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : com_Popup.js
* Author : Black Logic
* Note : [CLASS] Popup page
* License : GPLv3 (GNU General Public License v3.0)
* Date : 2021. 08. 14
* Change Date :
*/
//============================================================================
// [CLASS] Popup page
//============================================================================
define([
'vp_base/js/com/com_Const',
'vp_base/js/com/com_util',
'codemirror/lib/codemirror'
], function(com_Const, com_util, codemirror) {
'use strict';
//========================================================================
// Define variable
//========================================================================
const VP_PP = 'vp-pp';
const VP_PP_CONTAINER = 'vp-pp-container';
const VP_PP_TITLE = 'vp-pp-title';
const VP_PP_CLOSE = 'vp-pp-close';
const VP_PP_BODY = 'vp-pp-body';
const VP_PP_PREVIEW_BOX = 'vp-pp-preview-box';
const VP_PP_BUTTON_BOX = 'vp-pp-btn-box';
const VP_PP_BUTTON_PREVIEW = 'vp-pp-btn-preview';
const VP_PP_BUTTON_CANCEL = 'vp-pp-btn-cancel';
const VP_PP_BUTTON_RUNADD = 'vp-pp-btn-runadd';
const VP_PP_BUTTON_RUN = 'vp-pp-btn-run';
const VP_PP_BUTTON_DETAIL = 'vp-pp-btn-detail';
const VP_PP_DETAIL_BOX = 'vp-pp-detail-box';
const VP_PP_DETAIL_ITEM = 'vp-pp-detail-item';
//========================================================================
// [CLASS] com_Popup
//========================================================================
class com_Popup {
/**
* constructor
* @param {object} pageThis
* @param {string} targetId
*/
constructor(pageThis, targetId) {
this.pageThis = pageThis;
this.targetId = targetId;
this.uuid = 'u' + com_util.getUUID();
this.config = {
title: '',
width: '95%',
height: '95%',
pageDom: $('<div>Empty</div>')
};
this.cmReadonlyConfig = {
mode: {
name: 'python',
version: 3,
singleLineStringErrors: false
},
height: '100%',
width: '100%',
indentUnit: 4,
matchBrackets: true,
readOnly: true,
autoRefresh: true,
theme: 'ipython',
extraKeys: { 'Enter': 'newlineAndIndentContinueMarkdownList' },
scrollbarStyle: 'null'
};
this.previewOpened = false;
}
/**
* Wrap selector
*/
wrapSelector(selector = '') {
return com_util.formatString('.{0} {1}', this.uuid, selector);
}
/**
* Initialize
*/
init() {
com_util.loadCss(com_Const.STYLE_PATH + 'common/popupPage.css');
this.renderPopup();
this.bindEvent();
}
/**
* Render popup page
*/
renderPopup() {
var { title, width, height, pageDom } = this.config;
var page = new MakeString();
page.appendFormatLine('<div class="{0} {1}">', VP_PP, this.uuid);
page.appendFormatLine('<div class="{0}" style="width: {1}; height: {2};">', VP_PP_CONTAINER, width, height);
// title
page.appendFormat('<div class="{0}">{1}</div>',
VP_PP_TITLE,
title);
// close button
page.appendFormatLine('<div class="{0}"><i class="{1}"></i></div>',
VP_PP_CLOSE, 'fa fa-close');
// body start
page.appendFormatLine('<div class="{0} {1}">', VP_PP_BODY, 'vp-apiblock-scrollbar');
page.appendLine('</div>'); // body end
// Snippets menu don't use buttons
if (title != 'Snippets') {
// preview box
page.appendFormatLine('<div class="{0} {1}">', VP_PP_PREVIEW_BOX, 'vp-apiblock-scrollbar');
page.appendFormatLine('<textarea id="{0}" name="code"></textarea>', 'vp_codePreview');
page.appendLine('</div>');
// button box
page.appendFormatLine('<div class="{0}">', VP_PP_BUTTON_BOX);
page.appendFormatLine('<button type="button" class="{0} {1} {2}">{3}</button>',
'vp-button', 'vp-pp-btn', VP_PP_BUTTON_PREVIEW, 'Code view');
page.appendFormatLine('<button type="button" class="{0} {1} {2}">{3}</button>',
'vp-button cancel', 'vp-pp-btn', VP_PP_BUTTON_CANCEL, 'Cancel');
page.appendFormatLine('<div class="{0}">', VP_PP_BUTTON_RUNADD);
page.appendFormatLine('<button type="button" class="{0} {1}" title="{2}">{3}</button>',
'vp-button activated', VP_PP_BUTTON_RUN, 'Apply to Board & Run Cell', 'Run');
page.appendFormatLine('<button type="button" class="{0} {1}"><i class="{2}"></i></button>',
'vp-button activated', VP_PP_BUTTON_DETAIL, 'fa fa-sort-up');
page.appendFormatLine('<div class="{0} {1}">', VP_PP_DETAIL_BOX, 'vp-cursor');
page.appendFormatLine('<div class="{0}" data-type="{1}" title="{2}">{3}</div>', VP_PP_DETAIL_ITEM, 'apply', 'Apply to Board', 'Apply');
page.appendFormatLine('<div class="{0}" data-type="{1}" title="{2}">{3}</div>', VP_PP_DETAIL_ITEM, 'add', 'Apply to Board & Add Cell', 'Add');
page.appendLine('</div>'); // VP_PP_DETAIL_BOX
page.appendLine('</div>'); // VP_PP_BUTTON_RUNADD
page.appendLine('</div>'); // VP_PP_BUTTON_BOX
}
page.appendLine('</div>'); // container end
page.appendLine('</div>'); // VP_PP end
$('#vp-wrapper').append(page.toString());
$(pageDom).appendTo($(this.wrapSelector('.' + VP_PP_BODY)));
$(this.wrapSelector()).hide();
return page.toString();
}
/**
* Open
*/
open(config) {
this.config = {
...this.config,
...config
};
this.init();
$(this.wrapSelector()).show();
if (!this.cmpreview) {
// codemirror setting
this.cmpreview = codemirror.fromTextArea($('#vp_codePreview')[0], this.cmReadonlyConfig);
} else {
this.cmpreview.refresh();
}
}
/**
* Close
*/
close() {
this.unbindEvent();
$(this.wrapSelector()).remove();
}
/**
* Apply
*/
apply(addCell = false, runCell = false) {
if (this.pageThis) {
var code = this.pageThis.generateCode(false, false);
$(com_util.wrapSelector('#' + this.targetId)).val(code);
$(com_util.wrapSelector('#' + this.targetId)).trigger({
type: 'popup_run',
title: this.config.title,
code: code,
addCell: addCell,
runCell: runCell
});
}
}
/**
* Open preview
*/
openPreview() {
if (this.pageThis) {
var code = this.pageThis.generateCode(false, false);
this.cmpreview.setValue(code);
this.cmpreview.save();
this.cmpreview.focus();
var that = this;
setTimeout(function () {
that.cmpreview.refresh();
}, 1);
this.previewOpened = true;
$(this.wrapSelector('.' + VP_PP_PREVIEW_BOX)).show();
}
}
/**
* Close preview
*/
closePreview() {
this.previewOpened = false;
$(this.wrapSelector('.' + VP_PP_PREVIEW_BOX)).hide();
}
/**
* Bind event
*/
bindEvent() {
var that = this;
// close popup
$(document).on('click', this.wrapSelector('.' + VP_PP_CLOSE), function (event) {
that.close();
});
// click preview
$(document).on('click', this.wrapSelector('.' + VP_PP_BUTTON_PREVIEW), function (evt) {
evt.stopPropagation();
if (that.previewOpened) {
that.closePreview();
} else {
that.openPreview();
}
});
// click cancel
$(document).on('click', this.wrapSelector('.' + VP_PP_BUTTON_CANCEL), function () {
that.close();
});
// click run
$(document).on('click', this.wrapSelector('.' + VP_PP_BUTTON_RUN), function () {
that.apply(true, true);
that.close();
});
// click detail button
$(document).on('click', this.wrapSelector('.' + VP_PP_BUTTON_DETAIL), function (evt) {
evt.stopPropagation();
$(that.wrapSelector('.' + VP_PP_DETAIL_BOX)).show();
});
// click add / apply
$(document).on('click', this.wrapSelector('.' + VP_PP_DETAIL_ITEM), function () {
var type = $(this).data('type');
if (type == 'add') {
that.apply(true);
that.close();
} else if (type == 'apply') {
that.apply();
that.close();
}
});
// click other
$(document).on('click.' + this.uuid, function (evt) {
if (!$(evt.target).hasClass('.' + VP_PP_BUTTON_DETAIL)) {
$(that.wrapSelector('.' + VP_PP_DETAIL_BOX)).hide();
}
if (!$(evt.target).hasClass('.' + VP_PP_BUTTON_PREVIEW)
&& $(that.wrapSelector('.' + VP_PP_PREVIEW_BOX)).has(evt.target).length === 0) {
that.closePreview();
}
});
this.keyboardManager = {
keyCode: {
ctrlKey: 17,
cmdKey: 91,
shiftKey: 16,
altKey: 18,
enter: 13,
escKey: 27
},
keyCheck: {
ctrlKey: false,
shiftKey: false
}
};
$(document).on('keydown.' + this.uuid, function (e) {
var keyCode = that.keyboardManager.keyCode;
if (e.keyCode == keyCode.ctrlKey || e.keyCode == keyCode.cmdKey) {
that.keyboardManager.keyCheck.ctrlKey = true;
}
if (e.keyCode == keyCode.shiftKey) {
that.keyboardManager.keyCheck.shiftKey = true;
}
}).on('keyup.' + this.uuid, function (e) {
var keyCode = that.keyboardManager.keyCode;
if (e.keyCode == keyCode.ctrlKey || e.keyCode == keyCode.cmdKey) {
that.keyboardManager.keyCheck.ctrlKey = false;
}
if (e.keyCode == keyCode.shiftKey) {
that.keyboardManager.keyCheck.shiftKey = false;
}
if (e.keyCode == keyCode.escKey) {
// close on esc
that.close();
}
});
}
/**
* Unbind event
*/
unbindEvent() {
$(document).unbind(com_util.formatString(".{0} .{1}", this.uuid, VP_PP_BODY));
$(document).off('click', this.wrapSelector('.' + VP_PP_CLOSE));
$(document).off('click', this.wrapSelector('.' + VP_PP_BUTTON_PREVIEW));
$(document).off('click', this.wrapSelector('.' + VP_PP_BUTTON_CANCEL));
$(document).off('click', this.wrapSelector('.' + VP_PP_BUTTON_RUN));
$(document).off('click', this.wrapSelector('.' + VP_PP_BUTTON_DETAIL));
$(document).off('click', this.wrapSelector('.' + VP_PP_DETAIL_ITEM));
$(document).off('click.' + this.uuid);
$(document).off('keydown.' + this.uuid);
$(document).off('keyup.' + this.uuid);
}
}
return com_Popup;
}); /* function, define */
/* End of file */