@progress/kendo-angular-upload
Version:
Kendo UI Angular Upload Component
145 lines (144 loc) • 4.49 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { FileState } from '../types';
/**
* @hidden
*/
export class FileMap {
_files;
constructor() {
this._files = {};
}
add(file) {
const uid = file.uid;
if (this.has(uid)) {
if (file.validationErrors && file.validationErrors.length > 0) {
this._files[uid].unshift(file);
}
else {
this._files[uid].push(file);
}
}
else {
this._files[uid] = [file];
}
}
remove(uid) {
if (this.has(uid)) {
this._files[uid] = null;
delete this._files[uid];
}
}
clear() {
const allFiles = this._files;
for (const uid in allFiles) {
if (allFiles.hasOwnProperty(uid)) {
for (const file of allFiles[uid]) {
if (file.httpSubscription) {
file.httpSubscription.unsubscribe();
}
}
allFiles[uid] = null;
delete allFiles[uid];
}
}
}
has(uid) {
return uid in this._files;
}
get(uid) {
return this._files[uid];
}
setFilesState(files, state) {
for (const file of files) {
this.setFilesStateByUid(file.uid, state);
}
}
setFilesStateByUid(uid, state) {
this.get(uid).forEach((f) => {
f.state = state;
});
}
get count() {
return Object.getOwnPropertyNames(this._files).length;
}
get files() {
const initial = this._files;
const transformed = [];
for (const uid in initial) {
if (initial.hasOwnProperty(uid)) {
transformed.push(initial[uid]);
}
}
return transformed;
}
get filesFlat() {
const initial = this._files;
const transformed = [];
for (const uid in initial) {
if (initial.hasOwnProperty(uid)) {
const current = initial[uid];
current.forEach((file) => {
transformed.push(file);
});
}
}
return transformed;
}
get filesToUpload() {
const files = this._files;
const notUploaded = [];
for (const uid in files) {
if (files.hasOwnProperty(uid)) {
const currentFiles = files[uid];
let currentFilesValid = true;
for (const file of currentFiles) {
if (file.state !== FileState.Selected || (file.validationErrors && file.validationErrors.length > 0)) {
currentFilesValid = false;
}
}
if (currentFilesValid) {
notUploaded.push(currentFiles);
}
}
}
return notUploaded;
}
get firstFileToUpload() {
const files = this._files;
for (const uid in files) {
if (files.hasOwnProperty(uid)) {
const currentFiles = files[uid];
let currentFilesValid = true;
for (const file of currentFiles) {
if (file.state !== FileState.Selected || (file.validationErrors && file.validationErrors.length > 0)) {
currentFilesValid = false;
}
}
if (currentFilesValid) {
return currentFiles;
}
}
}
return null;
}
getFilesWithState(state) {
return this.filesFlat.filter(file => file.state === state);
}
hasFileWithState(fileStates) {
const files = this._files;
for (const uid in files) {
if (files.hasOwnProperty(uid)) {
const currentFiles = files[uid];
for (const file of currentFiles) {
if (fileStates.indexOf(file.state) >= 0) {
return true;
}
}
}
}
return false;
}
}