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

editor.js ➔ add   D

Complexity

Conditions 17
Paths 14

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
nc 14
dl 0
loc 32
rs 4.9807
c 0
b 0
f 0
nop 4

How to fix   Complexity   

Complexity

Complex classes like editor.js ➔ add often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import {Promise} from 'bluebird'
2
import path from 'path'
3
4
import {
5
  cmsData,
6
  cmsEditor,
7
  abeEngine,
8
  cmsTemplates,
9
  abeExtend
10
} from '../../cli'
11
12
function add(obj, json, text, util) {
13
  var value = obj.value
14
  
15
  if(obj.key.indexOf('[') > -1) {
16
    var key = obj.key.split('[')[0]
17
    var index = obj.key.match(/[^\[]+?(?=\])/)[0]
18
    var prop = obj.key.replace(/[^\.]+?\./, '')
19
20
    if(typeof json[key] !== 'undefined' && json[key] !== null &&
21
       typeof json[key][index] !== 'undefined' && json[key][index] !== null &&
22
       typeof json[key][index][prop] !== 'undefined' && json[key][index][prop] !== null) {
23
      obj.value = json[getDataIdWithNoSlash(key)][index][prop]
24
    }else if(typeof value !== 'undefined' && value !== null && value !== '') {
25
      if(typeof json[key] === 'undefined' || json[key] === null){
26
        json[key] = []
27
      }
28
      if(typeof json[key][index] === 'undefined' || json[key][index] === null){
29
        json[key][index] = {}
30
      }
31
      json[key][index][prop] = value
32
    }
33
  }
34
35
  obj.key = getDataIdWithNoSlash(obj.key)
36
  if (json != null && json.abe_meta != null) {
37
    obj.status = json.abe_meta.status
38
  }
39
40
  util.add(obj)
41
42
  return value
43
}
44
45
function getDataIdWithNoSlash(key) {
46
  var trueKey = key
47
  if (trueKey.indexOf('/') > -1) {
48
    trueKey = trueKey.split('/')
49
    trueKey = trueKey[trueKey.length - 1]
50
  }
51
  return trueKey
52
}
53
54
function addToForm(match, text, json, util, arrayBlock, keyArray = null, i = 0) {
55
  var v = `{{${match}}}`,
56
    obj = cmsData.attributes.getAll(v, json)
57
58
  var realKey
59
  if(typeof keyArray !== 'undefined' && keyArray !== null) {
60
    realKey = obj.key.replace(/[^\.]+?\./, '')
61
62
    if(obj.key.indexOf(keyArray + '.') >= 0 && realKey.length > 0){
63
      obj.keyArray = keyArray
64
      obj.realKey = realKey
65
      obj.key = keyArray + '[' + i + '].' + realKey
66
      obj.desc = obj.desc + ' ' + i,
0 ignored issues
show
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
67
      insertAbeEach(obj, text, json, util, arrayBlock)
68
69
    }else if(util.dontHaveKey(obj.key)) {
70
      obj.value = json[getDataIdWithNoSlash(obj.key)]
71
      json[getDataIdWithNoSlash(obj.key)] = add(obj, json, text, util)
72
    }
73
74
  }else if(util.dontHaveKey(obj.key) && cmsData.regex.isSingleAbe(v, text)) {
75
    realKey = obj.key.replace(/\./g, '-')
76
    obj.value = json[getDataIdWithNoSlash(realKey)]
77
    json[getDataIdWithNoSlash(obj.key)] = add(obj, json, text, util)
78
  }
79
}
80
81
function matchAttrAbe(text, json, util, arrayBlock) {
82
  var patt = /abe [^{{}}]+?(?=\}})/g,
83
    match
84
  // While regexp match HandlebarsJS template item => keepgoing
85
  while (match = patt.exec(text)) {
86
    addToForm(match[0], text, json, util, arrayBlock, null, null)
87
  }
