diff --git a/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js b/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js index fce3d43cc..6b143a11d 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js +++ b/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js @@ -37,7 +37,7 @@ function loadDynamicPicker(element) { ':types="types" ' + ':picked="picked" ' + ':uniqid="uniqid" ' + - ':suggested="suggested" ' + + ':suggested="notPickedSuggested" ' + '@addNewEntity="addNewEntity" ' + '@removeEntity="removeEntity">', components: { @@ -52,8 +52,21 @@ function loadDynamicPicker(element) { suggested: suggested } }, + computed: { + notPickedSuggested() { + if (this.multiple) { + const pickedIds = new Set(); + for (const p of this.picked) { + pickedIds.add(`${p.type}${p.id}`); + } + return this.suggested.filter(e => !pickedIds.has(`${e.type}${e.id}`)) + } + + return this.suggested.filter(e => e.type !== this.picked.type && e.id !== e.picked.id); + } + }, methods: { - addNewEntity({entity, isSuggested = false}) { + addNewEntity({entity}) { if (this.multiple) { if (!this.picked.some(el => { return el.type === entity.type && el.id === entity.id; @@ -61,10 +74,6 @@ function loadDynamicPicker(element) { this.picked.push(entity); input.value = JSON.stringify(this.picked); console.log(entity) - if (isSuggested) { - const indexToRemove = this.suggested.findIndex(e => e.id === entity.id) - this.suggested.splice(indexToRemove, 1); - } } } else { if (!this.picked.some(el => { @@ -76,12 +85,12 @@ function loadDynamicPicker(element) { } } }, - removeEntity({entity, isSuggested = false}) { - this.picked = this.picked.filter(e => !(e.type === entity.type && e.id === entity.id)); - input.value = JSON.stringify(this.picked); - if (isSuggested) { + removeEntity({entity}) { + if (-1 === this.suggested.findIndex(e => e.type === entity.type && e.id === entity.id)) { this.suggested.push(entity); } + this.picked = this.picked.filter(e => !(e.type === entity.type && e.id === entity.id)); + input.value = JSON.stringify(this.picked); }, } }) diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/PickEntity/PickEntity.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/PickEntity/PickEntity.vue index 7d6d0bd83..0a39718e2 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/PickEntity/PickEntity.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/PickEntity/PickEntity.vue @@ -104,11 +104,11 @@ export default { }, methods: { addNewSuggested(entity) { - this.$emit('addNewEntity', {entity: entity, isSuggested: true}); + this.$emit('addNewEntity', {entity: entity}); }, addNewEntity({ selected, modal }) { selected.forEach((item) => { - this.$emit('addNewEntity', { entity: item.result }); + this.$emit('addNewEntity', { entity: item.result}); }, this ); this.$refs.addPersons.resetSearch(); // to cast child method @@ -118,7 +118,7 @@ export default { if (!this.$props.removableIfSet) { return; } - this.$emit('removeEntity',{ entity: entity, isSuggested: true }); + this.$emit('removeEntity',{ entity: entity }); } }, }