Skip to content

Commit

Permalink
refactoring: the display attribute ind a {{abe type=data ...}} is pro…
Browse files Browse the repository at this point in the history
…perly handled in any case
  • Loading branch information
gregorybesson committed Nov 26, 2016
1 parent 2d5f837 commit 4205bde
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 310 deletions.
209 changes: 54 additions & 155 deletions src/cli/cms/editor/handlebars/sourceAttr.js
@@ -1,47 +1,3 @@
function replaceSourceAttr(variables, currentValue, valueToReplace) {
Array.prototype.forEach.call(variables, (variable) => {
var variableToUse = variable.value
if (variableToUse != null && variableToUse.indexOf('.') > -1) {
var checkMatch = variableToUse.split('.')
var found = false
while(!found) {
try {
var isFound = eval('currentValue.' + checkMatch[0])
if (isFound != null) {
variableToUse = checkMatch.join('.')
found = true
}else {
checkMatch.shift()
if (checkMatch.length === 0) {
variableToUse = ''
found = true
}
}
}catch(e) {
checkMatch.shift()
if (checkMatch.length === 0) {
variableToUse = ''
found = true
}
}
}
}

if (variableToUse != null && variableToUse != '') {
try {
var replaceVal = eval('currentValue.' + variableToUse)
valueToReplace = valueToReplace.replace(new RegExp(variable.replace, 'g'), replaceVal)
}catch(e) {

}
}else {
valueToReplace = currentValue
}
})

return valueToReplace
}

function isSelected(currentValue, values) {
var isEqual = false
if(typeof currentValue === 'object' && Object.prototype.toString.call(currentValue) === '[object Object]') {
Expand Down Expand Up @@ -69,125 +25,68 @@ function isSelected(currentValue, values) {
return isEqual
}

export default function sourceAttr(val, params) {
var hiddenVal = val
export default function sourceAttr(obj, params) {
var str = params.display
var selected = ''
var display = params.display

var variables = []
var displayValues = params.display
var match
var isVariable = false
while(match = /\{\{(.*?)\}\}/g.exec(displayValues)) {
if (match != null && match[1] != null) {
isVariable = true
variables.push({
replace: match[0],
value: match[1]
})
displayValues = displayValues.replace('{{' + match[1] + '}}', '')
}
}

if (!isVariable) {
variables.push({
replace: displayValues,
value: displayValues
})
var displayName = prepareDisplay(obj, str)
if (params.value === displayName) {
selected = 'selected'
}

var obectToValue = display
// var replaceValue = params.value
var replaceValue = val
if(typeof replaceValue === 'object' && Object.prototype.toString.call(replaceValue) === '[object Object]') {
hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, ''')
val = replaceSourceAttr(variables, replaceValue, obectToValue)

if (isSelected(replaceValue, params.value)) {
selected = 'selected="selected"'
}
}else if(typeof replaceValue === 'object' && Object.prototype.toString.call(replaceValue) === '[object Array]') {
hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, ''')
Array.prototype.forEach.call(replaceValue, (currentValue) => {
obectToValue = replaceSourceAttr(variables, currentValue, obectToValue)
})
if (isSelected(replaceValue, params.value)) {
selected = 'selected="selected"'
}
val = obectToValue
}else {
if (isSelected(replaceValue, params.value)) {
selected = 'selected="selected"'
}
return {
hiddenVal: JSON.stringify(obj).replace(/\'/g, '&quote;'),
selected: selected,
val: displayName
}
}

// if (variables.length === 0) {
// variables.push({
// replace: display,
// value: display
// })
// }

// if (display.indexOf('{{') > -1) {
// display = display.replace('{{', '').replace('}}', '')
// if (display.indexOf('.') > -1) {
// display = display.split('.')
// display = display[display.length - 1]
// }
// }
/**
* return the value from a json obj of a nested attribute
* @param {Object} obj the json object
* @param {string} path the path to object (dot notation)
* @return {[type]} the object containing the path object or undefined
*/
function get(obj, path) {
return path.split(".").reduce(function(prev, curr) {
return prev ? prev[curr] : undefined
}, obj || self)
}

// if(typeof hiddenVal === 'object' && Object.prototype.toString.call(hiddenVal) === '[object Object]') {
// hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, ''')
/**
* replace the variables in str by values from obj
* corresponding to keys
* @param {Object} obj the json object
* @param {string} str the string
* @return {string} the string with values
*/
function prepareDisplay(obj, str) {
var keys = getKeys(str)
Array.prototype.forEach.call(keys, (key) => {
var val = get(obj, key)
var pattern = new RegExp("{{"+key+"}}|"+key)
str = str.replace(pattern, val)
})

// try {
// var displayVal = eval('val.' + display)
// if(display != null && displayVal != null) {
// val = displayVal
// }else {
// val = val[Object.keys(val)[0]]
// }
// }catch(e) {
// val = val[Object.keys(val)[0]]
// }
// }
return str
}

// if(typeof params.value === 'object' && Object.prototype.toString.call(params.value) === '[object Array]') {
// Array.prototype.forEach.call(params.value, (v) => {
// var item = v
// try {
// var displayV = eval('item.' + display)
// if(display != null && displayV !== null) {
// item = displayV
// } else {
// if(typeof v === 'string') {
// item = v
// } else {
// item = v[Object.keys(v)[0]]
// }
// }
// } catch(e) {
// item = v[Object.keys(v)[0]]
// }

// if(typeof val === 'object' && Object.prototype.toString.call(val) === '[object Array]'
// && typeof item === 'object' && Object.prototype.toString.call(item) === '[object Array]') {

// Array.prototype.forEach.call(item, (i) => {
// if(val.indexOf(i) >= 0) {
// selected = 'selected="selected"'
// }
// })
// }else if(val === item) {
// selected = 'selected="selected"'
// }
// })
// }else if(params.value === hiddenVal) {
// selected = 'selected="selected"'
// }
/**
* return array of variables {{variable}} extracted from str
* @param {string} str the string containing variables
* @return {Array} the array of variables
*/
function getKeys(str){
var regex = /\{\{(.*?)\}\}/g;
var variables = []
var match

return {
hiddenVal: hiddenVal,
selected: selected,
val: val
while ((match = regex.exec(str)) !== null) {
variables.push(match[1])
}

if (variables.length == 0) {
variables.push(str)
}

return variables
}

0 comments on commit 4205bde

Please sign in to comment.