From dd442db5995a65d5cafdcc0bf690b0d8879c7cfe Mon Sep 17 00:00:00 2001 From: Vladimir Kovalev <47112293+vladkovl@users.noreply.github.com> Date: Wed, 5 Feb 2020 12:13:27 +0300 Subject: [PATCH] FileManager - Add public constructor for FileManagerItem class (#11885) --- .../file_provider/file_provider.js | 45 +++++++++++++++-- .../fileManagerParts/ajaxProvider.tests.js | 2 +- .../fileManagerParts/common.tests.js | 48 +++++++++++++------ 3 files changed, 77 insertions(+), 18 deletions(-) diff --git a/js/ui/file_manager/file_provider/file_provider.js b/js/ui/file_manager/file_provider/file_provider.js index 0b8fbd47aaff..dcb35eeaa09c 100644 --- a/js/ui/file_manager/file_provider/file_provider.js +++ b/js/ui/file_manager/file_provider/file_provider.js @@ -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; @@ -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; diff --git a/testing/tests/DevExpress.ui.widgets/fileManagerParts/ajaxProvider.tests.js b/testing/tests/DevExpress.ui.widgets/fileManagerParts/ajaxProvider.tests.js index 58e537325ef3..d032fa0b97cf 100644 --- a/testing/tests/DevExpress.ui.widgets/fileManagerParts/ajaxProvider.tests.js +++ b/testing/tests/DevExpress.ui.widgets/fileManagerParts/ajaxProvider.tests.js @@ -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'); diff --git a/testing/tests/DevExpress.ui.widgets/fileManagerParts/common.tests.js b/testing/tests/DevExpress.ui.widgets/fileManagerParts/common.tests.js index 9d14e09437af..fc2d85735999 100644 --- a/testing/tests/DevExpress.ui.widgets/fileManagerParts/common.tests.js +++ b/testing/tests/DevExpress.ui.widgets/fileManagerParts/common.tests.js @@ -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'], @@ -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`); + } + }); + });