Completed
Push — master ( 965e58...48e78d )
by greg
04:47 queued 01:50
created

EditorReferences.js ➔ ???   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 4 Features 0
Metric Value
cc 5
c 8
b 4
f 0
nc 3
nop 0
dl 0
loc 12
rs 8.8571
1
/*global document */
2
3
import Nanoajax from 'nanoajax'
4
import qs from 'qs'
5
6
export default class EditorReferences {
7
  constructor() {
8
    this._ajax = Nanoajax.ajax
9
    this.referenceLinks = document.querySelectorAll('[data-ref-json]')
10
    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...
11
    this.textArea = document.querySelector('.display-json')
12
    this.jsonError = document.querySelector('.json-error')
13
    this.addReference = document.querySelector('.btn-add-reference')
14
    this.addReferenceInput = document.querySelector('.btn-add-reference input')
15
    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...
16
    this.nameError = this.addReference.querySelector('.error-display')
17
    this.rebind()
18
  }
19
20
  bindReference(referenceLink){
21
    referenceLink.setAttribute('data-init', 1)
22
    referenceLink.addEventListener('click', (e) => {
23
      e.preventDefault()
24
      this.textArea.style.opacity = 1
25
      Array.prototype.forEach.call(this.referenceLinks, (referenceLink) => {
26
        this.textArea.classList.remove('error')
27
        this.jsonError.style.opacity = 0
28
        referenceLink.classList.remove('active')
29
      })
30
      e.target.classList.add('active')
31
      if(parseInt(e.target.getAttribute('data-error')) === 1) {
32
        this.textArea.classList.add('error')
33
        this.jsonError.style.opacity = 1
34
      }
35
      this.displayReference(e.target)
36
    })
37
  }
38
39
  nameAlreadyExist(name){
40
    var res = false
41
    Array.prototype.forEach.call(this.referenceLinks, (referenceLink) => {
42
      if(referenceLink.getAttribute('data-href') == name) res = true
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...
43
    })
44
    return res
45
  }
46
47
  addError(val, fullTest = true){
48
    var message = ''
49
    if(val.trim() === '') {
50
      message = 'filename is empty'
51
    }
52
    else if(!(/^[A-Za-z0-9-\.]+(?:-[A-Za-z0-9-\.]+)*$/.test(val))) {
53
      message = 'filename must not contains special characteres'
54
    }
55
    else if(val.indexOf('.json') < 0 && fullTest) {
56
      message = 'missing .json extension'
57
    }
58
    else if(this.nameAlreadyExist(val)) {
59
      message = 'json reference with this name already exist'
60
    }
61
    this.nameError.textContent = message
62
  }
63
64
  rebind() {
65
    Array.prototype.forEach.call(this.referenceLinks, (referenceLink) => {
66
      if(parseInt(referenceLink.getAttribute('data-init')) !== 0) 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...
67
      this.bindReference(referenceLink)
68
    })
69
    this.textArea.addEventListener('blur', () => {
70
      this.save()
71
    })
72
    this.addReference.querySelector('span').addEventListener('click', () => {
73
      this.add()
74
    })
75
    this.addReferenceInput.addEventListener('keyup', () => {
76
      var val = this.addReferenceInput.value
77
      if(!(/^[A-Za-z0-9-\.]+(?:-[A-Za-z0-9-\.]+)*$/.test(val)) || this.nameAlreadyExist(val)) this.addReference.classList.add('error')
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...
78
      else this.addReference.classList.remove('error')
79
      this.addError(val, false)
80
    })
81
  }
82
83
  add(){
84
    var val = this.addReferenceInput.value
85
    if(val.trim() !== '' && /^[A-Za-z0-9-\.]+(?:-[A-Za-z0-9-\.]+)*$/.test(val) && val.indexOf('.json') > -1 && !this.nameAlreadyExist(val)){
86
      this.addReference.classList.remove('error')
87
      var li = document.createElement('li')
88
      li.classList.add('list-group-item')
89
      li.setAttribute('data-error', '0')
90
      li.setAttribute('data-href', val)
91
      li.setAttribute('data-ref-json', '[]')
92
      li.textContent = val
93
      this.addReference.parentNode.insertBefore(li, this.addReference)
94
      this.referenceLinks = document.querySelectorAll('[data-ref-json]')
95
      this.bindReference(li)
96
    }
97
    else this.addReference.classList.remove('error')
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...
98
    this.addError(val)
99
  }
100
101
  save(){
102
    var selectedElement = document.querySelector('.references-files-wrapper .list-group-item.active')
103
    var isValidJson = true
104
    this.textArea.classList.remove('error')
105
    this.jsonError.style.opacity = 0
106
    try{
107
      (typeof JSON.parse(this.textArea.value)) // validate json
108
      var data = qs.stringify({json: this.textArea.value, url: this.textArea.getAttribute('data-file')})
109
      selectedElement.setAttribute('data-ref-json', this.textArea.value)
110
      selectedElement.setAttribute('data-error', 0)
111
    }
112
    catch(e){
113
      this.jsonError.textContent = e
114
      isValidJson = false
115
      selectedElement.setAttribute('data-error', 1)
116
    }
117
    if(isValidJson){
118
      this._ajax({
119
        url: '/abe/reference/',
120
        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 107 throws an error. Are you sure this can never be the case?
Loading history...
121
        cors: true,
122
        method: 'post'
123
      },
124
      () => {
125
        this.textArea.classList.add('saved')
126
        setTimeout(() => {
127
          this.textArea.classList.remove('saved')
128
        }, 400)
129
      })
130
    }
131
    else {
132
      this.textArea.classList.add('error')
133
      this.jsonError.style.opacity = 1
134
    }
135
  }
136
137
  displayReference(element) {
138
    this.textArea.value = JSON.stringify(JSON.parse(element.getAttribute('data-ref-json')), null, '\t')
139
    this.textArea.setAttribute('data-file', element.getAttribute('data-href'))
140
  }
141
}