UNPKG

@mopinion/deploy

Version:

Deploy your Mopinion feedback forms with ease

377 lines (376 loc) 15.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkDivs = checkDivs; exports.conditionValueIsEmpty = conditionValueIsEmpty; exports.createDiv = createDiv; exports.divExists = divExists; exports.getCssSelectorTextContent = getCssSelectorTextContent; exports.getDeviceType = getDeviceType; exports.getDiffInDays = getDiffInDays; exports.getJsFromString = getJsFromString; exports.handleDivPlacement = handleDivPlacement; exports.handleOrConditionArray = handleOrConditionArray; exports.parseUrlString = parseUrlString; exports.prepareDateComparison = prepareDateComparison; exports.prepareTimeComparison = prepareTimeComparison; exports.randomPercentage = randomPercentage; exports.testCondition = testCondition; exports.tryService = tryService; var _utils = 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 _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function handleOrConditionArray() { var conditionArray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var testFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {}; return conditionArray.some(function (orCondition) { if (orCondition instanceof Array) { return orCondition.every(function (andCondition) { return testFn(andCondition); }); } return testFn(orCondition); }); } function isNumericString(str) { if (typeof str !== 'string' || str.trim() === '') return false; var num = Number(str); return !isNaN(num) && num.toString() === str.trim(); } function testCondition(_ref) { var value1 = _ref.value1, value2 = _ref.value2, value3 = _ref.value3, _ref$operator = _ref.operator, operator = _ref$operator === void 0 ? 'matches exactly' : _ref$operator, _ref$regex = _ref.regex, regex = _ref$regex === void 0 ? false : _ref$regex; switch (operator) { case 'exists': return value1 !== undefined; case 'does not exist': return value1 === undefined; case 'contains': if (Array.isArray(value1)) { return value1.indexOf(value2) > -1; } if (Array.isArray(value2)) { return value2.some(function (item) { return (0, _utils.strContains)(value1, item, regex); }); } return (0, _utils.strContains)(value1, value2, regex); case 'does not contain': if (Array.isArray(value1)) { return value1.indexOf(value2) === -1; } if (Array.isArray(value2)) { return value2.some(function (item) { return !(0, _utils.strContains)(value1, item, regex); }); } return !(0, _utils.strContains)(value1, value2, regex); case 'is boolean': return typeof value1 === 'boolean' && String(value1) === value2; case 'earlier': return value1 > value3; case 'exactly': return value1 === value3; case 'between': return value1 <= value3 && value2 >= value3; case 'later': return value1 < value3; case 'matches exactly': default: if (Array.isArray(value2)) { return value2.some(function (item) { if (isNumericString(item)) { return value1 === Number(item) || value1 === item; } return value1 === item; }); } if (isNumericString(value2)) { return value1 === Number(value2) || value1 === value2; } return value1 === value2; } } function dateAsTime(date) { var dateInstance = typeof date === 'string' ? new Date(date) : new Date(); return dateInstance.getTime(); } function dateStringToTime(dateString, endOfDay) { var dateArr = dateString.split('/').reverse().map(function (str, index) { if (index === 0) { return str; } return prependZeroStr(str); }); var time = endOfDay ? 'T23:59' : 'T00:01'; var dateTimeString = "".concat(dateArr.join('-')).concat(time); return dateAsTime(dateTimeString); } function todaysDateString() { var date = new Date(); return "".concat(date.getDate(), "/").concat(date.getMonth() + 1, "/").concat(date.getFullYear()); } function prepareDateComparison(_ref2) { var _ref2$date = _ref2.date, date = _ref2$date === void 0 ? '' : _ref2$date, _ref2$date2 = _ref2.date2, date2 = _ref2$date2 === void 0 ? '' : _ref2$date2, _ref2$operator = _ref2.operator, operator = _ref2$operator === void 0 ? 'exactly' : _ref2$operator; return { value1: dateStringToTime(date), value2: dateStringToTime(date2, true), value3: dateStringToTime(todaysDateString()), operator: operator }; } function prepareTimeComparison(_ref3) { var _ref3$time = _ref3.time, time = _ref3$time === void 0 ? '' : _ref3$time, _ref3$time2 = _ref3.time2, time2 = _ref3$time2 === void 0 ? '' : _ref3$time2, _ref3$operator = _ref3.operator, operator = _ref3$operator === void 0 ? 'exactly' : _ref3$operator; var now = new Date(); var hours = prependZeroStr(now.getHours()); var minutes = prependZeroStr(now.getMinutes()); var currentTime = "".concat(hours, ":").concat(minutes); return { value1: time, value2: time2, value3: currentTime, operator: operator }; } function getCssSelectorTextContent() { var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; try { var node = document.querySelector(selector); if (!node) { return undefined; } var textContent = node.textContent || node.innerText; return textContent.replace(/^\s+|\s+$/g, ''); } catch (e) { return undefined; } } function getJsFromString(str) { try { return (0, _utils._get)(window, str, undefined); } catch (e) { return undefined; } } function getDeviceType() { var apple_phone = /iPhone/i, apple_ipod = /iPod/i, apple_tablet = /iPad/i, android_phone = /(?=.*\bAndroid\b)(?=.*\bMobile\b)/i, android_tablet = /Android/i, amazon_phone = /(?=.*\bAndroid\b)(?=.*\bSD4930UR\b)/i, amazon_tablet = /(?=.*\bAndroid\b)(?=.*\b(?:KFOT|KFTT|KFJWI|KFJWA|KFSOWI|KFTHWI|KFTHWA|KFAPWI|KFAPWA|KFARWI|KFASWI|KFSAWI|KFSAWA)\b)/i, windows_phone = /Windows Phone/i, windows_tablet = /(?=.*\bWindows\b)(?=.*\bARM\b)/i, other_blackberry = /BlackBerry/i, other_blackberry_10 = /BB10/i, other_opera = /Opera Mini/i, other_chrome = /(CriOS|Chrome)(?=.*\bMobile\b)/i, other_firefox = /(?=.*\bFirefox\b)(?=.*\bMobile\b)/i, seven_inch = new RegExp('(?:' + 'Nexus 7' + '|' + 'BNTV250' + '|' + 'Kindle Fire' + '|' + 'Silk' + '|' + 'GT-P1000' + ')', 'i'); var match = function match(regex, userAgent) { return regex.test(userAgent); }; var isAppleTabletIos13AndUp = function isAppleTabletIos13AndUp() { return /Macintosh/i.test(navigator.userAgent) && navigator.maxTouchPoints && navigator.maxTouchPoints > 1; }; var ua = navigator.userAgent; var tmp = ua.split('[FBAN'); if (typeof tmp[1] !== 'undefined') { ua = tmp[0]; } tmp = ua.split('Twitter'); if (typeof tmp[1] !== 'undefined') { ua = tmp[0]; } var deviceData = { apple: { phone: match(apple_phone, ua), ipod: match(apple_ipod, ua), tablet: !match(apple_phone, ua) && match(apple_tablet, ua) || isAppleTabletIos13AndUp(), device: match(apple_phone, ua) || match(apple_ipod, ua) || match(apple_tablet, ua) }, amazon: { phone: match(amazon_phone, ua), tablet: !match(amazon_phone, ua) && match(amazon_tablet, ua), device: match(amazon_phone, ua) || match(amazon_tablet, ua) }, android: { phone: match(amazon_phone, ua) || match(android_phone, ua), tablet: !match(amazon_phone, ua) && !match(android_phone, ua) && (match(amazon_tablet, ua) || match(android_tablet, ua)), device: match(amazon_phone, ua) || match(amazon_tablet, ua) || match(android_phone, ua) || match(android_tablet, ua) }, windows: { phone: match(windows_phone, ua), tablet: match(windows_tablet, ua), device: match(windows_phone, ua) || match(windows_tablet, ua) }, other: { blackberry: match(other_blackberry, ua), blackberry10: match(other_blackberry_10, ua), opera: match(other_opera, ua), firefox: match(other_firefox, ua), chrome: match(other_chrome, ua), device: match(other_blackberry, ua) || match(other_blackberry_10, ua) || match(other_opera, ua) || match(other_firefox, ua) || match(other_chrome, ua) } }; return { seven_inch: match(seven_inch, ua), phone: deviceData.apple.phone || deviceData.android.phone || deviceData.windows.phone, tablet: deviceData.apple.tablet || deviceData.android.tablet || deviceData.windows.tablet }; } function parseSelector() { var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; if (['#', '.', '['].indexOf(selector === null || selector === void 0 ? void 0 : selector.charAt(0)) > -1) { return selector; } return "#".concat(selector); } function cleanSelector() { var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; try { return selector.trim().split(' ').shift(); } catch (e) { return selector; } } function divExists(selector) { try { var parsedSelector = parseSelector(selector); return Boolean(document.querySelector(parsedSelector)); } catch (e) { return false; } } function parseAttributeSelector() { var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; try { var _attr$replace$replace, _value$replace; var _str$split = str.split('='), _str$split2 = _slicedToArray(_str$split, 2), attr = _str$split2[0], value = _str$split2[1]; return { attr: (_attr$replace$replace = attr === null || attr === void 0 ? void 0 : attr.replace(/\[/g, '').replace(/\]/g, '')) !== null && _attr$replace$replace !== void 0 ? _attr$replace$replace : '', value: (_value$replace = value === null || value === void 0 ? void 0 : value.replace(/\]/g, '')) !== null && _value$replace !== void 0 ? _value$replace : '' }; } catch (e) { return {}; } } function createDiv(selector) { var parsedAndCleaned = parseSelector(cleanSelector(selector)); if (!divExists(parsedAndCleaned)) { var div = document.createElement('div'); if ((parsedAndCleaned === null || parsedAndCleaned === void 0 ? void 0 : parsedAndCleaned.charAt(0)) === '#') { div.id = parsedAndCleaned.replace('#', ''); } if ((parsedAndCleaned === null || parsedAndCleaned === void 0 ? void 0 : parsedAndCleaned.charAt(0)) === '.') { div.className = parsedAndCleaned.replace('.', ''); } if ((parsedAndCleaned === null || parsedAndCleaned === void 0 ? void 0 : parsedAndCleaned.charAt(0)) === '[') { var _parseAttributeSelect = parseAttributeSelector(parsedAndCleaned), attr = _parseAttributeSelect.attr, value = _parseAttributeSelect.value; if (attr) { div.setAttribute(attr, value); } } document.body.appendChild(div); } } function conditionValueIsEmpty(value) { if (typeof value === 'undefined' || value === null) { return true; } if (typeof value === 'string') { return value.trim() === ''; } if (value && _typeof(value) === 'object') { return Object.keys(value).length === 0; } if (typeof value === 'number') { return isNaN(value); } return false; } function tryService(fn) { var limit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10; var counter = 0; var interval = setInterval(function () { try { fn(); clearInterval(interval); } catch (e) {} counter++; if (counter >= limit) { clearInterval(interval); } }, 100); } function handleDivPlacement() { var then = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var valid = arguments.length > 1 ? arguments[1] : undefined; if (!checkDivs(then)) { then.forEach(function () { var thenCondition = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var selector = thenCondition.args[2]; var exists = divExists(selector); if (!exists && !thenCondition.disableMakeDiv && valid) { createDiv(selector); } }); } return checkDivs(then); } function checkDivs(then) { return then.every(function () { var thenCondition = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var selector = thenCondition.args[2]; return divExists(selector); }); } function randomPercentage() { return (Math.random() * 100).toFixed(1).toString(); } function getDiffInDays() { var pastTimestamp = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var currentTimestamp = arguments.length > 1 ? arguments[1] : undefined; var pastTime = new Date(pastTimestamp).getTime(); var currentTime = currentTimestamp ? new Date(currentTimestamp).getTime() : new Date().getTime(); return Math.floor((currentTime - pastTime) / (1000 * 3600 * 24)); } function prependZeroStr(str) { var len = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; return "0".concat(str).slice(-len); } function parseUrlString() { var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; if (typeof str !== 'string') { return ''; } return str.replace(/^(?:https?:\/\/)?(?:www\.)?/, '').replace(/\/+$/, ''); } //# sourceMappingURL=utils.js.map