UNPKG

@true-directive/base

Version:

The set of base classes for the TrueDirective Grid

111 lines (110 loc) 3.58 kB
/** * Copyright (c) 2018-2019 Aleksey Melnikov, True Directive Company. * @link https://truedirective.com/ * @license MIT */ var Utils = /** @class */ (function () { function Utils() { } /** * Is the element ancestor to the given element. * @param ancestor Element to check. * @param element An element that may be descendant * @return True if the first element is an ancestor of the second element. */ Utils.isAncestor = function (ancestor, element) { if (!element) { return false; } var parent = element; while (parent && parent !== ancestor) { parent = parent.parentNode; } return parent === ancestor; }; /** * Is given element has ancestor with given css-class * @param element Element to check * @param ancestorClass class * @return True if the first element has an ancestor with given class. */ Utils.isClassDescendant = function (element, ancestorClass) { if (!element) { return false; } var parent = element; while (parent) { if (parent && parent.classList && parent.classList.contains(ancestorClass)) { return true; } parent = parent.parentNode; } return false; }; /** * Moving item inside the array. * @param arr Array. * @param oldIndex Old index. * @param newIndex New index. */ Utils.moveArrayItem = function (arr, oldIndex, newIndex) { if (newIndex >= arr.length) { var k = newIndex - arr.length + 1; while (k--) { arr.push(undefined); } } arr.splice(newIndex, 0, arr.splice(oldIndex, 1)[0]); }; /** * Removes html tags from the string. * @param txt String to remove tags * @return String without tags. */ Utils.htmlToPlaintext = function (txt) { var re = new RegExp('<[^>]+>', 'gm'); return txt ? String(txt).replace(re, '') : ''; }; Object.defineProperty(Utils, "userAgent", { get: function () { return navigator.userAgent || navigator.vendor; }, enumerable: true, configurable: true }); Utils.detectIOS = function () { var re = new RegExp('iPad|iPhone|iPod'); return re.test(Utils.userAgent); }; Utils.detectFirefox = function () { return Utils.userAgent.toLowerCase().indexOf('firefox') > -1; }; Utils.detectSafari = function () { var safari = Utils.userAgent.toLowerCase().indexOf('safari'); var chrome = Utils.userAgent.toLowerCase().indexOf('chrome'); return safari > -1 && chrome <= -1; }; Utils.detectAndroid = function () { var re = new RegExp('android', 'i'); return re.test(Utils.userAgent); }; Utils.detectMobile = function () { return Utils.detectIOS() || Utils.detectAndroid(); }; Utils.detectIE = function () { // Does not include EDGE var re = new RegExp('MSIE|Trident', 'i'); return re.test(Utils.userAgent); }; /** * Returns date if it is valid or null if not. */ Utils.purifyDate = function (v) { if (v === undefined || v === null || isNaN(v.getTime())) { return null; } return v; }; return Utils; }()); export { Utils };