-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.js
129 lines (116 loc) · 4.53 KB
/
Grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class Grid {
constructor (settings, event, eventHandler) {
this.settings = Object.assign({
rows: 5,
formSelector: "form",
hiddenFields: [],
fields: ["col-1", "col-2", "col-3", "col-4"],
nameSeparator: '-'
}, settings);
this.eventHandler = eventHandler;
this.event = event;
this.buildGrid();
}
buildGrid () {
console.time('building');
// CSS fits:
// length+1 for delete button
document.documentElement.style.setProperty('--columns', this.settings.fields.length+1);
Object.defineProperty(this, 'rowsNum', {
get: function () {return this.settings.rows},
set: function (val) {
this.settings.rows = val;
inputHiddenRowsNum.value = val;
},
});
// Add non-repeating data to the top
const nonRepeatFragment = document.createDocumentFragment();
const inputHiddenRowsNum = this.createInput('rows', this.rowsNum, 'hidden');
nonRepeatFragment.appendChild(inputHiddenRowsNum);
const inputSeparator = this.createInput('separator', this.settings.nameSeparator, 'hidden');
nonRepeatFragment.appendChild(inputSeparator);
// Adding static hidden inputs
this.settings.hiddenFields.forEach(hidden => nonRepeatFragment.appendChild(this.createInput(hidden.name, hidden.value, 'hidden')));
const staticHiddenInputNamesList = this.createInput('hidden_input_names', JSON.stringify(this.settings.hiddenFields.map(f => f.name)), 'hidden');
nonRepeatFragment.appendChild(staticHiddenInputNamesList);
document.querySelector(this.settings.formSelector).appendChild(nonRepeatFragment);
this.createRowTemplate();
const gridTemplate = document.createDocumentFragment();
for (let i = 0; i < this.rowsNum; i++) {
gridTemplate.appendChild(this.buildRow(i));
}
document.querySelector(this.settings.formSelector).appendChild(gridTemplate);
document.querySelector(this.settings.formSelector).appendChild(document.querySelector(this.settings.formSelector + ' input[type=submit]'))
console.timeEnd('building');
}
createRowTemplate() {
this.rowTemplate = document.createDocumentFragment();
let section = document.createElement('section');
section = this.rowTemplate.appendChild(section);
this.settings.fields.forEach((field, index) => {
const input = this.createInput(field);
input.dataset.column = index;
input.placeholder = field[0].toUpperCase() + field.slice(1).toLowerCase();
section.appendChild(input);
});
const deleteButton = document.createElement('button');
deleteButton.innerText = '✖';
deleteButton.classList.add('delete-btn');
section.appendChild(deleteButton);
}
buildRow (i) {
const clone = this.rowTemplate.cloneNode(true);
const section = clone.querySelector('section');
section.dataset.row = i;
section.querySelectorAll('input').forEach((input) => {
input.dataset.row = i;
input.name = `${input.name}${this.settings.nameSeparator}${i}`;
input.addEventListener(this.event, this.eventHandler);
});
section.querySelector('.delete-btn').addEventListener('click', e => {
e.preventDefault();
this.deleteRow(i);
})
return clone;
}
createInput (name = '', value = '', type = 'text') {
const input = document.createElement('input');
input.type = type;
input.name = name;
input.value = value;
return input;
}
addRow() {
document.querySelector(this.settings.formSelector).insertBefore(this.buildRow(this.rowsNum), document.querySelector(this.settings.formSelector + ' input[type=submit]'));
document.querySelector(this.settings.formSelector + ' input[name=rows]').value = this.rowsNum;
this.rowsNum++;
}
addRows(num) {
console.time('adding multiple rows');
num = parseInt(num);
if (isNaN(num)) {num = 0;}
const template = document.createDocumentFragment();
for (let i = this.rowsNum; i < this.rowsNum+num; i++) {template.appendChild(this.buildRow(i));}
document.querySelector(this.settings.formSelector).insertBefore(template, document.querySelector(this.settings.formSelector + ' input[type=submit]'));
this.rowsNum += num;
console.timeEnd('adding multiple rows');
}
deleteRow (i) {
console.time('delete rows');
document.querySelectorAll(this.settings.formSelector + ' section')[i].remove();
this.reorderRows(i);
this.rowsNum--;
console.timeEnd('delete rows');
}
reorderRows(i) {
let rows = Array.from(document.querySelectorAll(this.settings.formSelector + ' section')).slice(i);
rows.forEach((row, index) => {
index += i;
row.dataset.row = index;
row.querySelectorAll('input').forEach(input => {
input.dataset.row = index;
input.name = input.name.split(this.settings.nameSeparator)[0] + '-' + index;
});
});
}
}