From cfa7a8b640c2d5f0725463508800af075d299611 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 11 Jun 2018 15:59:22 +0300 Subject: [PATCH] feature(cloudcmd) add ability to set current file closer to removed files --- .../operation/get-next-current-name.js | 19 ++++++++ .../operation/get-next-current-name.spec.js | 43 +++++++++++++++++++ client/modules/operation/index.js | 13 ++++-- 3 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 client/modules/operation/get-next-current-name.js create mode 100644 client/modules/operation/get-next-current-name.spec.js diff --git a/client/modules/operation/get-next-current-name.js b/client/modules/operation/get-next-current-name.js new file mode 100644 index 00000000..dc2bed5e --- /dev/null +++ b/client/modules/operation/get-next-current-name.js @@ -0,0 +1,19 @@ +'use strict'; + +const currify = require('currify/legacy'); + +const not = currify((array, value) => !~array.indexOf(value)); +const notOneOf = currify((a, b) => a.filter(not(b))); + +module.exports = (currentName, names, removedNames) => { + const i = names.indexOf(currentName); + + const nextNames = notOneOf(names, removedNames); + const length = nextNames.length; + + if (nextNames[i]) + return nextNames[i]; + + return nextNames[length - 1]; +}; + diff --git a/client/modules/operation/get-next-current-name.spec.js b/client/modules/operation/get-next-current-name.spec.js new file mode 100644 index 00000000..514ecc29 --- /dev/null +++ b/client/modules/operation/get-next-current-name.spec.js @@ -0,0 +1,43 @@ +'use strict'; + +const test = require('tape'); +const getNextCurrentName = require('./get-next-current-name'); + +test('get-next-current-name', (t) => { + const names = [ + '..', + 'hello', + '1', + '2', + ]; + + const removedNames = [ + '1' + ]; + + const name = getNextCurrentName('hello', names, removedNames); + + t.equal(name, 'hello', 'should equal'); + t.end(); +}); + +test('get-next-current-name: all files removed', (t) => { + const names = [ + '..', + 'hello', + '1', + '2', + ]; + + const removedNames = [ + '1', + '2', + 'hello', + ]; + + const name = getNextCurrentName('2', names, removedNames); + + t.equal(name, '..', 'should equal'); + t.end(); +}); + diff --git a/client/modules/operation/index.js b/client/modules/operation/index.js index 5e614530..c577ab8c 100644 --- a/client/modules/operation/index.js +++ b/client/modules/operation/index.js @@ -18,6 +18,7 @@ const { const RESTful = require('../../dom/rest'); const removeExtension = require('./remove-extension'); const setListeners = require('./set-listeners'); +const getNextCurrentName = require('./get-next-current-name'); const removeQuery = (a) => a.replace(/\?.*/, ''); @@ -258,13 +259,19 @@ function OperationProto(operation, data) { showLoad(); - const names = DOM.getFilenames(files); + const removedNames = DOM.getFilenames(files); + const names = DOM.CurrentInfo.files.map(DOM.getCurrentName); + const prevCurrent = DOM.getCurrentName(); + const currentName = getNextCurrentName(prevCurrent, names, removedNames); - deleteFn(path + query, names, () => { + deleteFn(path + query, removedNames, (e) => { const Storage = DOM.Storage; const dirPath = Info.dirPath; - CloudCmd.refresh(); + !e && CloudCmd.refresh({ + currentName + }); + Storage.removeMatch(dirPath); }); }