Completed
Push — master ( af19a0...abfb1b )
by Albert
03:47 queued 01:23
created

NewFormButton.init   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 1 1
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