Passed
Branch master (c2f34b)
by greg
02:22
created

➔ $(ꞌa[data-toggle="tab"]ꞌ).shown.bs.tab   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
cc 1
rs 10
1
/*global document, top, $, location, confirm, alert */
2
3
import Nanoajax from 'nanoajax'
4
import qs from 'qs'
5
import {Promise} from 'bluebird'
6
import extend from 'extend'
7
import on from 'on'
8
9
export default class EditorManager {
10
  constructor() {
11
    this._ajax = Nanoajax.ajax
12
13
    this.remove = on(this)
14
15
    // wrapper files
16
    this._manager = document.querySelector('.manager-wrapper')
17
    this._managerTabs = document.querySelectorAll('[data-manager-show]')
18
    this._filesList = [].slice.call(document.querySelectorAll('.manager-files .list-group-item'))
19
20
    // manager config button
21
    this._btnSaveConfig = document.querySelectorAll('[data-save-config]')
22
23
    // button manager
24
    this._btnGeneratePosts = document.querySelector('[data-generate-posts]')
25
    this._btnCloseManager = document.querySelector('.close-manager')
26
    this._btnManager = document.querySelector('.btn-manager')
27
    this._btnDataFile = document.querySelector('[data-file="true"]')
28
29
    this._btnDeleteFile = [].slice.call(document.querySelectorAll('[data-delete="true"]'))
30
    this._btnUnpublishFile = [].slice.call(document.querySelectorAll('[data-unpublish="true"]'))
31
32
    // event handlers
33
    this._handleBtnGeneratePostsClick = this._btnGeneratePostsClick.bind(this)
34
    this._handleBtnCloseManagerClick = this._btnCloseManagerClick.bind(this)
35
    this._handleBtnManagerTabClick = this._btnManagerTabClick.bind(this)
36
    this._handleBtnManagerClick = this._btnManagerClick.bind(this)
37
    this._handleBtnSaveConfigClick = this._btnSaveConfigClick.bind(this)
38
39
    this._handleBtnDeleteClick = this._btnDeleteClick.bind(this)
40
    this._handleBtnUnpublishClick = this._btnUnpublishClick.bind(this)
41
42
    if(typeof top.location.hash !== 'undefined' && top.location.hash !== null && top.location.hash !== '') {
43
      var currentTab = document.querySelector('[href="' + top.location.hash + '"]')
44
      if(typeof currentTab !== 'undefined' && currentTab !== null) {
45
        currentTab.click() // retrieve old selected tab
46
      }
47
    }
48
49
    $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
50
      return location.hash = $(e.target).attr('href').substr(1)
51
    })
52
53
    this._bindEvents()
54
  }
55
56
  _btnDeleteClick(e){
57
    e.preventDefault()
58
    var confirmDelete = confirm(e.currentTarget.getAttribute('data-text'))
59
    if (!confirmDelete) 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...
60
    let href = e.currentTarget.getAttribute('href')
61
    let target = e.currentTarget
62
    this._ajax({
63
      url: href,
64
      method: 'get'
65
    },
66
      (e, responseText) => {
67
        var response = JSON.parse(responseText)
68
        if (response.success !== 1) {
69
          alert(response.message)
70
        }else {
71
          this.remove._fire(target.parentNode.parentNode.parentNode)
72
        }
73
      })
74
  }
75
76
  _btnUnpublishClick(e){
77
    e.preventDefault()
78
    var confirmDelete = confirm(e.currentTarget.getAttribute('data-text'))
79
    if (!confirmDelete) 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...
80
    let href = e.currentTarget.getAttribute('href')
81
    let target = e.currentTarget
82
    this._ajax({
83
      url: href,
84
      method: 'get'
85
    },
86
      (e, responseText) => {
87
        var response = JSON.parse(responseText)
88
        if (response.success !== 1) {
89
          alert(response.message)
90
        }else {
91
          var labels = target.parentNode.parentNode.parentNode.querySelectorAll('.label:not(.hidden)')
92
          var p = target.parentNode.parentNode.parentNode.querySelector('.label-published')
93
          Array.prototype.forEach.call(labels, (label) => {
94
            label.classList.add('hidden')
95
          })
96
          var draft = target.parentNode.parentNode.parentNode.querySelector('.label-draft')
97
          
98
          if(typeof draft !== 'undefined' && draft !== null) {
99
            draft.classList.remove('hidden')
100
          }
101
102
          if(typeof p !== 'undefined' && p !== null) p.remove()
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...
103
          target.remove()
104
        }
105
      })
106
  }
