Completed
Push — master ( 78d2f9...949122 )
by greg
42s
created

EditorReferences.js ➔ ???   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
nop 0

1 Function

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