2020-01-17 16:24:55 -08:00
|
|
|
import $ from 'jquery';
|
|
|
|
|
|
|
|
|
|
const body = $('body');
|
|
|
|
|
|
2020-01-22 14:25:47 -08:00
|
|
|
body.on('change', '[data-acl_picker] select', (event) => {
|
2020-01-17 16:24:55 -08:00
|
|
|
const target = $(event.currentTarget);
|
|
|
|
|
const value = target.val();
|
|
|
|
|
const inputs = target.closest('.permissions-item').find('input[name]');
|
2020-01-22 14:25:47 -08:00
|
|
|
const wrapper = target.closest('[data-acl_picker_id]');
|
2020-01-17 16:24:55 -08:00
|
|
|
|
|
|
|
|
inputs.each((index, input) => {
|
|
|
|
|
input = $(input);
|
|
|
|
|
const name = input.prop('name');
|
|
|
|
|
input.prop('name', name.replace(/(.*)(\[[^\]]*\])/, `$1[${value}]`));
|
|
|
|
|
});
|
|
|
|
|
|
2020-01-22 14:25:47 -08:00
|
|
|
wrapper.find('.permissions-item .button.add-item')[!value ? 'addClass' : 'removeClass']('disabled').prop('disabled', !value ? 'disabled' : null);
|
2020-01-17 16:24:55 -08:00
|
|
|
});
|
|
|
|
|
|
2020-01-22 14:25:47 -08:00
|
|
|
body.on('click', '[data-acl_picker] .remove-item', (event) => {
|
2020-01-17 16:24:55 -08:00
|
|
|
const target = $(event.currentTarget);
|
|
|
|
|
const container = target.closest('.permissions-item');
|
2020-01-22 14:25:47 -08:00
|
|
|
const wrapper = target.closest('[data-acl_picker_id]');
|
2020-01-17 16:24:55 -08:00
|
|
|
container.remove();
|
|
|
|
|
|
2020-01-22 14:25:47 -08:00
|
|
|
const empty = wrapper.find('.permissions-item').length === 1;
|
2020-01-17 16:24:55 -08:00
|
|
|
|
|
|
|
|
// show the initial + button
|
|
|
|
|
if (empty) {
|
2020-01-22 14:25:47 -08:00
|
|
|
wrapper.find('.permissions-item.empty-list').removeClass('hidden');
|
2020-01-17 16:24:55 -08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-01-22 14:25:47 -08:00
|
|
|
body.on('click', '[data-acl_picker] .add-item', (event) => {
|
2020-01-17 16:24:55 -08:00
|
|
|
const target = $(event.currentTarget);
|
|
|
|
|
const item = target.closest('.permissions-item');
|
2020-01-22 14:25:47 -08:00
|
|
|
const wrapper = target.closest('[data-acl_picker_id]');
|
|
|
|
|
const ID = wrapper.data('acl_picker_id');
|
|
|
|
|
const template = document.querySelector(`template[data-id="acl_picker-${ID}"]`);
|
2020-01-17 16:24:55 -08:00
|
|
|
|
|
|
|
|
const clone = $(template.content.querySelector(':first-child')).clone();
|
|
|
|
|
clone.insertAfter(item);
|
|
|
|
|
|
|
|
|
|
// randomize ids
|
|
|
|
|
clone.find('.switch-toggle input[type="radio"]').each((index, input) => {
|
|
|
|
|
input = $(input);
|
|
|
|
|
const id = input.prop('id');
|
|
|
|
|
const label = input.next('label');
|
|
|
|
|
const rand = (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toLowerCase();
|
|
|
|
|
|
|
|
|
|
input.prop('id', `${id}${rand}`);
|
|
|
|
|
label.prop('for', `${id}${rand}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// hide the initial + button
|
2020-01-22 14:25:47 -08:00
|
|
|
wrapper.find('.permissions-item.empty-list').addClass('hidden');
|
2020-01-17 16:24:55 -08:00
|
|
|
|
|
|
|
|
// disable all + buttons until one is selected
|
2020-01-22 14:25:47 -08:00
|
|
|
wrapper.find('.permissions-item .button.add-item').addClass('disabled').prop('disabled', 'disabled');
|
2020-01-17 16:24:55 -08:00
|
|
|
});
|