diff --git a/src/server/public/abejs/scripts/modules/EditorSave.js b/src/server/public/abejs/scripts/modules/EditorSave.js index 037a85bd..090b9c07 100755 --- a/src/server/public/abejs/scripts/modules/EditorSave.js +++ b/src/server/public/abejs/scripts/modules/EditorSave.js @@ -51,10 +51,11 @@ export default class EditorSave { inputs = inputs.concat(textareas) this._json.data = json - Array.prototype.forEach.call(inputs, (input) => { var dataId = input.getAttribute('data-id') var maxlength = input.getAttribute('data-maxlength') + var value + if(input.type === 'file') return if(typeof dataId !== 'undefined' && dataId !== null) { if(dataId.indexOf('[') > -1){ @@ -65,8 +66,24 @@ export default class EditorSave { if(typeof this._json.data[obj][index] === 'undefined' || this._json.data[obj][index] === null) this._json.data[obj][index] = {} var keyJson = key.replace(/.*?\[[0-9].*?\]\./, '') - setObjByString(this._json.data[obj][index], keyJson, input.value); - // this._json.data[obj][index][key] = input.value + if (input.getAttribute('data-autocomplete') === 'true') { + var results = input.parentNode.querySelectorAll('.autocomplete-result-wrapper .autocomplete-result') + value = [] + Array.prototype.forEach.call(results, (result) => { + var val = result.getAttribute('value') + if(val !== '') { + if(val.indexOf('{') > -1 || val.indexOf('[') > -1) { + value.push(JSON.parse(val)) + } else { + value.push(val) + } + } + }) + } else { + value = input.value + } + + setObjByString(this._json.data[obj][index], keyJson, value); var emptyObject = 0 for(var prop in this._json.data[obj][index]) { if(typeof this._json.data[obj][index][prop] !== "string" || this._json.data[obj][index][prop].trim() !== '') emptyObject++ @@ -74,9 +91,7 @@ export default class EditorSave { if(emptyObject === 0) { delete this._json.data[obj][index] } - }else { - var value - + } else { if (input.nodeName === 'SELECT' && maxlength != "1") { var checked = input.querySelectorAll('option:checked') value = [] @@ -84,12 +99,12 @@ export default class EditorSave { if(check.value !== '') { if(check.value.indexOf('{') > -1 || check.value.indexOf('[') > -1) { value.push(JSON.parse(check.value)) - }else { + } else { value.push(check.value) } } }) - }else if (input.getAttribute('data-autocomplete') === 'true') { + } else if (input.getAttribute('data-autocomplete') === 'true') { var results = input.parentNode.querySelectorAll('.autocomplete-result-wrapper .autocomplete-result') value = [] Array.prototype.forEach.call(results, (result) => { @@ -97,18 +112,17 @@ export default class EditorSave { if(val !== '') { if(val.indexOf('{') > -1 || val.indexOf('[') > -1) { value.push(JSON.parse(val)) - }else { + } else { value.push(val) } } }) - }else if (input.value.indexOf('{') > -1) { + } else if (input.value.indexOf('{') > -1) { value = JSON.parse(input.value) - }else { + } else { value = input.value //.replace(/\"/g, '\"') + '' } setObjByString(this._json.data, dataId, value); - // this._json.data[dataId] = value } } }) @@ -122,27 +136,27 @@ export default class EditorSave { var url = target.getAttribute('data-url') this._json.save(this._saveType, url) - .then((result) => { - target.classList.add('done') - // this._populateFromJson(this._json.data) + .then((result) => { + target.classList.add('done') + // this._populateFromJson(this._json.data) - target.classList.remove('loading') - target.classList.remove('done') - target.removeAttribute('disabled') + target.classList.remove('loading') + target.classList.remove('done') + target.removeAttribute('disabled') - if(result.success === 1) { - this._abeDisplayStatus.innerHTML = result.json.abe_meta.status - window.json = result.json - } - var formWrapper = document.querySelector('#abeForm') - Array.prototype.forEach.call(formWrapper.classList, (classStr) => { - if(classStr.indexOf('status-') > -1) formWrapper.classList.remove(classStr) - }) - formWrapper.classList.add('status-' + result.json.abe_meta.status) - this.onFileSaved._fire() - }).catch(function(e) { - console.error(e) - }) + if(result.success === 1) { + this._abeDisplayStatus.innerHTML = result.json.abe_meta.status + window.json = result.json + } + var formWrapper = document.querySelector('#abeForm') + Array.prototype.forEach.call(formWrapper.classList, (classStr) => { + if(classStr.indexOf('status-') > -1) formWrapper.classList.remove(classStr) + }) + formWrapper.classList.add('status-' + result.json.abe_meta.status) + this.onFileSaved._fire() + }).catch(function(e) { + console.error(e) + }) } /** @@ -160,7 +174,7 @@ export default class EditorSave { this._saveType = e.currentTarget.getAttribute('data-action') if (this._saveType !== 'draft' && this._saveType !== 'reject') { this._abeFormRequired() - }else { + } else { this.savePage(this._saveType) // this._abeFormSubmit.click() } @@ -189,14 +203,14 @@ export default class EditorSave { if (countValue.length <= 0) { formGroup.classList.add('has-error') valid = false - }else { + } else { formGroup.classList.remove('has-error') } - }else if(typeof required !== 'undefined' && required !== null && (required === 'true' || required === true)) { + } else if(typeof required !== 'undefined' && required !== null && (required === 'true' || required === true)) { if (input.value === '') { formGroup.classList.add('has-error') valid = false - }else { + } else { formGroup.classList.remove('has-error') } } @@ -206,7 +220,7 @@ export default class EditorSave { if (valid) { this.savePage(this._saveType) // this._abeFormSubmit.click() - }else { + } else { alert('Required fields are missing') } } @@ -225,7 +239,7 @@ export default class EditorSave { var value = json[id] if(typeof value === 'object' && Object.prototype.toString.call(value) === '[object Array]') { value = JSON.stringify(value) - }else if(typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]') { + } else if(typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]') { value = JSON.stringify(value) } form.value = value