-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathDef.js
More file actions
141 lines (125 loc) · 5.28 KB
/
Def.js
File metadata and controls
141 lines (125 loc) · 5.28 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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : Def.js
* Author : Black Logic
* Note : Logic > def
* License : GNU GPLv3 with Visual Python special exception
* Date : 2021. 11. 18
* Change Date :
*/
//============================================================================
// [CLASS] Def
//============================================================================
define([
'vp_base/js/com/com_Const',
'vp_base/js/com/com_String',
'vp_base/js/com/com_util',
'vp_base/js/com/component/PopupComponent'
], function(com_Const, com_String, com_util, PopupComponent) {
/**
* Def
*/
class Def extends PopupComponent {
_init() {
super._init();
/** Write codes executed before rendering */
this.config.dataview = false;
this.config.saveOnly = true;
this.state = {
v1: '',
v2: [],
...this.state
}
}
_bindEvent() {
super._bindEvent();
/** Implement binding events */
let that = this;
$(this.wrapSelector('#vp_addParam')).on('click', function() {
that.state.v2.push({ param: '', value: ''});
$(that.wrapSelector('.v2 tbody')).append(that.templateForList(that.state.v2.length, '', ''));
});
// Delete param
$(document).on('click', this.wrapSelector('.v2-del'), function() {
let pos = $(this).closest('.v2-tr').index();
$(that.wrapSelector('.v2-tr:nth('+pos+')')).remove();
that.state.v2.splice(pos, 1);
// re-numbering
$(that.wrapSelector('.v2-tr')).each((idx, tag) => {
$(tag).find('th').text(idx + 1);
});
});
}
_unbindEvent() {
super._unbindEvent();
$(document).off('click', this.wrapSelector('.v2-del'));
}
saveState() {
let that = this;
let v2 = [];
$(this.wrapSelector('.v2-tr')).each((idx, tag) => {
let v2_ele = {};
v2_ele['param'] = $(tag).find('.v2-param').val();
v2_ele['value'] = $(tag).find('.v2-value').val();
v2.push(v2_ele);
});
this.state.v2 = v2;
}
loadState() {
super.loadState();
let { v1, v2 } = this.state;
}
templateForBody() {
/** Implement generating template */
var page = new com_String();
page.appendLine('<div class="vp-orange-text vp-bold">Function Name</div>');
page.appendFormatLine('<input type="text" id="v1" class="vp-input wp100 vp-state" value="{0}" placeholder="{1}">'
, this.state.v1, 'Input code line');
page.appendLine('<table class="v2 wp100 vp-tbl-gap5" style="margin: 10px 0">');
page.appendLine('<thead><tr><td></td><td>Parameter</td><td></td><td>Default Value</td><td></td></tr></thead>');
page.appendLine('<tbody><colgroup><col width="20px"><col width="100px"><col width="30px"><col width="100px"><col width="*"></colgroup>');
let that = this;
this.state.v2.forEach((v, idx) => {
page.appendLine(that.templateForList(idx + 1, v.param, v.value));
});
page.appendLine('</tbody></table>');
page.appendFormatLine('<button class="vp-button w100" id="{0}">+ Parameter</button>', 'vp_addParam');
return page.toString();
}
templateForList(idx, param, value) {
if (!value) {
value = '';
}
var page = new com_String();
page.appendFormatLine('<tr class="{0}">', 'v2-tr');
page.appendFormatLine('<th>{0}</th>', idx);
page.appendFormatLine('<td><input type="text" class="vp-input w100 {0}" value="{1}" placeholder="{2}"/></td>'
, 'v2-param', param, 'Variable');
page.appendLine('<td class="w30 vp-center">=</td>');
page.appendFormatLine('<td><input type="text" class="vp-input w100 {0}" value="{1}" placeholder="{2}"/></td>'
, 'v2-value', value, 'Value');
// LAB: img to url
// page.appendFormatLine('<td class="{0} vp-cursor"><img src="{1}close_big.svg"/></td>', 'v2-del', com_Const.IMAGE_PATH);
page.appendFormatLine('<td class="{0} vp-cursor"><div class="vp-icon-close-big"></div></td>', 'v2-del');
page.appendLine('</tr>');
return page.toString();
}
generateCode() {
this.saveState();
let parameters = [];
this.state.v2.forEach(v => {
let param = v.param;
if (v.value != '') {
param += '=' + v.value;
}
if (param == '') {
return;
}
parameters.push(param);
});
return com_util.formatString('def {0}({1}):', this.state.v1, parameters.join(', '));
}
}
return Def;
});