Total Complexity | 5 |
Complexity/F | 1 |
Lines of Code | 40 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | const NewFormButton = { |
||
2 | BUTTON_QUERY: '#new-file', |
||
3 | LAST_FORM_QUERY: '.code-form--file:last-of-type', |
||
4 | |||
5 | init() { |
||
6 | const button = document.querySelector(this.BUTTON_QUERY); |
||
7 | button.addEventListener('click', () => this.onClick()); |
||
8 | }, |
||
9 | |||
10 | onClick() { |
||
11 | const lastForm = document.querySelector(this.LAST_FORM_QUERY); |
||
12 | const newForm = this.getNewForm(); |
||
13 | const formsWrapper = lastForm.parentNode; |
||
14 | |||
15 | formsWrapper.insertBefore(newForm, lastForm.nextSibling); |
||
16 | }, |
||
17 | |||
18 | getNewForm() { |
||
19 | const index = this.getNextIndex(); |
||
20 | const newForm = document.createElement('fieldset'); |
||
21 | newForm.setAttribute('class', 'code-form--file'); |
||
22 | newForm.setAttribute('data-index', index); |
||
23 | |||
24 | newForm.innerHTML = |
||
25 | `<label class="code-form--label" for="name[${index}]">Nazwa pliku (opcjonalna)</label> |
||
26 | <input type="text" id="name[${index}]" name="name[${index}]" class="code-form--control"> |
||
27 | <label for="content[${index}]" class="code-form--label">Treść pliku</label> |
||
28 | <textarea name="content[${index}]" id="content[${index}]" rows="10" class="code-form--control code-form--control__textarea"></textarea>`; |
||
29 | |||
30 | return newForm; |
||
31 | }, |
||
32 | |||
33 | getNextIndex() { |
||
34 | const currentIndex = parseInt(document.querySelector(this.LAST_FORM_QUERY).getAttribute('data-index'), 10); |
||
35 | |||
36 | return currentIndex + 1; |
||
37 | }, |
||
38 | }; |
||
39 | |||
40 | NewFormButton.init(); |
||
41 |