Skip to content

Commit

Permalink
FileManager - Add public constructor for FileManagerItem class (#11885)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkovl authored Feb 5, 2020
1 parent d1aa02f commit dd442db
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 18 deletions.
45 changes: 42 additions & 3 deletions js/ui/file_manager/file_provider/file_provider.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { compileGetter } from '../../../core/utils/data';
import { pathCombine, getFileExtension, PATH_SEPARATOR } from '../ui.file_manager.utils';
import { pathCombine, getFileExtension, getPathParts, getName, PATH_SEPARATOR } from '../ui.file_manager.utils';
import { ensureDefined } from '../../../core/utils/common';
import { deserializeDate } from '../../../core/utils/date_serialization';
import { each } from '../../../core/utils/iterator';
import { isPromise } from '../../../core/utils/type';
import { isPromise, isString } from '../../../core/utils/type';
import { Deferred } from '../../../core/utils/deferred';

const DEFAULT_FILE_UPLOAD_CHUNK_SIZE = 200000;
Expand Down Expand Up @@ -150,13 +150,52 @@ class FileProvider {
}

class FileManagerItem {
constructor(pathInfo, name, isDirectory) {
constructor() {
const ctor = isString(arguments[0]) ? this._publicCtor : this._internalCtor;
ctor.apply(this, arguments);
}

_internalCtor(pathInfo, name, isDirectory) {
this.name = name;

this.pathInfo = pathInfo && [...pathInfo] || [];
this.parentPath = this._getPathByPathInfo(this.pathInfo);
this.key = this.relativeName = pathCombine(this.parentPath, name);

this.path = pathCombine(this.parentPath, name);
this.pathKeys = this.pathInfo.map(({ key }) => key);
this.pathKeys.push(this.key);

this._initialize(isDirectory);
}

_publicCtor(path, isDirectory, pathKeys) {
this.path = path || '';
this.pathKeys = pathKeys || [];

const pathInfo = [];

const parts = getPathParts(path, true);
for(let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
const pathInfoPart = {
key: this.pathKeys[i] || part,
name: getName(part)
};
pathInfo.push(pathInfoPart);
}

this.pathInfo = pathInfo;

this.relativeName = path;
this.name = getName(path);
this.key = this.pathKeys.length ? this.pathKeys[this.pathKeys.length - 1] : path;
this.parentPath = parts.length > 1 ? parts[parts.length - 2] : '';

this._initialize(isDirectory);
}

_initialize(isDirectory) {
this.isDirectory = isDirectory || false;
this.isRoot = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ QUnit.module('Ajax File Provider', moduleConfig, () => {
responseText: fileItems
});

this.provider.getItems('')
this.provider.getItems()
.done(dirs => {
assert.equal(dirs.length, 2);
assert.equal(dirs[0].name, 'F1');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
const { test } = QUnit;
import { getPathParts, getEscapedFileName } from 'ui/file_manager/ui.file_manager.utils';
import { FileManagerItem } from 'ui/file_manager/file_provider/file_provider';

const moduleConfig = {

beforeEach: function() {
this.clock = sinon.useFakeTimers();
this.clock.tick(400);
},

afterEach: function() {
this.clock.restore();
}

};

QUnit.module('Commands', moduleConfig, () => {
QUnit.module('Common tests', () => {
test('getPathParts() function must correctly separate path string', function(assert) {
const testData = {
'Files/Documents': ['Files', 'Documents'],
Expand Down Expand Up @@ -57,4 +45,36 @@ QUnit.module('Commands', moduleConfig, () => {
}
});

test('create FileManagerItem by public constructor', function(assert) {
const testData = {
'1': {
path: 'folder1',
isDir: true,
name: 'folder1',
key: 'folder1',
pathInfo: []
},
'2': {
path: 'folder1/file1',
isDir: false,
pathKeys: [ '7', '11' ],
name: 'file1',
key: '11',
pathInfo: [ { key: '7', name: 'folder1' }]
}
};

for(const key in testData) {
const testCase = testData[key];

const item = new FileManagerItem(testCase.path, testCase.isDir, testCase.pathKeys);

assert.strictEqual(item.name, testCase.name, `${key}: name correct`);
assert.strictEqual(item.key, testCase.key, `${key}: key correct`);
assert.strictEqual(item.isDirectory, testCase.isDir, `${key}: isDirectory correct`);
assert.strictEqual(item.relativeName, testCase.path, `${key}: relativeName correct`);
assert.deepEqual(item.pathInfo, testCase.pathInfo, `${key}: pathInfo correct`);
}
});

});

0 comments on commit dd442db

Please sign in to comment.