|
1
|
|
|
export 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
|
|
|
|
|
33
|
|
|
Array.prototype.forEach.call(params.value, (pValue) => { |
|
34
|
|
|
var pDisplay = prepareDisplay(pValue, str) |
|
35
|
|
|
if (pDisplay === displayName) { |
|
36
|
|
|
selected = 'selected' |
|
37
|
|
|
} |
|
38
|
|
|
}) |
|
39
|
|
|
|
|
40
|
|
|
return { |
|
41
|
|
|
hiddenVal: (typeof obj == 'object') ? JSON.stringify(obj).replace(/\'/g, '"e;') : obj, |
|
42
|
|
|
selected: selected, |
|
43
|
|
|
val: displayName |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* return the value from a json obj of a nested attribute |
|
49
|
|
|
* @param {Object} obj the json object |
|
50
|
|
|
* @param {string} path the path to object (dot notation) |
|
51
|
|
|
* @return {[type]} the object containing the path object or undefined |
|
52
|
|
|
*/ |
|
53
|
|
|
export function get(obj, path) { |
|
54
|
|
|
if (path == null) { |
|
55
|
|
|
return obj |
|
56
|
|
|
} |
|
57
|
|
|
return path.split('.').reduce(function(prev, curr) { |
|
58
|
|
|
return prev ? prev[curr] : undefined |
|
59
|
|
|
}, obj || this) |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* replace the variables in str by values from obj |
|
64
|
|
|
* corresponding to keys |
|
65
|
|
|
* @param {Object} obj the json object |
|
66
|
|
|
* @param {string} str the string |
|
67
|
|
|
* @return {string} the string with values |
|
68
|
|
|
*/ |
|
69
|
|
|
export function prepareDisplay(obj, str = null) { |
|
70
|
|
|
var keys = getKeys(str) |
|
71
|
|
|
Array.prototype.forEach.call(keys, (key) => { |
|
72
|
|
|
var val = get(obj, key) |
|
73
|
|
|
var pattern = new RegExp('{{'+key+'}}|'+key) |
|
74
|
|
|
str = str.replace(pattern, val) |
|
75
|
|
|
}) |
|
76
|
|
|
|
|
77
|
|
|
if (str == null) { |
|
78
|
|
|
str = obj |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return str |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* return array of variables {{variable}} extracted from str |
|
86
|
|
|
* @param {string} str the string containing variables |
|
87
|
|
|
* @return {Array} the array of variables |
|
88
|
|
|
*/ |
|
89
|
|
|
export function getKeys(str){ |
|
90
|
|
|
var regex = /\{\{(.*?)\}\}/g |
|
91
|
|
|
var variables = [] |
|
92
|
|
|
var match |
|
93
|
|
|
|
|
94
|
|
|
while ((match = regex.exec(str)) !== null) { |
|
95
|
|
|
if (match[1] != null) { |
|
96
|
|
|
variables.push(match[1]) |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (variables.length == 0 && str != null) { |
|
101
|
|
|
variables.push(str) |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return variables |
|
105
|
|
|
} |
|
106
|
|
|
|