-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathrenderer.js
More file actions
438 lines (370 loc) · 19.2 KB
/
Copy pathrenderer.js
File metadata and controls
438 lines (370 loc) · 19.2 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
define([
'require'
, 'jquery'
, 'nbextensions/visualpython/src/common/vpCommon'
, 'nbextensions/visualpython/src/common/vpMakeDom'
, 'nbextensions/visualpython/src/common/StringBuilder'
//
, 'nbextensions/visualpython/src/pandas/fileNavigation/api'
, 'nbextensions/visualpython/src/pandas/fileNavigation/executor'
, 'nbextensions/visualpython/src/pandas/fileNavigation/constData'
], function ( requirejs, $, vpCommon, vpMakeDom, sb,
componentConstApi, executor, constData) {
const { NAVIGATION_DIRECTION_TYPE
, FILE_NAVIGATION_TYPE } = constData;
// kernel executor에 필요한 함수 import
const { executeKernelFromDirBody } = executor;
// dom 만드는데 필요한 함수 import
const { controlToggleInput } = componentConstApi;
const { renderSpan, renderLi } = vpMakeDom;
var FileNavigationRenderer = function(fileNavigationState, vpFuncJS, fileResultState) {
this.fileNavigationState = fileNavigationState;
this.vpFuncJS = vpFuncJS;
this.fileResultState = fileResultState;
}
FileNavigationRenderer.prototype.getFileNavigationState = function() {
return this.fileNavigationState;
}
// CSV 파일네비게이션 시작시 처음으로 렌더링하는 함수
FileNavigationRenderer.prototype.initRender = function() {
var fileNavigationRendererThis = this;
var dirObj = {
direction: NAVIGATION_DIRECTION_TYPE.INIT,
}
/** fileNavigation right tab에 현재 path의 폴더와 파일들 렌더링 */
executeKernelFromDirBody(dirObj, fileNavigationRendererThis);
/** 만약 타입이 'SAVE_FILE'일 경우' */
var fileNavigationState = fileNavigationRendererThis.getFileNavigationState();
var fileNavigationtype = fileNavigationState.getFileNavigationtype();
var fileOptionData = fileNavigationState.getFileOptionData();
/** Pandas > File IO > to_csv(), to_excel(), to_json(), to_pickle() 네비게이션 클릭시 */
if(fileNavigationtype == FILE_NAVIGATION_TYPE.SAVE_FILE){
var saveFileDataBtn = $(`<button class='fileNavigationPage-btn-saveVisualPython'
style='margin-left: 10px;'>Save</button>`);
var cancelFileDataBtn = $(`<button class='fileNavigationPage-btn-cabcelVisualPython'
style='margin-left: 10px;'>Cancel</button>`);
var saveFileDataInput = $(`<input id='vp-fileNavigation' type='text' placeholder='Input file name' />`);
var saveFileDataInput = $(vpCommon.formatString('<input id="{0}" type="text" placeholder="{1}" value="{2}" />'
, 'vp=fileNavigation', 'Input file name', fileNavigationState.getVisualPythonFileName()));
$('.fileNavigationPage-button').append(saveFileDataInput);
$('.fileNavigationPage-button').append(saveFileDataBtn);
$('.fileNavigationPage-button').append(cancelFileDataBtn);
controlToggleInput();
$(saveFileDataInput).on('change keyup paste', function() {
fileNavigationState.setVisualPythonFileName($(this).val());
});
/**
* 현재 디렉토리에 저장 버튼을 누르면 실행되는 이벤트 click 함수
*/
$(saveFileDataBtn).click(function(){
if(fileNavigationState.getVisualPythonFileName() === ``){
vpCommon.renderAlertModal('Please input file name');
return;
}
var dirPath = fileNavigationRendererThis.makeNewCurrRelativePath();
var fileName = `${fileNavigationState.getVisualPythonFileName()}`;
if (fileName.indexOf('.' + fileOptionData.fileExtension) === -1) {
fileName += `.${fileOptionData.fileExtension}`;
}
fileNavigationRendererThis.selectFile(dirPath, fileName);
fileNavigationState.resetStack();
});
$(cancelFileDataBtn).click(function(){
fileNavigationState.resetStack();
$('#vp_fileNavigation').remove();
// 파일 네비게이션에서 생성된 script 파일 삭제
vpCommon.removeHeadScript("fileNavigation");
});
}
}
/** */
FileNavigationRenderer.prototype.renderCurrentDirPathInfoRightBody = function(dirInfoArr) {
$('.fileNavigationPage-body').empty();
var currentDirRootDom = $('.fileNavigationPage-body');
var { folderArr, fileArr } = this.makeFolderOrFileDomArr(dirInfoArr);
var fileNavigationPageUl = $(`<ul class='fileNavigationPage-ul'>
</ul>`);
folderArr.forEach(dom => {
fileNavigationPageUl.append(dom);
});
fileArr.forEach(dom => {
fileNavigationPageUl.append(dom);
});
currentDirRootDom.append(fileNavigationPageUl);
}
/** <div/> 태그 형식의 폴더 디렉토리를 만드는 함수
* @param {JSON} node
*/
FileNavigationRenderer.prototype.makeFolderDom = function(node) {
var fileNavigationRendererThis = this;
var folderName = node.name;
var folderPath = node.path;
var directoryLi = renderLi({class: 'fileNavigationPage-li'});
var directorySpan = renderSpan({class:'fileNavigationPage-column'});
var directoryI = $(`<i class="fileNavigationPage-dir-text
item_icon
folder_icon
icon-fixed-width">
${folderName}
</i>`);
$(directoryI).click(function() {
var dirObj = {
direction: NAVIGATION_DIRECTION_TYPE.TO,
destDir: folderPath
}
// initialize sidebar-menu selection
$('.fnp-sidebar-menu').removeClass('selected');
executeKernelFromDirBody(dirObj, fileNavigationRendererThis);
});
$(directoryLi).append($(directorySpan).append(directoryI));
return directoryLi;
}
/** <div/> 태그 형식의 파일 디렉토리를 만드는 함수
* @param {JSON} node
*/
FileNavigationRenderer.prototype.makeFileDom = function(node) {
var that = this;
var fileNavigationState = this.getFileNavigationState();
var fileName = node.name;
var directoryLi = renderLi({class: 'fileNavigationPage-li'});
var directoryI = $(`<i class="fileNavigationPage-dir-text
item_icon
file_icon
icon-fixed-width">
${fileName}
</i>`);
directoryI.click(function() {
var dirPath = that.makeNewCurrRelativePath();
var dataOption = fileNavigationState.getFileOptionData();
var extension = fileName.substring(fileName.lastIndexOf('.') + 1);
if (Array.isArray(dataOption.fileExtension)) {
// 확장자 여러개 입력되었을 경우
// 확장자 목록에 포함되어있지 않을 경우
if (!dataOption.fileExtension.includes(extension)) {
vpCommon.renderAlertModal('Not supported file type');
return;
}
} else {
// Deprecated: 여러 항목 명시하지 않는 경우, 확장자 체크 제외
// 확장자가 맞지 않는 경우
// if (extension != dataOption.fileExtension) {
// vpCommon.renderAlertModal('Not ' + dataOption.fileExtension + ' file');
// return;
// }
}
that.selectFile(dirPath, fileName);
});
directoryLi.append(directoryI);
return directoryLi;
}
FileNavigationRenderer.prototype.makeFolderOrFileDomArr = function(dirInfoArr) {
var fileNavigationRendererThis = this;
var folderArr = [];
var fileArr = [];
var nodeInfo = { name:'',
type:'' };
// 디렉토리 정보가 담긴 자바스크립트 배열 dirInfoArr을 index 1부터 1씩 늘려가면서
// 폴더인지 파일인지 확인후 html 태그를 만든다.
// index 0의 데이터는 현재 디렉토리가 담긴 별도의 특수 데이터라 건너 뛴다.
var index = 1;
while(index < dirInfoArr.length) {
nodeInfo = dirInfoArr[index];
var tempDom = null;
// 디렉토리 정보가 폴더 일 경우
if (nodeInfo.type === 'dir') {
tempDom = fileNavigationRendererThis.makeFolderDom(nodeInfo);
folderArr.push(tempDom);
} else {
// 디렉토리 정보가 파일 일 경우
tempDom = fileNavigationRendererThis.makeFileDom(nodeInfo);
fileArr.push(tempDom);
}
index++;
}
return {
folderArr, fileArr
}
}
FileNavigationRenderer.prototype.makeNewCurrRelativePath = function() {
var fileNavigationState = this.getFileNavigationState();
var dirPath = fileNavigationState.getRelativeDir();
/**
* 만약 현재 전체 path가 C:/Users/L.E.E/Desktop/Bit Python이라면,
* baseFolder 변수에 Bit Python가 저장됨
* 즉 파일 네비게이션을 연 시점의 폴더가 baseFolder
*/
var baseFolder = fileNavigationState.getBaseFolder();
/**
* 만약 현재 전체 path가 C:/Users/L.E.E/Desktop/Bit Python이라면,
* Jupyter.notebook.notebook_path 는 'Desktop/Bit Python/Untitled4.ipynb'
* noteBookFolder 변수에 Desktop가 저장됨
* 즉 Jupyter.notebook.notebook_path의 폴더가 noteBookFolder
*/
var noteBookFolder = fileNavigationState.getNotebookFolder();
/** 파일 네비게이션을 실행한 baseFolder 이름이 현재 상대 경로에 있을때 */
if (dirPath.indexOf(baseFolder) !== -1) {
var baseFolderIndex = dirPath.indexOf(baseFolder);
dirPath = dirPath.substring(baseFolderIndex,dirPath.length);
dirPath = dirPath.replace(baseFolder, '');
}
/** 만약 baseFolder 이름과 noteBookFolder 이름이 동일하면
* dirPath 안에 존재하는 noteBookFolder이름을 replace 하지 않음
* */
if (baseFolder !== noteBookFolder) {
dirPath = dirPath.replace(noteBookFolder, '');
}
if (dirPath[0] === '/') {
dirPath = dirPath.substring(1, dirPath.length);
}
return dirPath;
}
/**
* 파일 네비게이션 위에 / Desktop / Bit Python 이런식으로 현재 디렉토리의 정보를 보여주는 메소드
*/
FileNavigationRenderer.prototype.renderNowLocation = function(currentDirStr, currentRelativePathStr) {
/** 이 메소드에서 사용할 state 데이터 가져오기 */
var fileNavigationRendererThis = this;
var fileNavigationState = this.getFileNavigationState();
fileNavigationState.setRelativeDir(currentRelativePathStr);
fileNavigationState.setCurrentDir(currentDirStr);
var notebookPathStr = fileNavigationState.getNotebookDir();
var notebookPathStrLength = notebookPathStr.length;
var baseFolder = fileNavigationState.getBaseFolder();
var notebookFolder = fileNavigationState.getNotebookFolder();
/** 파일 네비게이션에 현재 path를 렌더링해서 보여주는 logic */
var currentRelativePathStrArray = currentRelativePathStr.split('/');
if (baseFolder === notebookFolder && Jupyter.notebook.notebook_path.indexOf('/') === -1) {
var index = currentRelativePathStrArray.indexOf(baseFolder)
currentRelativePathStrArray.splice(index,1);
}
var currentRelativePathDomElement = $(`<div><div>`);
var basePathDomElement = $(`<span class='vp-filenavigation-nowLocation' value='/'> / </span>`);
currentRelativePathDomElement.append(basePathDomElement);
basePathDomElement.click(function() {
var dirObj = {
direction: NAVIGATION_DIRECTION_TYPE.TO,
destDir: notebookPathStr
}
// initialize sidebar-menu selection
$('.fnp-sidebar-menu').removeClass('selected');
// fileNavigation body에 현재 path의 폴더와 파일들 렌더링
executeKernelFromDirBody(dirObj, fileNavigationRendererThis);
});
var nestedLength = 0;
currentRelativePathStrArray.forEach((pathToken,index) => {
if (index === 0) {
slashStr = '';
} else {
slashStr = '/';
}
var spanElement = $(`<span class='vp-filenavigation-nowLocation' value='${pathToken}'>
${slashStr} ${pathToken}
</span>`);
var pathLength = notebookPathStrLength + pathToken.length + 1 + nestedLength;
spanElement.click(function() {
var currentRelativePathStr = `${currentDirStr.substring(0, pathLength)}`;
var dirObj = {
direction: NAVIGATION_DIRECTION_TYPE.TO,
destDir: currentRelativePathStr
}
// initialize sidebar-menu selection
$('.fnp-sidebar-menu').removeClass('selected');
// fileNavigation body에 현재 path의 폴더와 파일들 렌더링
executeKernelFromDirBody(dirObj, fileNavigationRendererThis);
});
currentRelativePathDomElement.append(spanElement);
nestedLength += pathToken.length + 1;
});
$('.fileNavigationPage-directory-nowLocation').empty();
$('.fileNavigationPage-directory-nowLocation').append(currentRelativePathDomElement);
}
FileNavigationRenderer.prototype.selectFile = function(relativeDirPath, filePathStr) {
var fileNavigationRendererThis = this;
var fileNavigationState = fileNavigationRendererThis.getFileNavigationState();
/** 상대 경로 string을 자르고 붙이는 로직 */
var baseFolder = fileNavigationState.getBaseFolder();
var baseDirStr = fileNavigationState.getBaseDir();
var noteBookPathStr = fileNavigationState.getNotebookDir();
var currentDirStr = fileNavigationState.getCurrentDir();
var noteBookPathStrLength = noteBookPathStr.length;
var baseDirStrLength = baseDirStr.length;
/** upDirectoryCount는 파일 네비게이션을 연 시점의 path 경로 에서
* 얼마만큼 상위 path로 올라갔는지 횟수를 count하는 변수
*/
var upDirectoryCount = 0;
var _upDirectoryCount = 0;
var splitedNoteBookPathStrArray = noteBookPathStr.split('/');
var splitedBaseDirArray = baseDirStr.split('/');
var splitedCurrentDirArray = currentDirStr.split('/');
var relativeBaseDirArray = splitedBaseDirArray.slice(splitedNoteBookPathStrArray.length, splitedBaseDirArray.length);
var relativeCurrentDirArray = splitedCurrentDirArray.slice(splitedNoteBookPathStrArray.length, splitedCurrentDirArray.length);
/** 최초 파일 네비게이션을 연 시점의 path 경로와 Jupyter notebook의 path 경로를 비교하여
* 최초 파일 네비게이션을 연 시점의 path에서 얼마만큼 상위로 올라가야 Jupyter notebook의 path 경로에 도달할 수 있는지 count
*/
var _baseDirStrLength = baseDirStrLength;
while ( noteBookPathStrLength < _baseDirStrLength ) {
_baseDirStrLength--;
if ( baseDirStr[_baseDirStrLength] === '/') {
upDirectoryCount += 1;
}
}
/** Jupyter notebook의 path 경로와 현재 이동한 path 경로를 비교하여
* Jupyter notebook의 path 경로에서 얼마만큼 하위로 내려와야 현재 이동한 path 경로에 도달할 수 있는지 count
*/
relativeCurrentDirArray.forEach((forderName,index) => {
if ( forderName === relativeBaseDirArray[index] ) {
upDirectoryCount -= 1;
}
});
/**upDirectoryCount의 숫자 만큼 '../'을 추가
* upDirectoryCount의 숫자 만큼 다시말하면 '../'가 추가 된 만큼, 현재 path 폴더에서 상위 폴더로 이동했다는 의미.
*/
_upDirectoryCount = upDirectoryCount;
var prefixUpDirectory = ``;
while (_upDirectoryCount-- > 0) {
prefixUpDirectory += `../`;
}
var slashstr = `/`;
if (relativeDirPath === '') {
slashstr = '';
}
/** 최초의 path가 C:/Users/L.E.E/Desktop/Bit Python이라면, baseFolder는 Bit Python
* 현재 이동한 시점의 path에서 baseFolder인 Bit Python가 존재하지 않을 때
*/
if (upDirectoryCount > 0
&& currentDirStr.indexOf(baseFolder) === -1) {
/** 2020년 12월 21일 single quote 삭제 */
$(fileNavigationRendererThis.fileResultState.pathInputId).val(`${prefixUpDirectory}${relativeDirPath}${slashstr}${filePathStr}`);
$(fileNavigationRendererThis.fileResultState.fileInputId).val(`${filePathStr}`);
/** 현재 이동한 시점의 path에서 baseFolder인 Bit Python가 존재할 때 */
} else {
$(fileNavigationRendererThis.fileResultState.pathInputId).val(`./${relativeDirPath}${slashstr}${filePathStr}`);
$(fileNavigationRendererThis.fileResultState.fileInputId).val(`${filePathStr}`);
}
// vpCommon.renderSuccessMessage(filePathStr + ' selection ' + 'completed');
/** 장안태 추가. 파일 선택 완료시 문서 이벤트 발동 */
if (fileNavigationState.getFileNavigationtype() === FILE_NAVIGATION_TYPE.SAVE_FILE) {
$(document).trigger({
type:"fileSaveSelected.fileNavigation",
path:`${filePathStr}`
});
} else {
$(document).trigger({
type:"fileReadSelected.fileNavigation",
path:`${filePathStr}`
});
}
/** 김민주 추가. Markdown 이미지 파일 선택 완료시 이벤트 발동 */
// imgFileOpenForMarkdown.fileNavigation
if (fileNavigationState.getFileNavigationtype() === FILE_NAVIGATION_TYPE.READ_IMG_FOR_MARKDOWN) {
$(document).trigger({
type: fileNavigationState.getTriggerName(),
path: `${filePathStr}`
})
}
$('#vp_fileNavigation').remove();
// 파일 네비게이션에서 생성된 script 파일 삭제
vpCommon.removeHeadScript('fileNavigation');
}
return FileNavigationRenderer;
});