activeStrategies.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* eslint-disable sonarjs/no-identical-functions */
  2. // Utilities
  3. import { toRaw } from 'vue';
  4. import { wrapInArray } from "../../util/index.mjs";
  5. export const independentActiveStrategy = mandatory => {
  6. const strategy = {
  7. activate: _ref => {
  8. let {
  9. id,
  10. value,
  11. activated
  12. } = _ref;
  13. id = toRaw(id);
  14. // When mandatory and we're trying to deselect when id
  15. // is the only currently selected item then do nothing
  16. if (mandatory && !value && activated.size === 1 && activated.has(id)) return activated;
  17. if (value) {
  18. activated.add(id);
  19. } else {
  20. activated.delete(id);
  21. }
  22. return activated;
  23. },
  24. in: (v, children, parents) => {
  25. let set = new Set();
  26. if (v != null) {
  27. for (const id of wrapInArray(v)) {
  28. set = strategy.activate({
  29. id,
  30. value: true,
  31. activated: new Set(set),
  32. children,
  33. parents
  34. });
  35. }
  36. }
  37. return set;
  38. },
  39. out: v => {
  40. return Array.from(v);
  41. }
  42. };
  43. return strategy;
  44. };
  45. export const independentSingleActiveStrategy = mandatory => {
  46. const parentStrategy = independentActiveStrategy(mandatory);
  47. const strategy = {
  48. activate: _ref2 => {
  49. let {
  50. activated,
  51. id,
  52. ...rest
  53. } = _ref2;
  54. id = toRaw(id);
  55. const singleSelected = activated.has(id) ? new Set([id]) : new Set();
  56. return parentStrategy.activate({
  57. ...rest,
  58. id,
  59. activated: singleSelected
  60. });
  61. },
  62. in: (v, children, parents) => {
  63. let set = new Set();
  64. if (v != null) {
  65. const arr = wrapInArray(v);
  66. if (arr.length) {
  67. set = parentStrategy.in(arr.slice(0, 1), children, parents);
  68. }
  69. }
  70. return set;
  71. },
  72. out: (v, children, parents) => {
  73. return parentStrategy.out(v, children, parents);
  74. }
  75. };
  76. return strategy;
  77. };
  78. export const leafActiveStrategy = mandatory => {
  79. const parentStrategy = independentActiveStrategy(mandatory);
  80. const strategy = {
  81. activate: _ref3 => {
  82. let {
  83. id,
  84. activated,
  85. children,
  86. ...rest
  87. } = _ref3;
  88. id = toRaw(id);
  89. if (children.has(id)) return activated;
  90. return parentStrategy.activate({
  91. id,
  92. activated,
  93. children,
  94. ...rest
  95. });
  96. },
  97. in: parentStrategy.in,
  98. out: parentStrategy.out
  99. };
  100. return strategy;
  101. };
  102. export const leafSingleActiveStrategy = mandatory => {
  103. const parentStrategy = independentSingleActiveStrategy(mandatory);
  104. const strategy = {
  105. activate: _ref4 => {
  106. let {
  107. id,
  108. activated,
  109. children,
  110. ...rest
  111. } = _ref4;
  112. id = toRaw(id);
  113. if (children.has(id)) return activated;
  114. return parentStrategy.activate({
  115. id,
  116. activated,
  117. children,
  118. ...rest
  119. });
  120. },
  121. in: parentStrategy.in,
  122. out: parentStrategy.out
  123. };
  124. return strategy;
  125. };
  126. //# sourceMappingURL=activeStrategies.mjs.map