Completed
Push — master ( 6c2f8d...9c8356 )
by
unknown
01:43
created

src/cli/cms/editor/handlebars/sourceAttr.js   B

Complexity

Total Complexity 36
Complexity/F 4.5

Size

Lines of Code 191
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
nc 1
dl 0
loc 191
rs 8.8
noi 3
cc 0
wmc 36
mnd 5
bc 34
fnc 8
bpm 4.25
cpm 4.5

2 Functions

Rating   Name   Duplication   Size   Complexity  
B sourceAttr.js ➔ isSelected 0 24 3
C sourceAttr.js ➔ sourceAttr 0 122 12
1
function replaceSourceAttr(variables, currentValue, valueToReplace) {
2
  Array.prototype.forEach.call(variables, (variable) => {
3
    var variableToUse = variable.value
4
    if (variableToUse != null && variableToUse.indexOf('.') > -1) {
5
      var checkMatch = variableToUse.split('.')
6
      var found = false
7
      while(!found) {
8
        try {
9
          var isFound = eval('currentValue.' + checkMatch[0])
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
10
          if (isFound != null) {
11
            variableToUse = checkMatch.join('.')
12
            found = true
13
          }else {
14
            checkMatch.shift()
15
            if (checkMatch.length === 0) {
16
              variableToUse = ""
17
              found = true
18
            }
19
          }
20
        }catch(e) {
21
          checkMatch.shift()
22
          if (checkMatch.length === 0) {
23
            variableToUse = ""
24
            found = true
25
          }
26
        }
27
      }
28
    }
29
30
    if (variableToUse != null && variableToUse != "") {
31
      try {
32
        var replaceVal = eval('currentValue.' + variableToUse)
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
33
        valueToReplace = valueToReplace.replace(new RegExp(variable.replace, 'g'), replaceVal)
34
      }catch(e) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
35
36
      }
37
    }else {
38
      valueToReplace = currentValue
39
    }
40
  })
41
42
  return valueToReplace
43
}
44
45
function isSelected(currentValue, values) {
46
  var isEqual = false
47
  if(typeof currentValue === 'object' && Object.prototype.toString.call(currentValue) === '[object Object]') {
48
    Array.prototype.forEach.call(values, (value) => {
49
      var checkAllEqual = false
50
      Array.prototype.forEach.call(Object.keys(value), (key) => {
51
        if (currentValue[key] != null && currentValue[key] == value[key] && checkAllEqual == false) {
52
          checkAllEqual = true
53
        }
54
      })
55
      if (checkAllEqual) {
56
        isEqual = true
57
      }
58
    })
59
  }else {
60
    Array.prototype.forEach.call(values, (value) => {
61
      if (currentValue == value) {
62
        isEqual = true
63
      }
64
    })
65
  }
66
67
  return isEqual
68
}
69
70
export default function sourceAttr(val, params) {
71
  var hiddenVal = val
72
  var selected = ''
73
  var display = params.display
74
75
  var variables = []
76
  var displayValues = params.display
77
  var match
78
  var isVariable = false
79
  while(match = /\{\{(.*?)\}\}/g.exec(displayValues)) {
80
    if (match != null && match[1] != null) {
81
      isVariable = true
82
      variables.push({
83
        replace: match[0],
84
        value: match[1]
85
      })
86
      displayValues = displayValues.replace('{{' + match[1] + '}}', "")
87
    }
88
  }
89
90
  if (!isVariable) {
91
    variables.push({
92
      replace: displayValues,
93
      value: displayValues
94
    })
95
  }
96
97
  var obectToValue = display
98
  // var replaceValue = params.value
99
  var replaceValue = val
100
  if(typeof replaceValue === 'object' && Object.prototype.toString.call(replaceValue) === '[object Object]') {
101
    hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, ''')
102
    val = replaceSourceAttr(variables, replaceValue, obectToValue)
103
104
    if (isSelected(replaceValue, params.value)) {
105
      selected = 'selected="selected"'
106
    }
107
  }else if(typeof replaceValue === 'object' && Object.prototype.toString.call(replaceValue) === '[object Array]') {
108
    hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, ''')
109
    Array.prototype.forEach.call(replaceValue, (currentValue) => {
110
      obectToValue = replaceSourceAttr(variables, currentValue, obectToValue)
111
    })
112
    if (isSelected(replaceValue, params.value)) {
113
      selected = 'selected="selected"'
114
    }
115
    val = obectToValue
116
  }else {
117
    if (isSelected(replaceValue, params.value)) {
118
      selected = 'selected="selected"'
119
    }
120
  }
121
122
  // if (variables.length === 0) {
123
  //   variables.push({
124
  //     replace: display,
125
  //     value: display
126
  //   })
127
  // }
128
129
  // if (display.indexOf('{{') > -1) {
130
  //   display = display.replace('{{', '').replace('}}', '')
131
  //   if (display.indexOf('.') > -1) {
132
  //     display = display.split('.')
133
  //     display = display[display.length - 1]
134
  //   }
135
  // }
136
137
  // if(typeof hiddenVal === 'object' && Object.prototype.toString.call(hiddenVal) === '[object Object]') {
138
  //   hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, ''')
139
140
  //   try {
141
  //     var displayVal = eval('val.' + display)
142
  //     if(display != null && displayVal != null) {
143
  //       val = displayVal
144
  //     }else {
145
  //       val = val[Object.keys(val)[0]]
146
  //     }
147
  //   }catch(e) {
148
  //     val = val[Object.keys(val)[0]]
149
  //   }
150
  // }
151
152
  // if(typeof params.value === 'object' && Object.prototype.toString.call(params.value) === '[object Array]') {
153
  //   Array.prototype.forEach.call(params.value, (v) => {
154
  //     var item = v
155
  //     try {
156
  //       var displayV = eval('item.' + display)
157
  //       if(display != null && displayV !== null) {
158
  //         item = displayV
159
  //       } else {
160
  //         if(typeof v === 'string') {
161
  //           item = v
162
  //         } else {
163
  //           item = v[Object.keys(v)[0]]
164
  //         }
165
  //       }
166
  //     } catch(e) {
167
  //       item = v[Object.keys(v)[0]]
168
  //     }
169
      
170
  //     if(typeof val === 'object' && Object.prototype.toString.call(val) === '[object Array]'
171
  //       && typeof item === 'object' && Object.prototype.toString.call(item) === '[object Array]') {
172
        
173
  //       Array.prototype.forEach.call(item, (i) => {
174
  //         if(val.indexOf(i) >= 0) {
175
  //           selected = 'selected="selected"'
176
  //         }
177
  //       })
178
  //     }else if(val === item) {
179
  //       selected = 'selected="selected"'
180
  //     }
181
  //   })
182
  // }else if(params.value === hiddenVal) {
183
  //   selected = 'selected="selected"'
184
  // }
185
186
  return {
187
    hiddenVal: hiddenVal,
188
    selected: selected,
189
    val: val
190
  }
191
}
192