-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfilesel.js
352 lines (316 loc) · 8.71 KB
/
filesel.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
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
var drives = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z'
];
var MSG_BORDER = 3;
var FONT_SPACING = 1;
var PREVIEW_WIDTH = 210;
/**
* create new FileSelector. Start directory is 'current dir'.
*/
function FileSelector() {
this.drives = [];
this.onClose = null;
this.dirFont = new Font();
this.msgBoxHeight = this.dirFont.height + 2 * MSG_BORDER;
this.selWidth = SizeX() - PREVIEW_WIDTH;
if (LINUX) {
this.drives.push('/');
} else {
// create list of possible drive letters
var currentDrive = GetDrive(); // get current drive
for (var i = 0; i < drives.length; i++) {
SetDrive(i + 1);
if (GetDrive() == i + 1) {
this.drives.push(drives[i]);
}
}
SetDrive(currentDrive); // return old state
}
this.currentDir = this.initDirInfo("./");
}
/**
* create a 'dir info' object containing all information about the current directory.
*
* @param {string} dir path to the directory
*
* @returns {*} 'dir info' object.
*/
FileSelector.prototype.initDirInfo = function (dir) {
Println("InitDir:" + dir);
// create directory object
var ret = {
path: dir,
top: 0,
cursor: this.drives.length,
rows: Math.floor((SizeY() - this.msgBoxHeight) / (this.dirFont.height + FONT_SPACING)),
list: [],
info: {}
};
// fill 'list' and 'info' properties
ret.list = List(ret.path);
ret.list.sort();
var self = ret.list.indexOf(".");
ret.list.splice(self, 1);
for (var l = 0; l < ret.list.length; l++) {
var name = ret.list[l];
ret.info[name] = Stat(ret.path + "/" + name);
}
for (var i = 0; i < this.drives.length; i++) {
ret.list.splice(i, 0, this.drives[i]);
ret.info[this.drives[i]] = { "is_drive": true };
}
return ret;
}
/**
* concatenate path with directory, handle ".." for parent directory.
*
* @param {*} orig current path, must end with a "/"
* @param {*} sub directory to concatenate or ".." for parent.
*/
FileSelector.prototype.concatPath = function (orig, sub) {
if (sub === '..') {
if (orig.endsWith(":/") || orig === './') {
return orig; // this is just a drive letter or current dir
} else {
var parts = orig.split("/");
if (parts.length >= 2) {
parts.splice(-2, 1);
return parts.join("/");
} else {
return orig; // split failed, just keep the dir
}
}
} else {
return orig + sub + "/";
}
}
/**
* draw a preview of the currently selected file.
*/
FileSelector.prototype.drawPreview = function () {
if (!this.preview) {
var idx = this.currentDir.top + this.currentDir.cursor;
var name = this.currentDir.list[idx];
var filename = null;
if (this.supportedImage(name)) {
filename = this.currentDir.path + name;
}
try {
if (!filename) {
throw "No filename";
}
var dl = new Bitmap(filename);
var previewWidth = PREVIEW_WIDTH;
var previewHeight = Height / 2;
var w = dl.width;
var h = dl.height;
if (w > previewWidth) {
var factor = w / previewWidth;
w = previewWidth;
h = h / factor;
}
if (h > previewHeight) {
var factor = h / previewHeight;
h = previewHeight;
w = w / factor;
}
this.preview = new Bitmap(w, h);
SetRenderBitmap(this.preview);
dl.DrawAdvanced(
0, 0, dl.width, dl.height,
0, 0, this.preview.width, this.preview.height
);
} catch (e) {
// Println(e);
this.preview = new Bitmap(PREVIEW_WIDTH, PREVIEW_WIDTH);
SetRenderBitmap(this.preview);
ClearScreen(EGA.BLACK);
Line(0, 0, this.preview.width, this.preview.height, EGA.RED);
Line(this.preview.width, 0, 0, this.preview.height, EGA.RED);
Box(0, 0, this.preview.width - 1, this.preview.height - 1, EGA.RED);
}
SetRenderBitmap(null);
}
this.preview.Draw(this.selWidth + 1, 0);
}
/**
* draw file description. NYI
*/
FileSelector.prototype.drawImgDesc = function () {
}
/**
* store the selected file (if any) in the value attribute and hide the selector.
*
* @param {string} file the file name to store or null for no file.
*/
FileSelector.prototype.selectFile = function (file) {
dstdn.dialog = null;
this.value = file;
if (this.onClose) {
this.onClose(file);
}
}
/**
* display the selector.
*
* @param onClose function to call when the selector closes.
*/
FileSelector.prototype.Activate = function (onClose) {
this.onClose = onClose;
this.value = null;
dstdn.dialog = dstdn.file_sel;
}
/**
* check by extension if this image type is supported
*
* @param {string} name
*
* @returns true if this is a supported image type
*/
FileSelector.prototype.supportedImage = function (name) {
var lowerName = name.toLowerCase();
return lowerName.endsWith(".jpg") || lowerName.endsWith(".png") || lowerName.endsWith(".web") || lowerName.endsWith(".webp");
}
/**
* draw file selector box.
*/
FileSelector.prototype.drawFileBox = function () {
Box(0, 0, this.selWidth, SizeY() - this.msgBoxHeight, EGA.RED);
for (var r = 0; r < this.currentDir.rows; r++) {
var idx = r + this.currentDir.top;
if (idx >= this.currentDir.list.length) {
break;
}
var name = this.currentDir.list[idx];
var filCol = EGA.DARK_GRAY;
if (this.currentDir.info[name]["is_drive"]) {
name += ":/";
filCol = EGA.GREEN;
} else if (this.currentDir.info[name]["is_directory"]) {
name += "/";
filCol = EGA.WHITE;
} else if (this.supportedImage(name)) {
filCol = EGA.LIGHT_GRAY;
}
var yPos = r * (this.dirFont.height + FONT_SPACING);
if (r == this.currentDir.cursor) {
FilledBox(0, yPos, this.selWidth, yPos + this.dirFont.height, EGA.LIGHT_BLUE);
}
this.dirFont.DrawStringLeft(MSG_BORDER, yPos, name, filCol, NO_COLOR);
}
}
/**
* draw message box with cursor position and current path.
*/
FileSelector.prototype.drawMsgBox = function () {
FilledBox(0, SizeY() - this.msgBoxHeight, this.selWidth, SizeY(), EGA.LIGHT_GREEN);
var txt = "[" + (this.currentDir.top + this.currentDir.cursor + 1) + "/" + this.currentDir.list.length + "] " + this.currentDir.path;
this.dirFont.DrawStringLeft(MSG_BORDER, SizeY() - this.dirFont.height - MSG_BORDER, txt, EGA.BLACK, NO_COLOR);
}
/**
* move one page up in file list.
*/
FileSelector.prototype.cursorPageUp = function () {
if (this.currentDir.top > this.currentDir.rows) {
this.currentDir.top -= this.currentDir.rows;
} else if (this.currentDir.top == 0) {
this.currentDir.cursor = 0;
} else {
this.currentDir.top = 0;
}
this.preview = null;
}
/**
* move one page down in file list.
*/
FileSelector.prototype.cursorPageDown = function () {
if (this.currentDir.list.length < this.currentDir.rows) {
this.currentDir.cursor = this.currentDir.list.length - 1;
} else if (this.currentDir.top == this.currentDir.list.length - this.currentDir.rows) {
this.currentDir.cursor = this.currentDir.rows - 1;
} else {
this.currentDir.top = Math.min(this.currentDir.top + this.currentDir.rows, this.currentDir.list.length - this.currentDir.rows);
}
this.preview = null;
}
/**
* move one file up in file list.
*/
FileSelector.prototype.cursorUp = function () {
if (this.currentDir.cursor > 0) {
this.currentDir.cursor--;
} else {
if (this.currentDir.top > 0) {
this.currentDir.top--;
}
}
this.preview = null;
}
/**
* move one file down in file list.
*/
FileSelector.prototype.cursorDown = function () {
if ((this.currentDir.cursor < this.currentDir.rows - 1) && (this.currentDir.cursor < this.currentDir.list.length - 1)) {
this.currentDir.cursor++;
} else {
if (this.currentDir.top + this.currentDir.rows < this.currentDir.list.length) {
this.currentDir.top++;
}
}
this.preview = null;
}
/**
* either play file or enter directory
*/
FileSelector.prototype.onEnter = function () {
var idx = this.currentDir.top + this.currentDir.cursor;
var name = this.currentDir.list[idx];
if (this.currentDir.info[name]["is_drive"]) {
this.currentDir = this.initDirInfo(name + ":/");
} else if (this.currentDir.info[name]["is_directory"]) {
this.currentDir = this.initDirInfo(this.concatPath(this.currentDir.path, name));
} else if (this.supportedImage(name)) {
this.selectFile(this.currentDir.path + name);
}
}
/**
* render file selector.
*/
FileSelector.prototype.Draw = function () {
ClearScreen(EGA.BLACK);
this.drawFileBox();
this.drawMsgBox();
this.drawImgDesc();
this.drawPreview();
}
/*
** This function is called on any input.
*/
FileSelector.prototype.Input = function (key, keyCode, char, eventKey) {
switch (keyCode) {
case KEY.Code.KEY_ENTER:
this.onEnter();
break;
case KEY.Code.KEY_DEL:
this.selectFile(null);
break;
case KEY.Code.KEY_UP:
this.cursorUp();
break;
case KEY.Code.KEY_DOWN:
this.cursorDown();
break;
case KEY.Code.KEY_PGUP:
this.cursorPageUp();
break;
case KEY.Code.KEY_PGDN:
this.cursorPageDown();
break;
}
}
// export functions and version
exports.__VERSION__ = 1;
exports.FileSelector = FileSelector;