1 import {htmlToDom} from "../services/dom";
2 import {Component} from "./component";
4 export class EntityPermissions extends Component {
7 this.container = this.$el;
8 this.entityType = this.$opts.entityType;
10 this.everyoneInheritToggle = this.$refs.everyoneInherit;
11 this.roleSelect = this.$refs.roleSelect;
12 this.roleContainer = this.$refs.roleContainer;
13 this.userContainer = this.$refs.userContainer;
14 this.userSelectContainer = this.$refs.userSelectContainer;
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.modelType) {
34 this.removeRowOnButtonClick(button)
39 this.roleSelect.addEventListener('change', event => {
40 const roleId = this.roleSelect.value;
42 this.addRoleRow(roleId);
47 this.userSelectContainer.querySelector('input[name="user_select"]').addEventListener('change', event => {
48 const userId = event.target.value;
50 this.addUserRow(userId);
55 async addRoleRow(roleId) {
56 this.roleSelect.disabled = true;
58 // Remove option from select
59 const option = this.roleSelect.querySelector(`option[value="${roleId}"]`);
64 // Get and insert new row
65 const resp = await window.$http.get(`/permissions/role-form-row/${this.entityType}/${roleId}`);
66 const row = htmlToDom(resp.data);
67 this.roleContainer.append(row);
69 this.roleSelect.disabled = false;
72 async addUserRow(userId) {
73 const exists = this.userContainer.querySelector(`[name^="permissions[user][${userId}]"]`) !== null;
78 const toggle = this.userSelectContainer.querySelector('.dropdown-search-toggle-select');
79 toggle.classList.add('disabled');
80 this.userContainer.style.pointerEvents = 'none';
82 // Get and insert new row
83 const resp = await window.$http.get(`/permissions/user-form-row/${this.entityType}/${userId}`);
84 const row = htmlToDom(resp.data);
85 this.userContainer.append(row);
87 toggle.classList.remove('disabled');
88 this.userContainer.style.pointerEvents = null;
90 /** @var {UserSelect} **/
91 const userSelect = window.$components.firstOnElement(this.userSelectContainer.querySelector('.dropdown-search'), 'user-select');
95 removeRowOnButtonClick(button) {
96 const row = button.closest('.item-list-row');
97 const modelId = button.dataset.modelId;
98 const modelName = button.dataset.modelName;
99 const modelType = button.dataset.modelType;
101 const option = document.createElement('option');
102 option.value = modelId;
103 option.textContent = modelName;
105 if (modelType === 'role') {
106 this.roleSelect.append(option);