Skip to content

Commit

Permalink
enhancement: select autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Nov 18, 2016
1 parent b853a02 commit 9c83561
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/cli/cms/editor/handlebars/printInput.js
Expand Up @@ -92,14 +92,15 @@ export default function printInput () {
res += '</div>'
res += `<input value="" autocomplete="off" data-value='${lastValues}' type="text" ${disabled} ${commonParams} class="${inputClass}" />`
}else {

lastValues = JSON.stringify(params.value).replace(/\'/g, '&quote;')
res += `<select ${multiple} ${disabled} ${commonParams} class="${inputClass}"
last-values='${lastValues}'>`

if (!params.required) {
res += '<option value=\'\'></option>'
}

if(typeof params.source === 'object' && Object.prototype.toString.call(params.source) === '[object Array]') {
Array.prototype.forEach.call(params.source, (val) => {
res += sourceOption(val, params)
Expand Down
25 changes: 13 additions & 12 deletions src/cli/cms/editor/handlebars/sourceAttr.js
@@ -1,7 +1,7 @@
function replaceSourceAttr(variables, currentValue, valueToReplace) {
Array.prototype.forEach.call(variables, (variable) => {
var variableToUse = variable.value
if (variableToUse.indexOf('.') > -1) {
if (variableToUse != null && variableToUse.indexOf('.') > -1) {
var checkMatch = variableToUse.split('.')
var found = false
while(!found) {
Expand Down Expand Up @@ -34,24 +34,29 @@ function replaceSourceAttr(variables, currentValue, valueToReplace) {
}catch(e) {

}
}else {
valueToReplace = currentValue
}
})

return valueToReplace
}

function isSelected(currentValue, values) {
var isEqual = true
if(typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]') {
var isEqual = false
if(typeof currentValue === 'object' && Object.prototype.toString.call(currentValue) === '[object Object]') {
Array.prototype.forEach.call(values, (value) => {
var checkAllEqual = false
Array.prototype.forEach.call(Object.keys(value), (key) => {
if (currentValue[key] != null && currentValue[key] != value[key]) {
isEqual = false
if (currentValue[key] != null && currentValue[key] == value[key] && checkAllEqual == false) {
checkAllEqual = true
}
})
if (checkAllEqual) {
isEqual = true
}
})
}else {
isEqual = false
Array.prototype.forEach.call(values, (value) => {
if (currentValue == value) {
isEqual = true
Expand Down Expand Up @@ -89,22 +94,18 @@ export default function sourceAttr(val, params) {
})
}

if (params.key === 'articles') {
console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
console.log('params', params)
console.log('val', val)
}

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, '&apos;')
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, '&apos;')
Array.prototype.forEach.call(replaceValue, (currentValue) => {
obectToValue = replaceSourceAttr(variables, currentValue, obectToValue)
})
Expand Down
48 changes: 34 additions & 14 deletions src/server/public/scripts/modules/EditorAutocomplete.js
Expand Up @@ -196,10 +196,12 @@ export default class EditorAutocomplete {
}

if (!isVariable) {
variables.push({
replace: displayValues,
value: displayValues
})
if (display != null) {
variables.push({
replace: display,
value: display
})
}
}

this._divWrapper.innerHTML = ""
Expand All @@ -225,25 +227,43 @@ export default class EditorAutocomplete {
if (Object.prototype.toString.call(trueJson) === '[object Array]') {
var j = 0
Array.prototype.forEach.call(trueJson, (item) => {
if (deepValues[j] == null) {
deepValues[j] = {replace: []}
var sourceVal = ""
var replace = []
var sourceDisplay = display
if (Object.prototype.toString.call(item) === '[object Object]') {
try {
var sourceValCheck = eval('item.' + variables[i].value)
if (sourceValCheck.indexOf(val) > -1) {
replace.push({key: variables[i].replace, value: sourceValCheck})
sourceVal = item
sourceDisplay = sourceValCheck
}
}catch(e) {
console.log(e)
}
}else {
if (item.indexOf(val) > -1) {
sourceVal = item
}
}
try {
var val = eval('item.' + variables[i].value)
deepValues[j].replace.push({key: variables[i].replace, value: val})
deepValues[j].value = item
}catch(e) {
console.log(e)
if (sourceVal != "") {
if (deepValues[j] == null) {
deepValues[j] = {
replace: replace,
value: typeof sourceVal == 'string' ? [sourceVal] : sourceVal,
display: (display == null || display == "null") ? sourceVal : display
}
}
j++
}
j++
})
}

i++
})

Array.prototype.forEach.call(deepValues, (item) => {
var displayName = display
var displayName = item.display
Array.prototype.forEach.call(item.replace, (replace) => {
displayName = displayName.replace(new RegExp(replace.key, 'g'), replace.value)
})
Expand Down
2 changes: 1 addition & 1 deletion src/server/public/scripts/modules/EditorInputs.js
Expand Up @@ -148,7 +148,7 @@ export default class EditorInputs {
var count = optionsChecked.length
var attr = EditorUtils.getAttr(target)

if(typeof maxLength !== 'undefined' && maxLength !== null && maxLength !== '') {
if(typeof maxLength !== 'undefined' && maxLength !== null && maxLength !== '' && !isNaN(maxLength)) {
var lastValues
if(count > maxLength) {
lastValues = JSON.parse(target.getAttribute('last-values'))
Expand Down
2 changes: 1 addition & 1 deletion src/server/sass/modules/_editor-inputs.scss
Expand Up @@ -56,7 +56,7 @@
}

select[multiple], select[size] {
height: $select-size;
max-height: $select-size;
}

.has-error {
Expand Down
2 changes: 1 addition & 1 deletion src/server/sass/modules/_editor.scss
Expand Up @@ -32,7 +32,7 @@

select,
input {
height: 30px;
min-height: 30px;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/sass/modules/_manager-left.scss
Expand Up @@ -52,7 +52,7 @@
select,
input {
font-size: 12px;
height: 30px;
min-height: 30px;
}
}

Expand Down

0 comments on commit 9c83561

Please sign in to comment.