Skip to content

Commit 38f7902

Browse files
committed
refact: @select passes around only iDs
1 parent e9b1c23 commit 38f7902

File tree

6 files changed

+47
-49
lines changed

6 files changed

+47
-49
lines changed

panel/src/components/Collection/Collection.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
@item="$emit('item', $event)"
2626
@option="onOption"
2727
@select="onSelect"
28-
@selected="onSelected"
2928
@sort="$emit('sort', $event)"
3029
>
3130
<template v-if="$slots.options" #options="{ item, index }">
@@ -147,9 +146,6 @@ export default {
147146
},
148147
onSelect(...args) {
149148
this.$emit("select", ...args);
150-
},
151-
onSelected(...args) {
152-
this.$emit("selected", ...args);
153149
}
154150
}
155151
};

panel/src/components/Collection/Items.vue

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:class="$attrs.class"
77
:style="$attrs.style"
88
@change="$emit('change', $event)"
9-
@select="onSelect"
9+
@select="$emit('select', $event)"
1010
@sort="$emit('sort', $event)"
1111
@option="onOption"
1212
>
@@ -48,7 +48,7 @@
4848
@drag="onDragStart($event, item.dragText)"
4949
@mouseover="$emit('hover', $event, item, itemIndex)"
5050
@option="onOption($event, item, itemIndex)"
51-
@select="onSelect(item, itemIndex)"
51+
@select="onSelect(item)"
5252
>
5353
<template v-if="$slots.options" #options>
5454
<slot name="options" v-bind="{ item, index: itemIndex }" />
@@ -62,28 +62,6 @@
6262
<script>
6363
import { layout } from "@/mixins/props.js";
6464
65-
export function findSelectedIndex(items, item) {
66-
return items.findIndex((selected) =>
67-
Object.hasOwn(selected, "_id")
68-
? selected._id === item._id
69-
: selected.id === item.id
70-
);
71-
}
72-
73-
export function updateSelection(items, item, mode = "multiple") {
74-
if (mode === "single") {
75-
return [item];
76-
}
77-
78-
const index = findSelectedIndex(items, item);
79-
80-
if (index !== -1) {
81-
return items.toSpliced(index, 1);
82-
}
83-
84-
return [...items, item];
85-
}
86-
8765
/**
8866
* Collection items that can be displayed in various layouts
8967
*/
@@ -173,7 +151,7 @@ export default {
173151
default: () => ({})
174152
}
175153
},
176-
emits: ["change", "hover", "item", "option", "select", "selected", "sort"],
154+
emits: ["change", "hover", "item", "option", "select", "sort"],
177155
computed: {
178156
dragOptions() {
179157
return {
@@ -205,24 +183,32 @@ export default {
205183
}
206184
},
207185
methods: {
186+
findSelectedIndex(item) {
187+
return this.selected.findIndex(
188+
(selected) => selected === item._id ?? item.id
189+
);
190+
},
208191
isSelected(item) {
209-
return findSelectedIndex(this.selected, item) !== -1;
192+
return this.findSelectedIndex(item) !== -1;
210193
},
211194
onDragStart($event, dragText) {
212195
this.$panel.drag.start("text", dragText);
213196
},
214197
onOption(option, item, itemIndex) {
215198
this.$emit("option", option, item, itemIndex);
216199
},
217-
onSelect(item, itemIndex) {
218-
this.$emit("select", item, itemIndex);
219-
this.onSelected(item);
220-
},
221-
onSelected(item) {
222-
this.$emit(
223-
"selected",
224-
updateSelection(this.selected, item, this.selectmode)
225-
);
200+
onSelect(item) {
201+
if (this.selectmode === "single") {
202+
this.$emit("select", [item._id ?? item.id]);
203+
}
204+
205+
const index = this.findSelectedIndex(item);
206+
207+
if (index !== -1) {
208+
return this.selected.toSpliced(index, 1);
209+
}
210+
211+
return this.$emit("select", [...this.selected, item._id ?? item.id]);
226212
},
227213
imageOptions(item) {
228214
let globalOptions = this.image;

panel/src/components/Forms/Field/StructureField.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,9 @@ export default {
561561
},
562562
563563
removeSelected() {
564-
this.items = this.items.filter((item) => !this.selected.includes(item));
564+
this.items = this.items.filter(
565+
(item) => this.selected.includes(item._id) === false
566+
);
565567
this.save();
566568
},
567569

panel/src/components/Layout/Table.vue

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
:disabled="row.selectable === false"
146146
:type="selectmode === 'single' ? 'radio' : 'checkbox'"
147147
:checked="isSelected(row)"
148-
@change="$emit('select', row, rowIndex)"
148+
@change="onSelect(row)"
149149
/>
150150
</label>
151151
</template>
@@ -174,8 +174,6 @@
174174
</template>
175175

176176
<script>
177-
import { findSelectedIndex } from "@/components/Collection/Items.vue";
178-
179177
/**
180178
* A simple table component with columns and rows
181179
*/
@@ -334,6 +332,11 @@ export default {
334332
}
335333
},
336334
methods: {
335+
findSelectedIndex(row) {
336+
return this.selected.findIndex(
337+
(selected) => selected === row._id ?? row.id
338+
);
339+
},
337340
/**
338341
* Checks if specific column is fully empty
339342
* @param {number} columnIndex
@@ -347,7 +350,7 @@ export default {
347350
);
348351
},
349352
isSelected(row) {
350-
return findSelectedIndex(this.selected, row) !== -1;
353+
return this.findSelectedIndex(row) !== -1;
351354
},
352355
/**
353356
* Returns label for a column
@@ -396,6 +399,19 @@ export default {
396399
onOption(option, row, rowIndex) {
397400
this.$emit("option", option, row, rowIndex);
398401
},
402+
onSelect(row) {
403+
if (this.selectmode === "single") {
404+
this.$emit("select", [row._id ?? row.id]);
405+
}
406+
407+
const index = this.findSelectedIndex(row);
408+
409+
if (index !== -1) {
410+
return this.selected.toSpliced(index, 1);
411+
}
412+
413+
return this.$emit("select", [...this.selected, row._id ?? row.id]);
414+
},
399415
/**
400416
* When the table has been sorted,
401417
* emit all items in new order

panel/src/components/Sections/ModelsSection.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export default {
229229
await this.$api.delete(
230230
this.parent + "/sections/" + this.name + "/delete",
231231
{
232-
ids: this.selected.map((item) => item.id)
232+
ids: this.selected
233233
}
234234
);
235235
} catch (error) {

panel/src/mixins/batchEditing.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { updateSelection } from "@/components/Collection/Items.vue";
2-
31
/**
42
* The Batch Editing mixin is intended for all components
53
* that want to introduce batch editing capabilities. It provides the
@@ -82,8 +80,8 @@ export default {
8280
onBatchDelete() {
8381
throw new Error("Not implemented");
8482
},
85-
onSelect(item) {
86-
this.selected = updateSelection(this.selected, item);
83+
onSelect(ids) {
84+
this.selected = ids;
8785
},
8886
onSelectToggle() {
8987
this.isSelecting ? this.stopSelecting() : this.startSelecting();

0 commit comments

Comments
 (0)