UNPKG

funda-ui

Version:

React components using pure Bootstrap 5+ which does not contain any external style and script libraries.

195 lines (188 loc) 11 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["RPB"] = factory(); else root["RPB"] = factory(); })(this, () => { return /******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ // The require scope /******/ var __webpack_require__ = {}; /******/ /************************************************************************/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "addTreeDepth": () => (/* binding */ addTreeDepth), /* harmony export */ "addTreeIndent": () => (/* binding */ addTreeIndent), /* harmony export */ "convertTree": () => (/* binding */ convertTree), /* harmony export */ "flatTree": () => (/* binding */ flatTree), /* harmony export */ "getAllDepth": () => (/* binding */ getAllDepth) /* harmony export */ }); function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } var _excluded = ["children"]; function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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 _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure " + obj); } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } /** * Convert Tree * @param {Array} arr - Flat array * @param {?string | ?number} parentId - Parent id * @param {?string} keyId - Key value of id. * @param {?string} keyParentId - Key value of parent id. * @returns Array */ function convertTree(arr) { var parentId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var keyId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'id'; var keyParentId = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'parent_id'; if (!parentId) { // If there is no parent id (when recursing for the first time), all parents will be queried return arr.filter(function (item) { return !item[keyParentId]; }).map(function (item) { // Query all child nodes by parent node ID item.children = convertTree(arr, item[keyId], keyId, keyParentId); return item; }); } else { return arr.filter(function (item) { return item[keyParentId] === parentId; }).map(function (item) { // Query all child nodes by parent node ID item.children = convertTree(arr, item[keyId], keyId, keyParentId); return item; }); } } /** * Flat tree * @param {Array} arr - Hierarchical array * @returns Array */ function flatTree(arr) { var flatData = function flatData(_ref) { var rest = _extends({}, (_objectDestructuringEmpty(_ref), _ref)); var _rest$children = rest.children, children = _rest$children === void 0 ? [] : _rest$children; return [_objectSpread({}, rest)].concat(_toConsumableArray(children.flatMap(flatData))); }; var result = arr.flatMap(flatData); //remove children from item result = result.map(function (item) { delete item.children; return item; }); return result; } /** * Get all depth * @param {Object} node * @returns Number */ function getAllDepth(arr) { var count = function count(children) { return children.reduce(function (depth, child) { return Math.max(depth, 1 + count(child.children)); // increment depth of children by 1, and compare it with accumulated depth of other children within the same element }, 0); //default value 0 that's returned if there are no children }; return count(arr); } /** * Add depth to each item in the tree * @param {Array} arr - Hierarchical array * @param {?string} keyId - Key value of id. * @param {?string} keyParentId - Key value of parent id. * @param {?number} depth - Depth of the item. * @returns Number */ function addTreeDepth(arr) { var keyId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'id'; var parentItem = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; var depth = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; return arr.reduce(function (acc, el) { var children = el.children, otherProps = _objectWithoutProperties(el, _excluded); acc.push(_objectSpread(_objectSpread({}, otherProps), {}, { parentItem: parentItem, depth: depth })); if (children) { return acc.concat(addTreeDepth(children, keyId, el[keyId], depth + 1)); } return acc; }, []); } /** * Add indent placeholder * @param {Array} arr - Flat array * @param {?string} placeholder - String of placeholder * @param {?string} lastPlaceholder - Last String of placeholder * @param {?string} keyName - Key value of name. * @returns Array */ function addTreeIndent(arr) { var placeholder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '&nbsp;&nbsp;&nbsp;&nbsp;'; var lastPlaceholder = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; var keyName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'label'; arr.forEach(function (item) { var indent = ''; if (item.depth) { Array(item.depth).fill(0).forEach(function (k, i) { indent += placeholder; if (i === item.depth - 1) { item[keyName] = indent + lastPlaceholder + item[keyName]; } }); } }); } /******/ return __webpack_exports__; /******/ })() ; });