From 5b9e250c95df7355a76c6d126832b861759248a9 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 3 Apr 2020 18:08:44 +0300 Subject: [PATCH] fix(client) dom: getCurrentType --- client/dom/current-file.js | 12 ++++--- client/dom/current-file.spec.js | 61 ++++++++++++++++++++++++++++----- client/dom/index.js | 31 ++++++++--------- client/key/index.js | 8 +++-- client/listeners/index.js | 16 ++++----- client/modules/konsole.js | 4 +-- 6 files changed, 90 insertions(+), 42 deletions(-) diff --git a/client/dom/current-file.js b/client/dom/current-file.js index 99fc6822..012d68dc 100644 --- a/client/dom/current-file.js +++ b/client/dom/current-file.js @@ -289,14 +289,16 @@ module.exports.isCurrentIsDir = (currentFile) => { const current = currentFile || DOM.getCurrentFile(); const fileType = DOM.getCurrentType(current); - return DOM.isContainClass(fileType, [ - 'directory', - 'directory-link', - ]); + return /^directory(-link)?/.test(fileType); }; module.exports.getCurrentType = (currentFile) => { const current = currentFile || DOM.getCurrentFile(); - return DOM.getByDataName('js-type', current); + const el = DOM.getByDataName('js-type', current); + const type = el.className + .split(' ') + .pop(); + + return type; }; diff --git a/client/dom/current-file.spec.js b/client/dom/current-file.spec.js index 8b0c33f8..22441b36 100644 --- a/client/dom/current-file.spec.js +++ b/client/dom/current-file.spec.js @@ -179,6 +179,10 @@ test('current-file: getCurrentType', (t) => { const {getByDataName} = global.DOM; + getByDataName.returns({ + className: 'mini-icon directory', + }); + const current = create(); currentFile.getCurrentType(current); @@ -212,7 +216,53 @@ test('current-file: isCurrentIsDir: getCurrentType', (t) => { t.end(); }); -test('current-file: isCurrentIsDir: isContainClass', (t) => { +test('current-file: isCurrentIsDir: directory', (t) => { + const { + DOM, + CloudCmd, + } = global; + + global.DOM = getDOM({ + getCurrentType: stub().returns('directory'), + }); + + global.CloudCmd = getCloudCmd(); + + const current = create(); + + const result = currentFile.isCurrentIsDir(current); + + global.DOM = DOM; + global.CloudCmd = CloudCmd; + + t.ok(result); + t.end(); +}); + +test('current-file: isCurrentIsDir: directory-link', (t) => { + const { + DOM, + CloudCmd, + } = global; + + global.DOM = getDOM({ + getCurrentType: stub().returns('directory-link'), + }); + + global.CloudCmd = getCloudCmd(); + + const current = create(); + + const result = currentFile.isCurrentIsDir(current); + + global.DOM = DOM; + global.CloudCmd = CloudCmd; + + t.ok(result); + t.end(); +}); + +test('current-file: isCurrentIsDir: file', (t) => { const { DOM, CloudCmd, @@ -224,19 +274,14 @@ test('current-file: isCurrentIsDir: isContainClass', (t) => { global.CloudCmd = getCloudCmd(); - const {isContainClass} = global.DOM; - const current = create(); - currentFile.isCurrentIsDir(current); + const result = currentFile.isCurrentIsDir(current); global.DOM = DOM; global.CloudCmd = CloudCmd; - t.ok(isContainClass.calledWith('file', [ - 'directory', - 'directory-link', - ])); + t.notOk(result); t.end(); }); diff --git a/client/dom/index.js b/client/dom/index.js index f9b8d6aa..23515bdf 100644 --- a/client/dom/index.js +++ b/client/dom/index.js @@ -801,12 +801,12 @@ function CmdProto() { if (cancel) return; - CloudCmd.loadDir({ + await CloudCmd.loadDir({ path, }); }; - this.duplicatePanel = () => { + this.duplicatePanel = async () => { const Info = CurrentInfo; const {isDir} = Info; const panel = Info.panelPassive; @@ -821,14 +821,14 @@ function CmdProto() { const path = getPath(isDir); - CloudCmd.loadDir({ + await CloudCmd.loadDir({ path, panel, noCurrent, }); }; - this.swapPanels = () => { + this.swapPanels = async () => { const Info = CurrentInfo; const { panel, @@ -842,26 +842,25 @@ function CmdProto() { let currentIndex = files.indexOf(element); - CloudCmd.loadDir({ + await CloudCmd.loadDir({ path, panel: panelPassive, noCurrent: true, }); - CloudCmd.loadDir({ + await CloudCmd.loadDir({ path: dirPathPassive, panel, - }, () => { - const {files} = Info; - const length = files.length - 1; - - if (currentIndex > length) - currentIndex = length; - - const el = files[currentIndex]; - - DOM.setCurrentFile(el); }); + + const length = Info.files.length - 1; + + if (currentIndex > length) + currentIndex = length; + + const el = Info.files[currentIndex]; + + DOM.setCurrentFile(el); }; this.CurrentInfo = CurrentInfo; diff --git a/client/key/index.js b/client/key/index.js index ca1801c0..d3578e30 100644 --- a/client/key/index.js +++ b/client/key/index.js @@ -196,7 +196,9 @@ function KeyProto() { case Key.F3: if (Info.isDir) - loadDir({path}); + await loadDir({ + path, + }); else if (shift) CloudCmd.Markdown.show(path); else if (ctrlMeta) @@ -393,7 +395,7 @@ function KeyProto() { case Key.ENTER: if (Info.isDir) - loadDir({path}); + await loadDir({path}); else CloudCmd.View.show(); break; @@ -405,7 +407,7 @@ function KeyProto() { case Key.BACKSLASH: if (ctrlMeta) - loadDir({ + await loadDir({ path: '/', }); break; diff --git a/client/listeners/index.js b/client/listeners/index.js index faf104ae..cf0044bb 100644 --- a/client/listeners/index.js +++ b/client/listeners/index.js @@ -192,7 +192,7 @@ function decodePath(path) { .replace(NBSP_REG, SPACE) || '/'; } -function onPathElementClick(panel, event) { +async function onPathElementClick(panel, event) { event.preventDefault(); const element = event.target; @@ -214,7 +214,7 @@ function onPathElementClick(panel, event) { const {href} = element; const path = decodePath(href); - CloudCmd.loadDir({ + await CloudCmd.loadDir({ path, isRefresh: false, panel: noCurrent ? panel : Info.panel, @@ -264,13 +264,13 @@ function changePanel(element) { DOM.changePanel(); } -function onDblClick(event) { +async function onDblClick(event) { const current = getLIElement(event.target); const isDir = DOM.isCurrentIsDir(current); const path = DOM.getCurrentPath(current); if (isDir) { - CloudCmd.loadDir({ + await CloudCmd.loadDir({ path: path === '/' ? '/' : path + '/', }); @@ -282,7 +282,7 @@ function onDblClick(event) { } } -function onTouch(event) { +async function onTouch(event) { const current = getLIElement(event.target); const isDir = DOM.isCurrentIsDir(current); @@ -294,7 +294,7 @@ function onTouch(event) { if (!isCurrent) return; - CloudCmd.loadDir({ + await CloudCmd.loadDir({ path: DOM.getCurrentPath(current), }); } @@ -470,14 +470,14 @@ function unload() { } function pop() { - Events.add('popstate', ({state}) => { + Events.add('popstate', async ({state}) => { const path = (state || '').replace(FS, ''); if (!path) return CloudCmd.route(location.hash); const history = false; - CloudCmd.loadDir({ + await CloudCmd.loadDir({ path, history, }); diff --git a/client/modules/konsole.js b/client/modules/konsole.js index e1fda7d5..867bf8b2 100644 --- a/client/modules/konsole.js +++ b/client/modules/konsole.js @@ -69,11 +69,11 @@ function getEnv() { }; } -function onPath(path) { +async function onPath(path) { if (Info.dirPath === path) return; - CloudCmd.loadDir({ + await CloudCmd.loadDir({ path, }); }