-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathopen.js
More file actions
299 lines (253 loc) · 12.3 KB
/
open.js
File metadata and controls
299 lines (253 loc) · 12.3 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
define([
'require'
, 'jquery'
, 'nbextensions/visualpython/src/common/vpCommon'
, 'nbextensions/visualpython/src/common/constant'
, 'nbextensions/visualpython/src/common/StringBuilder'
, 'nbextensions/visualpython/src/common/vpFuncJS'
, 'nbextensions/visualpython/src/common/component/vpSuggestInputText'
, 'nbextensions/visualpython/src/pandas/common/pandasGenerator'
, 'nbextensions/visualpython/src/component/fileNavigation/index'
], function (requirejs, $, vpCommon, vpConst, sb, vpFuncJS, vpSuggestInputText, pdGen, fileNavigation) {
// 옵션 속성
const funcOptProp = {
funcName : "open()"
, funcID : "pyBuilt_open"
}
/**
* html load 콜백. 고유 id 생성하여 부과하며 js 객체 클래스 생성하여 컨테이너로 전달
* @param {function} callback 호출자(컨테이너) 의 콜백함수
* @param {JSON} meta 메타 데이터
*/
var optionLoadCallback = function(callback, meta) {
// document.getElementsByTagName("head")[0].appendChild(link);
// 컨테이너에서 전달된 callback 함수가 존재하면 실행.
if (typeof(callback) === 'function') {
var uuid = vpCommon.getUUID();
// 최대 10회 중복되지 않도록 체크
for (var idx = 0; idx < 10; idx++) {
// 이미 사용중인 uuid 인 경우 다시 생성
if ($(vpConst.VP_CONTAINER_ID).find("." + uuid).length > 0) {
uuid = vpCommon.getUUID();
}
}
$(vpCommon.wrapSelector(vpCommon.formatString("#{0}", vpConst.OPTION_GREEN_ROOM))).find(vpCommon.formatString(".{0}", vpConst.API_OPTION_PAGE)).addClass(uuid);
// 옵션 객체 생성
var osSample = new PythonCommon(uuid);
osSample.metadata = meta;
// 옵션 속성 할당.
osSample.setOptionProp(funcOptProp);
// html 설정.
osSample.initHtml();
// TODO: meta load 처리 방안 검토.
// 방안 1. callback 에서 처리
// 방안 2. initHtml 내에서 meta 존재 시 init과 동시에 처리.
// 방안 3. initHtml 후에 옵션 내에서 load 함수 호출.
callback(osSample); // 객체를 callback 인자로 전달
}
}
/**
* html 로드.
* @param {function} callback 호출자(컨테이너) 의 콜백함수
* @param {JSON} meta 메타 데이터
*/
var initOption = function(callback, meta) {
vpCommon.loadHtml(vpCommon.wrapSelector(vpCommon.formatString("#{0}", vpConst.OPTION_GREEN_ROOM)), "python_common/index.html", optionLoadCallback, callback, meta);
}
/**
* 본 옵션 처리 위한 클래스
* @param {String} uuid 고유 id
*/
var PythonCommon = function(uuid) {
this.uuid = uuid; // Load html 영역의 uuid.
this.package = {
input: [
{ name: 'vp_pyFile' },
{ name: 'vp_pyReturn' },
{ name: 'vp_pyMode' },
{ name: 'vp_pyBuffering' },
{ name: 'vp_pyEncoding' },
{ name: 'vp_pyErrors' },
{ name: 'vp_userOption' }
]
}
// file navigation : state 데이터 목록
this.state = {
paramData:{
encoding: "utf-8" // 인코딩
, delimiter: "," // 구분자
},
returnVariable:"", // 반환값
isReturnVariable: false,
fileExtension: "" // 확장자 // FIXME: file navigation 모든 파일 선택 가능하게
};
this.fileResultState = {
pathInputId : this.wrapSelector('#vp_pyFile'),
fileInputId : this.wrapSelector('#fileName')
};
}
/**
* vpFuncJS 에서 상속
*/
PythonCommon.prototype = Object.create(vpFuncJS.VpFuncJS.prototype);
/**
* 유효성 검사
* @returns 유효성 검사 결과. 적합시 true
*/
PythonCommon.prototype.optionValidation = function() {
var required = [ 'vp_pyFile' ];
for (var i=0; i < required.length; i++) {
var tag = $(this.wrapSelector('#' + required[i]));
var value = $(tag).val();
if (value == '') {
var label = $(tag).closest('tr').find('th').text().trim();
vpCommon.renderAlertModal("'" + label + "' is required.");
return false;
}
}
return true;
}
/**
* html 내부 binding 처리
*/
PythonCommon.prototype.initHtml = function() {
this.loadCss(Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.STYLE_PATH + "python_common/index.css");
var sbPageContent = new sb.StringBuilder();
var sbTagString = new sb.StringBuilder();
// 필수 옵션 테이블 레이아웃
var tblLayoutRequire = this.createVERSimpleLayout("25%");
sbTagString.clear();
sbTagString.appendFormat('<input type="text" id="{0}" class="{1}" />', 'vp_pyFile', 'vp-input');
sbTagString.appendFormatLine('<div id="{0}" class="{1}"></div>', 'vp_openFileNavigationBtn', 'vp-file-browser-button');
tblLayoutRequire.addReqRow("Select File", sbTagString.toString());
sbTagString.clear();
sbTagString.appendFormat('<input type="text" id="{0}" class="{1}" />', 'vp_pyReturn', 'vp-input');
tblLayoutRequire.addRow("Return to", sbTagString.toString());
sbTagString.clear();
sbTagString.appendFormat('<select id="{0}" class="{1}">', 'vp_pyModeSelector', 'vp-select');
sbTagString.appendFormatLine('<option value="{0}">{1}</option>', '', 'Default');
sbTagString.appendFormatLine('<option value="{0}">{1}</option>', 'custom', 'Custom');
sbTagString.appendFormatLine('<option value="{0}">{1}</option>', 'r', 'read');
sbTagString.appendFormatLine('<option value="{0}">{1}</option>', 'w', 'write');
sbTagString.appendFormatLine('<option value="{0}">{1}</option>', 'x', 'create');
sbTagString.appendFormatLine('<option value="{0}">{1}</option>', 'a', 'append');
sbTagString.appendFormatLine('<option value="{0}">{1}</option>', 'b', 'binary mode');
sbTagString.appendFormatLine('<option value="{0}">{1}</option>', 't', 'text mode');
sbTagString.appendFormatLine('<option value="{0}">{1}</option>', '+', 'read & write');
sbTagString.appendLine('</select>');
sbTagString.appendFormat('<input type="text" id="{0}" class="{1}" />', 'vp_pyMode', 'vp-input');
tblLayoutRequire.addRow("Mode", sbTagString.toString());
// 필수 옵션 영역 (아코디언 박스)
var accBoxRequire = this.createOptionContainer(vpConst.API_REQUIRE_OPTION_BOX_CAPTION);
accBoxRequire.setOpenBox(true);
accBoxRequire.appendContent(tblLayoutRequire.toTagString());
sbPageContent.appendLine(accBoxRequire.toTagString());
// 추가 옵션 테이블 레이아웃
var tblLayoutAdditional = this.createVERSimpleLayout("25%");
sbTagString.clear();
sbTagString.appendFormat('<input type="number" id="{0}" class="{1}" />', 'vp_pyBuffering', 'vp-input');
tblLayoutAdditional.addRow("Buffering", sbTagString.toString());
sbTagString.clear();
sbTagString.appendFormat('<input type="text" id="{0}" class="{1}" />', 'vp_pyEncoding', 'vp-input');
tblLayoutAdditional.addRow("Encoding", sbTagString.toString());
sbTagString.clear();
// sbTagString.appendFormat('<input type="text" id="{0}" class="{1}" />', 'vp_pyErrors', 'vp-input');
var suggestInput = new vpSuggestInputText.vpSuggestInputText();
suggestInput.setComponentID("vp_pyErrors");
suggestInput.addClass('vp-input');
suggestInput.setSuggestList(function() { return [
{ label: 'strict', value: "'strict'" },
{ label: 'ignore', value: "'ignore'" },
{ label: 'replace', value: "'replace'" },
{ label: 'surrogateescape', value: "'surrogateescape'" },
{ label: 'xmlcharrefreplace', value: "'xmlcharrefreplace'" },
{ label: 'backslashreplace', value: "'backslashreplace'" },
{ label: 'namereplace', value: "'namereplace'" }
]; });
suggestInput.setNormalFilter(false);
// tblLayoutRequire.addReqRow("Errors", sbTagString.toString());
tblLayoutAdditional.addRow("Errors", suggestInput.toTagString());
sbTagString.clear();
sbTagString.appendFormatLine('<input id="{0}" type="text" class="vp-input" placeholder="{1}"/>'
, 'vp_userOption', 'key=value, key=value ...');
tblLayoutAdditional.addRow('User Option', sbTagString.toString());
// // 추가 옵션 영역
var accBoxAdditional = this.createOptionContainer(vpConst.API_ADDITIONAL_OPTION_BOX_CAPTION);
accBoxAdditional.appendContent(tblLayoutAdditional.toTagString());
sbPageContent.appendLine(accBoxAdditional.toTagString());
this.setPage(sbPageContent.toString());
sbPageContent.clear();
var that = this;
// E1. mode selector
$(this.wrapSelector('#vp_pyMode')).hide();
$(this.wrapSelector('#vp_pyModeSelector')).change(function() {
var value = $(this).val();
if (value == 'custom') {
$(that.wrapSelector('#vp_pyMode')).show();
$(that.wrapSelector('#vp_pyMode')).val('');
} else {
$(that.wrapSelector('#vp_pyMode')).hide();
$(that.wrapSelector('#vp_pyMode')).val(value);
}
});
// E2. 파일 네비게이션 오픈
$(this.wrapSelector('#vp_openFileNavigationBtn')).click(async function() {
var loadURLstyle = Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.STYLE_PATH;
var loadURLhtml = Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.SOURCE_PATH + "component/fileNavigation/index.html";
that.loadCss( loadURLstyle + "component/fileNavigation.css");
await $(`<div id="vp_fileNavigation"></div>`)
.load(loadURLhtml, () => {
$('#vp_fileNavigation').removeClass("hide");
$('#vp_fileNavigation').addClass("show");
var { vp_init
, vp_bindEventFunctions } = fileNavigation;
fileNavigation.vp_init(that, "SAVE_FILE");
// fileNavigation.vp_init(that.getStateAll());
fileNavigation.vp_bindEventFunctions();
})
.appendTo("#site");
});
}
/**
* 코드 생성
* @param {boolean} addCell 셀에 추가
* @param {boolean} exec 실행여부
* @returns 생성된 코드
*/
PythonCommon.prototype.generateCode = function(addCell = false, exec = false) {
var code = new sb.StringBuilder();
var returnVar = $(this.wrapSelector('#vp_pyReturn')).val();
if (returnVar != '') {
code.appendFormat("{0} = ", returnVar);
}
code.appendFormat("open('{0}'", $(this.wrapSelector('#vp_pyFile')).val());
var mode = $(this.wrapSelector('#vp_pyMode')).val();
if (mode != '') {
code.appendFormat(", mode='{0}'", mode);
}
var buffering = $(this.wrapSelector('#vp_pyBuffering')).val();
if (buffering != '') {
code.appendFormat(", buffering={0}", buffering);
}
var encoding = $(this.wrapSelector('#vp_pyEncoding')).val();
if (encoding != '') {
code.appendFormat(", encoding='{0}'", encoding);
}
var errors = $(this.wrapSelector('#vp_pyErrors')).val();
if (errors != '') {
code.appendFormat(", errors={0}", errors);
}
var userOption = $(this.wrapSelector('#vp_userOption')).val();
if (userOption != '') {
code.appendFormat(", {0}", userOption);
}
code.append(')');
if (addCell) {
this.cellExecute(code.toString(), exec);
}
return code.toString();
}
return {
initOption: initOption
};
});