kitchen-simulator
Version:
It is a kitchen simulator (self-contained micro-frontend).
358 lines (345 loc) • 15 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof3 = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.animateDoor = void 0;
exports.applyEdges = applyEdges;
exports.base64Decode = void 0;
exports.debugUtil = debugUtil;
exports.getSignedUrl = exports.getPathInfo = exports.getPath = void 0;
exports.handleCamRect = handleCamRect;
exports.isCeilLimitation = isCeilLimitation;
exports.isElevationView = isElevationView;
exports.isEmpty = void 0;
exports.isImmutable = isImmutable;
exports.isValidNumber = isValidNumber;
exports.refineProductForSummaryPdf = exports.refineMoldingForSummaryPdf = void 0;
exports.replaceMeshesWithLineSegments = replaceMeshesWithLineSegments;
exports.textureCube = void 0;
exports.toPlainObject = toPlainObject;
exports.translateDrawer = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _HDRCubeTextureLoader = require("three/examples/jsm/loaders/HDRCubeTextureLoader.js");
var _constants = require("../constants");
var Three = _interopRequireWildcard(require("three"));
var _utils = require("./../components/viewer2d/utils");
var _convertUnitsLite = require("./convert-units-lite");
var _math = require("./math");
var _awsSdk = _interopRequireDefault(require("aws-sdk"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof3(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var s3 = new _awsSdk["default"].S3({
accessKeyId: process.env.REACT_APP_AWS_ID,
secretAccessKey: process.env.REACT_APP_AWS_SECRET
});
var s3ParseUrl = function s3ParseUrl(url) {
var _decodedUrl = decodeURIComponent(url);
var _result = null;
// http://s3.amazonaws.com/bucket/key1/key2
var match = decodedUrl.match(/^https?:\/\/s3.amazonaws.com\/([^\/]+)\/?(.*?)$/);
if (_match) {
_result = {
bucket: _match[1],
key: _match[2],
region: ''
};
}
// http://s3-aws-region.amazonaws.com/bucket/key1/key2
match = decodedUrl.match(/^https?:\/\/s3-([^.]+).amazonaws.com\/([^\/]+)\/?(.*?)$/);
if (_match) {
_result = {
bucket: _match[2],
key: _match[3],
region: _match[1]
};
}
// http://bucket.s3.amazonaws.com/key1/key2
match = decodedUrl.match(/^https?:\/\/([^.]+).s3.amazonaws.com\/?(.*?)$/);
if (_match) {
_result = {
bucket: _match[1],
key: _match[2],
region: ''
};
}
// http://bucket.s3-aws-region.amazonaws.com/key1/key2 or,
// http://bucket.s3.aws-region.amazonaws.com/key1/key2
match = decodedUrl.match(/^https?:\/\/([^.]+).(?:s3-|s3\.)([^.]+).amazonaws.com\/?(.*?)$/);
if (_match) {
_result = {
bucket: _match[1],
key: _match[3],
region: _match[2]
};
}
return _result;
};
var textureCube = exports.textureCube = new _HDRCubeTextureLoader.HDRCubeTextureLoader().setPath('/catalog/envMap/').load(_constants.HDR_URLS, function () {
textureCube.magFilter = Three.LinearFilter;
textureCube.needsUpdate = true;
});
var getSignedUrl = exports.getSignedUrl = function getSignedUrl(url) {
if (!url) return url;
var signedUrl = url;
try {
var _s3ParseUrl = s3ParseUrl(url),
bucket = _s3ParseUrl.bucket,
key = _s3ParseUrl.key;
signedUrl = s3.getSignedUrl('getObject', {
Bucket: bucket,
Key: key,
ResponseCacheControl: 'no-cache'
});
} catch (e) {
signedUrl = url;
}
return signedUrl;
};
var base64Decode = exports.base64Decode = function base64Decode(targetStr) {
if (!targetStr) return null;
try {
// If URL-encoded, decode first
var base64Str = targetStr.includes('%') ? decodeURIComponent(targetStr) : targetStr;
var binaryStr = atob(base64Str);
// Convert binary string to UTF-8 string
return decodeURIComponent(binaryStr.split('').map(function (_char) {
return "%".concat(_char.charCodeAt(0).toString(16).padStart(2, '0'));
}).join(''));
} catch (e) {
console.log('Invalid Base64 or UTF-8 string: ', e);
return null;
}
};
var refineProductForSummaryPdf = exports.refineProductForSummaryPdf = function refineProductForSummaryPdf(refinedProductList, catalog, categoryData, itemSKU, suffix, item, product, measurementUnit, count) {
var _doorColor;
var review;
var doorColor = item.doorStyle;
if (doorColor.size) {
doorColor = doorColor.toJS();
}
var doorStyleName = (_doorColor = doorColor) === null || _doorColor === void 0 ? void 0 : _doorColor.name;
if (catalog !== undefined) {
var _doorColor2, _doorColor4;
if (!((_doorColor2 = doorColor) !== null && _doorColor2 !== void 0 && _doorColor2.install)) {
categoryData.data.doorStyles.items.forEach(function (category) {
category.items.forEach(function (element) {
if (element.items.filter(function (it) {
var _doorColor3;
return it.id === ((_doorColor3 = doorColor) === null || _doorColor3 === void 0 ? void 0 : _doorColor3.id);
})) {
doorColor = _objectSpread(_objectSpread({}, doorColor), {}, {
install: category.name
});
}
});
});
}
review = catalog.elements[item.type];
if (!review) {
review = catalog.elements[(0, _utils.returnReplaceableDeepSearchType)(item.type)];
}
if (((_doorColor4 = doorColor) === null || _doorColor4 === void 0 || (_doorColor4 = _doorColor4.install) === null || _doorColor4 === void 0 ? void 0 : _doorColor4.toLowerCase()) === 'pre-assembled') {
itemSKU += '-assembled';
} else {
itemSKU += '-rta';
}
var refinedData = {
thumbnail: review.img,
sku: itemSKU,
sizeInfo: {
width: Math.round((0, _convertUnitsLite.convert)(review.info.sizeinfo.width).from('in').to(measurementUnit._unit) * 100) / 100,
depth: Math.round((0, _convertUnitsLite.convert)(review.info.sizeinfo.depth).from('in').to(measurementUnit._unit) * 100) / 100,
height: Math.round((0, _convertUnitsLite.convert)(review.info.sizeinfo.height).from('in').to(measurementUnit._unit) * 100) / 100,
_unit: measurementUnit._unit
},
doorstyle: doorStyleName,
installation: suffix,
subTotal: product && product.discounted_price ? product && Number(product.discounted_price * (count || 1)) : 0
};
var index = (refinedProductList || []).findIndex(function (row) {
return row.sku === itemSKU;
});
if (index !== -1) {
refinedProductList[index] = refinedData;
} else {
refinedProductList.push(refinedData);
}
}
};
var refineMoldingForSummaryPdf = exports.refineMoldingForSummaryPdf = function refineMoldingForSummaryPdf(refinedProductList, molding, product, newSKU) {
if (molding) {
var review = {
img: molding.thumbnail,
name: molding.name,
count: molding.count
};
var subTotalPrice = product && product.discounted_price ? (0, _math.formatNumber)(product.discounted_price * review.count, _constants.DECIMAL_PLACES_2) : 0;
var refinedData = {
id: newSKU,
thumbnail: review.img,
sku: review.name,
sizeInfo: null,
subTotal: Number(subTotalPrice)
};
var index = (refinedProductList || []).findIndex(function (row) {
return row.sku === newSKU;
});
if (index !== -1) {
refinedProductList[index] = refinedData;
} else {
refinedProductList.push(refinedData);
}
}
};
function applyEdges(object) {
if (object && object.isMesh && !(object instanceof Three.LineSegments)) {
var parent = object.parent;
if (!isEmpty(parent)) {
if (object.name.match('interior_drawer') || object.name.match('interior') || object.name.match('soul')) {
parent.remove(object);
} else {
// Create a replacement
var edges = new Three.EdgesGeometry(object.geometry, 10);
var lineMaterial = new Three.LineBasicMaterial({
color: 0x000000,
depthTest: true,
depthFunc: Three.LessEqualDepth,
depthWrite: false
});
var line = new Three.LineSegments(edges, lineMaterial);
line.position.copy(object.position);
line.scale.copy(object.scale);
line.rotation.copy(object.rotation);
line.name = object.name;
// Replace in parent
parent.remove(object);
parent.add(line);
}
}
}
}
function replaceMeshesWithLineSegments(object) {
if (object && object.children.length > 0) {
for (var i = object.children.length - 1; i >= 0; i--) {
var child = object.children[i];
// Recursively process children first
replaceMeshesWithLineSegments(child);
}
} else {
//
// Check if the child is a THREE.Mesh
applyEdges(object);
}
}
var isEmpty = exports.isEmpty = function isEmpty(value) {
return value === undefined || value === null || typeof value === 'array' && value.length === 0 || (0, _typeof2["default"])(value) === 'object' && Object.keys(value).length === 0 || typeof value === 'string' && value.trim().length === 0;
};
// animate the Opening or Closing Door action
var animateDoor = exports.animateDoor = function animateDoor(offsetData, doorObj, open_doors, item, isCorner, mode) {
var offset = new Three.Vector3(offsetData.x, offsetData.y, offsetData === null || offsetData === void 0 ? void 0 : offsetData.z);
doorObj.translateOnAxis(offset, 1);
var rotation = doorObj.name.match(/\d_(door[^LR1-9]*)(_[LR])?(_[1-9])?$/)[2] === '_L' ? -_constants.ANIMATE_OBJECT_OPEN_DOOR_ROTATION_UNIT : _constants.ANIMATE_OBJECT_OPEN_DOOR_ROTATION_UNIT;
var isflip = item.properties.flip_doorhandle;
rotation = isflip ? -rotation : rotation;
if (isCorner == 1) {
if (!doorObj.name.includes('base_end')) {
rotation *= 2;
}
var tmp = doorObj.children[0].children.filter(function (item) {
return item.children.length != 0;
});
if (tmp.length != 0) {
rotation = open_doors ? -rotation : rotation;
} else {
rotation = open_doors ? rotation : -rotation;
}
} else {
rotation = open_doors ? rotation : -rotation;
}
if (mode === 'Opened2D') rotation *= _constants.ANIMATE_STEP_MAX; // Check whether the object's door is opened in 2D.
doorObj.rotateY(rotation);
doorObj.translateOnAxis(offset, -1);
};
// Opening or Closing the drawer action
var translateDrawer = exports.translateDrawer = function translateDrawer(item, drawerObj, open_doors, mode) {
var animate_translate_unit = (0, _convertUnitsLite.convert)(item.properties.depth._length).from(item.properties.depth._unit).to('cm') * (1 / 2) / _constants.ANIMATE_STEP_MAX * 0.01;
// Check whether the object's drawer is opened in 2D.
if (mode === 'Opened2D') animate_translate_unit *= _constants.ANIMATE_STEP_MAX;
drawerObj.translateZ(open_doors ? -animate_translate_unit : animate_translate_unit);
};
var getPath = exports.getPath = function getPath() {
return window.location.pathname;
};
var getPathInfo = exports.getPathInfo = function getPathInfo(num) {
var paths = window.location.pathname.split('/');
if (paths.length >= num + 1) {
return paths[num];
} else {
return paths[num - 1 >= 0 ? num - 1 : 0];
}
};
// determine if elevation mode or not
function isElevationView(mode) {
if (mode === _constants.MODE_ELEVATION_VIEW) return true;else return false;
}
// calculate camera rect in elevation mode
/**
*
* @param {number} width width of elevation view - canvas
* @param {number} height height of elevation view - canvas
* @param {number} ceilHeight ceiling Height
* @param {number} lineLength wall's length
* @returns {object}
*/
//** elevation view (canvas) **//
//// h /////////////////////// width //////////////////////// h ////
//// e //// **c***************lineLength*************c** //// e ////
//// i //// **e e** //// i ////
//// g //// **i i** //// g ////
//// h //// **l***************lineLength*************l** //// h ////
//// t /////////////////////// width //////////////////////// t ////
function handleCamRect(width, height, ceilHeight, lineLength) {
var scale = Math.min(width * 0.6 / lineLength, height * 0.6 / ceilHeight); // scale is min value
var cameraRect = {
width: width / scale,
height: height / scale
};
return cameraRect;
}
/**
*
* @param {*} layer
* @param {*} ceilHeight
* @param {*} ceilUnit
* @returns true ceilingHeight is bigger than totalHeight(item's height + altitude), otherwise false
*/
function isCeilLimitation(layer, ceilHeight, ceilUnit) {
var items = layer.getIn(['layers', layer.selectedLayer, 'items']);
return items.some(function (item) {
var height = item.properties.getIn(['height', '_length']);
var heightUnit = item.properties.getIn(['height', '_unit']);
var altitude = item.properties.getIn(['altitude', '_length']);
var altitudeUnit = item.properties.getIn(['altitude', '_unit']);
var totalHeight = (0, _convertUnitsLite.convert)(height).from(heightUnit).to(ceilUnit._unit) + (0, _convertUnitsLite.convert)(altitude).from(altitudeUnit).to(ceilUnit._unit);
return totalHeight > ceilHeight;
});
}
function isValidNumber(n) {
return !isNaN(n) && isFinite(n);
}
function toPlainObject(maybeImmutable) {
if (maybeImmutable && typeof maybeImmutable.toJS === 'function') {
return maybeImmutable.toJS();
}
return maybeImmutable; // already a plain JS object
}
function isImmutable(maybeImmutable) {
return !!(maybeImmutable && typeof maybeImmutable.toJS === 'function');
}
function debugUtil() {
return new Date();
}