88
}
89
90
function insertAbeEach (obj, text, json, util, arrayBlock) {
91
  if(typeof arrayBlock[obj.keyArray][obj.realKey] === 'undefined' || arrayBlock[obj.keyArray][obj.realKey] === null) {
92
    arrayBlock[obj.keyArray][obj.realKey] = []
93
  }
94
  var exist = false
95
  Array.prototype.forEach.call(arrayBlock[obj.keyArray][obj.realKey], (block) => {
96
    if(block.key === obj.key) {
97
      exist = true
98
    }
99
  })
100
  if(!exist) {
101
    arrayBlock[obj.keyArray][obj.realKey].push(obj)
102
  }
103
}
104
105
function each(text, json, util, arrayBlock) {
106
  let pattEach = /(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g
107
  let patt = /abe [^{{}}]+?(?=\}})/g
108
  var textEach, match
109
110
  while (textEach = pattEach.exec(text)) {
111
    var i
112
    var keyArray = textEach[0].match(/#each (\n|.)*?\}/)
113
    keyArray = keyArray[0].slice(6, keyArray[0].length - 1)
114
115
    if(keyArray.split(' ').length > 1){
116
      keyArray = keyArray.split(' ')[0]
117
    }
118
    arrayBlock[keyArray] = []
119
    // ce while boucle sur les block de contenu {{abe}}
120
    while (match = patt.exec(textEach[0])) {
121
      var v = match[0]
122
123
      if(v.indexOf('abe') > -1){
124
        if(json[keyArray]){
125
          for (i = 0; i < json[keyArray].length; i++) {
126
            addToForm(v, text, json, util, arrayBlock, keyArray, i)
127
          }
128
        }else{
129
          addToForm(v, text, json, util, arrayBlock, keyArray, 0)
130
        }
131
      }
132
    }
133
134
    // ici on boucle a nouveau sur les champs pour les placer a la suite dans le formulaire
135
    var attrArray = [],
136
      length = 0
137
    for(var index in arrayBlock[keyArray]) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
138
      attrArray.push(index)
139
      length = arrayBlock[keyArray][index].length
140
    }
141
142
    for (i = 0; i < length; i++) {
143
      for (var j = 0; j < attrArray.length; j++) {
144
        add(arrayBlock[keyArray][attrArray[j]][i], json, text, util)
145
      }
146
    }
147
  }
