Skip to content

Commit 37d4e73

Browse files
committed
Added state aware 'Edit' menu to toolbar
1 parent 98febe9 commit 37d4e73

File tree

1 file changed

+63
-39
lines changed

1 file changed

+63
-39
lines changed

index.js

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ const view = (bus, core, proc, win) => (state, actions) => {
9797
h(MenubarItem, {
9898
onclick: ev => bus.emit('openMenu', ev, state, actions, {name: 'file'})
9999
}, _('LBL_FILE')),
100+
h(MenubarItem, {
101+
onclick: ev => bus.emit('openMenu', ev, state, actions, {name: 'edit'})
102+
}, _('LBL_EDIT')),
100103
h(MenubarItem, {
101104
onclick: ev => bus.emit('openMenu', ev, state, actions, {name: 'view'})
102105
}, _('LBL_VIEW')),
@@ -222,6 +225,10 @@ const actions = (bus, core, proc, win) => ({
222225
const index = Math.min(state.history.length - 1, state.historyIndex + 1);
223226
bus.emit('openDirectory', state.history[index], true);
224227
return {historyIndex: index};
228+
},
229+
230+
getSelectedIndex: () => state => {
231+
return state.fileview.selectedIndex;
225232
}
226233
});
227234

@@ -289,6 +296,7 @@ const createDialog = (bus, core, proc, win) => (type, item, cb) => {
289296
const createApplication = (core, proc, win, $content) => {
290297
const homePath = {path: 'home:/'}; // FIXME
291298
let currentPath = proc.args.path ? Object.assign({}, homePath, proc.args.path) : homePath;
299+
let currentFile = undefined;
292300

293301
// FIXME
294302
const settings = {
@@ -313,7 +321,57 @@ const createApplication = (core, proc, win, $content) => {
313321
return core.make('osjs/vfs').writefile({path: uploadpath}, f);
314322
};
315323

316-
bus.on('selectFile', file => a.setStatus(getFileStatus(file)));
324+
const _ = core.make('osjs/locale').translate;
325+
const __ = core.make('osjs/locale').translatable(translations);
326+
327+
const createEditMenuItems = (item, fromContext) => {
328+
const isDirectory = item && item.isDirectory;
329+
// FIXME: Check read-only ?
330+
const isValidFile = item && ['..', '.'].indexOf(item.filename) === -1;
331+
332+
const openMenu = isDirectory
333+
? [{
334+
label: _('LBL_GO'),
335+
disabled: !item,
336+
onclick: () => bus.emit('readFile', item)
337+
}]
338+
: [{
339+
label: _('LBL_OPEN'),
340+
disabled: !item,
341+
onclick: () => bus.emit('readFile', item)
342+
}, {
343+
label: __('LBL_OPEN_WITH'),
344+
disabled: !item,
345+
onclick: () => bus.emit('readFile', item, true)
346+
}];
347+
348+
const menu = [
349+
...openMenu,
350+
{
351+
label: _('LBL_RENAME'),
352+
disabled: !isValidFile,
353+
onclick: () => dialog('rename', item, () => refresh())
354+
},
355+
{
356+
label: _('LBL_DELETE'),
357+
disabled: !isValidFile,
358+
onclick: () => dialog('delete', item, () => refresh())
359+
}
360+
];
361+
362+
menu.push({
363+
label: _('LBL_DOWNLOAD'),
364+
disabled: !item || isDirectory || !isValidFile,
365+
onclick: () => core.make('osjs/vfs').download(item)
366+
});
367+
368+
return menu;
369+
};
370+
371+
bus.on('selectFile', file => {
372+
currentFile = file;
373+
a.setStatus(getFileStatus(file));
374+
});
317375
bus.on('selectMountpoint', mount => bus.emit('openDirectory', {path: mount.root}));
318376

319377
bus.on('readFile', (file, forceDialog) => {
@@ -376,14 +434,12 @@ const createApplication = (core, proc, win, $content) => {
376434

377435
win.setTitle(`${title} - ${path}`);
378436

437+
currentFile = undefined;
379438
currentPath = file;
380439
proc.args.path = file;
381440
});
382441

383442
bus.on('openMenu', (ev, state, actions, item) => {
384-
const _ = core.make('osjs/locale').translate;
385-
const __ = core.make('osjs/locale').translatable(translations);
386-
387443
const menus = {
388444
file: [
389445
{label: _('LBL_UPLOAD'), onclick: () => {
@@ -402,6 +458,8 @@ const createApplication = (core, proc, win, $content) => {
402458
{label: _('LBL_QUIT'), onclick: () => proc.destroy()}
403459
],
404460

461+
edit: createEditMenuItems(currentFile, false),
462+
405463
view: [
406464
{label: _('LBL_REFRESH'), onclick: () => refresh()},
407465
{label: __('LBL_MINIMALISTIC'), checked: state.minimalistic, onclick: () => {
@@ -427,41 +485,7 @@ const createApplication = (core, proc, win, $content) => {
427485
return;
428486
}
429487

430-
const _ = core.make('osjs/locale').translate;
431-
const __ = core.make('osjs/locale').translatable(translations);
432-
433-
const openMenu = item.isDirectory
434-
? [{
435-
label: _('LBL_GO'),
436-
onclick: () => bus.emit('readFile', item)
437-
}]
438-
: [{
439-
label: _('LBL_OPEN'),
440-
onclick: () => bus.emit('readFile', item)
441-
}, {
442-
label: __('LBL_OPEN_WITH'),
443-
onclick: () => bus.emit('readFile', item, true)
444-
}];
445-
446-
const menu = [
447-
...openMenu,
448-
{
449-
label: _('LBL_RENAME'),
450-
onclick: () => dialog('rename', item, () => refresh())
451-
},
452-
{
453-
label: _('LBL_DELETE'),
454-
onclick: () => dialog('delete', item, () => refresh())
455-
}
456-
];
457-
458-
if (!item.isDirectory) {
459-
menu.push({
460-
label: _('LBL_DOWNLOAD'),
461-
onclick: () => core.make('osjs/vfs').download(item)
462-
});
463-
}
464-
488+
const menu = createEditMenuItems(item, true);
465489
core.make('osjs/contextmenu').show({
466490
position: ev,
467491
menu

0 commit comments

Comments
 (0)