107
108
  _bindEvents() {
109
    Array.prototype.forEach.call(this._managerTabs, (managerTab) => {
110
      managerTab.addEventListener('click', this._handleBtnManagerTabClick)
111
    })
112
    Array.prototype.forEach.call(this._btnSaveConfig, (btnSaveConfig) => {
113
      btnSaveConfig.addEventListener('click', this._handleBtnSaveConfigClick)
114
    })
115
116
    if (this._btnManager != null) {
117
      this._btnManager.addEventListener('click', this._handleBtnManagerClick)
118
    }
119
    
120
    if(typeof this._btnGeneratePosts !== 'undefined' && this._btnGeneratePosts !== null) {
121
      this._btnGeneratePosts.addEventListener('click', this._handleBtnGeneratePostsClick)
122
    }
123
124
    if(typeof this._btnCloseManager !== 'undefined' && this._btnCloseManager !== null) {
125
      this._btnCloseManager.addEventListener('click', this._handleBtnCloseManagerClick)
126
    }
127
128
    Array.prototype.forEach.call(this._btnDeleteFile, (deleteFile) => {
129
      deleteFile.addEventListener('click', this._handleBtnDeleteClick)
130
    })
131
132
    Array.prototype.forEach.call(this._btnUnpublishFile, (unpublishFile) => {
133
      unpublishFile.addEventListener('click', this._handleBtnUnpublishClick)
134
    })
135
  }
136
137
  _btnGeneratePostsClick(e) {
138
    e.preventDefault()
139
    this._btnGeneratePosts.querySelector('[data-not-clicked]').className = 'hidden'
140
    this._btnGeneratePosts.querySelector('[data-clicked]').className = ''
141
    this._ajax(
142
      {
143
        url: document.location.origin + '/abe/generate-posts',
144
        method: 'get'
145
      },
146
        (e, responseText) => {
147
          var response = JSON.parse(responseText)
148
          if (response.success !== 1) {
149
            alert(response.msg)
150
          }
151
        })
152
  }
153
154
  _btnCloseManagerClick() {
155
    this._manager.classList.remove('visible')
156
  }
157
158
  _save(website, json, path) {
159
    var p = new Promise((resolve) => {
160
      var toSave = qs.stringify({
161
        website: website,
162
        json: json
163
      })
164
165
      this._ajax(
166
        {
167
          url: document.location.origin + path + '?' + toSave,
168
          method: 'get'
169
        },
170
        () => {
171
172
          resolve()
173
        })
174
    })
175
176
    return p
177
  }
178
179
  _dotStringToJson(str, value) {
180
    var keys = str.split('.')
181
    var objStrStart = ''
182
    var objStrEnd = ''
183
    Array.prototype.forEach.call(keys, (key) => {
184
      objStrStart += '{"'+key+'":'
185
      objStrEnd += '}'
186
    })
187
    return JSON.parse(objStrStart + '"' + value + '"' + objStrEnd)
188
  }
189
190
  _serializeForm(form) {
191
    var json = {}
192
    let inputs = [].slice.call(form.querySelectorAll('input[type=text]'))
193
    Array.prototype.forEach.call(inputs, (input) => {
194
      extend(true, json, this._dotStringToJson(input.getAttribute('data-json-key'), input.value))
195
    })
196
197
    return json
198
  }
199
200
  _btnSaveConfigClick(e) {
201
    e.preventDefault()
202
    let website = e.currentTarget.getAttribute('data-website')
203
    let route = e.currentTarget.getAttribute('data-route')
204
    let json = this._serializeForm(document.querySelector(`form#config-${website}`))
205
    this._save(website, json, route)
206
  }
207
208
  _hideManagerBlock() {
209
    Array.prototype.forEach.call(this._managerTabs, (managerTab) => {
210
      var classname = `.${managerTab.getAttribute('data-manager-show')}`
211
      var blockElement = document.querySelector(classname)
212
      if(typeof blockElement !== 'undefined' && blockElement !== null) blockElement.classList.remove('visible')
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...
213
    })
214
  }
215
216
  _btnManagerTabClick(e) {
217
    e.preventDefault()
218
    var classname = e.currentTarget.getAttribute('data-manager-show')
219
    this._hideManagerBlock()
220
    var blockElement = document.querySelector(`.${classname}`)
221
    if(typeof blockElement !== 'undefined' && blockElement !== null) blockElement.classList.add('visible')
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...
222
  }
223
224
  _btnManagerClick() {
225
    if(this._manager.classList.contains('visible')) {
226
      this._manager.classList.remove('visible')
227
    }else {
228
      this._manager.classList.add('visible')
229
    }
230
  }
231
}