Skip to content

Commit 3b8c410

Browse files
committed
Clean, functional, awesome nested add/remove of groups
1 parent 895dde7 commit 3b8c410

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

resources/js/calendar/nested_sortable_component.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ export default () => ({
1414
let condition = this.processCondition(["Year", "0", [0]], add_to_id);
1515
this.conditionMap[condition.id] = condition;
1616

17-
let parentGroup = this.conditionMap[add_to_id];
17+
if (add_to_id) {
18+
let parentGroup = this.conditionMap[add_to_id];
1819

19-
if (parentGroup) {
2020
if (parentGroup.children.length) {
2121
parentGroup.children[parentGroup.children.length - 1].operator = "&&"
2222
}
@@ -30,13 +30,31 @@ export default () => ({
3030
}
3131
},
3232

33-
addGroup(add_to_id) {
33+
deleteElement(element_id) {
34+
let element = this.conditionMap[element_id];
35+
let siblings = this.conditionMap[element.parent_id]?.children
36+
?? this.sortable_data;
37+
38+
let element_index = siblings.indexOf(element);
39+
40+
if (siblings.length > 1 && element_index === siblings.length - 1) {
41+
siblings[element_index - 1].operator = false
42+
}
43+
44+
siblings.splice(element_index, 1);
45+
46+
if (element.parent_id && siblings.length == 0) {
47+
this.deleteElement(element.parent_id);
48+
}
49+
},
50+
51+
addGroup(add_to_id = null) {
3452
let group = this.processGroup(["", []], add_to_id, 0);
3553
this.conditionMap[group.id] = group;
3654

37-
let parentGroup = this.conditionMap[add_to_id];
55+
if (add_to_id) {
56+
let parentGroup = this.conditionMap[add_to_id];
3857

39-
if (parentGroup) {
4058
if (parentGroup.children.length) {
4159
parentGroup.children[parentGroup.children.length - 1].operator = "&&"
4260
}
@@ -48,6 +66,8 @@ export default () => ({
4866

4967
this.sortable_data.push(group);
5068
}
69+
70+
this.addCondition(group.id);
5171
},
5272

5373
set sortableData(value) {
@@ -544,7 +564,7 @@ export default () => ({
544564
${condition_types}
545565
</select>
546566
${this.renderConditionOptions(condition)}
547-
<div @click="delete(${condition.id})" class="cursor-pointer order-last ml-2 mr-1.5"><i class="fa fa-trash"></i></div>
567+
<div @click="deleteElement('${condition.id}')" class="cursor-pointer order-last ml-2 mr-1.5"><i class="fa fa-trash"></i></div>
548568
</div>
549569
${condition.operator && (!parent || parent.type !== "num") ? this.renderOperator(condition) : ""}
550570
</li>`;

resources/views/layouts/event.blade.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ class='form-control'
209209
<template x-for="(element, index) in sortable_data" :key="element.id">
210210
<li class="list-none select-none" x-html="renderSortableElement(element, sortable_data, index)" :data-id="element.id"></li>
211211
</template>
212+
213+
<div class='flex mb-1'>
214+
<button type='button' @click="addCondition()" class='btn btn-outline-secondary full'>Add condition</button>
215+
<button type='button' @click="addGroup()" class='btn btn-outline-secondary full'>Add group</button>
216+
</div>
212217
</ul>
213218
<textarea class="w-full resize-none bg-gray-900 text-white" style="height:5000px;" placeholder="json code here" x-text="JSON.stringify(sortable_data, null, 2)"></textarea>
214219
</div>

0 commit comments

Comments
 (0)