-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
49 lines (44 loc) · 1.53 KB
/
main.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
window.addEventListener('load', e => {
const settings = {
fields: ["category", "title", "city", "address", "phone"],
hiddenFields: [{name: "some_hidden_name", value: "some_hidden_value"}],
rows: 3,
formSelector: ".container",
nameSeparator: "$$",
};
const grid = new Grid(settings, 'paste', onPasteHandler);
document.getElementById('add_row').addEventListener('click', grid.addRow.bind(grid), false);
document.querySelector('#add_multiple_rows button').addEventListener('click', addMultipleRows.bind(grid), false);
document.querySelector('#add_multiple_rows input[type=number]').addEventListener('keydown', addMultipleRows.bind(grid), false);
});
function addMultipleRows (e) {
if (e.type === 'keydown' && e.keyCode !== 13) {return false;}
this.addRows(document.querySelector('#add_multiple_rows input[type=number]').value);
}
function onPasteHandler (e) {
e.preventDefault();
addData(e.currentTarget, e.clipboardData.getData('Text'));
}
function addData (input, text) {
console.time('addData');
const [rowInput, columnInput] = [
parseInt(input.dataset.row),
parseInt(input.dataset.column)
];
const cells = [];
const rows = text
.split('\n')
.forEach((row, index) => {
cells[index] = row.split('\t');
});
cells.forEach((rowData, indexRow) => {
rowData.forEach((value, indexColumn) => {
const query = `input[data-row="${indexRow + rowInput}"][data-column="${indexColumn + columnInput}"]`;
const el = document.querySelector(query);
if (el) {
el.value = value.trim();
}
});
});
console.timeEnd('addData');
}