Completed
Push — master ( 9cf608...6ea79d )
by greg
01:55 queued 11s
created

printBlock.js ➔ printBlock   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
c 0
b 0
f 0
nc 10
nop 2
dl 0
loc 72
rs 8.5164

1 Function

Rating   Name   Duplication   Size   Complexity  
A printBlock.js ➔ ... ➔ ??? 0 4 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import printInput from './printInput'
2
import abeEngine from './abeEngine'
3
4
import {
5
  config
6
  ,cmsTemplates
7
} from '../../../../cli'
8
9
export default function printBlock (ctx, root) {
10
  var res = ''
11
  var precontrib = false
12
  if (root.precontrib != null) {
13
    precontrib = root.precontrib
14
  }
15
16
  if(ctx[0].block != null && ctx[0].block !== '') {
17
    res += `<div class="form-group">
18
              <label class="title">${ctx[0].block}</label>
19
              <div class='single-block well well-sm'>`
20
    Array.prototype.forEach.call(ctx, (item) => {
21
      if (precontrib) item.value = ''
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...
22
      res += printInput(item, root)
23
    })
24
    res += '</div></div>'
25
  }else if(ctx[0].key.indexOf('[') > -1) {
26
    var ctxBlock = ctx[0].key.split('[')[0]
27
    res += `<div class="form-group">
28
              <div class="list-group" data-block="${ctxBlock}" >
29
                <label>
30
                  ${ctxBlock}
31
                  <button type="button" class="btn btn-success add-block" title="Add new block" >
32
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
33
                  </button>
34
                </label>`
35
    
36
    var arrItem = []
37
    Array.prototype.forEach.call(ctx, (item) => {
38
      var index = item.key.match(/[^\[\]]+?(?=\])/)
39
      if(arrItem[index] == null) {
40
        arrItem[index] = []
41
      }
42
      arrItem[index].push(item)
43
    })
44
45
    Array.prototype.forEach.call(Object.keys(arrItem), (i) => {
46
      var key = arrItem[i][0].key.split('[')[0]
47
      var display = ''
48
      if(abeEngine.instance.content[key] == null
49
        || abeEngine.instance.content[key].length === 0) {
50
        display = 'style="display: none"'
51
      }
52
      res += `<div class="list-block" data-block="${key}${i}" ${display}>
53
                <button type="button" class="btn btn-info collapsed" data-toggle="collapse" data-target="#${key}${i}" >
54
                  Section <span class='label-count'>${i}</span> :
55
                  <span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span>
56
                </button>
57
                <button type="button" class="btn btn-danger remove-block" title="Delete block" >
58
                  <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
59
                </button>
60
                <div id="${key}${i}" class="collapse" >
61
                `
62
      Array.prototype.forEach.call(arrItem[i], (item) => {
63
        if (precontrib) item.value = ''
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...
64
        res += printInput(item, root)
65
      })
66
      res += '</div></div>'
67
    })
68
69
    res +=  `
70
          </div>
71
        </div>`
72
  }else {
73
    if (precontrib) ctx[0].value = ''
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...
74
    res += printInput(ctx[0], root)
75
  }
76
77
  // var template = cmsTemplates.Handlebars.compile(res)
78
  // return new cmsTemplates.Handlebars.SafeString(template(ctx, {data: {intl: config.intlData}}))
79
  return res
80
}
81