VciTree.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import VciTreeItem from "./VciTreeItem.vue";
  2. import { isEmpty } from "@vci/helper/src/other";
  3. import Vue from "vue";
  4. import { uuid } from "@vci/helper/src/string";
  5. function scopeSlotChildren(h, context) {
  6. return function (props) {
  7. const parent = props.item;
  8. if(props.children && props.children.length > 0) return h(
  9. "div",
  10. { class: "VciTreeChildren" },
  11. props.children.map(item => {
  12. return h(
  13. VciTreeItem,
  14. {
  15. props: {
  16. item,
  17. parent,
  18. enableSelection: context.props.enableSelection,
  19. enableSelectionItem: context.props.enableSelectionItem
  20. },
  21. on: context.listeners,
  22. scopedSlots: {
  23. ...context.scopedSlots,
  24. children: scopeSlotChildren(h, context)
  25. }
  26. }
  27. );
  28. })
  29. );
  30. };
  31. }
  32. function formatList(list, parent = null) {
  33. list.forEach(item => {
  34. item.$parent = parent;
  35. if(isEmpty(item.id)) Vue.set(item, "id", uuid());
  36. if(isEmpty(item.checked)) Vue.set(item, "checked", true);
  37. if(isEmpty(item.folding)) Vue.set(item, "folding", false);
  38. item.children && item.children.length > 0 && formatList(item.children, item);
  39. });
  40. }
  41. function handleChangeCheckedUp(item) {
  42. const parent = item.$parent;
  43. if(parent) {
  44. const children = parent.children;
  45. parent.checked = children.filter(item => item.checked).length > 0;
  46. handleChangeCheckedUp(parent);
  47. }
  48. }
  49. function handleChangeCheckedDown(item, checked) {
  50. const children = item.children;
  51. children && children.length > 0 && children.forEach(child => {
  52. child.checked = checked;
  53. handleChangeCheckedDown(child, checked);
  54. });
  55. }
  56. export { scopeSlotChildren };
  57. export default {
  58. functional: true,
  59. props: {
  60. list: {
  61. type: Array,
  62. default: () => []
  63. },
  64. enableSelection: {
  65. type: Boolean,
  66. default: false
  67. },
  68. enableSelectionItem: {
  69. type: Boolean,
  70. default: false
  71. }
  72. },
  73. render(h, context) {
  74. formatList(context.props.list);
  75. if(context.listeners["change-checked"]) {
  76. const listener = context.listeners["change-checked"];
  77. context.listeners["change-checked"] = function (e) {
  78. handleChangeCheckedUp(e.item);
  79. handleChangeCheckedDown(e.item, e.checked);
  80. Array.isArray(listener) ? listener.forEach(l => l(e)) : listener(e);
  81. };
  82. }
  83. return h(
  84. "div",
  85. { class: "VciTree" },
  86. context.props.list.map(item => {
  87. return h(
  88. VciTreeItem,
  89. {
  90. props: {
  91. item,
  92. parent: null,
  93. enableSelection: context.props.enableSelection,
  94. enableSelectionItem: context.props.enableSelectionItem
  95. },
  96. on: context.listeners,
  97. scopedSlots: {
  98. ...context.scopedSlots,
  99. children: scopeSlotChildren(h, context)
  100. }
  101. }
  102. );
  103. })
  104. );
  105. }
  106. };