UNPKG

@eccenca/gui-elements

Version:

GUI elements based on other libraries, usable in React application, written in Typescript.

152 lines 6.39 kB
/** * Based on CSS Tricks tutorial. * @see https://css-tricks.com/how-to-get-all-custom-properties-on-a-page-in-javascript/ */ var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var CssCustomProperties = /** @class */ (function () { function CssCustomProperties(props) { if (props === void 0) { props = {}; } var _this = this; this.getterDefaultProps = {}; this.customprops = {}; // Methods this.customProperties = function (props) { if (props === void 0) { props = {}; } // FIXME: // in case of performance issues results should get saved at least into intern variables // other cache strategies could be also tested if (Object.keys(_this.customprops).length > 1) { return _this.customprops; } var customprops = CssCustomProperties.listCustomProperties(__assign(__assign({}, _this.getterDefaultProps), props)); _this.customprops = customprops; return customprops; }; this.getterDefaultProps = props; } CssCustomProperties.listLocalStylesheets = function () { if (document && document.styleSheets) { return Array.from(document.styleSheets).filter(function (stylesheet) { // is inline stylesheet or from same domain if (!stylesheet.href) { return true; } return stylesheet.href.indexOf(window.location.origin) === 0; }); } return []; }; CssCustomProperties.listLocalCssRules = function () { return CssCustomProperties.listLocalStylesheets() .map(function (stylesheet) { return Array.from(stylesheet.cssRules); }) .flat(); }; CssCustomProperties.listLocalCssStyleRules = function (filter) { if (filter === void 0) { filter = {}; } var _a = filter.cssRuleType, cssRuleType = _a === void 0 ? "CSSStyleRule" : _a, selectorText = filter.selectorText; var cssStyleRules = CssCustomProperties.listLocalCssRules().filter(function (rule) { var cssrule = rule; if (cssrule.style) { if (cssrule.constructor.name !== cssRuleType) { return false; } if (!!selectorText && cssrule.selectorText !== selectorText) { return false; } return true; } else { return false; } }); return cssStyleRules; }; CssCustomProperties.listLocalCssStyleRuleProperties = function (filter) { if (filter === void 0) { filter = {}; } var _a = filter.propertyType, propertyType = _a === void 0 ? "all" : _a, otherFilters = __rest(filter, ["propertyType"]); return CssCustomProperties.listLocalCssStyleRules(otherFilters) .map(function (cssrule) { return __spreadArray([], __read(cssrule.style), false).map(function (propertyname) { return [propertyname.trim(), cssrule.style.getPropertyValue(propertyname).trim()]; }); }) .flat() .filter(function (declaration) { switch (propertyType) { case "normal": return declaration[0].indexOf("--") !== 0; case "custom": return declaration[0].indexOf("--") === 0; } return true; // case "all" }); }; CssCustomProperties.listCustomProperties = function (props) { if (props === void 0) { props = {}; } var _a = props.removeDashPrefix, removeDashPrefix = _a === void 0 ? true : _a, _b = props.returnObject, returnObject = _b === void 0 ? true : _b, _c = props.filterName, filterName = _c === void 0 ? function () { return true; } : _c, filterProps = __rest(props, ["removeDashPrefix", "returnObject", "filterName"]); var customProperties = CssCustomProperties.listLocalCssStyleRuleProperties(__assign(__assign({}, filterProps), { propertyType: "custom" })) .filter(function (declaration) { return filterName(declaration[0]); }) .map(function (declaration) { if (removeDashPrefix) { return [declaration[0].substr(2), declaration[1]]; } return declaration; }); return returnObject ? Object.fromEntries(customProperties) : customProperties; }; return CssCustomProperties; }()); export default CssCustomProperties; //# sourceMappingURL=CssCustomProperties.js.map