Completed
Pull Request — master (#66)
by
unknown
01:50
created

src/server/public/scripts/modules/EditorReferences.js   A

Size

Lines of Code 78

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 78
rs 10
c 0
b 0
f 0
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A EditorReferences.js ➔ ??? 0 8 3
1
/*global document */
2
3
import Handlebars from 'handlebars'
4
import Nanoajax from 'nanoajax'
5
import qs from 'qs'
6
7
export default class EditorReferences {
8
  constructor() {
9
    this._ajax = Nanoajax.ajax
10
    this.referenceLinks = document.querySelectorAll('[data-ref-json]')
11
    this.textArea = document.querySelector('.display-json')
12
    this.jsonError = document.querySelector('.json-error')
13
    if(!this.referenceLinks || this.referenceLinks.length < 1) return
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...
14
    this.rebind()
15
  }
16
17
  rebind() {
18
    Array.prototype.forEach.call(this.referenceLinks, (referenceLink) => {
19
      referenceLink.addEventListener('click', (e) => {
20
        e.preventDefault()
21
        this.textArea.style.opacity = 1
22
        Array.prototype.forEach.call(this.referenceLinks, (referenceLink) => {
23
          this.textArea.classList.remove('error')
24
          this.jsonError.style.opacity = 0
25
          referenceLink.classList.remove('active')
26
        })
27
        e.target.classList.add('active')
28
        if(parseInt(e.target.getAttribute('data-error')) === 1) {
29
          this.textArea.classList.add('error')
30
          this.jsonError.style.opacity = 1
31
        }
32
        this.displayReference(e.target)
33
      })
34
    })
35
    this.textArea.addEventListener('blur', () => {
36
      this.save()
37
    })
38
  }
39
40
  save(){
41
    var selectedElement = document.querySelector('.references-files-wrapper .list-group-item.active')
42
    var isValidJson = true
43
    this.textArea.classList.remove('error')
44
    this.jsonError.style.opacity = 0
45
    try{
46
      (typeof JSON.parse(this.textArea.value)) // validate json
47
      var data = qs.stringify({json: this.textArea.value, url: this.textArea.getAttribute('data-file')})
48
      selectedElement.setAttribute('data-ref-json', this.textArea.value)
49
      selectedElement.setAttribute('data-error', 0)
50
    }
51
    catch(e){
52
      this.jsonError.textContent = e
53
      isValidJson = false
54
      selectedElement.setAttribute('data-error', 1)
55
    }
56
    if(isValidJson){
57
      this._ajax({
58
        url: '/abe/reference/',
59
        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 46 throws an error. Are you sure this can never be the case?
Loading history...
60
        cors: true,
61
        method: 'post'
62
      },
63
      () => {
64
        this.textArea.classList.add('saved')
65
        setTimeout(() => {
66
          this.textArea.classList.remove('saved')
67
        }, 400)
68
      })
69
    }
70
    else {
71
      this.textArea.classList.add('error')
72
      this.jsonError.style.opacity = 1
73
    }
74
  }
75
76
  displayReference(element) {
77
    this.textArea.value = JSON.stringify(JSON.parse(element.getAttribute('data-ref-json')), null, '\t')
78
    this.textArea.setAttribute('data-file', element.getAttribute('data-href'))
79
  }
80
}