@mopinion/survey
Version:
Collect customer feedback with the Mopinion survey library
131 lines (130 loc) • 5.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getCurrentVisibleId = getCurrentVisibleId;
exports.getInitialElementValue = getInitialElementValue;
exports.getNextVisibleId = getNextVisibleId;
exports.isNestedStateProp = isNestedStateProp;
var _utils = require("../../../utils");
var _constants = require("../../../constants");
function getInitialElementValue() {
var _block$properties, _block$properties2;
var block = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var preselectScoreValue = null;
if ((_block$properties = block.properties) !== null && _block$properties !== void 0 && _block$properties.preselectScore) {
switch (block.typeName) {
case 'nps':
case 'thumbs':
preselectScoreValue = (0, _utils.getUrlParam)(block.typeName, (0, _utils.getPageSearch)());
break;
case 'rating':
default:
if (block.properties.type === 'emoji') {
preselectScoreValue = (0, _utils.getUrlParam)('smiley', (0, _utils.getPageSearch)());
if (preselectScoreValue && block.properties.labelsAsValue && block.properties.labelsAsValue) {
var _block$properties$emo, _block$properties$emo2;
preselectScoreValue = (_block$properties$emo = (_block$properties$emo2 = block.properties.emoji) === null || _block$properties$emo2 === void 0 || (_block$properties$emo2 = _block$properties$emo2[preselectScoreValue]) === null || _block$properties$emo2 === void 0 ? void 0 : _block$properties$emo2.label) !== null && _block$properties$emo !== void 0 ? _block$properties$emo : '';
}
break;
}
preselectScoreValue = (0, _utils.getUrlParam)('score', (0, _utils.getPageSearch)());
}
if (preselectScoreValue) {
return {
post: true,
logic: true,
value: preselectScoreValue
};
}
}
var urlParamValue = (0, _utils.getUrlParam)("".concat(block.typeName, "_").concat(block.id), (0, _utils.getPageSearch)()) || (0, _utils.getUrlParam)("".concat(block.typeName, "_").concat(block.uuid), (0, _utils.getPageSearch)());
if (urlParamValue && isValidValue({
value: urlParamValue,
block: block
})) {
return {
post: true,
logic: true,
value: urlParamValue
};
}
if ((_block$properties2 = block.properties) !== null && _block$properties2 !== void 0 && _block$properties2.value) {
return {
logic: true,
value: block.properties.value
};
}
return {
value: ''
};
}
function isValidValue(_ref) {
var _block$properties3;
var value = _ref.value,
_ref$block = _ref.block,
block = _ref$block === void 0 ? {} : _ref$block;
var _ref2 = (_block$properties3 = block === null || block === void 0 ? void 0 : block.properties) !== null && _block$properties3 !== void 0 ? _block$properties3 : {},
type = _ref2.type,
_ref2$scale = _ref2.scale,
scale = _ref2$scale === void 0 ? 10 : _ref2$scale;
switch (block.typeName) {
case 'nps':
return Number(value) >= 0 && Number(value) <= 10;
case 'rating':
if (['emoji', 'bar'].indexOf(type) > -1) {
return Number(value) >= 1 && Number(value) <= 5;
}
return Number(value) >= 0 && Number(value) <= scale;
case 'ces':
return Number(value) >= 1 && Number(value) <= 5;
case 'thumbs':
case 'gcr':
return Object.keys(block.properties.elements).some(function (elementKey) {
var _block$properties$ele, _block$properties4;
var element = (_block$properties$ele = (_block$properties4 = block.properties) === null || _block$properties4 === void 0 || (_block$properties4 = _block$properties4.elements) === null || _block$properties4 === void 0 ? void 0 : _block$properties4[elementKey]) !== null && _block$properties$ele !== void 0 ? _block$properties$ele : {};
return element.value === value;
});
case 'category':
return Object.keys(block.properties.elements).some(function (elementKey) {
var _block$properties$ele2, _block$properties5;
var element = (_block$properties$ele2 = (_block$properties5 = block.properties) === null || _block$properties5 === void 0 || (_block$properties5 = _block$properties5.elements) === null || _block$properties5 === void 0 ? void 0 : _block$properties5[elementKey]) !== null && _block$properties$ele2 !== void 0 ? _block$properties$ele2 : {};
return element.label === value;
});
default:
return false;
}
}
function isNestedStateProp() {
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
return ['dom', 'selector'].indexOf(str) > -1 || !isNaN(Number(str));
}
function getCurrentVisibleId(_ref3) {
var _layout$filter$pop;
var layout = _ref3.layout,
blockState = _ref3.blockState,
blocks = _ref3.blocks;
return (_layout$filter$pop = layout.filter(function (blockId) {
var state = blockState[blockId];
var block = blocks[blockId];
return state.isVisible && state.showInConvo && _constants.alwaysNotVisibleBlocks.indexOf(block === null || block === void 0 ? void 0 : block.typeName) === -1;
}).pop()) !== null && _layout$filter$pop !== void 0 ? _layout$filter$pop : null;
}
function getNextVisibleId(_ref4) {
var layout = _ref4.layout,
blockState = _ref4.blockState,
blocks = _ref4.blocks;
var currentVisibleBlockId = getCurrentVisibleId({
layout: layout,
blockState: blockState,
blocks: blocks
});
var currentIndex = layout.indexOf(currentVisibleBlockId);
var nextVisibleId = layout.slice(currentIndex + 1, layout.length).filter(function (blockId) {
var state = blockState[blockId];
var block = blocks[blockId];
return state.isVisible && _constants.alwaysNotVisibleBlocks.indexOf(block.typeName) === -1;
})[0];
return nextVisibleId;
}
//# sourceMappingURL=index.js.map