selectStrategies.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* eslint-disable sonarjs/no-identical-functions */
  2. // Utilities
  3. import { toRaw } from 'vue';
  4. export const independentSelectStrategy = mandatory => {
  5. const strategy = {
  6. select: _ref => {
  7. let {
  8. id,
  9. value,
  10. selected
  11. } = _ref;
  12. id = toRaw(id);
  13. // When mandatory and we're trying to deselect when id
  14. // is the only currently selected item then do nothing
  15. if (mandatory && !value) {
  16. const on = Array.from(selected.entries()).reduce((arr, _ref2) => {
  17. let [key, value] = _ref2;
  18. return value === 'on' ? [...arr, key] : arr;
  19. }, []);
  20. if (on.length === 1 && on[0] === id) return selected;
  21. }
  22. selected.set(id, value ? 'on' : 'off');
  23. return selected;
  24. },
  25. in: (v, children, parents) => {
  26. let map = new Map();
  27. for (const id of v || []) {
  28. map = strategy.select({
  29. id,
  30. value: true,
  31. selected: new Map(map),
  32. children,
  33. parents
  34. });
  35. }
  36. return map;
  37. },
  38. out: v => {
  39. const arr = [];
  40. for (const [key, value] of v.entries()) {
  41. if (value === 'on') arr.push(key);
  42. }
  43. return arr;
  44. }
  45. };
  46. return strategy;
  47. };
  48. export const independentSingleSelectStrategy = mandatory => {
  49. const parentStrategy = independentSelectStrategy(mandatory);
  50. const strategy = {
  51. select: _ref3 => {
  52. let {
  53. selected,
  54. id,
  55. ...rest
  56. } = _ref3;
  57. id = toRaw(id);
  58. const singleSelected = selected.has(id) ? new Map([[id, selected.get(id)]]) : new Map();
  59. return parentStrategy.select({
  60. ...rest,
  61. id,
  62. selected: singleSelected
  63. });
  64. },
  65. in: (v, children, parents) => {
  66. let map = new Map();
  67. if (v?.length) {
  68. map = parentStrategy.in(v.slice(0, 1), children, parents);
  69. }
  70. return map;
  71. },
  72. out: (v, children, parents) => {
  73. return parentStrategy.out(v, children, parents);
  74. }
  75. };
  76. return strategy;
  77. };
  78. export const leafSelectStrategy = mandatory => {
  79. const parentStrategy = independentSelectStrategy(mandatory);
  80. const strategy = {
  81. select: _ref4 => {
  82. let {
  83. id,
  84. selected,
  85. children,
  86. ...rest
  87. } = _ref4;
  88. id = toRaw(id);
  89. if (children.has(id)) return selected;
  90. return parentStrategy.select({
  91. id,
  92. selected,
  93. children,
  94. ...rest
  95. });
  96. },
  97. in: parentStrategy.in,
  98. out: parentStrategy.out
  99. };
  100. return strategy;
  101. };
  102. export const leafSingleSelectStrategy = mandatory => {
  103. const parentStrategy = independentSingleSelectStrategy(mandatory);
  104. const strategy = {
  105. select: _ref5 => {
  106. let {
  107. id,
  108. selected,
  109. children,
  110. ...rest
  111. } = _ref5;
  112. id = toRaw(id);
  113. if (children.has(id)) return selected;
  114. return parentStrategy.select({
  115. id,
  116. selected,
  117. children,
  118. ...rest
  119. });
  120. },
  121. in: parentStrategy.in,
  122. out: parentStrategy.out
  123. };
  124. return strategy;
  125. };
  126. export const classicSelectStrategy = mandatory => {
  127. const strategy = {
  128. select: _ref6 => {
  129. let {
  130. id,
  131. value,
  132. selected,
  133. children,
  134. parents
  135. } = _ref6;
  136. id = toRaw(id);
  137. const original = new Map(selected);
  138. const items = [id];
  139. while (items.length) {
  140. const item = items.shift();
  141. selected.set(item, value ? 'on' : 'off');
  142. if (children.has(item)) {
  143. items.push(...children.get(item));
  144. }
  145. }
  146. let parent = parents.get(id);
  147. while (parent) {
  148. const childrenIds = children.get(parent);
  149. const everySelected = childrenIds.every(cid => selected.get(cid) === 'on');
  150. const noneSelected = childrenIds.every(cid => !selected.has(cid) || selected.get(cid) === 'off');
  151. selected.set(parent, everySelected ? 'on' : noneSelected ? 'off' : 'indeterminate');
  152. parent = parents.get(parent);
  153. }
  154. // If mandatory and planned deselect results in no selected
  155. // items then we can't do it, so return original state
  156. if (mandatory && !value) {
  157. const on = Array.from(selected.entries()).reduce((arr, _ref7) => {
  158. let [key, value] = _ref7;
  159. return value === 'on' ? [...arr, key] : arr;
  160. }, []);
  161. if (on.length === 0) return original;
  162. }
  163. return selected;
  164. },
  165. in: (v, children, parents) => {
  166. let map = new Map();
  167. for (const id of v || []) {
  168. map = strategy.select({
  169. id,
  170. value: true,
  171. selected: new Map(map),
  172. children,
  173. parents
  174. });
  175. }
  176. return map;
  177. },
  178. out: (v, children) => {
  179. const arr = [];
  180. for (const [key, value] of v.entries()) {
  181. if (value === 'on' && !children.has(key)) arr.push(key);
  182. }
  183. return arr;
  184. }
  185. };
  186. return strategy;
  187. };
  188. //# sourceMappingURL=selectStrategies.mjs.map