@mopinion/deploy
Version:
Deploy your Mopinion feedback forms with ease
165 lines (164 loc) • 7.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = createRuleStore;
var _storage = require("../storage");
var _utils = require("../utils");
var _utils2 = require("./utils");
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 initialState() {
var rule = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var prevState = (0, _storage.getStorage)(rule.id);
return _objectSpread({
conditions: rule["if"].map(function (condition) {
return {
triggered: false,
formEvents: {},
domEvents: {}
};
})
}, prevState);
}
function initialValidState() {
var rule = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return {
conditions: rule["if"].map(function () {
return false;
})
};
}
function initialZeroDayState() {
var rule = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return {
conditions: rule["if"].map(function () {
return {
triggered: false,
formEvents: {}
};
})
};
}
function initialEventListenerState(rule) {
return {
conditions: rule["if"].map(function (condition) {
return {
domEvents: {}
};
})
};
}
function createRuleStore(rule) {
var state = initialState(rule);
var validState = initialValidState(rule);
var zeroDayState = initialZeroDayState(rule);
var timeOnPageState = {};
var eventListenerState = initialEventListenerState(rule);
function setState() {
var fn = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
var newState = fn(state);
if (newState) {
Object.assign(state, newState);
}
(0, _storage.setStorage)(rule.id, state);
}
function setConditionState(index, fn) {
var _rule$if$index;
var sessionDays = Number((_rule$if$index = rule["if"][index]) === null || _rule$if$index === void 0 ? void 0 : _rule$if$index.session);
if (sessionDays === 0) {
return fn(zeroDayState.conditions[index]);
}
setState(function (state) {
try {
if (!state.conditions[index].timestamp) {
state.conditions[index].timestamp = new Date();
}
fn(state.conditions[index]);
} catch (e) {}
});
}
function getState() {
var fn = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
return fn(state);
}
function handleSession(index) {
var _rule$if$index2, _state$conditions;
var sessionDays = (_rule$if$index2 = rule["if"][index]) === null || _rule$if$index2 === void 0 ? void 0 : _rule$if$index2.session;
var timestamp = (_state$conditions = state.conditions) === null || _state$conditions === void 0 || (_state$conditions = _state$conditions[index]) === null || _state$conditions === void 0 ? void 0 : _state$conditions.timestamp;
if (typeof sessionDays !== 'undefined' && typeof timestamp !== 'undefined') {
var daysSinceTimestamp = (0, _utils2.getDiffInDays)(timestamp);
if (daysSinceTimestamp >= Number(sessionDays)) {
setConditionState(index, function (conditionState) {
Object.keys(conditionState).forEach(function (key) {
delete conditionState[key];
});
});
}
}
}
function getConditionState(index) {
var _rule$if$index3, _state$conditions$ind, _state$conditions2;
var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
var sessionDays = Number((_rule$if$index3 = rule["if"][index]) === null || _rule$if$index3 === void 0 ? void 0 : _rule$if$index3.session);
if (sessionDays === 0) {
var _zeroDayState$conditi, _zeroDayState$conditi2;
return fn((_zeroDayState$conditi = (_zeroDayState$conditi2 = zeroDayState.conditions) === null || _zeroDayState$conditi2 === void 0 ? void 0 : _zeroDayState$conditi2[index]) !== null && _zeroDayState$conditi !== void 0 ? _zeroDayState$conditi : {});
}
handleSession(index);
return fn((_state$conditions$ind = (_state$conditions2 = state.conditions) === null || _state$conditions2 === void 0 ? void 0 : _state$conditions2[index]) !== null && _state$conditions$ind !== void 0 ? _state$conditions$ind : {});
}
function setValidState(index, valid) {
try {
validState.conditions[index] = valid;
} catch (e) {}
}
function getValidState(index) {
var _validState$condition, _validState$condition2;
return (_validState$condition = (_validState$condition2 = validState.conditions) === null || _validState$condition2 === void 0 ? void 0 : _validState$condition2[index]) !== null && _validState$condition !== void 0 ? _validState$condition : false;
}
function getValidConditions() {
var conditions = [];
for (var i = 0; i < validState.conditions.length; i++) {
var valid = validState.conditions[i];
if (valid) {
var _rule$if;
conditions.push({
condition: (_rule$if = rule["if"]) === null || _rule$if === void 0 ? void 0 : _rule$if[i],
index: i
});
}
}
return conditions;
}
function setTimeOnPageState(index) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'time';
try {
if (!timeOnPageState[index]) {
timeOnPageState[index] = {};
}
timeOnPageState[index][type] = (0, _utils.timeInSeconds)();
} catch (e) {}
}
function getTimeOnPageState(index) {
var _timeOnPageState$inde;
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'time';
return (_timeOnPageState$inde = timeOnPageState[index]) === null || _timeOnPageState$inde === void 0 ? void 0 : _timeOnPageState$inde[type];
}
return {
setState: setState,
setConditionState: setConditionState,
getState: getState,
getConditionState: getConditionState,
setValidState: setValidState,
getValidState: getValidState,
getValidConditions: getValidConditions,
setTimeOnPageState: setTimeOnPageState,
getTimeOnPageState: getTimeOnPageState
};
}
//# sourceMappingURL=createRuleStore.js.map