combo/combo/apps/dataviz/static/js/combo.multiselectwidget.js

70 lines
1.9 KiB
JavaScript

const multiSelectWidget = (function () {
const add_row = function(widget) {
event.preventDefault();
/* get last row node */
const rows = widget.querySelectorAll('.combo-multi-select-widget--field');
const $last_row = $(rows).last();
/* clone the row */
const $new_row = $last_row.clone();
/* set new label and ids */
const row_label = widget.dataset.rowLabel;
const new_label = row_label + ' ' + rows.length;
$new_row.find('label').text(new_label);
const row_id = widget.dataset.rowId;
const new_id = row_id + '_' + rows.length;
$new_row.find('label').attr('for', new_id);
$new_row.find('select').attr('id', new_id);
/* add new row after the last row */
$last_row.after($new_row);
$('.combo-multi-select-widget--button-remove', $new_row).click(remove_row);
}
const remove_row = function(event) {
event.preventDefault();
var $field = $(this).parents('.content');
var $row = $(this).parents('.combo-multi-select-widget--field');
$row.remove();
$field.change();
}
const init = function(cell) {
const widgets = cell.querySelectorAll('.combo-multi-select-widget');
if (!widgets.length) return;
widgets.forEach(function(widget){
const deletBtn = widget.querySelectorAll('.combo-multi-select-widget--button-remove');
const addBtn = widget.querySelectorAll('.combo-multi-select-widget--button-add');
$(addBtn).off('click');
$(addBtn).click( () => add_row(widget) );
$(deletBtn).off('click');
$(deletBtn).click(remove_row);
});
}
return {
init
}
})();
$(function() {
$('.cell').each(function(i, cell) {
multiSelectWidget.init(cell);
});
$(document).on('combo:cell-loaded', function(e, cell) {
multiSelectWidget.init(cell);
});
$('.cell').on('combo:cellform-reloaded', function() {
multiSelectWidget.init(this);
});
});