1
|
|
|
function isSelected(currentValue, values) { |
2
|
|
|
var isEqual = false |
3
|
|
|
if(typeof currentValue === 'object' && Object.prototype.toString.call(currentValue) === '[object Object]') { |
4
|
|
|
Array.prototype.forEach.call(values, (value) => { |
5
|
|
|
if (value != null) { |
6
|
|
|
var checkAllEqual = false |
7
|
|
|
Array.prototype.forEach.call(Object.keys(value), (key) => { |
8
|
|
|
if (currentValue[key] != null && currentValue[key] == value[key] && checkAllEqual == false) { |
9
|
|
|
checkAllEqual = true |
10
|
|
|
} |
11
|
|
|
}) |
12
|
|
|
if (checkAllEqual) { |
13
|
|
|
isEqual = true |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
}) |
17
|
|
|
}else { |
18
|
|
|
Array.prototype.forEach.call(values, (value) => { |
19
|
|
|
if (currentValue == value) { |
20
|
|
|
isEqual = true |
21
|
|
|
} |
22
|
|
|
}) |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return isEqual |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
export default function sourceAttr(obj, params) { |
29
|
|
|
var str = params.display |
30
|
|
|
var selected = '' |
31
|
|
|
var displayName = prepareDisplay(obj, str) |
32
|
|
|
if (params.value === displayName) { |
33
|
|
|
selected = 'selected' |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return { |
37
|
|
|
hiddenVal: JSON.stringify(obj).replace(/\'/g, '"e;'), |
38
|
|
|
selected: selected, |
39
|
|
|
val: displayName |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* return the value from a json obj of a nested attribute |
45
|
|
|
* @param {Object} obj the json object |
46
|
|
|
* @param {string} path the path to object (dot notation) |
47
|
|
|
* @return {[type]} the object containing the path object or undefined |
48
|
|
|
*/ |
49
|
|
|
function get(obj, path) { |
50
|
|
|
return path.split(".").reduce(function(prev, curr) { |
51
|
|
|
return prev ? prev[curr] : undefined |
52
|
|
|
}, obj || self) |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* replace the variables in str by values from obj |
57
|
|
|
* corresponding to keys |
58
|
|
|
* @param {Object} obj the json object |
59
|
|
|
* @param {string} str the string |
60
|
|
|
* @return {string} the string with values |
61
|
|
|
*/ |
62
|
|
|
function prepareDisplay(obj, str) { |
63
|
|
|
var keys = getKeys(str) |
64
|
|
|
Array.prototype.forEach.call(keys, (key) => { |
65
|
|
|
var val = get(obj, key) |
66
|
|
|
var pattern = new RegExp("{{"+key+"}}|"+key) |
67
|
|
|
str = str.replace(pattern, val) |
68
|
|
|
}) |
69
|
|
|
|
70
|
|
|
return str |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* return array of variables {{variable}} extracted from str |
75
|
|
|
* @param {string} str the string containing variables |
76
|
|
|
* @return {Array} the array of variables |
77
|
|
|
*/ |
78
|
|
|
function getKeys(str){ |
79
|
|
|
var regex = /\{\{(.*?)\}\}/g; |
80
|
|
|
var variables = [] |
81
|
|
|
var match |
82
|
|
|
|
83
|
|
|
while ((match = regex.exec(str)) !== null) { |
84
|
|
|
variables.push(match[1]) |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (variables.length == 0) { |
88
|
|
|
variables.push(str) |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return variables |
92
|
|
|
} |
93
|
|
|
|