react-application-core
Version:
A react-based application core for the business applications.
241 lines • 11.5 kB
JavaScript
"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 };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewField = void 0;
var React = require("react");
var R = require("ramda");
var BPromise = require("bluebird");
var util_1 = require("../../../util");
var field_component_1 = require("../field/field.component");
var definition_1 = require("../../../definition");
var multifield_support_1 = require("../multifield/multifield.support");
var ViewField = /** @class */ (function (_super) {
__extends(ViewField, _super);
/**
* @stable [16.10.2020]
* @param originalProps
*/
function ViewField(originalProps) {
var _this = _super.call(this, originalProps) || this;
_this.state = { url: null, type: null };
return _this;
}
/**
* @stable [29.07.2019]
*/
ViewField.prototype.componentWillUnmount = function () {
_super.prototype.componentWillUnmount.call(this);
this.doClear();
};
/**
* @stable [16.10.2020]
*/
ViewField.prototype.componentDidMount = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
_super.prototype.componentDidMount.call(this);
this.doRefresh();
return [2 /*return*/];
});
});
};
/**
* @stable [16.10.2020]
* @param prevProps
* @param prevState
*/
ViewField.prototype.componentDidUpdate = function (prevProps, prevState) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
_super.prototype.componentDidUpdate.call(this, prevProps, prevState);
this.doRefresh();
return [2 /*return*/];
});
});
};
Object.defineProperty(ViewField.prototype, "attachmentBodyElement", {
/**
* @stable [16.10.2020]
* @protected
*/
get: function () {
var _a = this.originalProps, usePreview = _a.usePreview, viewer = _a.viewer, viewerClassName = _a.viewerClassName, viewerConfiguration = _a.viewerConfiguration;
var _b = this.state, type = _b.type, url = _b.url;
var $viewer = type === definition_1.MimeTypesEnum.APPLICATION_PDF
? definition_1.ViewersEnum.PDF
: viewer;
var Component = this.viewerLocator.resolve($viewer);
return (React.createElement(Component, __assign({}, viewerConfiguration, { className: util_1.ClsUtils.joinClassName(viewerClassName, definition_1.ViewFieldClassesEnum.VIEW_FIELD_VIEWER), src: url, usePreview: usePreview })));
},
enumerable: false,
configurable: true
});
/**
* @stable [16.10.2020]
* @protected
*/
ViewField.prototype.getFieldClassName = function () {
return util_1.ClsUtils.joinClassName(_super.prototype.getFieldClassName.call(this), definition_1.ViewFieldClassesEnum.VIEW_FIELD);
};
/**
* stable [19.09.2019]
*/
ViewField.prototype.doRefresh = function () {
var _this = this;
var currentValue = this.currentValue;
var previousValue = this.previousValue;
if (R.equals(currentValue, previousValue)) {
return;
}
this.previousValue = currentValue;
this.doClear();
if (R.isNil(currentValue)) {
return;
}
/**
* https://ru.reactjs.org/blog/2015/12/16/ismounted-antipattern.html
*
* The primary use case for isMounted() is to avoid calling setState() after a component has unmounted,
* because calling setState() after a component has unmounted will emit a warning. The «setState warning»
* exists to help you catch bugs, because calling setState() on an unmounted component is an indication
* that your app/component has somehow failed to clean up properly. Specifically, calling setState() in
* an unmounted component means that your app is still holding a reference to the component after the
* component has been unmounted — which often indicates a memory leak!
*/
// This is a cancellable async task
this.detectorFileTypeTask = new BPromise(function (resolve) {
// This is not a cancellable async task
return _this.getFileInfo(currentValue).then(resolve);
});
this.detectorFileTypeTask.then(function (payload) { return _this.setState(payload); });
};
Object.defineProperty(ViewField.prototype, "currentValue", {
/**
* @stable [29.07.2019]
* @returns {string}
*/
get: function () {
return multifield_support_1.toLastAddedMultiItemEntityId(this.value);
},
enumerable: false,
configurable: true
});
/**
* @stable [19.09.2019]
*/
ViewField.prototype.doClear = function () {
if (!R.isNil(this.detectorFileTypeTask)) {
this.detectorFileTypeTask.cancel();
this.detectorFileTypeTask = null;
}
var url = this.state.url;
if (!R.isNil(url)) {
URL.revokeObjectURL(url);
this.setState({ url: null, type: null });
}
};
/**
* @stable [19.09.2019]
* @param {string} currentValue
* @returns {Promise<IViewFieldState>}
*/
ViewField.prototype.getFileInfo = function (currentValue) {
return __awaiter(this, void 0, void 0, function () {
var _a, detectFileTypeTransportConfiguration, detectFileType, file, url, type, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_a = this.props, detectFileTypeTransportConfiguration = _a.detectFileTypeTransportConfiguration, detectFileType = _a.detectFileType;
if (!util_1.UrlUtils.isAbsoluteURI(currentValue)) return [3 /*break*/, 4];
url = currentValue;
if (!detectFileType) return [3 /*break*/, 3];
_c = (_b = util_1.BlobUtils).detectBlobMimeType;
return [4 /*yield*/, this.transport.request(__assign(__assign({ blobResponse: true, method: definition_1.TransportMethodsEnum.GET, noCache: true, noAuth: true }, detectFileTypeTransportConfiguration), { url: currentValue }))];
case 1: return [4 /*yield*/, _c.apply(_b, [_d.sent()])];
case 2:
type = _d.sent();
_d.label = 3;
case 3: return [3 /*break*/, 7];
case 4: return [4 /*yield*/, this.databaseStorage.get(currentValue)];
case 5:
file = _d.sent();
if (!!R.isNil(file)) return [3 /*break*/, 7];
url = URL.createObjectURL(file); // No memory leak! Will be clear later (see doClear)
if (!detectFileType) return [3 /*break*/, 7];
return [4 /*yield*/, util_1.BlobUtils.detectBlobMimeType(file)];
case 6:
type = _d.sent();
_d.label = 7;
case 7: return [2 /*return*/, util_1.FilterUtils.defValuesFilter({ url: url, type: type })];
}
});
});
};
ViewField.defaultProps = {
errorMessageRendered: false,
fieldRendered: false,
viewer: definition_1.ViewersEnum.PICTURE,
};
return ViewField;
}(field_component_1.Field));
exports.ViewField = ViewField;
//# sourceMappingURL=view-field.component.js.map