@mopinion/deploy
Version:
Deploy your Mopinion feedback forms with ease
205 lines (161 loc) • 7.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = createRuleStore;
var _storage = require("../storage");
var _utils = require("../utils");
var _utils2 = require("./utils");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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, _state$conditions$ind;
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 ? void 0 : (_state$conditions$ind = _state$conditions[index]) === null || _state$conditions$ind === void 0 ? void 0 : _state$conditions$ind.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$ind2, _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$ind2 = (_state$conditions2 = state.conditions) === null || _state$conditions2 === void 0 ? void 0 : _state$conditions2[index]) !== null && _state$conditions$ind2 !== void 0 ? _state$conditions$ind2 : {});
}
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