|
| 1 | +import Vue from 'vue'; |
| 2 | +import { |
| 3 | + RuleSet, QueryBuilderGroup, ComponentRegistration, MergeTrap as MergeTrapInterface, Rule, |
| 4 | +} from '@/types'; |
| 5 | + |
| 6 | +function getNextGroup(group: QueryBuilderGroup): QueryBuilderGroup { |
| 7 | + if (group.depth < 1) { |
| 8 | + return group; |
| 9 | + } |
| 10 | + |
| 11 | + let vm: Vue = group; |
| 12 | + |
| 13 | + do { |
| 14 | + vm = vm.$parent; |
| 15 | + } while (vm.$options.name !== 'QueryBuilderGroup'); |
| 16 | + |
| 17 | + return vm as QueryBuilderGroup; |
| 18 | +} |
| 19 | + |
| 20 | +function getCommonAncestor( |
| 21 | + nodeA: QueryBuilderGroup, |
| 22 | + nodeB: QueryBuilderGroup, |
| 23 | +): QueryBuilderGroup { |
| 24 | + let a = nodeA; |
| 25 | + let b = nodeB; |
| 26 | + |
| 27 | + if (a.depth !== b.depth) { |
| 28 | + let lower: QueryBuilderGroup = a.depth > b.depth ? a : b; |
| 29 | + const higher: QueryBuilderGroup = a.depth < b.depth ? a : b; |
| 30 | + |
| 31 | + while (lower.depth !== higher.depth) { |
| 32 | + lower = getNextGroup(lower); |
| 33 | + } |
| 34 | + |
| 35 | + // Now both operate on the same level. |
| 36 | + a = lower; |
| 37 | + b = higher; |
| 38 | + } |
| 39 | + |
| 40 | + while (a !== b) { |
| 41 | + a = getNextGroup(a); |
| 42 | + b = getNextGroup(b); |
| 43 | + } |
| 44 | + |
| 45 | + return a; |
| 46 | +} |
| 47 | + |
| 48 | +function triggerUpdate(adder: ComponentRegistration, remover: ComponentRegistration): void { |
| 49 | + const commonAncestor = getCommonAncestor(adder.component, remover.component); |
| 50 | + |
| 51 | + if (![adder.component, remover.component].includes(commonAncestor)) { |
| 52 | + mergeViaParent(commonAncestor, adder, remover); |
| 53 | + |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + mergeViaNode(commonAncestor, adder, remover); |
| 58 | +} |
| 59 | + |
| 60 | +function mergeViaParent( |
| 61 | + commonAncestor: QueryBuilderGroup, |
| 62 | + adder: ComponentRegistration, |
| 63 | + remover: ComponentRegistration, |
| 64 | +): void { |
| 65 | + let children: Array<RuleSet | Rule> | null = null; |
| 66 | + |
| 67 | + commonAncestor.trap = (position: number, newChild: RuleSet | Rule): void => { |
| 68 | + if (children === null) { |
| 69 | + children = [...commonAncestor.children]; |
| 70 | + children.splice(position, 1, newChild); |
| 71 | + |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + commonAncestor.trap = null; |
| 76 | + |
| 77 | + children.splice(position, 1, newChild); |
| 78 | + |
| 79 | + commonAncestor.$emit( |
| 80 | + 'query-update', |
| 81 | + { |
| 82 | + operatorIdentifier: commonAncestor.selectedOperator, |
| 83 | + children, |
| 84 | + } as RuleSet, |
| 85 | + ); |
| 86 | + }; |
| 87 | + |
| 88 | + adder.component.$emit('query-update', adder.ev); |
| 89 | + remover.component.$emit('query-update', remover.ev); |
| 90 | +} |
| 91 | + |
| 92 | +function mergeViaNode( |
| 93 | + parentEmitter: QueryBuilderGroup, |
| 94 | + adder: ComponentRegistration, |
| 95 | + remover: ComponentRegistration, |
| 96 | +): void { |
| 97 | + const childEmitter = parentEmitter === adder.component ? remover : adder; |
| 98 | + const children = [...parentEmitter.children]; |
| 99 | + |
| 100 | + parentEmitter.trap = (position: number, newChild: RuleSet | Rule): void => { |
| 101 | + parentEmitter.trap = null; // Release trap |
| 102 | + children.splice(position, 1, newChild); // First... accept the update from the child |
| 103 | + |
| 104 | + // Now we'd need to know if the update on the parent is an add or remove action. |
| 105 | + if (parentEmitter === adder.component) { |
| 106 | + // Parent emitter is adding and child is removing an item. |
| 107 | + // |
| 108 | + // First, use the event from the child to patch the current state (see above), |
| 109 | + // then use the state from the adder for inserting at the right idx. |
| 110 | + children.splice(adder.affectedIdx, 0, adder.ev.children[adder.affectedIdx]); |
| 111 | + } else { |
| 112 | + // Parent emitter is removing and child is adding an item. |
| 113 | + // |
| 114 | + // Use the event from the child to patch the current state (see above), |
| 115 | + // then use the state from the remover to remove the correct item. |
| 116 | + children.splice(remover.affectedIdx, 1); |
| 117 | + } |
| 118 | + |
| 119 | + parentEmitter.$emit( |
| 120 | + 'query-update', |
| 121 | + { |
| 122 | + operatorIdentifier: parentEmitter.selectedOperator, |
| 123 | + children, |
| 124 | + } as RuleSet, |
| 125 | + ); |
| 126 | + }; |
| 127 | + |
| 128 | + childEmitter.component.$emit('query-update', childEmitter.ev); |
| 129 | +} |
| 130 | + |
| 131 | +export default class MergeTrap implements MergeTrapInterface { |
| 132 | + private eventBus: Vue |
| 133 | + |
| 134 | + constructor() { |
| 135 | + this.eventBus = new Vue(); |
| 136 | + |
| 137 | + Promise.all<ComponentRegistration>([ |
| 138 | + new Promise(res => this.eventBus.$once('adderRegistered', res)), |
| 139 | + new Promise(res => this.eventBus.$once('removerRegistered', res)), |
| 140 | + ]) |
| 141 | + .then((args: ComponentRegistration[]) => triggerUpdate(args[0], args[1])); |
| 142 | + } |
| 143 | + |
| 144 | + public registerSortUpdate(update: ComponentRegistration): void { |
| 145 | + if (update.adding) { |
| 146 | + return this.registerAdder(update); |
| 147 | + } |
| 148 | + |
| 149 | + return this.registerRemover(update); |
| 150 | + } |
| 151 | + |
| 152 | + protected registerAdder(ev: ComponentRegistration): void { |
| 153 | + this.eventBus.$emit('adderRegistered', ev); |
| 154 | + } |
| 155 | + |
| 156 | + protected registerRemover(ev: ComponentRegistration): void { |
| 157 | + this.eventBus.$emit('removerRegistered', ev); |
| 158 | + } |
| 159 | +} |
0 commit comments