2022-10-10 07:24:23 -04:00
|
|
|
/**
|
|
|
|
* @extends {Component}
|
|
|
|
*/
|
2022-10-21 05:13:11 -04:00
|
|
|
import {htmlToDom} from "../services/dom";
|
|
|
|
|
2022-10-02 13:09:48 -04:00
|
|
|
class EntityPermissions {
|
|
|
|
|
|
|
|
setup() {
|
2022-10-10 07:24:23 -04:00
|
|
|
this.container = this.$el;
|
|
|
|
this.entityType = this.$opts.entityType;
|
|
|
|
|
2022-10-02 13:09:48 -04:00
|
|
|
this.everyoneInheritToggle = this.$refs.everyoneInherit;
|
2022-10-10 07:24:23 -04:00
|
|
|
this.roleSelect = this.$refs.roleSelect;
|
|
|
|
this.roleContainer = this.$refs.roleContainer;
|
2022-10-02 13:09:48 -04:00
|
|
|
|
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
2022-10-10 07:24:23 -04:00
|
|
|
// "Everyone Else" inherit toggle
|
2022-10-02 13:09:48 -04:00
|
|
|
this.everyoneInheritToggle.addEventListener('change', event => {
|
|
|
|
const inherit = event.target.checked;
|
2022-10-10 12:22:38 -04:00
|
|
|
const permissions = document.querySelectorAll('input[name^="permissions[0]["]');
|
2022-10-02 13:09:48 -04:00
|
|
|
for (const permission of permissions) {
|
|
|
|
permission.disabled = inherit;
|
|
|
|
permission.checked = false;
|
|
|
|
}
|
2022-10-10 07:24:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Remove role row button click
|
|
|
|
this.container.addEventListener('click', event => {
|
|
|
|
const button = event.target.closest('button');
|
|
|
|
if (button && button.dataset.roleId) {
|
|
|
|
this.removeRowOnButtonClick(button)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Role select change
|
|
|
|
this.roleSelect.addEventListener('change', event => {
|
|
|
|
const roleId = this.roleSelect.value;
|
|
|
|
if (roleId) {
|
|
|
|
this.addRoleRow(roleId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async addRoleRow(roleId) {
|
|
|
|
this.roleSelect.disabled = true;
|
|
|
|
|
|
|
|
// Remove option from select
|
|
|
|
const option = this.roleSelect.querySelector(`option[value="${roleId}"]`);
|
|
|
|
if (option) {
|
|
|
|
option.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get and insert new row
|
|
|
|
const resp = await window.$http.get(`/permissions/form-row/${this.entityType}/${roleId}`);
|
2022-10-21 05:13:11 -04:00
|
|
|
const row = htmlToDom(resp.data);
|
2022-10-10 07:24:23 -04:00
|
|
|
this.roleContainer.append(row);
|
|
|
|
|
|
|
|
this.roleSelect.disabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeRowOnButtonClick(button) {
|
2022-10-29 10:23:21 -04:00
|
|
|
const row = button.closest('.item-list-row');
|
2022-10-10 07:24:23 -04:00
|
|
|
const roleId = button.dataset.roleId;
|
|
|
|
const roleName = button.dataset.roleName;
|
|
|
|
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = roleId;
|
|
|
|
option.textContent = roleName;
|
|
|
|
|
|
|
|
this.roleSelect.append(option);
|
|
|
|
row.remove();
|
2022-10-02 13:09:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EntityPermissions;
|