mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
feature(cloudcmd) show size as <link> for links
This commit is contained in:
parent
e18ddb2e18
commit
846f4aa78f
5 changed files with 138 additions and 6 deletions
|
|
@ -1,5 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const currify = require('currify/legacy');
|
||||
|
||||
const DOM = module.exports;
|
||||
|
||||
/**
|
||||
|
|
@ -8,19 +10,23 @@ const DOM = module.exports;
|
|||
* @param element
|
||||
* @param className
|
||||
*/
|
||||
module.exports.isContainClass = (element, className) => {
|
||||
const isContainClass = (element, className) => {
|
||||
if (!element)
|
||||
throw Error('element could not be empty!');
|
||||
|
||||
if (!className)
|
||||
throw Error('className could not be empty!');
|
||||
|
||||
const classList = element.classList;
|
||||
const ret = classList.contains(className);
|
||||
if (Array.isArray(className))
|
||||
return className.some(currify(isContainClass, element));
|
||||
|
||||
return ret;
|
||||
const classList = element.classList;
|
||||
|
||||
return classList.contains(className);
|
||||
};
|
||||
|
||||
module.exports.isContainClass = isContainClass;
|
||||
|
||||
/**
|
||||
* Function search element by tag
|
||||
* @param tag - className
|
||||
|
|
|
|||
57
client/dom/dom-tree.spec.js
Normal file
57
client/dom/dom-tree.spec.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
'use strict';
|
||||
|
||||
const test = require('tape');
|
||||
const diff = require('sinon-called-with-diff');
|
||||
const sinon = diff(require('sinon'));
|
||||
const tryCatch = require('try-catch');
|
||||
|
||||
const {
|
||||
isContainClass,
|
||||
} = require('./dom-tree');
|
||||
|
||||
test('dom: isContainClass: no element', (t) => {
|
||||
const [e] = tryCatch(isContainClass);
|
||||
t.equal(e.message, 'element could not be empty!', 'should throw when no element');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: isContainClass: no className', (t) => {
|
||||
const [e] = tryCatch(isContainClass, {});
|
||||
t.equal(e.message, 'className could not be empty!', 'should throw when no element');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: isContainClass: contains', (t) => {
|
||||
const contains = sinon.stub();
|
||||
const el = {
|
||||
classList: {
|
||||
contains,
|
||||
}
|
||||
};
|
||||
|
||||
const className = 'hello';
|
||||
isContainClass(el, className);
|
||||
|
||||
t.ok(contains.calledWith(className), 'should call contains');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: isContainClass: contains: array', (t) => {
|
||||
const contains = sinon.stub();
|
||||
const el = {
|
||||
classList: {
|
||||
contains,
|
||||
}
|
||||
};
|
||||
|
||||
const className = 'hello';
|
||||
isContainClass(el, [
|
||||
'world',
|
||||
className,
|
||||
'hello',
|
||||
]);
|
||||
|
||||
t.ok(contains.calledWith(className), 'should call contains');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
@ -497,9 +497,11 @@ function CmdProto() {
|
|||
this.isCurrentIsDir = (currentFile) => {
|
||||
const current = currentFile || DOM.getCurrentFile();
|
||||
const fileType = DOM.getByDataName('js-type', current);
|
||||
const ret = DOM.isContainClass(fileType, 'directory');
|
||||
|
||||
return ret;
|
||||
return DOM.isContainClass(fileType, [
|
||||
'directory',
|
||||
'directory-link',
|
||||
]);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -230,6 +230,7 @@ function getAttribute(type) {
|
|||
return 'target="_blank" ';
|
||||
}
|
||||
|
||||
module.exports._getSize = getSize;
|
||||
function getSize(file) {
|
||||
const {
|
||||
size,
|
||||
|
|
@ -239,6 +240,9 @@ function getSize(file) {
|
|||
if (type === 'directory')
|
||||
return '<dir>';
|
||||
|
||||
if (/link/.test(type))
|
||||
return '<link>';
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
|
|
|||
63
common/cloudfunc.spec.js
Normal file
63
common/cloudfunc.spec.js
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
'use strict';
|
||||
|
||||
const test = require('tape');
|
||||
const cloudfunc = require('./cloudfunc');
|
||||
const {
|
||||
_getSize,
|
||||
} = cloudfunc;
|
||||
|
||||
test('cloudfunc: getSize: dir', (t) => {
|
||||
const type = 'directory';
|
||||
const size = 0;
|
||||
const result = _getSize({
|
||||
type,
|
||||
size,
|
||||
});
|
||||
|
||||
const expected = '<dir>';
|
||||
|
||||
t.equal(result, expected, 'should equal');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudfunc: getSize: link: dir', (t) => {
|
||||
const type = 'directory-link';
|
||||
const size = 0;
|
||||
const result = _getSize({
|
||||
type,
|
||||
size,
|
||||
});
|
||||
|
||||
const expected = '<link>';
|
||||
|
||||
t.equal(result, expected, 'should equal');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudfunc: getSize: link: file', (t) => {
|
||||
const type = 'file-link';
|
||||
const size = 0;
|
||||
const result = _getSize({
|
||||
type,
|
||||
size,
|
||||
});
|
||||
|
||||
const expected = '<link>';
|
||||
|
||||
t.equal(result, expected, 'should equal');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudfunc: getSize: file', (t) => {
|
||||
const type = 'file';
|
||||
const size = '100.00kb';
|
||||
const result = _getSize({
|
||||
type,
|
||||
size,
|
||||
});
|
||||
|
||||
const expected = '100.00kb';
|
||||
|
||||
t.equal(result, expected, 'should equal');
|
||||
t.end();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue