UNPKG

react-application-core

Version:

A react-based application core for the business applications.

491 lines 20.2 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseFileField = void 0; var React = require("react"); var R = require("ramda"); var ts_smart_logger_1 = require("ts-smart-logger"); var base_text_field_component_1 = require("../text-field/base-text-field.component"); var util_1 = require("../../../util"); var dnd_component_1 = require("../../dnd/dnd.component"); var multifield_plugin_1 = require("../multifield/multifield.plugin"); var dialog_component_1 = require("../../dialog/dialog.component"); var web_camera_component_1 = require("../../web-camera/web-camera.component"); var definition_1 = require("../../../definition"); var BaseFileField = /** @class */ (function (_super) { __extends(BaseFileField, _super); /** * @stable [20.10.2020] * @param originalProps */ function BaseFileField(originalProps) { var _this = _super.call(this, originalProps) || this; _this.multiFieldPlugin = new multifield_plugin_1.MultiFieldPlugin(_this); /**/ _this.dndRef = React.createRef(); _this.videoDialogRef = React.createRef(); _this.videoRef = React.createRef(); _this.downloadFile = _this.downloadFile.bind(_this); _this.onAttachFileClick = _this.onAttachFileClick.bind(_this); _this.onCameraActionClick = _this.onCameraActionClick.bind(_this); _this.onSelect = _this.onSelect.bind(_this); _this.onVideoDialogAccept = _this.onVideoDialogAccept.bind(_this); _this.onVideoDialogDeactivate = _this.onVideoDialogDeactivate.bind(_this); _this.onVideoSnapshotSelect = _this.onVideoSnapshotSelect.bind(_this); _this.openVideoDialog = _this.openVideoDialog.bind(_this); var useCamera = originalProps.useCamera, useDownload = originalProps.useDownload, useVideo = originalProps.useVideo; var actions = util_1.FilterUtils.objectValuesArrayFilter(useVideo && ({ type: definition_1.FieldActionTypesEnum.VIDEO, onClick: _this.openVideoDialog, }), useCamera && ({ type: definition_1.FieldActionTypesEnum.CAMERA, disabled: function () { return _this.isCameraActionDisabled; }, onClick: _this.onCameraActionClick, }), ({ type: definition_1.FieldActionTypesEnum.ATTACH_FILE, onClick: _this.onAttachFileClick, }), useDownload && ({ type: definition_1.FieldActionTypesEnum.DOWNLOAD, onClick: _this.downloadFile, })); _this.defaultActions = __spreadArrays(actions, _this.defaultActions); return _this; } /** * @stable [19.10.2020] * @param event */ BaseFileField.prototype.onChange = function (event) { return __awaiter(this, void 0, void 0, function () { var files; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isNativeFileFieldUsed) return [3 /*break*/, 2]; files = event.target.files; return [4 /*yield*/, this.onSelect(files)]; case 1: _a.sent(); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, _super.prototype.onChange.call(this, event)]; case 3: _a.sent(); _a.label = 4; case 4: return [2 /*return*/]; } }); }); }; /** * @stable [20.10.2020] * @param event */ BaseFileField.prototype.onKeyBackspace = function (event) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: _super.prototype.onKeyBackspace.call(this, event); return [4 /*yield*/, this.clearValue()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @stable [09.11.2020] * @param url */ BaseFileField.prototype.doAddItem = function (url) { if (util_1.ObjectUtils.isObjectNotEmpty(url)) { this.multiFieldPlugin.onAddItem({ id: url }); } }; /** * @stable [20.10.2020] * @protected */ BaseFileField.prototype.doClearValue = function () { return __awaiter(this, void 0, void 0, function () { var activeValue; return __generator(this, function (_a) { switch (_a.label) { case 0: activeValue = this.multiFieldPlugin.activeValue[0]; if (R.isNil(activeValue)) { return [2 /*return*/]; } return [4 /*yield*/, this.clearValueById(activeValue.id)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @stable [19.10.2020] * @param event * @protected */ BaseFileField.prototype.onClick = function (event) { _super.prototype.onClick.call(this, event); if (!this.isNativeFileFieldUsed) { this.onAttachFileClick(); } }; /** * @stable [19.10.2020] * @param event * @protected */ BaseFileField.prototype.doCancelEvent = function (event) { if (this.isNativeFileFieldUsed) { return; } _super.prototype.doCancelEvent.call(this, event); }; Object.defineProperty(BaseFileField.prototype, "attachmentBodyElement", { /** * @stable [07.11.2020] * @protected */ get: function () { var _this = this; var useVideo = this.originalProps.useVideo; var dndElement = util_1.ConditionUtils.orNull(!this.isNativeFileFieldUsed, function () { return (React.createElement(dnd_component_1.DnD, { ref: _this.dndRef, disabled: _this.isInactive, onSelect: _this.onSelect })); }); var _a = this.settings.messages, ACCEPT = _a.ACCEPT, TAKE_SNAPSHOT = _a.TAKE_SNAPSHOT; var videoElement = util_1.ConditionUtils.orNull(useVideo && this.state.$$videoOpened, function () { return (React.createElement(dialog_component_1.Dialog, { ref: _this.videoDialogRef, title: TAKE_SNAPSHOT, acceptText: ACCEPT, onDeactivate: _this.onVideoDialogDeactivate, onBeforeAccept: _this.onVideoDialogAccept }, React.createElement(web_camera_component_1.WebCamera, { ref: _this.videoRef, onSelect: _this.onVideoSnapshotSelect }))); }); return (React.createElement(React.Fragment, null, dndElement, videoElement)); }, enumerable: false, configurable: true }); Object.defineProperty(BaseFileField.prototype, "originalEmptyValue", { /** * @stable [02.08.2018] * @returns {EntityIdT[]} */ get: function () { return []; }, enumerable: false, configurable: true }); /** * @stable [05.10.2018] * @returns {string} */ BaseFileField.prototype.getFieldClassName = function () { return util_1.ClsUtils.joinClassName(_super.prototype.getFieldClassName.call(this), 'rac-file-field'); }; /** * @stable [14.10.2020] * @protected */ BaseFileField.prototype.getInputElementProps = function () { var attributes = _super.prototype.getInputElementProps.call(this); return __assign(__assign(__assign({}, attributes), { capture: util_1.NvlUtils.nvl(attributes.capture, this.environment.mobilePlatform ? 'environment' : 'user') }), (this.isNativeFileFieldUsed ? { className: util_1.ClsUtils.joinClassName(attributes.className, definition_1.ComponentClassesEnum.TRANSPARENT), type: 'file', } : {})); }; /** * @stable [19.10.2020] * @param value * @protected */ BaseFileField.prototype.decorateDisplayValue = function (value) { if (this.isNativeFileFieldUsed) { return definition_1.FieldConstants.DISPLAY_EMPTY_VALUE; } var len = this.multiFieldPlugin.getActiveValueLength(value); return this.buildDisplayMessage(len > 0, len); }; /** * @stable [11.05.2020] */ BaseFileField.prototype.onVideoDialogDeactivate = function () { this.setState({ $$videoOpened: false }); }; /** * @stable [11.05.2020] */ BaseFileField.prototype.onVideoDialogAccept = function () { this.video.capture(); }; /** * @stable [19.10.2020] * @param files * @private */ BaseFileField.prototype.onSelect = function (files) { return __awaiter(this, void 0, void 0, function () { var multi, file; return __generator(this, function (_a) { switch (_a.label) { case 0: multi = this.originalProps.multi; file = files[0]; if (!!multi) return [3 /*break*/, 2]; return [4 /*yield*/, this.clearValue()]; case 1: _a.sent(); _a.label = 2; case 2: return [4 /*yield*/, this.doAddBlob(file)]; case 3: _a.sent(); this.setFocus(); return [2 /*return*/]; } }); }); }; /** * @stable [19.10.2020] * @param blob * @private */ BaseFileField.prototype.doAddBlob = function (blob) { return __awaiter(this, void 0, void 0, function () { var url; return __generator(this, function (_a) { switch (_a.label) { case 0: url = util_1.UuidUtils.uuid(); return [4 /*yield*/, this.databaseStorage.set(url, blob)]; case 1: _a.sent(); this.doAddItem(url); return [2 /*return*/]; } }); }); }; /** * @stable [19.10.2020] * @param blob * @private */ BaseFileField.prototype.onVideoSnapshotSelect = function (blob) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.onSelect([blob])]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @stable [19.10.2020] * @private */ BaseFileField.prototype.openVideoDialog = function () { var _this = this; this.setState({ $$videoOpened: true }, function () { return _this.videoDialog.activate(); }); }; /** * @stable [07.11.2020] * @private */ BaseFileField.prototype.onCameraActionClick = function () { util_1.ConditionUtils.ifNotNilThanValue(this.originalProps.onCamera, function (onCamera) { return onCamera(); }); }; /** * @stable [19.10.2020] * @private */ BaseFileField.prototype.onAttachFileClick = function () { if (this.isNativeFileFieldUsed) { this.input.click(); } else { this.dnd.open(); } }; /** * @stable [19.10.2020] * @param id * @private */ BaseFileField.prototype.clearValueById = function (id) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.databaseStorage.remove(id)]; case 1: _a.sent(); this.multiFieldPlugin.onDeleteItem({ id: id }); return [2 /*return*/]; } }); }); }; /** * @stable [13.12.2020] * @private */ BaseFileField.prototype.downloadFile = function () { return __awaiter(this, void 0, void 0, function () { var entities, blobs, fileName, detectFileType; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: entities = this.fieldConverter .fromNotMultiFieldValueToEntities(this.fieldConverter .fromMultiFieldValueToDefinedEntities(this.value)); return [4 /*yield*/, Promise.all(entities .map(function (entity) { return _this.databaseStorage.get(entity.id); }))]; case 1: blobs = _a.sent(); fileName = this.originalProps.fileName; detectFileType = this.mergedProps.detectFileType; return [2 /*return*/, Promise.all(blobs.map(function (data, index) { var url = entities[index].id; var actualFileName = util_1.NvlUtils.nvl(fileName, url); return R.isNil(data) ? _this.domAccessor.downloadFile({ url: url, fileName: actualFileName, detectFileType: detectFileType }) : _this.domAccessor.downloadFileByBlob({ data: data, fileName: actualFileName, detectFileType: detectFileType }); }))]; } }); }); }; Object.defineProperty(BaseFileField.prototype, "isCameraActionDisabled", { /** * @stable [19.10.2020] * @private */ get: function () { return this.originalProps.cameraActionDisabled; }, enumerable: false, configurable: true }); Object.defineProperty(BaseFileField.prototype, "isNativeFileFieldUsed", { /** * @stable [19.10.2020] * @private */ get: function () { return this.environment.mobilePlatform; }, enumerable: false, configurable: true }); Object.defineProperty(BaseFileField.prototype, "video", { /** * @stable [20.10.2020] * @private */ get: function () { return this.videoRef.current; }, enumerable: false, configurable: true }); Object.defineProperty(BaseFileField.prototype, "videoDialog", { /** * @stable [20.10.2020] * @private */ get: function () { return this.videoDialogRef.current; }, enumerable: false, configurable: true }); Object.defineProperty(BaseFileField.prototype, "dnd", { /** * @stable [20.10.2020] * @private */ get: function () { return this.dndRef.current; }, enumerable: false, configurable: true }); BaseFileField.defaultProps = util_1.PropsUtils.mergeWithParentDefaultProps({}, base_text_field_component_1.BaseTextField); BaseFileField.logger = ts_smart_logger_1.LoggerFactory.makeLogger('BaseFileField'); return BaseFileField; }(base_text_field_component_1.BaseTextField)); exports.BaseFileField = BaseFileField; //# sourceMappingURL=base-file-field.component.js.map