Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiselect support (#22) #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 115 additions & 61 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,34 @@ const createInitialPaths = (core, proc) => {
return {homePath, initialPath};
};

/**
* Formats file status message
*/
const formatFileMessage = file => `${file.filename} (${file.size} bytes)`;
const getDirectoryCount = files =>
files.filter(file => file.isDirectory).length;
const getFileCount = files =>
files.filter(file => !file.isDirectory).length;
const getTotalSize = files =>
files.reduce((total, file) => total + (file.size || 0), 0);

/**
* Formats directory status message
*/
const formatStatusMessage = (core) => {
const formatStatusMessage = (core, files) => {
const {translatable} = core.make('osjs/locale');
const __ = translatable(translations);

return (path, files) => {
const directoryCount = files.filter(f => f.isDirectory).length;
const fileCount = files.filter(f => !f.isDirectory).length;
const totalSize = files.reduce((t, f) => t + (f.size || 0), 0);

return __('LBL_STATUS', directoryCount, fileCount, totalSize);
const directoryCount = getDirectoryCount(files);
const fileCount = getFileCount(files);
const totalSize = getTotalSize(files);
const directoryCountMessage = `${directoryCount} ${__(directoryCount === 1 ? 'SINGLE_DIR' : 'MULTI_DIR')}`;
const fileCountMessage = `${fileCount} ${__(fileCount === 1 ? 'SINGLE_FILE' : 'MULTI_FILE')}`;

if (directoryCount > 0 && fileCount > 0) {
return __('LBL_DIR_AND_FILE_STATUS', directoryCountMessage, fileCountMessage, totalSize);
} else if (directoryCount > 0) {
return __('LBL_DIR_OR_FILE_STATUS', directoryCountMessage, totalSize);
} else {
return __('LBL_DIR_OR_FILE_STATUS', fileCountMessage, totalSize);
}
};
};

Expand Down Expand Up @@ -318,6 +328,8 @@ const vfsActionFactory = (core, proc, win, dialog, state) => {
const readdir = async (dir, history, selectFile) => {
if (win.getState('loading')) {
return;
} else if (Array.isArray(dir)) {
dir = dir[0];
}

try {
Expand Down Expand Up @@ -347,7 +359,7 @@ const vfsActionFactory = (core, proc, win, dialog, state) => {
} catch (error) {
dialog('error', error, __('MSG_READDIR_ERROR', dir.path));
} finally {
state.currentFile = undefined;
state.currentFile = [];
win.setState('loading', false);
}
};
Expand All @@ -358,26 +370,33 @@ const vfsActionFactory = (core, proc, win, dialog, state) => {
.catch(error => dialog('error', error, __('MSG_UPLOAD_ERROR')));
});

const paste = (move, currentPath) => ({item, callback}) => {
const dest = {path: pathJoin(currentPath.path, item.filename)};
const paste = (move, currentPath) => ({items, callback}) => {
const promises = items.map(item => {
const dest = {
path: pathJoin(currentPath.path, item.filename)
};

const fn = move
? vfs.move(item, dest, {pid: proc.pid})
: vfs.copy(item, dest, {pid: proc.pid});
return move
? vfs.move(item, dest, {pid: proc.pid})
: vfs.copy(item, dest, {pid: proc.pid});
});

return fn
.then(() => {
return Promise
.all(promises)
.then(results => {
refresh(true);

if (typeof callback === 'function') {
callback();
}

return results;
})
.catch(error => dialog('error', error, __('MSG_PASTE_ERROR')));
};

return {
download: file => vfs.download(file),
download: files => files.forEach(file => vfs.download(file)),
upload,
refresh,
action,
Expand Down Expand Up @@ -430,23 +449,34 @@ const dialogFactory = (core, proc, win) => {
value: __('DIALOG_MKDIR_PLACEHOLDER')
}, usingPositiveButton(value => {
const newPath = pathJoin(currentPath.path, value);
action(() => vfs.mkdir({path: newPath}, {pid: proc.pid}), value, __('MSG_MKDIR_ERROR'));
action(
() => vfs.mkdir({path: newPath}, {pid: proc.pid}),
value,
__('MSG_MKDIR_ERROR')
);
}));

const renameDialog = (action, file) => dialog('prompt', {
message: __('DIALOG_RENAME_MESSAGE', file.filename),
value: file.filename
}, usingPositiveButton(value => {
const idx = file.path.lastIndexOf(file.filename);
const newPath = file.path.substr(0, idx) + value;
const renameDialog = (action, files) => files.forEach(file =>
dialog('prompt', {
message: __('DIALOG_RENAME_MESSAGE', file.filename),
value: file.filename
}, usingPositiveButton(value => {
const idx = file.path.lastIndexOf(file.filename);
const newPath = file.path.substr(0, idx) + value;

action(() => vfs.rename(file, {path: newPath}), value, __('MSG_RENAME_ERROR'));
}));
action(() => vfs.rename(file, {path: newPath}), value, __('MSG_RENAME_ERROR'));
})));

const deleteDialog = (action, file) => dialog('confirm', {
message: __('DIALOG_DELETE_MESSAGE', file.filename),
const deleteDialog = (action, files) => dialog('confirm', {
message: __('DIALOG_DELETE_MESSAGE', files.length),
}, usingPositiveButton(() => {
action(() => vfs.unlink(file, {pid: proc.pid}), true, __('MSG_DELETE_ERROR'));
action(
() => Promise.all(
files.map(file => vfs.unlink(file, {pid: proc.pid}))
),
true,
__('MSG_DELETE_ERROR')
);
}));

const progressDialog = (file) => dialog('progress', {
Expand Down Expand Up @@ -512,40 +542,44 @@ const menuFactory = (core, proc, win) => {
{label: _('LBL_QUIT'), onclick: () => win.emit('filemanager:menu:quit')}
]);

const createEditMenu = async (item, isContextMenu) => {
const emitter = name => win.emit(name, item);
const createEditMenu = async (items, isContextMenu) => {
const emitter = name => win.emit(name, items);
const item = items[items.length - 1];

if (item && isSpecialFile(item.filename)) {
if (items.length === 1 && item && isSpecialFile(item.filename)) {
return [{
label: _('LBL_GO'),
onclick: () => emitter('filemanager:navigate')
onclick: () => emitter('filemanager:navigate'),
}];
}

const isValidFile = item && !isSpecialFile(item.filename);
const isDirectory = item && item.isDirectory;
const canDownload = items.some(
item => !item.isDirectory && !isSpecialFile(item.filename)
);
const hasValidFile = items.some(item => !isSpecialFile(item.filename));
const isDirectory = items.length === 1 && item.isDirectory;

const openMenu = isDirectory ? [{
label: _('LBL_GO'),
disabled: !item,
disabled: !items.length,
onclick: () => emitter('filemanager:navigate')
}] : [{
label: _('LBL_OPEN'),
disabled: !item,
disabled: !items.length,
onclick: () => emitter('filemanager:open')
}, {
label: __('LBL_OPEN_WITH'),
disabled: !item,
disabled: !items.length,
onclick: () => emitter('filemanager:openWith')
}];

const clipboardMenu = [{
label: _('LBL_COPY'),
disabled: !isValidFile,
disabled: !hasValidFile,
onclick: () => emitter('filemanager:menu:copy')
}, {
label: _('LBL_CUT'),
disabled: !isValidFile,
disabled: !hasValidFile,
onclick: () => emitter('filemanager:menu:cut')
}];

Expand All @@ -563,7 +597,7 @@ const menuFactory = (core, proc, win) => {
if (core.config('filemanager.disableDownload', false) !== true) {
configuredItems.push({
label: _('LBL_DOWNLOAD'),
disabled: !item || isDirectory || !isValidFile,
disabled: !canDownload,
onclick: () => emitter('filemanager:menu:download')
});
}
Expand All @@ -572,12 +606,12 @@ const menuFactory = (core, proc, win) => {
...openMenu,
{
label: _('LBL_RENAME'),
disabled: !isValidFile,
disabled: !hasValidFile,
onclick: () => emitter('filemanager:menu:rename')
},
{
label: _('LBL_DELETE'),
disabled: !isValidFile,
disabled: !hasValidFile,
onclick: () => emitter('filemanager:menu:delete')
},
...clipboardMenu,
Expand Down Expand Up @@ -685,7 +719,6 @@ const createApplication = (core, proc) => {
const createRows = listViewRowFactory(core, proc);
const createMounts = mountViewRowsFactory(core);
const {draggable} = core.make('osjs/dnd');
const statusMessage = formatStatusMessage(core);

const initialState = {
path: '',
Expand All @@ -705,7 +738,9 @@ const createApplication = (core, proc) => {
}),

fileview: listView.state({
columns: []
columns: [],
multiselect: true,
previousSelectedIndex: 0
})
};

Expand Down Expand Up @@ -742,18 +777,18 @@ const createApplication = (core, proc) => {
setStatus: status => ({status}),
setMinimalistic: minimalistic => ({minimalistic}),
setList: ({list, path, selectFile}) => ({fileview, mountview}) => {
let selectedIndex;
let selectedIndex = [];

if (selectFile) {
const foundIndex = list.findIndex(file => file.filename === selectFile);
if (foundIndex !== -1) {
selectedIndex = foundIndex;
selectedIndex = [foundIndex];
}
}

return {
path,
status: statusMessage(path, list),
status: formatStatusMessage(list),
mountview: Object.assign({}, mountview, {
rows: createMounts()
}),
Expand All @@ -771,7 +806,10 @@ const createApplication = (core, proc) => {

fileview: listView.actions({
select: ({data}) => win.emit('filemanager:select', data),
activate: ({data}) => win.emit(`filemanager:${data.isFile ? 'open' : 'navigate'}`, data),
activate: ({data}) =>
data.forEach(item =>
win.emit(`filemanager:${item.isFile ? 'open' : 'navigate'}`, item)
),
contextmenu: args => win.emit('filemanager:contextmenu', args),
created: ({el, data}) => {
if (data.isFile) {
Expand All @@ -793,7 +831,7 @@ const createApplication = (core, proc) => {
*/
const createWindow = (core, proc) => {
let wired;
const state = {currentFile: undefined, currentPath: undefined};
const state = {currentFile: [], currentPath: undefined};
const {homePath, initialPath} = createInitialPaths(core, proc);

const title = core.make('osjs/locale').translatableFlat(proc.metadata.title);
Expand All @@ -812,13 +850,29 @@ const createWindow = (core, proc) => {
const onDrop = (...args) => vfs.drop(...args);
const onHome = () => vfs.readdir(homePath, 'clear');
const onNavigate = (...args) => vfs.readdir(...args);
const onSelectItem = file => (state.currentFile = file);
const onSelectStatus = file => win.emit('filemanager:status', formatFileMessage(file));
const onSelectItem = files => (state.currentFile = files);
const onSelectStatus = files => win.emit('filemanager:status', formatStatusMessage(files));
const onContextMenu = ({ev, data}) => createMenu({ev, name: 'edit'}, data, true);
const onReaddirRender = args => wired.setList(args);
const onRefresh = (...args) => vfs.refresh(...args);
const onOpen = file => core.open(file, {useDefault: true});
const onOpenWith = file => core.open(file, {useDefault: true, forceDialog: true});
const onOpen = files => {
if (!Array.isArray(files)) {
files = [files];
}

return files.forEach(
file => core.open(file, {useDefault: true})
);
};
const onOpenWith = files => {
if (!Array.isArray(files)) {
files = [files];
}

return files.forEach(
file => core.open(file, {useDefault: true, forceDialog: true})
);
};
const onHistoryPush = file => wired.history.push(file);
const onHistoryClear = () => wired.history.clear();
const onMenu = (props, args) => createMenu(props, args || state.currentFile);
Expand All @@ -829,11 +883,11 @@ const createWindow = (core, proc) => {
const onMenuToggleMinimalistic = () => wired.toggleMinimalistic();
const onMenuShowDate = () => setSetting('showDate', !proc.settings.showDate);
const onMenuShowHidden = () => setSetting('showHiddenFiles', !proc.settings.showHiddenFiles);
const onMenuRename = file => dialog('rename', vfs.action, file);
const onMenuDelete = file => dialog('delete', vfs.action, file);
const onMenuDownload = (...args) => vfs.download(...args);
const onMenuCopy = item => clipboard.set(item);
const onMenuCut = item => clipboard.cut(item);
const onMenuRename = files => dialog('rename', vfs.action, files);
const onMenuDelete = files => dialog('delete', vfs.action, files);
const onMenuDownload = (files) => vfs.download(files);
const onMenuCopy = items => clipboard.set(items);
const onMenuCut = items => clipboard.cut(items);
const onMenuPaste = () => clipboard.paste();

return win
Expand Down
Loading