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

src/cli/cms/editor/handlebars/printBlock.js   A

Complexity

Total Complexity 15
Complexity/F 3

Size

Lines of Code 80
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
c 0
b 0
f 0
nc 1
dl 0
loc 80
rs 10
wmc 15
mnd 3
bc 11
fnc 5
bpm 2.2
cpm 3
noi 3

1 Function

Rating   Name   Duplication   Size   Complexity  
B printBlock.js ➔ printBlock 0 72 6
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