@ecomplus/utils
Version:
JS utility functions to E-Com Plus (not only) related apps
102 lines (101 loc) • 4.06 kB
JavaScript
;
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.symbol.iterator.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
var _config2 = _interopRequireDefault(require("./../lib/config"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
/**
* @method
* @memberof ecomUtils
* @name img
* @description Returns img object (with url and alt) from product body or pictures list.
* @param {Object.<string, *>|Array} product - Product body object or list of picture objects
* @param {string} [pictureId] - ObjectID of preferred picture to find in the list
* @param {string} [size=$ecomConfig.get('default_img_size')] - Preferred image size
* (generally `normal`) to find on picture object
* @returns {{ url: string; size?: string; alt?: string; }|undefined}
*
* @example
* const product = { 'pictures': [ { 'small': { 'url': 'https://ecom.com/imgs/100px/64gb.jpg'}, 'big': { 'url': 'https://ecom.com/imgs/700px/64gb.jpg' }, '_id': '694890155127368133600000' }, { 'small': { 'url': 'https://ecom.com/imgs/100px/e-5-64gb.jpg' }, 'big': { 'url': 'https://ecom.com/imgs/700px/e-5-64gb.jpg' }, '_id': '694890155127368133600001' }, { 'small': { 'url': 'https://ecom.com/imgs/100px/5-64gb.jpg' }, 'big': { 'url': 'https://ecom.com/imgs/700px/5-64gb.jpg' }, '_id': '694890155127368133600002' } ], 'name': 'Smartphone Xiaomi' }
* const id = '694890155127368133600001'
* const size = 'big'
* ecomUtils.img(product, id, size)
* // => {url: 'https://ecom.com/imgs/700px/e-5-64gb.jpg'}
*/
var img = function img(product, pictureId, size) {
if (!size) {
size = _config2.default.get('default_img_size') || 'normal';
}
if (product) {
// product object has 'pictures'
// cart item object has 'picture'
var pictures = product.pictures,
picture = product.picture;
if (!picture) {
if (!pictures) {
if (Array.isArray(product)) {
// received list of pictures ?
pictures = product;
} else {
// received picture object ?
picture = product;
}
}
}
if (Array.isArray(pictures)) {
// select one img object from product pictures
picture = pictureId && pictures.filter(function (pic) {
return pic._id === pictureId;
})[0] || pictures[0];
}
var _img;
if (_typeof(picture) === 'object' && picture !== null) {
_img = picture[size];
if (_img && _img.url) {
return _img;
}
// try predefined any size
var sizes;
switch (size) {
case 'small':
sizes = ['normal', 'big'];
break;
case 'normal':
sizes = ['big', 'zoom', 'small'];
break;
case 'big':
sizes = ['zoom', 'normal'];
break;
case 'zoom':
sizes = ['big'];
break;
default:
sizes = ['big', 'zoom', 'normal', 'small'];
}
for (var i = 0; i < sizes.length; i++) {
var _size = sizes[i];
if (picture[_size] && picture[_size].url) {
return picture[_size];
}
}
// last try with custom sizes
for (var _size2 in picture) {
if (picture[_size2] && picture[_size2].url) {
return picture[_size2];
}
}
}
}
return undefined;
};
var _default = exports.default = img;