Passed
Push — master ( 61dd3f...482d1b )
by greg
01:51
created

listPage.js ➔ listPage   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
c 1
b 0
f 0
nc 48
dl 0
loc 83
rs 5.034
nop 3

How to fix   Long Method    Complexity   

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:

Complexity

Complex classes like listPage.js ➔ listPage 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 Handlebars from 'handlebars'
2
import moment from 'moment'
3
import {math, Hooks} from '../../../'
4
5
export default function listPage(file, index, text) {
6
  var res = ''
7
8
  file = Hooks.instance.trigger('beforeListPage', file, index, text)
9
10
  res += '<tr>'
11
  res += `<td>${math(index, '+', 1)}</td>
12
        <td>
13
          <a href="/abe/${file.abe_meta.template}?filePath=${file.abe_meta.link}" class="file-path">
14
            ${file.abe_meta.link}
15
          </a>
16
        </td>`
17
  
18
  if(file.abe_meta.template){
19
    res += `<td align="center">
20
              ${file.abe_meta.template}
21
            </td>`
22
  }else {
23
    res += '<td align="center"></td>'
24
  }
25
  
26
  if(file.date){
27
    var dateSearch = moment(file.date).format('YYYY-MM-DD')
28
    var dateOrder = new Date(file.date).getTime()
29
    res += `<td align="center" data-search="${dateSearch}" data-order="${dateOrder}">
30
              ${dateSearch}
31
            </td>`
32
  }else {
33
    res += '<td align="center" data-search="0000-00-00" data-order="0"></td>'
34
  }
35
36
  var workflow = ''
37
38
  workflow += '<td align="center" class="draft">'
39
  if(typeof file.draft !== 'undefined' && file.draft !== null) {
40
    if((typeof file.publish === 'undefined' || file.publish === null)
41
      || (file.publish && file.publish.date < file.draft.date)) {
42
      workflow += `<a href="/abe/${file.abe_meta.template}?filePath=${file.draft.html}" class="label label-default label-draft">draft</a>`
43
    }else {
44
      workflow += `<a href="/abe/${file.abe_meta.template}?filePath=${file.draft.html}" class="hidden label label-default label-draft">draft</a>`
45
    }
46
  }else {
47
    workflow += `<a href="/abe/${file.abe_meta.template}?filePath=${file.abe_meta.link}" class="hidden label label-default label-draft">draft</a>`
48
  }
49
50
  workflow += '</td>'
51
  workflow += '<td align="center" class="publish">'
52
53
  if (file.publish){
54
    workflow += `<a href="/abe/${file.abe_meta.template}?filePath=${file.publish.html}" class="checkmark label-published">&#10004;</a>`
55
  }
56
  workflow += '</td>'
57
58
  workflow = Hooks.instance.trigger('afterListPageDraft', workflow, file, index, text)
59
  res += workflow
60
61
  res += `<td align="center">
62
            <div class="row icons-action">`
63
64
  if(typeof file.publish !== 'undefined' && file.publish !== null) {
65
    res += `<a href="/unpublish/?filePath=${file.abe_meta.link}"
66
               title="${text.unpublish}"
67
               class="icon" data-unpublish="true" data-text="${text.confirmUnpublish} ${file.abe_meta.link}">
68
              <span class="glyphicon glyphicon-eye-close"></span>
69
            </a>`
70
  }
71
      
72
  res += `<a href="/delete/?filePath=${file.abe_meta.link}"
73
             title="${text.delete}"
74
             class="icon"
75
             data-delete="true"
76
             data-text="${text.confirmDelete} ${file.abe_meta.link}">
77
            <span class="glyphicon glyphicon-trash"></span>
78
          </a>`
79
80
  res += `
81
        </div>
82
      </td>
83
    </tr>`
84
85
  res = Hooks.instance.trigger('afterListPage', res, file, index, text)
86
  return new Handlebars.SafeString(res)
87
}
88