Completed
Pull Request — master (#21)
by
unknown
02:20
created

EditorReferences.js ➔ ???   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
nc 2
dl 0
loc 7
rs 9.4285
c 4
b 0
f 0
cc 2
nop 0
1
/*global document, FormData, CONFIG, XMLHttpRequest */
2
3
import Nanoajax from 'nanoajax'
4
import EditorUtils from '../modules/EditorUtils'
5
import {IframeNode} from '../utils/iframe'
6
import on from 'on'
7
import qs from 'qs'
8
9
export default class EditorFiles {
10
  constructor() {
11
    this._ajax = Nanoajax.ajax
12
    this.referenceLinks = document.querySelectorAll('[data-ref-json]')
13
    this.textArea = document.querySelector('.display-json')
14
    this.jsonError = document.querySelector('.json-error')
15
    if(this.referenceLinks) this.rebind()
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
16
  }
17
18
  rebind() {
19
    Array.prototype.forEach.call(this.referenceLinks, (referenceLink) => {
20
      referenceLink.addEventListener('click', (e) => {
21
        e.preventDefault()
22
        this.textArea.style.opacity = 1;
23
        Array.prototype.forEach.call(this.referenceLinks, (referenceLink) => {
24
          this.textArea.classList.remove('error')
25
          this.jsonError.style.opacity = 0
26
          referenceLink.classList.remove('active')
27
        });
28
        e.target.classList.add('active')
29
        if(parseInt(e.target.getAttribute('data-error')) === 1) {
30
          this.textArea.classList.add('error')
31
          this.jsonError.style.opacity = 1
32
        }
33
        this.displayReference(e.target)
34
      })
35
    })
36
37
    this.textArea.addEventListener('blur', () => {
38
      this.save()
39
    })
40
  }
41
42
  save(){
43
    var selectedElement = document.querySelector('.references-files-wrapper .list-group-item.active')
44
    var isValidJson = true
45
    this.textArea.classList.remove('error')
46
    this.jsonError.style.opacity = 0
47
    try{
48
      (typeof JSON.parse(this.textArea.value)) // validate json
49
      var data = qs.stringify({json: this.textArea.value, url: this.textArea.getAttribute('data-file')})
50
      selectedElement.setAttribute('data-ref-json', this.textArea.value)
51
      selectedElement.setAttribute('data-error', 0)
52
    }
53
    catch(e){
54
      this.jsonError.textContent = e
55
      isValidJson = false
56
      selectedElement.setAttribute('data-error', 1)
57
    }
58
    if(isValidJson){
59
      this._ajax(
60
      {
61
        url: `/abe/write-json/`,
62
        body: data,
0 ignored issues
show
Bug introduced by
The variable data does not seem to be initialized in case typeof JSON.parse(this.textArea.value) on line 48 throws an error. Are you sure this can never be the case?
Loading history...
63
        cors: true,
64
        method: 'post'
65
      },
66
      (code, responseText) => {
0 ignored issues
show
Unused Code introduced by
The parameter responseText is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter code is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
67
        this.textArea.classList.add('saved')
68
        setTimeout(() => {
69
          this.textArea.classList.remove('saved')
70
        }, 400)
71
      })
72
    }
73
    else {
74
      this.textArea.classList.add('error')
75
      this.jsonError.style.opacity = 1
76
    }
77
  }
78
79
  displayReference(element) {
80
    this.textArea.value = JSON.stringify(JSON.parse(element.getAttribute('data-ref-json')), null, '\t')
81
    this.textArea.setAttribute('data-file', element.getAttribute('data-href'))
82
  }
83
84
}