nested.mjs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Composables
  2. import { useProxiedModel } from "../proxiedModel.mjs"; // Utilities
  3. import { computed, inject, onBeforeMount, onBeforeUnmount, provide, ref, shallowRef, toRaw, toRef } from 'vue';
  4. import { independentActiveStrategy, independentSingleActiveStrategy, leafActiveStrategy, leafSingleActiveStrategy } from "./activeStrategies.mjs";
  5. import { listOpenStrategy, multipleOpenStrategy, singleOpenStrategy } from "./openStrategies.mjs";
  6. import { classicSelectStrategy, independentSelectStrategy, independentSingleSelectStrategy, leafSelectStrategy, leafSingleSelectStrategy } from "./selectStrategies.mjs";
  7. import { consoleError, getCurrentInstance, getUid, propsFactory } from "../../util/index.mjs"; // Types
  8. export const VNestedSymbol = Symbol.for('vuetify:nested');
  9. export const emptyNested = {
  10. id: shallowRef(),
  11. root: {
  12. register: () => null,
  13. unregister: () => null,
  14. parents: ref(new Map()),
  15. children: ref(new Map()),
  16. open: () => null,
  17. openOnSelect: () => null,
  18. activate: () => null,
  19. select: () => null,
  20. activatable: ref(false),
  21. selectable: ref(false),
  22. opened: ref(new Set()),
  23. activated: ref(new Set()),
  24. selected: ref(new Map()),
  25. selectedValues: ref([]),
  26. getPath: () => []
  27. }
  28. };
  29. export const makeNestedProps = propsFactory({
  30. activatable: Boolean,
  31. selectable: Boolean,
  32. activeStrategy: [String, Function, Object],
  33. selectStrategy: [String, Function, Object],
  34. openStrategy: [String, Object],
  35. opened: null,
  36. activated: null,
  37. selected: null,
  38. mandatory: Boolean
  39. }, 'nested');
  40. export const useNested = props => {
  41. let isUnmounted = false;
  42. const children = ref(new Map());
  43. const parents = ref(new Map());
  44. const opened = useProxiedModel(props, 'opened', props.opened, v => new Set(v), v => [...v.values()]);
  45. const activeStrategy = computed(() => {
  46. if (typeof props.activeStrategy === 'object') return props.activeStrategy;
  47. if (typeof props.activeStrategy === 'function') return props.activeStrategy(props.mandatory);
  48. switch (props.activeStrategy) {
  49. case 'leaf':
  50. return leafActiveStrategy(props.mandatory);
  51. case 'single-leaf':
  52. return leafSingleActiveStrategy(props.mandatory);
  53. case 'independent':
  54. return independentActiveStrategy(props.mandatory);
  55. case 'single-independent':
  56. default:
  57. return independentSingleActiveStrategy(props.mandatory);
  58. }
  59. });
  60. const selectStrategy = computed(() => {
  61. if (typeof props.selectStrategy === 'object') return props.selectStrategy;
  62. if (typeof props.selectStrategy === 'function') return props.selectStrategy(props.mandatory);
  63. switch (props.selectStrategy) {
  64. case 'single-leaf':
  65. return leafSingleSelectStrategy(props.mandatory);
  66. case 'leaf':
  67. return leafSelectStrategy(props.mandatory);
  68. case 'independent':
  69. return independentSelectStrategy(props.mandatory);
  70. case 'single-independent':
  71. return independentSingleSelectStrategy(props.mandatory);
  72. case 'classic':
  73. default:
  74. return classicSelectStrategy(props.mandatory);
  75. }
  76. });
  77. const openStrategy = computed(() => {
  78. if (typeof props.openStrategy === 'object') return props.openStrategy;
  79. switch (props.openStrategy) {
  80. case 'list':
  81. return listOpenStrategy;
  82. case 'single':
  83. return singleOpenStrategy;
  84. case 'multiple':
  85. default:
  86. return multipleOpenStrategy;
  87. }
  88. });
  89. const activated = useProxiedModel(props, 'activated', props.activated, v => activeStrategy.value.in(v, children.value, parents.value), v => activeStrategy.value.out(v, children.value, parents.value));
  90. const selected = useProxiedModel(props, 'selected', props.selected, v => selectStrategy.value.in(v, children.value, parents.value), v => selectStrategy.value.out(v, children.value, parents.value));
  91. onBeforeUnmount(() => {
  92. isUnmounted = true;
  93. });
  94. function getPath(id) {
  95. const path = [];
  96. let parent = id;
  97. while (parent != null) {
  98. path.unshift(parent);
  99. parent = parents.value.get(parent);
  100. }
  101. return path;
  102. }
  103. const vm = getCurrentInstance('nested');
  104. const nodeIds = new Set();
  105. const nested = {
  106. id: shallowRef(),
  107. root: {
  108. opened,
  109. activatable: toRef(props, 'activatable'),
  110. selectable: toRef(props, 'selectable'),
  111. activated,
  112. selected,
  113. selectedValues: computed(() => {
  114. const arr = [];
  115. for (const [key, value] of selected.value.entries()) {
  116. if (value === 'on') arr.push(key);
  117. }
  118. return arr;
  119. }),
  120. register: (id, parentId, isGroup) => {
  121. if (nodeIds.has(id)) {
  122. const path = getPath(id).map(String).join(' -> ');
  123. const newPath = getPath(parentId).concat(id).map(String).join(' -> ');
  124. consoleError(`Multiple nodes with the same ID\n\t${path}\n\t${newPath}`);
  125. return;
  126. } else {
  127. nodeIds.add(id);
  128. }
  129. parentId && id !== parentId && parents.value.set(id, parentId);
  130. isGroup && children.value.set(id, []);
  131. if (parentId != null) {
  132. children.value.set(parentId, [...(children.value.get(parentId) || []), id]);
  133. }
  134. },
  135. unregister: id => {
  136. if (isUnmounted) return;
  137. nodeIds.delete(id);
  138. children.value.delete(id);
  139. const parent = parents.value.get(id);
  140. if (parent) {
  141. const list = children.value.get(parent) ?? [];
  142. children.value.set(parent, list.filter(child => child !== id));
  143. }
  144. parents.value.delete(id);
  145. },
  146. open: (id, value, event) => {
  147. vm.emit('click:open', {
  148. id,
  149. value,
  150. path: getPath(id),
  151. event
  152. });
  153. const newOpened = openStrategy.value.open({
  154. id,
  155. value,
  156. opened: new Set(opened.value),
  157. children: children.value,
  158. parents: parents.value,
  159. event
  160. });
  161. newOpened && (opened.value = newOpened);
  162. },
  163. openOnSelect: (id, value, event) => {
  164. const newOpened = openStrategy.value.select({
  165. id,
  166. value,
  167. selected: new Map(selected.value),
  168. opened: new Set(opened.value),
  169. children: children.value,
  170. parents: parents.value,
  171. event
  172. });
  173. newOpened && (opened.value = newOpened);
  174. },
  175. select: (id, value, event) => {
  176. vm.emit('click:select', {
  177. id,
  178. value,
  179. path: getPath(id),
  180. event
  181. });
  182. const newSelected = selectStrategy.value.select({
  183. id,
  184. value,
  185. selected: new Map(selected.value),
  186. children: children.value,
  187. parents: parents.value,
  188. event
  189. });
  190. newSelected && (selected.value = newSelected);
  191. nested.root.openOnSelect(id, value, event);
  192. },
  193. activate: (id, value, event) => {
  194. if (!props.activatable) {
  195. return nested.root.select(id, true, event);
  196. }
  197. vm.emit('click:activate', {
  198. id,
  199. value,
  200. path: getPath(id),
  201. event
  202. });
  203. const newActivated = activeStrategy.value.activate({
  204. id,
  205. value,
  206. activated: new Set(activated.value),
  207. children: children.value,
  208. parents: parents.value,
  209. event
  210. });
  211. newActivated && (activated.value = newActivated);
  212. },
  213. children,
  214. parents,
  215. getPath
  216. }
  217. };
  218. provide(VNestedSymbol, nested);
  219. return nested.root;
  220. };
  221. export const useNestedItem = (id, isGroup) => {
  222. const parent = inject(VNestedSymbol, emptyNested);
  223. const uidSymbol = Symbol(getUid());
  224. const computedId = computed(() => id.value !== undefined ? id.value : uidSymbol);
  225. const item = {
  226. ...parent,
  227. id: computedId,
  228. open: (open, e) => parent.root.open(computedId.value, open, e),
  229. openOnSelect: (open, e) => parent.root.openOnSelect(computedId.value, open, e),
  230. isOpen: computed(() => parent.root.opened.value.has(computedId.value)),
  231. parent: computed(() => parent.root.parents.value.get(computedId.value)),
  232. activate: (activated, e) => parent.root.activate(computedId.value, activated, e),
  233. isActivated: computed(() => parent.root.activated.value.has(toRaw(computedId.value))),
  234. select: (selected, e) => parent.root.select(computedId.value, selected, e),
  235. isSelected: computed(() => parent.root.selected.value.get(toRaw(computedId.value)) === 'on'),
  236. isIndeterminate: computed(() => parent.root.selected.value.get(computedId.value) === 'indeterminate'),
  237. isLeaf: computed(() => !parent.root.children.value.get(computedId.value)),
  238. isGroupActivator: parent.isGroupActivator
  239. };
  240. onBeforeMount(() => {
  241. !parent.isGroupActivator && parent.root.register(computedId.value, parent.id.value, isGroup);
  242. });
  243. onBeforeUnmount(() => {
  244. !parent.isGroupActivator && parent.root.unregister(computedId.value);
  245. });
  246. isGroup && provide(VNestedSymbol, item);
  247. return item;
  248. };
  249. export const useNestedGroupActivator = () => {
  250. const parent = inject(VNestedSymbol, emptyNested);
  251. provide(VNestedSymbol, {
  252. ...parent,
  253. isGroupActivator: true
  254. });
  255. };
  256. //# sourceMappingURL=nested.mjs.map