-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilelist.js
326 lines (297 loc) · 8.48 KB
/
filelist.js
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
/*
* Usage:
* Create element in html:
* <div id="fileList1"></div>
* Create FileList object in javascript:
* createFileList("fileList1")
* Get FileList object by id:
* getFileList("fileList1")
*
* File object:
* {
* name: "someFile",
* isDir: false,
* date: "Tue, 28 Jan 2020 02:00:00 GMT",
* parsedDate: 123456,
* size: "86K"
* }
* {
* name: "someFolder/",
* isDir: true,
* date: "Tue, 28 Jan 2020 02:00:00 GMT",
* parsedDate: 123456,
* size: "-"
* }
*
* FileList object:
* {
* showFiles: true,
* showDirs: true,
* sortKey: "parsedDate",
* sortDesc: true,
* allowCustomSorting: true,
* newPage: false,
* inPlaceUpdate: false,
* files: [File objects],
* getId: function(),
* getDirPath: function(),
* setShownCb: function(cb),
* loadAndShow: function(path),
* }
*/
var _fileListIdToObj = {};
/*
* @id: Element id to bind (typically a <div>), the DOM Node with id @id
* must exist.
* @return: FileList object.
*
*/
function createFileList(id) {
var jqObj = $("#" + id);
var fileList = {
showFiles: true,
showDirs: true,
sortKey: "parsedDate",
sortDesc: true,
// Whether a user can change the sorting by clicking the column header.
allowCustomSorting: true,
// Open a new page when clicking a file.
newPage: false,
// Don't enter a new page when clicking a folder.
inPlaceUpdate: false,
files: [],
_id: id,
_dirPath: "",
_jqObj: jqObj,
_error: null, // html string
_shownCb: function(fileList) {},
// @return: string.
getId: function() {
return this._id;
},
// @return: string.
getDirPath: function() {
return this._dirPath;
},
// @cb: function(fileList).
setShownCb: function(cb) {
this._shownCb = cb;
},
/*
* Download, sort and show file list.
* @path: normalized path. If not specified, use this._dirPath.
*/
loadAndShow: function(path) {
if (path == null) {
path = this._dirPath;
}
var id = this._id;
var url = "./files" + escapePathForURI(path);
$.ajax({
url: url,
type: "GET",
cache: false,
// The type of data that you're expecting back from the server.
dataType: "json",
success: function(response) {
fileList._error = null;
fileList._load(response, path);
fileList._sort();
},
error: function(xhr, status, error) {
fileList._error = xhr.responseText;
},
complete: function() {
fileList._show();
}
});
},
_sort: function() {
this.files.sort(function(a, b) {
var msba = a.isDir ? 0 : 1;
var msbb = b.isDir ? 0 : 1;
if (fileList.sortDesc) {
return (msba < msbb) ||
(msba == msbb && a[fileList.sortKey] > b[fileList.sortKey]) ?
-1 : 1;
}
else {
return (msba < msbb) ||
(msba == msbb && a[fileList.sortKey] < b[fileList.sortKey]) ?
-1 : 1;
}
});
},
// Display the file list.
_show: function() {
this._jqObj.html(this._error == null ?
this._genTableHtml() :
this._error);
this._shownCb(this);
},
/*
* Convert and save an nginx file list to this.files.
* @list: the JSON returned by nginx autoindex.
* @dirPath: normalized dir path of the current file list.
*/
_load: function(list, dirPath) {
this._dirPath = dirPath;
this.files = [];
for (var i = 0; i < list.length; i++) {
var isDir = list[i].type == "directory";
if (isDir && !this.showDirs || !isDir && !this.showFiles) {
continue;
}
this.files.push({
name: list[i].name + (isDir ? "/" : ""),
isDir: isDir,
date: (new Date(list[i].mtime)).toLocaleString(),
parsedDate: Date.parse(list[i].mtime),
size: isDir ? "-" : humanFileSize(list[i].size)
});
}
},
/*
* Generate the link for a file/folder.
* @path: normalized path (folders must have a trailing slash).
* @return: html string, which should be placed inside <a> node.
*/
_genLinkHtml: function(path) {
var link = 'href=';
if (path.slice(-1) == "/") {
// folder
if (this.inPlaceUpdate) {
link += '"javascript:getFileList(\'' + this._id +'\')' +
'.loadAndShow(\'' + escapeHtml(path).replace(/'/g, "\\'") + '\')"';
} else {
link += '"./listdir.html?path=' + escapePathForURI(path) + '"';
}
} else {
// file
link += '".' + escapePathForURI(path) + '"';
if (this.newPage) {
link += ' target="_blank"';
}
}
return link;
},
// @return: html string of the file list table.
_genTableHtml: function() {
var pPath = getParentPath(this._dirPath);
var html =
'<table class="table">' +
' <thead>' +
' <tr>' +
' <th class="file-list-name" scope="col"' +
' style="white-space: nowrap;">' +
' Name' +
(this.allowCustomSorting ? ' ' + arrowDownUp : '') +
' </th>' +
' <th class="file-list-date d-none d-md-table-cell" scope="col"' +
' style="white-space: nowrap;">' +
' Date' +
(this.allowCustomSorting ? ' ' + arrowDownUp : '') +
' </th>' +
' <th scope="col">Size</th>' +
' <th>' +
' <button class="file-list-refresh close" type="button"' +
' aria-label="Refresh">' +
refreshIcon +
' </button>' +
' </th>' +
' </tr>' +
' </thead>' +
' <tbody>';
if (this.showDirs) {
html +=
'<tr>' +
' <td>' +
' <a ' + this._genLinkHtml(pPath) + '>' +
' <parent>/' +
' </a>' +
' </td>' +
' <td></td>' +
' <td></td>' +
' <td></td>' +
'</tr>';
}
for (var i = 0; i < this.files.length; i++) {
var file = this.files[i];
var name = file.name;
var path = this._dirPath + name;
html +=
'<tr>' +
' <td>' +
' <a ' + this._genLinkHtml(path) + '>' +
escapeHtml(name) +
' </a>' +
' </td>' +
' <td class="small d-none d-md-table-cell">' + file.date + '</td>' +
' <td class="small">' + file.size + '</td>' +
' <td>' +
' <button class="file-list-delete close" type="button"' +
' aria-label="Delete"' +
' data-path="' + escapeHtml(path) + '">' +
trashBin +
' </button>' +
' </td>' +
'</tr>';
}
html +=
' </tbody>' +
'</table>';
return html;
}
};
jqObj.on("click", ".file-list-name", function(event) {
if (fileList.allowCustomSorting) {
fileList.sortKey = "name";
fileList.sortDesc = !fileList.sortDesc;
fileList._sort();
fileList._show();
}
});
jqObj.on("click", ".file-list-date", function(event) {
if (fileList.allowCustomSorting) {
fileList.sortKey = "parsedDate";
fileList.sortDesc = !fileList.sortDesc;
fileList._sort();
fileList._show();
}
});
jqObj.on("click", ".file-list-refresh", function(event) {
fileList.loadAndShow();
});
jqObj.on("click", ".file-list-delete", function(event) {
var deleteButton = getElementOnEventPathByClass(event.originalEvent,
"file-list-delete");
if (deleteButton == null) {
return;
}
// Read "data-path" attribute.
var path = deleteButton.dataset.path;
$.ajax({
url: escapePathForURI(path),
type: "DELETE",
// The type of data that you're expecting back from the server.
dataType: "text",
success: function (response) {
},
error: function(xhr, status, error) {
},
complete: function(xhr, status){
fileList.loadAndShow();
}
});
});
_fileListIdToObj[id] = fileList;
return fileList;
}
/*
* Get FileList object by id.
* @id: string.
* @return: FileList object.
*/
function getFileList(id) {
return _fileListIdToObj[id];
}