commonUtil.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.VALUE_SPLIT = exports.SHOW_PARENT = exports.SHOW_CHILD = void 0;
  6. exports.fillFieldNames = fillFieldNames;
  7. exports.isLeaf = isLeaf;
  8. exports.scrollIntoParentView = scrollIntoParentView;
  9. exports.toPathKey = toPathKey;
  10. exports.toPathKeys = toPathKeys;
  11. exports.toPathValueStr = toPathValueStr;
  12. const VALUE_SPLIT = exports.VALUE_SPLIT = '__RC_CASCADER_SPLIT__';
  13. const SHOW_PARENT = exports.SHOW_PARENT = 'SHOW_PARENT';
  14. const SHOW_CHILD = exports.SHOW_CHILD = 'SHOW_CHILD';
  15. function toPathKey(value) {
  16. return value.join(VALUE_SPLIT);
  17. }
  18. function toPathKeys(value) {
  19. return value.map(toPathKey);
  20. }
  21. function toPathValueStr(pathKey) {
  22. return pathKey.split(VALUE_SPLIT);
  23. }
  24. function fillFieldNames(fieldNames) {
  25. const {
  26. label,
  27. value,
  28. children
  29. } = fieldNames || {};
  30. const val = value || 'value';
  31. return {
  32. label: label || 'label',
  33. value: val,
  34. key: val,
  35. children: children || 'children'
  36. };
  37. }
  38. function isLeaf(option, fieldNames) {
  39. var _a, _b;
  40. return (_a = option.isLeaf) !== null && _a !== void 0 ? _a : !((_b = option[fieldNames.children]) === null || _b === void 0 ? void 0 : _b.length);
  41. }
  42. function scrollIntoParentView(element) {
  43. const parent = element.parentElement;
  44. if (!parent) {
  45. return;
  46. }
  47. const elementToParent = element.offsetTop - parent.offsetTop; // offsetParent may not be parent.
  48. if (elementToParent - parent.scrollTop < 0) {
  49. parent.scrollTo({
  50. top: elementToParent
  51. });
  52. } else if (elementToParent + element.offsetHeight - parent.scrollTop > parent.offsetHeight) {
  53. parent.scrollTo({
  54. top: elementToParent + element.offsetHeight - parent.offsetHeight
  55. });
  56. }
  57. }