UNPKG

@mopinion/survey

Version:

Collect customer feedback with the Mopinion survey library

377 lines (376 loc) 16.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyLogic = applyLogic; exports.applyStartingLogic = applyStartingLogic; var _constants = require("../../../constants"); var _utils = require("../../../utils"); var _2 = require("./"); 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); } 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) { _defineProperty(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; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function trim(str) { return String(str).trim(); } var operatorFn = { '===': function _(x, y) { return trim(x) === trim(y); }, '!==': function _(x, y) { return trim(x) !== trim(y); }, '*': function _(x, y) { return y.indexOf(trim(x)) > -1; }, '!*': function _(x, y) { return !x && y.length > 0 || y.indexOf(trim(x)) === -1; }, is_not_empty: function is_not_empty(y) { return y.length > 0; } }; var startingOperatorFn = { url: function url(x) { return (0, _utils.getPageUrl)().indexOf(x) > -1; }, title: function title(x) { return document.title.indexOf(x) > -1; }, url_query: function url_query(x) { return (0, _utils.getUrlParam)(x, (0, _utils.getPageSearch)()); }, cookie: function cookie(x) { return (0, _utils.getCookie)(x); }, js: function js(x) { return (0, _utils.getJsFromString)(x); }, css_selector: function css_selector(x) { return (0, _utils.getTextContent)(x); }, data_attr: function data_attr(x) { return (0, _utils.getDataAttr)(x); } }; function getStartingOperatorReturnValue() { var _startingOperatorFn$a, _startingOperatorFn$a2; var action = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var operatorReturnValue = (_startingOperatorFn$a = (_startingOperatorFn$a2 = startingOperatorFn[action === null || action === void 0 ? void 0 : action.source]) === null || _startingOperatorFn$a2 === void 0 ? void 0 : _startingOperatorFn$a2.call(startingOperatorFn, action === null || action === void 0 ? void 0 : action.key)) !== null && _startingOperatorFn$a !== void 0 ? _startingOperatorFn$a : null; return operatorReturnValue; } function isValidStartingRule() { var _startingOperatorFn$a3, _startingOperatorFn$a4; var action = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var operatorReturnValue = (_startingOperatorFn$a3 = (_startingOperatorFn$a4 = startingOperatorFn[action === null || action === void 0 ? void 0 : action.source]) === null || _startingOperatorFn$a4 === void 0 ? void 0 : _startingOperatorFn$a4.call(startingOperatorFn, action === null || action === void 0 ? void 0 : action.key)) !== null && _startingOperatorFn$a3 !== void 0 ? _startingOperatorFn$a3 : null; if (!operatorReturnValue) { return false; } if (Array.isArray(action.values) && action.values.length > 0) { return action.values.filter(function (value) { return operatorReturnValue === value; }).length > 0; } if (typeof action.value === 'string' && action.value.length > 0) { return operatorReturnValue === action.value; } return operatorReturnValue; } function getElementExtraKey(elementKey) { var _block$properties$ele; var block = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (Number(elementKey) > Object.keys((_block$properties$ele = block.properties.elements) !== null && _block$properties$ele !== void 0 ? _block$properties$ele : {}).length) { var _block$properties$ele2; var extraElementKey = Number(elementKey) - Object.keys((_block$properties$ele2 = block.properties.elements) !== null && _block$properties$ele2 !== void 0 ? _block$properties$ele2 : {}).length; return block.properties.elements_extra[extraElementKey] && extraElementKey; } return null; } function getCheckboxDatafield(elementKey) { var _block$properties$ele4; var block = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var elementExtraKey = getElementExtraKey(elementKey, block); if (elementExtraKey !== null) { var _block$properties$ele3; return (_block$properties$ele3 = block.properties.elements_extra[elementExtraKey]) === null || _block$properties$ele3 === void 0 ? void 0 : _block$properties$ele3.data_field; } return (_block$properties$ele4 = block.properties.elements[elementKey]) === null || _block$properties$ele4 === void 0 ? void 0 : _block$properties$ele4.data_field; } function getValue(elementKey) { var block = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (['gcr', 'radio', 'checkbox', 'select', 'thumbs', 'category'].indexOf(block.typeName) > -1) { var _block$properties$ele8, _block$properties$ele9, _block$properties$ele0; var elementExtraKey = getElementExtraKey(elementKey, block); if (elementExtraKey !== null) { var _block$properties$ele5, _block$properties$ele6, _block$properties$ele7; return (_block$properties$ele5 = (_block$properties$ele6 = block.properties.elements_extra[elementExtraKey]) === null || _block$properties$ele6 === void 0 ? void 0 : _block$properties$ele6.value) !== null && _block$properties$ele5 !== void 0 ? _block$properties$ele5 : (_block$properties$ele7 = block.properties.elements_extra[elementExtraKey]) === null || _block$properties$ele7 === void 0 ? void 0 : _block$properties$ele7.label; } return (_block$properties$ele8 = (_block$properties$ele9 = block.properties.elements[elementKey]) === null || _block$properties$ele9 === void 0 ? void 0 : _block$properties$ele9.value) !== null && _block$properties$ele8 !== void 0 ? _block$properties$ele8 : (_block$properties$ele0 = block.properties.elements[elementKey]) === null || _block$properties$ele0 === void 0 ? void 0 : _block$properties$ele0.label; } return String(elementKey); } function isValidRule() { var _condition$values, _condition$elements, _condition$testValue; var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, state = _ref.state, _ref$condition = _ref.condition, condition = _ref$condition === void 0 ? {} : _ref$condition, _ref$block = _ref.block, block = _ref$block === void 0 ? {} : _ref$block, didExecuteLogic = _ref.didExecuteLogic; if (condition.always && !didExecuteLogic) { return true; } var operator = operatorFn[condition === null || condition === void 0 ? void 0 : condition.operator]; if (!operator) { return false; } if (condition.operator === 'is_not_empty') { return operator(state.value); } if (Array.isArray(condition === null || condition === void 0 ? void 0 : condition.values) && ((_condition$values = condition.values) === null || _condition$values === void 0 ? void 0 : _condition$values.length) > 0) { return condition.values.filter(function (value) { return operator(value, state.value); }).length > 0; } if (condition.hasOwnProperty('trigger_element')) { return operator(condition.trigger_element, state.value); } var conditionsTrue = (_condition$elements = condition.elements) === null || _condition$elements === void 0 ? void 0 : _condition$elements.filter(function (elementKey) { if (block.typeName === 'screenshot') { return operator(_constants.screenshotIsRendering, state.value); } if (block.typeName === 'checkbox') { var _state$getCheckboxDat; return operator(true, (_state$getCheckboxDat = state[getCheckboxDatafield(elementKey, block)]) === null || _state$getCheckboxDat === void 0 ? void 0 : _state$getCheckboxDat.value); } var valueFromBlock = getValue(elementKey, block); return operator(valueFromBlock, state.value); }); return (condition === null || condition === void 0 ? void 0 : condition.concat) !== '&&' ? (conditionsTrue === null || conditionsTrue === void 0 ? void 0 : conditionsTrue.length) > 0 : (conditionsTrue === null || conditionsTrue === void 0 ? void 0 : conditionsTrue.length) === ((_condition$testValue = condition.testValue) === null || _condition$testValue === void 0 ? void 0 : _condition$testValue.length); } function getTargetId(_ref2) { var _state$idOrUuid; var _ref2$target = _ref2.target, target = _ref2$target === void 0 ? '' : _ref2$target, _ref2$state = _ref2.state, state = _ref2$state === void 0 ? {} : _ref2$state; var fieldAsArr = String(target).split('_'); var idOrUuid = /^[a-z]+$/i.test(fieldAsArr[fieldAsArr.length - 1]) ? fieldAsArr[fieldAsArr.length - 2] : fieldAsArr[fieldAsArr.length - 1]; if (fieldAsArr[0] === 'thanks-page') { return fieldAsArr[1]; } return Number((_state$idOrUuid = state[idOrUuid]) === null || _state$idOrUuid === void 0 ? void 0 : _state$idOrUuid.id); } function clearNestedStateValues() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return Object.keys(state).reduce(function (newNestedState, currentKey) { if ((0, _2.isNestedStateProp)(currentKey)) { newNestedState[currentKey] = _objectSpread({}, state[currentKey]); newNestedState[currentKey].value = ''; } return newNestedState; }, {}); } function sortAlwaysRules() { var rules = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var sorted = []; var always = []; for (var i = 0; i < rules.length; i++) { var _rule$condition; var rule = rules[i]; if (rule !== null && rule !== void 0 && (_rule$condition = rule.condition) !== null && _rule$condition !== void 0 && _rule$condition.always) { always.push(rule); } else { sorted.push(rule); } } return [].concat(sorted, always); } function applyLogic(_ref3) { var _block$rules; var state = _ref3.state, _ref3$block = _ref3.block, block = _ref3$block === void 0 ? {} : _ref3$block, _ref3$rules = _ref3.rules, rules = _ref3$rules === void 0 ? [] : _ref3$rules, layout = _ref3.layout; if (!((_block$rules = block.rules) !== null && _block$rules !== void 0 && _block$rules.length)) { return { newState: state }; } var executeSubmitLogic = false; var didExecuteLogic = false; var elementsToBeUpdated = {}; sortAlwaysRules(rules).forEach(function () { var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref4$action = _ref4.action, action = _ref4$action === void 0 ? {} : _ref4$action, _ref4$condition = _ref4.condition, condition = _ref4$condition === void 0 ? {} : _ref4$condition; if (action.action === 'submit') { executeSubmitLogic = isValidRule({ state: state[block.id], condition: condition, block: block }); return; } if (['hide', 'show'].indexOf(action.action) > -1) { action.targets.forEach(function (target) { var _state$targetId; var targetId = getTargetId({ target: target, state: state }); if (!targetId) { return; } var valid = isValidRule({ state: state[block.id], condition: condition, block: block, didExecuteLogic: didExecuteLogic }); var show = action.action === 'show'; var isVisible = valid ? show : !show; if (valid) { didExecuteLogic = true; } elementsToBeUpdated[targetId] = _objectSpread({ isVisible: isVisible }, !isVisible && _objectSpread({ value: '' }, clearNestedStateValues((_state$targetId = state[targetId]) !== null && _state$targetId !== void 0 ? _state$targetId : {}))); }); return; } if (action.action === 'jump_to') { var targetId = getTargetId({ target: action.target, state: state }); if (!targetId) { return; } var valid = isValidRule({ state: state[block.id], condition: condition, block: block, didExecuteLogic: didExecuteLogic }); if (valid) { didExecuteLogic = true; var targetIndex = layout.indexOf(targetId); var blockIndex = layout.indexOf(Number(block.id)); var isBackwardsJump = targetIndex < blockIndex; var layoutSlice = isBackwardsJump ? layout.slice(targetIndex, blockIndex + 1) : layout.slice(blockIndex + 1, targetIndex); layoutSlice.forEach(function (blockId, index) { elementsToBeUpdated[blockId] = _objectSpread(_objectSpread({}, !isBackwardsJump && { isVisible: false }), {}, { showInConvo: isBackwardsJump ? index === 0 : false, isSkipped: false, value: '' }); }); return; } } }); return { updateFromLogic: elementsToBeUpdated, submitFromLogic: executeSubmitLogic, newState: mergeLogicUpdates(state, elementsToBeUpdated) }; } function mergeLogicUpdates(prevState, updateFromLogic) { var updatedState = {}; try { Object.keys(updateFromLogic || {}).forEach(function (blockId) { if (prevState[blockId]) { updatedState[blockId] = _objectSpread(_objectSpread({}, prevState[blockId]), updateFromLogic[blockId]); } }); } catch (e) {} return _objectSpread(_objectSpread({}, prevState), updatedState); } function applyStartingLogic(_ref5) { var state = _ref5.state, layout = _ref5.layout, _ref5$surveyRules = _ref5.surveyRules, surveyRules = _ref5$surveyRules === void 0 ? {} : _ref5$surveyRules; var elementsToBeUpdated = {}; Object.keys(surveyRules).forEach(function (ruleKey) { var _surveyRules$ruleKey; var _ref6 = (_surveyRules$ruleKey = surveyRules[ruleKey]) !== null && _surveyRules$ruleKey !== void 0 ? _surveyRules$ruleKey : {}, action = _ref6.action; if (!isValidStartingRule(action)) { return; } if (['hide', 'show'].indexOf(action.action) > -1) { action.targets.forEach(function (target) { var targetId = getTargetId({ target: target, state: state }); if (!targetId) { return; } var isVisible = action.action === 'show'; elementsToBeUpdated[targetId] = _objectSpread({ isVisible: isVisible }, !isVisible && { value: '' }); }); return; } if (action.action === 'jump_to') { var targetId = getTargetId({ target: action.target, state: state }); if (!targetId) { return; } var targetIndex = layout.indexOf(targetId); var layoutSlice = layout.slice(0, targetIndex); layoutSlice.forEach(function (blockId, index) { elementsToBeUpdated[blockId] = { isVisible: false, showInConvo: false, isSkipped: false, value: '' }; }); return; } if (action.action === 'prefill') { var prefillValue = getStartingOperatorReturnValue(action); action.targets.forEach(function (target) { var targetId = getTargetId({ target: target, state: state }); if (!targetId) { return; } elementsToBeUpdated[targetId] = _objectSpread(_objectSpread({}, elementsToBeUpdated[targetId] && _objectSpread({}, elementsToBeUpdated[targetId])), {}, { value: prefillValue, wasPrefilled: true }); }); return; } }); return { newState: mergeLogicUpdates(state, elementsToBeUpdated), updateFromLogic: elementsToBeUpdated }; } //# sourceMappingURL=logic.js.map