148
}
149
150
function addSource(text, json, util) {
151
  var listReg = /({{abe.*type=[\'|\"]data.*}})/g
152
  var match
153
154
  while (match = listReg.exec(text)) {
155
    var obj = cmsData.attributes.getAll(match[0], json)
156
157
    if(obj.editable) {
158
      obj.value = json[getDataIdWithNoSlash(obj.key)]
159
      add(obj, json, text, util)
160
    }else {
161
      json[getDataIdWithNoSlash(obj.key)] = obj.source
162
    }
163
  }
164
}
165
166
function orderByTabindex(a, b) {
167
  if(a.order < b.order) {
168
    return -1
169
  }else if(a.order > b.order) {
170
    return 1
171
  }
172
173
  return 0
174
}
175
176
function orderBlock(util) {
177
    
178
  var formBlock = {}
179
180
  for(var tab in util.form) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
181
182
    var formBlockTab = {}
183
    for (var i = 0; i < util.form[tab].item.length; i++) {
184
      var blockName = (util.form[tab].item[i].block === '') ? 'default_' + i : util.form[tab].item[i].block
185
      if(util.form[tab].item[i].key.indexOf('[') > -1){
186
        blockName = util.form[tab].item[i].key.split('[')[0]
187
      }
188
      if(typeof formBlockTab[blockName] === 'undefined' || formBlockTab[blockName] === null) {
189
        formBlockTab[blockName] = []
190
      }
191
      formBlockTab[blockName].push(util.form[tab].item[i])
192
    }
193
    if(typeof blockName !== 'undefined' && blockName !== null) {
0 ignored issues
show
Bug introduced by
The variable blockName seems to not be initialized for all possible execution paths.
Loading history...
194
      formBlockTab[blockName].sort(orderByTabindex)
195
    }
196
    if(typeof formBlock[tab] === 'undefined' || formBlock[tab] === null) {
197
      formBlock[tab] = {}
198
    }
199
200
    var formBlockOrdered = {}
201
    var arKeys = Object.keys(formBlockTab).sort((a,b) => {
202
      if(parseFloat(formBlockTab[a][0].order) < parseFloat(formBlockTab[b][0].order)) {
0 ignored issues
show
Bug introduced by
The variable formBlockTab is changed as part of the for-each loop for example by {} on line 182. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
203
        return -1
204
      }else if(parseFloat(formBlockTab[a][0].order) > parseFloat(formBlockTab[b][0].order)) {
205
        return 1
206
      }
207
      return 0
208
    })
209
210
    Array.prototype.forEach.call(arKeys, (arKey) => {
211
      formBlockOrdered[arKey] = formBlockTab[arKey]
0 ignored issues
show
Bug introduced by
The variable formBlockTab is changed as part of the for-each loop for example by {} on line 182. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
Bug introduced by
The variable formBlockOrdered is changed as part of the for-each loop for example by {} on line 200. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
212
    })
213
    formBlock[tab] = formBlockOrdered
214
  }
215
216
  var formTabsOrdered = {}
217
  var arKeysTabs = Object.keys(formBlock).sort((a,b) => {
218
    if(parseFloat(formBlock[a][Object.keys(formBlock[a])[0]][0].order) < parseFloat(formBlock[b][Object.keys(formBlock[b])[0]][0].order)) {
219
      return -1
220
    }else if(parseFloat(formBlock[a][Object.keys(formBlock[a])[0]][0].order) > parseFloat(formBlock[b][Object.keys(formBlock[b])[0]][0].order)) {
221
      return 1
222
    }
223
    return 0
224
  })
225
226
  Array.prototype.forEach.call(arKeysTabs, (arKeysTab) => {
227
    if(arKeysTab !== 'slug') formTabsOrdered[arKeysTab] = formBlock[arKeysTab]
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...
228
  })
229
230
  formTabsOrdered['slug'] = formBlock['slug']
231
232
  return formTabsOrdered
233
}
234
235
export function editor(text, json, documentLink, precontrib = false) {
236
  let p = new Promise((resolve) => {
237
    var util = new cmsEditor.form()
238
    var arrayBlock = []
239
    
240
    cmsData.source.getDataList(path.dirname(documentLink), text, json)
241
      .then(() => {
242
        addSource(text, json, util)
243
244
        text = cmsData.source.removeDataList(text)
245
246
        if (!precontrib) {
247
          text = cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist(text)
248
          text = cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist(text)
249
        }
250
251
        matchAttrAbe(text, json, util, arrayBlock)
252
        arrayBlock = []
253
        each(text, json, util, arrayBlock)
254
255
        if(typeof json.abe_meta !== 'undefined' && json.abe_meta !== null) {
256
          var links = json.abe_meta.link.split('/')
257
          var link = links.pop()
258
          json.abe_meta.cleanName = link.replace(/\..+$/, '')
259
          json.abe_meta.cleanFilename = links.join('/').replace(/\..+$/, '')
260
        }
261
262
        if (!precontrib) {
263
          // HOOKS beforeEditorFormBlocks
264
          json = abeExtend.hooks.instance.trigger('beforeEditorFormBlocks', json, text)
265
        }
266
267
        var blocks = orderBlock(util)
268
269
270
        if (!precontrib) {
271
          // HOOKS afterEditorFormBlocks
272
          blocks = abeExtend.hooks.instance.trigger('afterEditorFormBlocks', blocks, json, text)
273
        }
274
275
        abeEngine.instance.content = json
276
277
        resolve({
278
          text: text,
279
          form: blocks,
280
          json: json
281
        })
282
      }).catch(function(e) {
283
        console.error(e)
284
      })
285
  })
286
287
  return p
288
}