selectStrategies.mjs 4.9 KB

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