Skip to content

Commit

Permalink
fix: bug abe_meta.template
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Dec 19, 2016
1 parent 58098b5 commit 58ccfe3
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/cli/cms/editor/form.js
Expand Up @@ -60,7 +60,7 @@ export default class Form {
}

obj = extend(true, defaultValues, obj)
obj.key = obj.key.replace(/\./, '-')
obj.key = obj.key//.replace(/\./, '-')

if(obj.key.indexOf('[') < 0 && obj.key.indexOf('.') > -1) {
obj.block = obj.key.split('.')[0]
Expand Down
11 changes: 10 additions & 1 deletion src/cli/cms/editor/handlebars/compileAbe.js
Expand Up @@ -46,7 +46,16 @@ export default function compileAbe(){
key = arguments[0].hash['key'].replace('.', '-')

hash = arguments[0].hash
value = ((content) ? content[hash.key.replace('.', '-')] : hash.key)
if(content) {
try {
value = eval('content.' + hash.key)
}catch(e) {
value = hash.key
}
}else {
value = hash.key
}
// value = ((content) ? content[hash.key.replace('.', '-')] : hash.key)
if(typeof value === 'function' || value == null) {
value = ''
}
Expand Down
10 changes: 8 additions & 2 deletions src/server/controllers/editor.js
Expand Up @@ -72,8 +72,14 @@ function addToForm(match, text, json, util, arrayBlock, keyArray = null, i = 0)
}

}else if(util.dontHaveKey(obj.key) && cmsData.regex.isSingleAbe(v, text)) {
realKey = obj.key.replace(/\./g, '-')
obj.value = json[getDataIdWithNoSlash(realKey)]
realKey = obj.key//.replace(/\./g, '-')
// obj.value = json[getDataIdWithNoSlash(realKey)]
try {
console.log(`json.${getDataIdWithNoSlash(realKey)}`)
obj.value = eval(`json.${getDataIdWithNoSlash(realKey)}`)
}catch(e) {
obj.value = null
}
json[getDataIdWithNoSlash(obj.key)] = add(obj, json, text, util)
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/server/public/abejs/scripts/modules/EditorSave.js
Expand Up @@ -4,6 +4,7 @@ import {IframeNode} from '../utils/iframe'
import EditorUtils from './EditorUtils'
import Json from '../modules/EditorJson'
import on from 'on'
import {setObjByString} from '../utils/jsonObject'

export default class EditorSave {
constructor() {
Expand Down Expand Up @@ -40,7 +41,7 @@ export default class EditorSave {
* @return {Object} json
*/
serializeForm() {
var abeForm = document.getElementById('abeForm')
var abeForm = document.querySelector('.abeform-wrapper')
if (abeForm == null) return

var inputs = [].slice.call(abeForm.querySelectorAll('input'))
Expand All @@ -53,6 +54,7 @@ export default class EditorSave {

Array.prototype.forEach.call(inputs, (input) => {
var dataId = input.getAttribute('data-id')
var maxlength = input.getAttribute('data-maxlength')
if(input.type === 'file') return
if(typeof dataId !== 'undefined' && dataId !== null) {
if(dataId.indexOf('[') > -1){
Expand All @@ -72,7 +74,7 @@ export default class EditorSave {
}else {
var value

if (input.nodeName === 'SELECT') {
if (input.nodeName === 'SELECT' && maxlength != "1") {
var checked = input.querySelectorAll('option:checked')
value = []
Array.prototype.forEach.call(checked, (check) => {
Expand Down Expand Up @@ -100,7 +102,8 @@ export default class EditorSave {
}else {
value = input.value.replace(/\"/g, '\&quot;') + ''
}
this._json.data[dataId] = value
setObjByString(this._json.data, dataId, value);
// this._json.data[dataId] = value
}
}
})
Expand All @@ -109,6 +112,8 @@ export default class EditorSave {
savePage(type) {
var target = document.querySelector(`[data-action="${type}"]`)
this.serializeForm()
console.log('this._json.data', this._json.data)
return
target.classList.add('loading')
target.setAttribute('disabled', 'disabled')
var url = target.getAttribute('data-url')
Expand Down
4 changes: 2 additions & 2 deletions src/server/public/abejs/scripts/modules/EditorUtils.js
Expand Up @@ -35,7 +35,7 @@ export default class EditorUtils {
if (visible === 'false' || visible === false) {
return
}
var id = target.getAttribute('data-id')
var id = target.getAttribute('data-id').replace(/\./g, '-')
var nodes = IframeNode('#page-template', '[data-abe-' + id + ']')

if(typeof nodes === 'undefined' || nodes === null || nodes.length === 0) {
Expand Down Expand Up @@ -77,7 +77,7 @@ export default class EditorUtils {
}

static getAttr(target) {
var id = target.getAttribute('data-id')
var id = target.getAttribute('data-id').replace(/\./g, '-')

return {
abe: 'data-abe-' + id.replace(/\[([0-9]*)\]/g, '$1'),
Expand Down
11 changes: 7 additions & 4 deletions src/server/public/abejs/scripts/modules/FormCreate.js
Expand Up @@ -4,6 +4,7 @@ import limax from 'limax'
import Nanoajax from 'nanoajax'
import qs from 'qs'
import FolderSelect from './FolderSelect'
import {setObjByString} from '../utils/jsonObject'

export default class FormCreate {
constructor(parentForm) {
Expand All @@ -20,7 +21,7 @@ export default class FormCreate {
this._formInputs = [].slice.call(this._form.querySelectorAll('input, select'))
this._precontribTemplate = [].slice.call(this._form.querySelectorAll('[data-precontrib-templates]'))

this._selectTemplate = this._form.querySelector('[data-id="selectTemplate"]')
this._selectTemplate = this._form.querySelector('[data-id="abe_meta.template"]')
this._showHideSelect(this._selectTemplate)
this._handleBtnSelectTemplate = this._btnSelectTemplate.bind(this)

Expand Down Expand Up @@ -126,7 +127,8 @@ export default class FormCreate {
if (resultValue.indexOf('{') > -1) {
try {
var jsonValue = JSON.parse(resultValue)
values[id].push(jsonValue)
setObjByString(values, id, jsonValue);
// values[id].push(jsonValue)
}catch(e) {
// values[id].push(value)
}
Expand All @@ -140,7 +142,7 @@ export default class FormCreate {
if (value.indexOf('{') > -1) {
try {
var jsonValue = JSON.parse(value)
values[id] = [jsonValue]
setObjByString(values, id, [jsonValue]);

if (required && values[id].length == 0) {
isValid = false
Expand All @@ -150,7 +152,8 @@ export default class FormCreate {
// values[id].push(value)
}
}else {
values[id] = value
setObjByString(values, id, value);
// values[id] = value
if (required && values[id] == '') {
isValid = false
if(showErrors) parentNode.classList.add('has-error')
Expand Down
25 changes: 25 additions & 0 deletions src/server/public/abejs/scripts/utils/jsonObject.js
@@ -0,0 +1,25 @@
export function setObjByString(obj, str, val) {
var keys, key;
//make sure str is a string with length
if (!str || !str.length || Object.prototype.toString.call(str) !== "[object String]") {
return false;
}
if (obj !== Object(obj)) {
//if it's not an object, make it one
obj = {};
}
keys = str.split(".");
while (keys.length > 1) {
key = keys.shift();
if (obj !== Object(obj)) {
//if it's not an object, make it one
obj = {};
}
if (!(key in obj)) {
//if obj doesn't contain the key, add it and set it to an empty object
obj[key] = {};
}
obj = obj[key];
}
return obj[keys[0]] = val;
}
4 changes: 4 additions & 0 deletions src/server/routes/operations/post/edit.js
Expand Up @@ -9,6 +9,10 @@ var route = function(req, res, next){
if(typeof res._header !== 'undefined' && res._header !== null) return

var operation = cmsData.regex.getWorkflowFromOperationsUrl(req.originalUrl)
console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * $*')
console.log('operation.postUrl, req.body.json, operation.workflow', operation.postUrl,
req.body.json,
operation.workflow)

var p = cmsOperations.post.submit(
operation.postUrl,
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/post-create.js
Expand Up @@ -11,7 +11,7 @@ var route = function(req, res, next) {
var postName = folderName.pop()
folderName = folderName.join('/')

var p = cmsOperations.create(req.body.selectTemplate, folderName, postName, req, req.body)
var p = cmsOperations.create(req.body.abe_meta.template, folderName, postName, req, req.body)

p.then((resSave) => {
var result = {
Expand Down
5 changes: 4 additions & 1 deletion src/server/routes/post-duplicate.js
Expand Up @@ -11,7 +11,10 @@ var route = function(req, res, next) {
var postName = folderName.pop()
folderName = folderName.join('/')

var p = cmsOperations.duplicate(req.body.oldFilePath, req.body.selectTemplate, folderName, postName, req)
var oldFilePath = req.body.oldFilePath
delete req.body.oldFilePath

var p = cmsOperations.duplicate(oldFilePath, req.body.abe_meta.template, folderName, postName, req)

p.then((resSave) => {
var result = {
Expand Down
5 changes: 4 additions & 1 deletion src/server/routes/post-update.js
Expand Up @@ -11,7 +11,10 @@ var route = function(req, res, next) {
var postName = folderName.pop()
folderName = folderName.join('/')

var p = cmsOperations.duplicate(req.body.oldFilePath, req.body.selectTemplate, folderName, postName, req, true)
var oldFilePath = req.body.oldFilePath
delete req.body.oldFilePath

var p = cmsOperations.duplicate(oldFilePath, req.body.abe_meta.template, folderName, postName, req, true)

p.then((resSave) => {
var result = {
Expand Down
2 changes: 1 addition & 1 deletion src/server/views/partials/create-form-template.html
@@ -1,6 +1,6 @@
<div class="form-group">
<label for="selectTemplate" class="control-label">{{@root.text.template}} *</label>
<select class="form-control" id="selectTemplate" data-id="selectTemplate" data-required="true" name="selectTemplate" data-type-template-abe="true">
<select class="form-control" id="selectTemplate" data-id="abe_meta.template" max-length="1" data-maxlength="1" data-required="true" name="selectTemplate" data-type-template-abe="true">
<option></option>
{{#each @root.manager.list.templates}}
{{#ifCond @root.json.abe_meta.template name}}
Expand Down

0 comments on commit 58ccfe3

Please sign in to comment.