@carbon/ibm-cloud-cognitive-cdai
Version:
Carbon for Cloud & Cognitive CD&AI UI components
111 lines (110 loc) • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FILE_UPLOAD_STATUS = void 0;
exports.countFiles = countFiles;
exports.sortFiles = sortFiles;
Object.defineProperty(exports, "uuid", {
enumerable: true,
get: function get() {
return _uuid.v4;
}
});
exports.validateFileAdded = validateFileAdded;
var _uuid = require("uuid");
var _FileHelper = require("../../component_helpers/FileHelper");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } //
// Copyright IBM Corp. 2020, 2020
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//
var FILE_UPLOAD_STATUS = exports.FILE_UPLOAD_STATUS = {
EDIT: 'edit',
UPLOADING: 'uploading',
COMPLETE: 'complete'
};
function countFiles(files) {
var invalid = 0;
var uploading = 0;
var total = 0;
var _iterator = _createForOfIteratorHelper(files),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var fileToUpload = _step.value;
total++;
if (fileToUpload.invalid) {
invalid++;
}
if (fileToUpload.status === FILE_UPLOAD_STATUS.UPLOADING) {
uploading++;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
var finished = total - invalid - uploading;
return {
invalid: invalid,
uploading: uploading,
total: total,
finished: finished
};
}
var statusScore = [FILE_UPLOAD_STATUS.COMPLETE, FILE_UPLOAD_STATUS.UPLOADING, FILE_UPLOAD_STATUS.EDIT];
function sortFiles(a, b, numFiles) {
var aIdx = a.index / numFiles + statusScore.indexOf(a.status);
if (a.invalid) {
aIdx += statusScore.length + 1;
}
var bIdx = b.index / numFiles + statusScore.indexOf(b.status);
if (b.invalid) {
bIdx += statusScore.length + 1;
}
return aIdx === bIdx ? 0 : aIdx < bIdx ? 1 : -1;
}
function validateFileAdded(fileToUpload, props, getInvalidExtensionText) {
if (fileToUpload.url && props.allowInvalidFilenameInUrl) {
return {
invalid: false
};
}
if (fileToUpload.file.size > props.maxFileSize) {
return {
invalid: true,
errorSubject: props.maxFileSizeSubject,
errorBody: props.maxFileSizeMessage
};
}
var ext = (0, _FileHelper.getExtensionFromFilename)(fileToUpload.file.name);
if (props.extensionRequired && !ext) {
return {
invalid: true,
errorSubject: props.invalidFileText,
errorBody: props.extensionIsRequiredText
};
}
if (!(0, _FileHelper.hasValidFilename)(fileToUpload.file.name)) {
return {
invalid: true,
errorSubject: props.invalidFileText,
errorBody: props.invalidFileNameText
};
}
if (!(0, _FileHelper.hasValidExtension)(ext, props.validExtensions)) {
return {
invalid: true,
errorSubject: props.invalidFileText,
errorBody: getInvalidExtensionText()
};
}
return {
invalid: false
};
}