react-application-core
Version:
A react-based application core for the business applications.
323 lines • 12.8 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlaceField = void 0;
var React = require("react");
var R = require("ramda");
var util_1 = require("../../../util");
var base_select_component_1 = require("../select/base-select.component");
var dialog_1 = require("../../dialog");
var google_1 = require("../../google");
var definition_1 = require("../../../definition");
var PlaceField = /** @class */ (function (_super) {
__extends(PlaceField, _super);
/**
* @stable [09.01.2020]
* @param {IPlaceFieldProps} props
*/
function PlaceField(props) {
var _this = _super.call(this, props) || this;
_this.dialogRef = React.createRef();
_this.googleMapsRef = React.createRef();
_this.googleMapsMenuOptions = [{
label: _this.settings.messages.PUT_MARKER_HERE,
value: definition_1.PlaceMarkerActionsEnum.PUT_MARKER,
}];
_this.initPlaceMarker = _this.initPlaceMarker.bind(_this);
_this.onDialogAccept = _this.onDialogAccept.bind(_this);
_this.onDialogDeactivate = _this.onDialogDeactivate.bind(_this);
_this.onDialogMenuActionSelect = _this.onDialogMenuActionSelect.bind(_this);
_this.openDialog = _this.openDialog.bind(_this);
_this.refreshGeocodeInfo = _this.refreshGeocodeInfo.bind(_this);
if (util_1.isPlaceActionRendered(props)) {
_this.defaultActions = R.insert(_this.isExpandActionRendered ? 1 : 0, { type: 'location_on', onClick: _this.openDialog }, _this.defaultActions);
}
return _this;
}
/**
* @stable [09.01.2020]
*/
PlaceField.prototype.componentWillUnmount = function () {
this.cancelPlaceGeoCodeTask();
_super.prototype.componentWillUnmount.call(this);
};
/**
* @stable [11.08.2020]
* @param option
* @protected
*/
PlaceField.prototype.onSelect = function (option) {
this.refreshGeocodeInfo({ placeId: this.fromSelectValueToId(option) }, option);
};
/**
* @stable [11.01.2020]
* @param {PlaceEntityValueT} value
* @returns {string}
*/
PlaceField.prototype.decorateDisplayValue = function (value) {
return this.useZipCode
? this.zipCodeEntityAsDisplayValue(value)
: this.fromPlaceEntityToDisplayValue(value);
};
Object.defineProperty(PlaceField.prototype, "attachmentElement", {
/**
* @stable [09.01.2020]
* @returns {JSX.Element}
*/
get: function () {
var _this = this;
var _a = this.state, dialogOpened = _a.dialogOpened, placeEntity = _a.placeEntity;
var googleMapsConfiguration = this.mergedProps.googleMapsConfiguration;
return util_1.ConditionUtils.orNull(dialogOpened, // To improve a performance
function () {
return (React.createElement(dialog_1.Dialog, { ref: _this.dialogRef, scrollable: false, title: _this.settings.messages.ADDRESS_SELECTION, acceptDisabled: !_this.isPlaceChanged, onDeactivate: _this.onDialogDeactivate, onAccept: _this.onDialogAccept, className: util_1.ClsUtils.joinClassName(_this.props.dialogClassName, definition_1.DialogClassesEnum.PLACE_DIALOG) },
util_1.ConditionUtils.ifNotNilThanValue(_this.fromPlaceEntityToDisplayValue(util_1.nvl(placeEntity, _this.value)), function (value) { return (React.createElement("div", { className: definition_1.DialogClassesEnum.PLACE_DIALOG_TITLE }, value)); }),
React.createElement(google_1.GoogleMaps, __assign({}, googleMapsConfiguration, { ref: _this.googleMapsRef, menuOptions: _this.googleMapsMenuOptions, onSelect: _this.onDialogMenuActionSelect, onInit: _this.initPlaceMarker, onMarkerDragEnd: _this.refreshGeocodeInfo }))));
});
},
enumerable: false,
configurable: true
});
/**
* @stable [09.01.2020]
* @returns {string}
*/
PlaceField.prototype.getFieldClassName = function () {
return util_1.ClsUtils.joinClassName(_super.prototype.getFieldClassName.call(this), 'rac-place-field');
};
/**
* @stable [11.01.2020]
* @param {IPlaceGeoCodeRequestEntity} geoCodeRequest
* @param {IPlaceSelectOptionEntity} option
*/
PlaceField.prototype.refreshGeocodeInfo = function (geoCodeRequest, option) {
var _this = this;
this.cancelPlaceGeoCodeTask();
this.setState({ progress: true }, function () {
_this.placeGeoCodeTask = _this.placeApi.getPlaceGeoCode(geoCodeRequest)
.then(function (result) {
_this.setState({ progress: false });
if (util_1.ObjectUtils.isObjectNotEmpty(result)) {
_this.onGeoCodeRequestDone(result, option);
}
return result;
}, function (error) {
_this.setState({ progress: false });
return error;
});
});
};
/**
* @stable [11.01.2020]
* @param {IPlaceEntity[]} result
* @param {IPlaceSelectOptionEntity} option
*/
PlaceField.prototype.onGeoCodeRequestDone = function (result, option) {
var placeEntity = result[0];
if (R.isNil(option)) {
// To preview in a dialog
this.setState({ placeEntity: placeEntity });
}
else {
// The selection via a select component
this.doSelect(placeEntity, option);
}
};
/**
* @stable [29.01.2020]
* @param {IPlaceEntity} placeEntity
* @param {IPlaceSelectOptionEntity} option
*/
PlaceField.prototype.doSelect = function (placeEntity, option) {
var payload;
if (this.isPlainValueApplied) {
payload = this.useZipCode
? this.zipCodeEntityAsDisplayValue(placeEntity)
: this.fromPlaceEntityToDisplayValue(placeEntity);
}
else {
payload = placeEntity;
}
this.onChangeManually(payload);
this.notifySelectOption(option);
};
/**
* @stable [09.01.2020]
* @param {IGoogleMapsMenuItemEntity} payload
*/
PlaceField.prototype.onDialogMenuActionSelect = function (payload) {
switch (payload.item.value) {
case definition_1.PlaceMarkerActionsEnum.PUT_MARKER:
this.googleMaps.refreshMarker({
marker: PlaceField.PLACE_MARKER,
refreshMap: false,
lat: payload.lat,
lng: payload.lng,
});
this.refreshGeocodeInfo(payload);
break;
}
};
/**
* @stable [11.01.2020]
*/
PlaceField.prototype.onDialogAccept = function () {
var _this = this;
this.setState({ dialogOpened: false }, function () { return _this.doSelect(_this.state.placeEntity); });
};
/**
* @stable [09.01.2020]
*/
PlaceField.prototype.onDialogDeactivate = function () {
this.setState({ dialogOpened: false });
};
/**
* @stable [09.01.2020]
*/
PlaceField.prototype.openDialog = function () {
var _this = this;
this.setState({ dialogOpened: true, placeEntity: null }, function () { return _this.dialog.activate(); });
};
/**
* @stable [09.01.2020]
*/
PlaceField.prototype.cancelPlaceGeoCodeTask = function () {
var _this = this;
util_1.ConditionUtils.ifNotNilThanValue(this.placeGeoCodeTask, function (promise) {
if (promise.isPending()) {
promise.cancel();
}
_this.placeGeoCodeTask = null;
});
};
/**
* @stable [03.04.2020]
*/
PlaceField.prototype.initPlaceMarker = function () {
this.googleMaps.addMarker({ draggable: true, position: null }, PlaceField.PLACE_MARKER);
var _a = this.valueAsPlaceEntity || {}, lat = _a.lat, lng = _a.lng;
var initialCfg = { marker: PlaceField.PLACE_MARKER };
this.googleMaps.refreshMarker(__assign(__assign({}, initialCfg), (!R.isNil(lat) && !R.isNil(lng)
? {
lat: lat,
lng: lng,
refreshMap: true,
zoom: this.settings.googleMaps.prettyZoom,
}
: { visible: false, refreshMap: !this.hasMapInitialMarkers })));
};
/**
* @stable [17.05.2020]
* @param {PlaceEntityValueT} placeEntity
* @returns {string}
*/
PlaceField.prototype.fromPlaceEntityToDisplayValue = function (placeEntity) {
return this.fieldConverter.fromPlaceEntityToDisplayValue(placeEntity);
};
/**
* @stable [28.01.2020]
* @param {PlaceEntityValueT} placeEntity
* @returns {string}
*/
PlaceField.prototype.zipCodeEntityAsDisplayValue = function (placeEntity) {
return this.fieldConverter.convert({
from: definition_1.FieldConverterTypesEnum.ZIP_CODE_ENTITY,
to: definition_1.FieldConverterTypesEnum.DISPLAY_VALUE,
value: placeEntity,
});
};
Object.defineProperty(PlaceField.prototype, "isPlaceChanged", {
/**
* @stable [09.01.2020]
* @returns {boolean}
*/
get: function () {
return !R.isNil(this.state.placeEntity);
},
enumerable: false,
configurable: true
});
Object.defineProperty(PlaceField.prototype, "valueAsPlaceEntity", {
/**
* @stable [03.04.2020]
* @returns {IPlaceEntity}
*/
get: function () {
return this.value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(PlaceField.prototype, "dialog", {
/**
* @stable [09.10.2020]
*/
get: function () {
return this.dialogRef.current;
},
enumerable: false,
configurable: true
});
Object.defineProperty(PlaceField.prototype, "googleMaps", {
/**
* @stable [09.01.2020]
* @returns {IGoogleMaps}
*/
get: function () {
return this.googleMapsRef.current;
},
enumerable: false,
configurable: true
});
Object.defineProperty(PlaceField.prototype, "useZipCode", {
/**
* @stable [11.01.2020]
* @returns {boolean}
*/
get: function () {
return util_1.isUseZipCode(this.props);
},
enumerable: false,
configurable: true
});
Object.defineProperty(PlaceField.prototype, "hasMapInitialMarkers", {
/**
* @stable [03.04.2020]
* @returns {boolean}
*/
get: function () {
var _a = this.props.googleMapsConfiguration, googleMapsConfiguration = _a === void 0 ? {} : _a;
return util_1.ObjectUtils.isObjectNotEmpty(googleMapsConfiguration.initialMarkers);
},
enumerable: false,
configurable: true
});
PlaceField.defaultProps = util_1.PropsUtils.mergeWithParentDefaultProps(definition_1.DefaultEntities.PLACE_FIELD_ENTITY, base_select_component_1.BaseSelect);
PlaceField.PLACE_MARKER = util_1.uuid();
return PlaceField;
}(base_select_component_1.BaseSelect));
exports.PlaceField = PlaceField;
//# sourceMappingURL=place-field.component.js.map