Skip to content

Commit a59e572

Browse files
authored
[6.x] Permissions Check/Uncheck All buttons (#13762)
* Button to check all permissions in a section * Uncheck All per section and a master Un/Check All button * This button only relevant if making a not-super role * Oops, that's supposed to be over here
1 parent 715abc5 commit a59e572

4 files changed

Lines changed: 77 additions & 5 deletions

File tree

resources/js/components/roles/PermissionTree.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ export default {
3636
};
3737
},
3838
39+
watch: {
40+
initialPermissions: {
41+
handler(newPermissions) {
42+
this.permissions = newPermissions;
43+
},
44+
deep: true,
45+
},
46+
},
47+
3948
methods: {
4049
updatePermission(permission, checked) {
4150
permission.checked = checked;

resources/js/components/roles/PublishForm.vue

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
prioritize
1010
v-slot="{ text, action }"
1111
>
12+
<Button
13+
v-if="!isSuper"
14+
:icon="areAllCheckedInAllGroups() ? 'checkbox-uncheck' : 'checkbox'"
15+
@click="toggleAllInAllGroups()"
16+
:text="areAllCheckedInAllGroups() ? __('Uncheck All') : __('Check All')"
17+
/>
1218
<Button type="submit" variant="primary" @click="action" :text="text" />
1319
</CommandPaletteItem>
1420
</Header>
@@ -49,15 +55,27 @@
4955
</Panel>
5056

5157
<div v-if="!isSuper" class="space-y-6 mt-6">
52-
<CardPanel v-for="group in permissions" :key="group.handle" :heading="group.label">
53-
<PermissionTree :depth="1" :initial-permissions="group.permissions" />
54-
</CardPanel>
58+
<Panel :heading="group.label" v-for="group in permissions" :key="group.handle">
59+
<template #header-actions>
60+
<Button
61+
size="sm"
62+
variant="subtle"
63+
:icon="areAllChecked(group) ? 'checkbox-uncheck' : 'checkbox'"
64+
@click="toggleAllInGroup(group)"
65+
>
66+
{{ areAllChecked(group) ? __('Uncheck All') : __('Check All') }}
67+
</Button>
68+
</template>
69+
<Card>
70+
<PermissionTree :depth="1" :initial-permissions="group.permissions" />
71+
</Card>
72+
</Panel>
5573
</div>
5674
</div>
5775
</template>
5876

5977
<script>
60-
import { Header, Button, CardPanel, Panel, PanelHeader, Heading, Card, Switch, Field, Input, CommandPaletteItem } from '@/components/ui';
78+
import { Header, Button, Panel, PanelHeader, Heading, Card, Switch, Field, Input, CommandPaletteItem } from '@/components/ui';
6179
import { requireElevatedSession } from '@/components/elevated-sessions';
6280
import PermissionTree from '@/components/roles/PermissionTree.vue';
6381
import { router } from '@inertiajs/vue3';
@@ -74,7 +92,6 @@ export default {
7492
PermissionTree,
7593
Header,
7694
Button,
77-
CardPanel,
7895
Panel,
7996
PanelHeader,
8097
Heading,
@@ -141,6 +158,50 @@ export default {
141158
},
142159
143160
methods: {
161+
areAllChecked(group) {
162+
const checkAll = (permissions) => {
163+
return permissions.every(permission => {
164+
const childrenChecked = permission.children && permission.children.length
165+
? checkAll(permission.children)
166+
: true;
167+
return permission.checked && childrenChecked;
168+
});
169+
};
170+
return checkAll(group.permissions);
171+
},
172+
173+
areAllCheckedInAllGroups() {
174+
return this.permissions.every(group => this.areAllChecked(group));
175+
},
176+
177+
toggleAllInGroup(group) {
178+
const allChecked = this.areAllChecked(group);
179+
const toggle = (permissions, checked) => {
180+
permissions.forEach(permission => {
181+
permission.checked = checked;
182+
if (permission.children && permission.children.length) {
183+
toggle(permission.children, checked);
184+
}
185+
});
186+
};
187+
toggle(group.permissions, !allChecked);
188+
},
189+
190+
toggleAllInAllGroups() {
191+
const allChecked = this.areAllCheckedInAllGroups();
192+
this.permissions.forEach(group => {
193+
const toggle = (permissions, checked) => {
194+
permissions.forEach(permission => {
195+
permission.checked = checked;
196+
if (permission.children && permission.children.length) {
197+
toggle(permission.children, checked);
198+
}
199+
});
200+
};
201+
toggle(group.permissions, !allChecked);
202+
});
203+
},
204+
144205
clearErrors() {
145206
this.error = null;
146207
this.errors = {};
Lines changed: 1 addition & 0 deletions
Loading

resources/svg/icons/checkbox.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)