2020-06-27 14:29:00 +02:00
|
|
|
import {onChildEvent} from "../services/dom";
|
2020-06-29 23:11:03 +02:00
|
|
|
import {uniqueId} from "../services/util";
|
2020-06-27 14:29:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AddRemoveRows
|
|
|
|
* Allows easy row add/remove controls onto a table.
|
|
|
|
* Needs a model row to use when adding a new row.
|
|
|
|
* @extends {Component}
|
|
|
|
*/
|
|
|
|
class AddRemoveRows {
|
|
|
|
setup() {
|
|
|
|
this.modelRow = this.$refs.model;
|
|
|
|
this.addButton = this.$refs.add;
|
|
|
|
this.removeSelector = this.$opts.removeSelector;
|
2020-06-29 23:11:03 +02:00
|
|
|
this.rowSelector = this.$opts.rowSelector;
|
2020-06-27 14:29:00 +02:00
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
2020-06-29 23:11:03 +02:00
|
|
|
this.addButton.addEventListener('click', this.add.bind(this));
|
2020-06-27 14:29:00 +02:00
|
|
|
|
|
|
|
onChildEvent(this.$el, this.removeSelector, 'click', (e) => {
|
2020-06-29 23:11:03 +02:00
|
|
|
const row = e.target.closest(this.rowSelector);
|
2020-06-27 14:29:00 +02:00
|
|
|
row.remove();
|
|
|
|
});
|
|
|
|
}
|
2020-06-29 23:11:03 +02:00
|
|
|
|
|
|
|
// For external use
|
|
|
|
add() {
|
|
|
|
const clone = this.modelRow.cloneNode(true);
|
|
|
|
clone.classList.remove('hidden');
|
|
|
|
this.setClonedInputNames(clone);
|
|
|
|
this.modelRow.parentNode.insertBefore(clone, this.modelRow);
|
|
|
|
window.components.init(clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the HTML names of a clone to be unique if required.
|
|
|
|
* Names can use placeholder values. For exmaple, a model row
|
|
|
|
* may have name="tags[randrowid][name]".
|
|
|
|
* These are the available placeholder values:
|
|
|
|
* - randrowid - An random string ID, applied the same across the row.
|
|
|
|
* @param {HTMLElement} clone
|
|
|
|
*/
|
|
|
|
setClonedInputNames(clone) {
|
|
|
|
const rowId = uniqueId();
|
|
|
|
const randRowIdElems = clone.querySelectorAll(`[name*="randrowid"]`);
|
|
|
|
for (const elem of randRowIdElems) {
|
|
|
|
elem.name = elem.name.split('randrowid').join(rowId);
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 14:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default AddRemoveRows;
|