4 import {htmlToDom} from "../services/dom";
6 class EntityPermissions {
9 this.container = this.$el;
10 this.entityType = this.$opts.entityType;
12 this.everyoneInheritToggle = this.$refs.everyoneInherit;
13 this.roleSelect = this.$refs.roleSelect;
14 this.roleContainer = this.$refs.roleContainer;
16 this.setupListeners();
20 // "Everyone Else" inherit toggle
21 this.everyoneInheritToggle.addEventListener('change', event => {
22 const inherit = event.target.checked;
23 const permissions = document.querySelectorAll('input[name^="permissions[0]["]');
24 for (const permission of permissions) {
25 permission.disabled = inherit;
26 permission.checked = false;
30 // Remove role row button click
31 this.container.addEventListener('click', event => {
32 const button = event.target.closest('button');
33 if (button && button.dataset.roleId) {
34 this.removeRowOnButtonClick(button)
39 this.roleSelect.addEventListener('change', event => {
40 const roleId = this.roleSelect.value;
42 this.addRoleRow(roleId);
47 async addRoleRow(roleId) {
48 this.roleSelect.disabled = true;
50 // Remove option from select
51 const option = this.roleSelect.querySelector(`option[value="${roleId}"]`);
56 // Get and insert new row
57 const resp = await window.$http.get(`/permissions/form-row/${this.entityType}/${roleId}`);
58 const row = htmlToDom(resp.data);
59 this.roleContainer.append(row);
61 this.roleSelect.disabled = false;
64 removeRowOnButtonClick(button) {
65 const row = button.closest('.content-permissions-row');
66 const roleId = button.dataset.roleId;
67 const roleName = button.dataset.roleName;
69 const option = document.createElement('option');
70 option.value = roleId;
71 option.textContent = roleName;
73 this.roleSelect.append(option);
79 export default EntityPermissions;