react-application-core
Version:
A react-based application core for the business applications.
361 lines • 16.6 kB
JavaScript
"use strict";
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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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.UiFactory = void 0;
var React = require("react");
var R = require("ramda");
var inversify_1 = require("inversify");
var ts_smart_logger_1 = require("ts-smart-logger");
var di_1 = require("../../../di");
var definition_1 = require("../../../definition");
var util_1 = require("../../../util");
var UiFactory = /** @class */ (function () {
/**
* @stable [07.10.2019]
*/
function UiFactory() {
this.onRestartAndReload = this.onRestartAndReload.bind(this);
}
UiFactory_1 = UiFactory;
/**
* @stable [17.12.2020]
* @param cfg
*/
UiFactory.prototype.makeIcon = function (cfg) {
if (R.isNil(cfg)) {
return cfg;
}
var config = this.toIconConfig(cfg);
var disabled = util_1.CalcUtils.calc(config.disabled);
var className = util_1.CalcUtils.calc(config.className);
var onClick = config.onClick, ref = config.ref, title = config.title, touched = config.touched, type = config.type;
return (React.createElement("div", __assign({ ref: ref, title: title, className: util_1.ClsUtils.joinClassName(definition_1.IconClassesEnum.ICON, util_1.TypeUtils.isFn(onClick) && (util_1.ClsUtils.joinClassName(definition_1.IconClassesEnum.ACTION_ICON, "rac-action-" + type + "-icon")), disabled && definition_1.IconClassesEnum.DISABLED_ICON, className) }, util_1.PropsUtils.buildClickHandlerProps(onClick, !disabled, util_1.NvlUtils.nvl(touched, false))), this.uiIconFactory.makeIcon(config)));
};
/**
* @stable [28.11.2019]
* @param {IUiMessageConfigEntity} cfg
* @returns {React.ReactNode}
*/
UiFactory.prototype.makeMessage = function (cfg) {
var message = cfg.message, _a = cfg.wrapper, wrapper = _a === void 0 ? true : _a;
var body = (React.createElement("div", { className: util_1.ClsUtils.joinClassName.apply(util_1.ClsUtils, __spreadArrays(this.getMessageBodyClassNames(), [cfg.className])) }, message));
if (!wrapper) {
return body;
}
return (React.createElement("div", { className: util_1.ClsUtils.joinClassName.apply(util_1.ClsUtils, __spreadArrays(this.getMessageClassNames(), [cfg.wrapperClassName])) }, body));
};
/**
* @stable [16.10.2019]
* @param {Error} e
* @returns {JSX.Element}
*/
UiFactory.prototype.makeWindowError = function (e) {
var _a, _b;
this.logError(definition_1.ErrorEventCategoriesEnum.WINDOW_ERROR, e);
UiFactory_1.logger.error('$[UiFactory][makeWindowError] Error:', e);
var el = this.domAccessor.getElement(UiFactory_1.WIN_ERROR_ID);
if (R.isNil(el)) {
var errorMessageWrapperEl = this.domAccessor.createElement();
var errorMessageEl = this.domAccessor.createElement('div', errorMessageWrapperEl);
errorMessageEl.id = UiFactory_1.WIN_ERROR_ID;
(_a = this.domAccessor).addClassNames.apply(_a, __spreadArrays([errorMessageWrapperEl], this.getErrorWrapperClassNames()));
(_b = this.domAccessor).addClassNames.apply(_b, __spreadArrays([errorMessageEl], this.getErrorClassNames()));
this.makeWindowErrorBodyElement(e, errorMessageEl);
return errorMessageWrapperEl;
}
else {
// Do nothing, because only one error is shown
return el.parentElement;
}
};
/**
* @stable [24.12.2020]
* @param e
*/
UiFactory.prototype.makeReactError = function (e) {
this.logError(definition_1.ErrorEventCategoriesEnum.REACT_ERROR, e);
UiFactory_1.logger.error('$[UiFactory][makeReactError] Error:', e);
return (React.createElement("div", { className: util_1.ClsUtils.joinClassName.apply(util_1.ClsUtils, this.getErrorWrapperClassNames()) },
React.createElement("div", { className: util_1.ClsUtils.joinClassName.apply(util_1.ClsUtils, this.getErrorClassNames()) }, this.makeReactErrorBodyElement(e))));
};
/**
* @stable [16.10.2019]
* @param {Error} e
* @param {Element} parent
*/
UiFactory.prototype.makeWindowErrorBodyElement = function (e, parent) {
this.makeWindowRestartActionElement(parent);
var content = this.domAccessor.createElement('div', parent);
content.textContent = this.buildErrorMessages(e).join('<br>');
};
/**
* @stable [07.10.2019]
* @param {Error} e
* @returns {JSX.Element}
*/
UiFactory.prototype.makeReactErrorBodyElement = function (e) {
return (React.createElement(React.Fragment, null,
this.makeReactRestartActionElement(),
this.getErrorMessagesElement(e)));
};
/**
* @stable [07.10.2019]
* @param {Error} error
* @returns {string[]}
*/
UiFactory.prototype.buildErrorMessages = function (error) {
var messages = this.settings.messages;
return [
messages.SOMETHING_WENT_WRONG,
messages.PLS_SEND_THIS_SCR_TO_SUPPORT_MANAGER,
this.errorSystemInfoLine,
messages.ERROR + ": " + error.message,
messages.DETAILS_INFO + ": [" + error.stack + "]"
];
};
Object.defineProperty(UiFactory.prototype, "errorSystemInfoLine", {
/**
* @stable [07.10.2019]
* @returns {string}
*/
get: function () {
var environment = this.environment;
var messages = this.settings.messages;
var user = this.store.getState().user;
return [
messages.ENVIRONMENT + ": ",
[
util_1.ifNotNilThanValue(user, function () { return messages.USER + " " + user.id; }),
messages.BUILD + " " + environment.appVersion,
environment.browserName + " " + environment.browserVersion + environment.platformType,
messages.PATH + " " + util_1.getCurrentUrlPath()
].join(', ')
].join('');
},
enumerable: false,
configurable: true
});
/**
* @stable [07.10.2019]
* @param {Error} e
* @returns {JSX.Element}
*/
UiFactory.prototype.getErrorMessagesElement = function (e) {
return (React.createElement(React.Fragment, null, this.buildErrorMessages(e).map(function (v, index) { return React.createElement("div", { key: "error-" + index }, v); })));
};
/**
* @stable [28.11.2019]
* @returns {string[]}
*/
UiFactory.prototype.getMessageClassNames = function () {
return [definition_1.UiFactoryClassesEnum.MESSAGE, definition_1.ComponentClassesEnum.FULL_SIZE, definition_1.ComponentClassesEnum.FIXED];
};
/**
* @stable [18.04.2020]
* @returns {string[]}
*/
UiFactory.prototype.getMessageBodyClassNames = function () {
return [definition_1.UiFactoryClassesEnum.MESSAGE_BODY, definition_1.ComponentClassesEnum.ALIGNMENT_CENTER];
};
/**
* @stable [07.10.2019]
* @returns {string[]}
*/
UiFactory.prototype.getErrorWrapperClassNames = function () {
return ['rac-window-error-wrapper', definition_1.ComponentClassesEnum.FULL_SIZE, definition_1.ComponentClassesEnum.FIXED];
};
/**
* @stable [07.10.2019]
* @returns {string[]}
*/
UiFactory.prototype.getErrorClassNames = function () {
return ['rac-window-error', definition_1.ComponentClassesEnum.ALIGNMENT_CENTER];
};
/**
* @stable [16.10.2019]
* @returns {JSX.Element}
*/
UiFactory.prototype.makeReactRestartActionElement = function () {
return (React.createElement("button", __assign({}, util_1.PropsUtils.buildClickHandlerProps(this.onRestartAndReload), { className: util_1.ClsUtils.joinClassName.apply(util_1.ClsUtils, this.getRestartActionClassName()) }), this.settings.messages.RESTART_APP));
};
/**
* @stable [16.10.2019]
* @param {Element} parent
* @returns {Element}
*/
UiFactory.prototype.makeWindowRestartActionElement = function (parent) {
var _a;
var actionEl = this.domAccessor.createElement('button', parent);
(_a = this.domAccessor).addClassNames.apply(_a, __spreadArrays([actionEl], this.getRestartActionClassName()));
actionEl.textContent = this.settings.messages.RESTART_APP;
actionEl.onclick = this.onRestartAndReload;
return actionEl;
};
/**
* @stable [07.10.2019]
* @returns {string[]}
*/
UiFactory.prototype.getRestartActionClassName = function () {
return ['rac-window-error-restart-action'];
};
/**
* In case of an out memory error, it would be better to reload the page
* @stable [16.10.2019]
*/
UiFactory.prototype.onRestartAndReload = function () {
return __awaiter(this, void 0, void 0, function () {
var ignored_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.router.go(-this.router.length);
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.onBeforeReload()];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
ignored_1 = _a.sent();
return [3 /*break*/, 4];
case 4:
this.domAccessor.reload(true);
return [2 /*return*/];
}
});
});
};
/**
* @stable [29.11.2019]
* @returns {Promise<void>}
*/
UiFactory.prototype.onBeforeReload = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.router.push(this.routes.logout);
return [2 /*return*/];
});
});
};
/**
* @stable [07.10.2019]
* @param {ErrorEventCategoriesEnum} errorCategory
* @param {Error} e
*/
UiFactory.prototype.logError = function (errorCategory, e) {
this.logManager.send(errorCategory, e.name, { message: e.message, stack: e.stack });
};
/**
* @stable [18.04.2020]
* @param {IIconConfigEntity | string} cfg
* @returns {IIconConfigEntity}
*/
UiFactory.prototype.toIconConfig = function (cfg) {
return (util_1.TypeUtils.isString(cfg) ? { type: cfg } : cfg);
};
var UiFactory_1;
UiFactory.logger = ts_smart_logger_1.LoggerFactory.makeLogger('UiFactory');
UiFactory.WIN_ERROR_ID = '$$windowErrorElement';
__decorate([
di_1.lazyInject(di_1.DI_TYPES.DomAccessor),
__metadata("design:type", Object)
], UiFactory.prototype, "domAccessor", void 0);
__decorate([
di_1.lazyInject(di_1.DI_TYPES.Environment),
__metadata("design:type", Object)
], UiFactory.prototype, "environment", void 0);
__decorate([
di_1.lazyInject(di_1.DI_TYPES.LogManager),
__metadata("design:type", Object)
], UiFactory.prototype, "logManager", void 0);
__decorate([
di_1.lazyInject(di_1.DI_TYPES.Router),
__metadata("design:type", Object)
], UiFactory.prototype, "router", void 0);
__decorate([
di_1.lazyInject(di_1.DI_TYPES.Routes),
__metadata("design:type", Object)
], UiFactory.prototype, "routes", void 0);
__decorate([
di_1.lazyInject(di_1.DI_TYPES.Settings),
__metadata("design:type", Object)
], UiFactory.prototype, "settings", void 0);
__decorate([
di_1.lazyInject(di_1.DI_TYPES.Store),
__metadata("design:type", Object)
], UiFactory.prototype, "store", void 0);
__decorate([
di_1.lazyInject(di_1.DI_TYPES.UiIconFactory),
__metadata("design:type", Object)
], UiFactory.prototype, "uiIconFactory", void 0);
UiFactory = UiFactory_1 = __decorate([
inversify_1.injectable(),
__metadata("design:paramtypes", [])
], UiFactory);
return UiFactory;
}());
exports.UiFactory = UiFactory;
//# sourceMappingURL=ui-factory.service.js.map