UNPKG

qwc2

Version:
347 lines (346 loc) 16.3 kB
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 _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } 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 _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 _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 _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } 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; } /** * Copyright 2018-2024 Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ import ConfigUtils from './ConfigUtils'; var MiscUtils = { addLinkAnchors: function addLinkAnchors(text) { // If text already contains tags, do nothing var tagRegEx = /(<.[^(><.)]+>)/; if (tagRegEx.exec(text)) { return text; } var urlRegEx = new RegExp(ConfigUtils.getConfigProp("urlRegEx", null, // Original String: (\s|^)((http(s)?|(s)?ftp):\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=\u00C0-\u00FF\u0370-\u03FF]*) // Escaped with https://www.freeformatter.com/json-escape.html // eslint-disable-next-line "(\\s|^)((http(s)?|(s)?ftp):\\/\\/.)?(www\\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&//=\\u00C0-\\u00FF\\u0370-\\u03FF]*)"), "g"); var value = text; var match = null; while (match = urlRegEx.exec(value)) { // If URL is part of a HTML attribute, don't add anchor if (value.substring(match.index - 2, match.index).match(/^=['"]$/) === null) { var url = match[0].substr(match[1].length); var protoUrl = url; if (match[2] === undefined) { if (match[0].indexOf('@') !== -1) { protoUrl = "mailto:" + url; } else { protoUrl = "http://" + url; } } var pos = match.index + match[1].length; var anchor = "<a href=\"" + MiscUtils.htmlEncode(protoUrl) + "\" target=\"_blank\">" + MiscUtils.htmlEncode(url) + "</a>"; value = value.substring(0, pos) + anchor + value.substring(pos + url.length); urlRegEx.lastIndex = pos + anchor.length; } } // Reset urlRegEx.lastIndex = 0; return value; }, htmlEncode: function htmlEncode(text) { return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"); }, getCsrfToken: function getCsrfToken() { var csrfTag = Array.from(document.getElementsByTagName('meta')).find(function (tag) { return tag.getAttribute('name') === "csrf-token"; }); return csrfTag ? csrfTag.getAttribute('content') : ""; }, setupKillTouchEvents: function setupKillTouchEvents(el) { if (el) { // To stop touchmove propagating to parent which can trigger a swipe el.addEventListener('touchmove', function (ev) { ev.stopPropagation(); }, { passive: false }); } }, killEvent: function killEvent(ev) { if (ev.cancelable) { var _ev$nativeEvent, _ev$nativeEvent$stopI; ev.stopPropagation(); (_ev$nativeEvent = ev.nativeEvent) === null || _ev$nativeEvent === void 0 || (_ev$nativeEvent$stopI = _ev$nativeEvent.stopImmediatePropagation) === null || _ev$nativeEvent$stopI === void 0 || _ev$nativeEvent$stopI.call(_ev$nativeEvent); ev.preventDefault(); } }, checkKeyActivate: function checkKeyActivate(ev) { var onEsc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; if (ev.code === "Space" || ev.code === "Enter") { MiscUtils.killEvent(ev); ev.currentTarget.click(); } else if (ev.code === "Escape" && onEsc) { MiscUtils.killEvent(ev); onEsc(); } }, checkKeyActivatePointerDown: function checkKeyActivatePointerDown(ev) { if (ev.code === "Space" || ev.code === "Enter") { MiscUtils.killEvent(ev); ev.currentTarget.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, cancelable: true, pointerId: 1, pointerType: "mouse", view: ev.view })); ev.currentTarget.addEventListener("keyup", function (ev2) { ev2.currentTarget.dispatchEvent(new PointerEvent("pointerup", { bubbles: true, cancelable: true, pointerId: 1, pointerType: "mouse", view: ev2.view })); }, { once: true }); } }, blendColors: function blendColors(color1, color2, ratio) { color1 = [parseInt(color1[1] + color1[2], 16), parseInt(color1[3] + color1[4], 16), parseInt(color1[5] + color1[6], 16)]; color2 = [parseInt(color2[1] + color2[2], 16), parseInt(color2[3] + color2[4], 16), parseInt(color2[5] + color2[6], 16)]; var color3 = [(1 - ratio) * color1[0] + ratio * color2[0], (1 - ratio) * color1[1] + ratio * color2[1], (1 - ratio) * color1[2] + ratio * color2[2]]; var toHex = function toHex(num) { return ("0" + Math.round(num).toString(16)).slice(-2); }; return '#' + toHex(color3[0]) + toHex(color3[1]) + toHex(color3[2]); }, hslToRgb: function hslToRgb(h, s, l) { // Takes h, s, l in [0, 1] range and returns [r, g, b] in [0, 1] range var c = (1 - Math.abs(2 * l - 1)) * s; var x = c * (1 - Math.abs(h / 60 % 2 - 1)); var m = l - c / 2; h *= 360; var r = 0; var g = 0; var b = 0; if (h >= 0 && h < 60) { r = c; g = x; b = 0; } else if (h >= 60 && h < 120) { r = x; g = c; b = 0; } else if (h >= 120 && h < 180) { r = 0; g = c; b = x; } else if (h >= 180 && h < 240) { r = 0; g = x; b = c; } else if (h >= 240 && h < 300) { r = x; g = 0; b = c; } else if (h >= 300 && h < 360) { r = c; g = 0; b = x; } return [r + m, g + m, b + m]; }, ensureArray: function ensureArray(el) { if (el === undefined) { return []; } else if (Array.isArray(el)) { return el; } return [el]; }, capitalizeFirst: function capitalizeFirst(text) { return text.slice(0, 1).toUpperCase() + text.slice(1); }, isBrightColor: function isBrightColor(hex) { var color = +("0x" + hex.slice(1).replace(hex.length < 5 && /./g, '$&$&')); var r = color >> 16; var g = color >> 8 & 255; var b = color & 255; var hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b)); return hsp > 127.5; }, adjustProtocol: function adjustProtocol(url) { if (location.protocol === 'https:' && url.startsWith('http:')) { return 'https:' + url.substr(5); } return url; }, convertEmToPx: function convertEmToPx(emsize) { var defaultfontsize = getComputedStyle(document.documentElement).fontSize; return emsize * parseFloat(defaultfontsize); }, getFaviconFromIcon: function getFaviconFromIcon(icon, size) { var glyph = null; var _iterator = _createForOfIteratorHelper(document.styleSheets), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var sheet = _step.value; var _iterator2 = _createForOfIteratorHelper(sheet.cssRules), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var rule = _step2.value; if (rule.selectorText === ".icon-".concat(icon, "::before")) { glyph = rule.style.content.replace(/["']/g, ''); break; } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } if (glyph === null) { return null; } var canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; var ctx = canvas.getContext('2d'); ctx.font = "".concat(size - 5, "px qwc2-icons"); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillStyle = '#FFF'; // You can change this color if needed ctx.fillText(glyph, size / 2, size / 2); return canvas.toDataURL('image/png'); }, resolveAssetsPath: function resolveAssetsPath(path) { return path && path.startsWith(":/") ? ConfigUtils.getAssetsPath() + path.substr(1) : path; }, formatDate: function formatDate(input, pattern) { var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined; // Try to parse the input as a full date-time or as time-only var date; if (/^\d{2}:\d{2}(:\d{2})?$/.test(input)) { var now = new Date(); var _input$split$map = input.split(':').map(Number), _input$split$map2 = _slicedToArray(_input$split$map, 3), h = _input$split$map2[0], m = _input$split$map2[1], s = _input$split$map2[2]; date = new Date(now.getFullYear(), now.getMonth(), now.getDate(), h, m, s || 0); } else { date = new Date(input); } if (isNaN(date)) throw new Error("Invalid date"); var pad = function pad(n) { var len = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; return String(n).padStart(len, "0"); }; var tokens = { yyyy: date.getFullYear(), yy: String(date.getFullYear()).slice(-2), MMMM: date.toLocaleString(locale, { month: "long" }), MMM: date.toLocaleString(locale, { month: "short" }), MM: pad(date.getMonth() + 1), M: date.getMonth() + 1, dd: pad(date.getDate()), d: date.getDate(), dddd: date.toLocaleString(locale, { weekday: "long" }), ddd: date.toLocaleString(locale, { weekday: "short" }), hh: pad((date.getHours() + 11) % 12 + 1), h: (date.getHours() + 11) % 12 + 1, HH: pad(date.getHours()), H: date.getHours(), mm: pad(date.getMinutes()), m: date.getMinutes(), ss: pad(date.getSeconds()), s: date.getSeconds(), AP: date.getHours() < 12 ? "AM" : "PM", ap: date.getHours() < 12 ? "am" : "pm" }; return pattern.replace(/(yyyy|yy|MMMM|MMM|MM|M|dddd|ddd|dd|d|hh|h|HH|H|mm|m|ss|s|AP|ap)/g, function (match) { return tokens[match]; }); } }; export var ToggleSet = /*#__PURE__*/_createClass(function ToggleSet() { var _this = this; var set = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Set(); _classCallCheck(this, ToggleSet); _defineProperty(this, "has", function (key) { return _this._set.has(key); }); _defineProperty(this, "toggle", function (key) { var newset = new Set(_this._set); if (newset.has(key)) { newset["delete"](key); } else { newset.add(key); } return new ToggleSet(newset); }); _defineProperty(this, "delete", function (key) { if (_this._set.has(key)) { var newset = new Set(_this._set); newset["delete"](key); return new ToggleSet(newset); } else { return _this; } }); _defineProperty(this, "filtered", function (test) { var newset = _construct(Set, _toConsumableArray(_toConsumableArray(_this._set).filter(test))); return new ToggleSet(newset); }); _defineProperty(this, "add", function (key) { if (!_this._set.has(key)) { var newset = new Set(_this._set); newset.add(key); return new ToggleSet(newset); } else { return _this; } }); _defineProperty(this, "size", function () { return _this._set.size; }); _defineProperty(this, "entries", function () { return _toConsumableArray(_this._set); }); this._set = set; }); export default MiscUtils;