qwc2-lts
Version:
QGIS Web Client
158 lines (157 loc) • 7.25 kB
JavaScript
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, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
},
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) {
ev.stopPropagation();
ev.preventDefault();
}
},
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]);
},
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;
}
};
export default MiscUtils;