Skip to content

Commit

Permalink
refactoring: Rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Jan 2, 2017
2 parents db7f261 + 965e58d commit 98fb9dc
Show file tree
Hide file tree
Showing 28 changed files with 432 additions and 138 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -21,6 +21,7 @@ abe-logs/*
# test-coverage
coverage/*
test/templates/hbs/*
tests/demo/data/autocomplete*

# dist
dist
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -15,7 +15,7 @@
"install-selenium": "node nightwatch.conf.js",
"pretest-func": "ROOT=$PWD/tests/demo ./dist/index.js serve -p 3003 &",
"test-func": "sleep 6 && node_modules/.bin/nightwatch --config nightwatch.conf.js",
"posttest-func": "killall node",
"posttest-func": "kill -9 $(lsof -ti tcp:3003)",
"start": "node --debug --harmony ./dist/server/index.js",
"babel": "babelify --presets [ es2015 ] src/server/public/abejs/scripts/template-engine.js -o src/server/public/abejs/scripts/template-engine-compiled.js",
"watch": "./node_modules/.bin/parallelshell './node_modules/.bin/watchify -v -t [ babelify --presets [ es2015 ] ] src/server/public/abejs/scripts/template-engine.js -o src/server/public/abejs/scripts/template-engine-compiled.js' './node_modules/.bin/watchify -v -t [ babelify --presets [ es2015 ] ] src/server/public/abejs/scripts/admin.js -o src/server/public/abejs/scripts/admin-compiled.js' './node_modules/.bin/watchify -v -t [ babelify --presets [ es2015 ] ] src/server/public/abejs/scripts/user-login.js -o src/server/public/abejs/scripts/user-login-compiled.js' 'npm run watch:sass'",
Expand Down
12 changes: 9 additions & 3 deletions src/cli/cms/data/attr.js
Expand Up @@ -37,12 +37,18 @@ export default class Attr {
* @return {String} str without attributes
*/
remove() {
return this.str.replace(new RegExp(fullAttr + this.getExtension() + ')'), '')
if (this.str != null) {
return this.str.replace(new RegExp(fullAttr + this.getExtension() + ')'), '')
}
return this.str
}

getExtension(){
var ext = this.str.split('.')
return ext[ext.length - 1]
if (this.str != null) {
var ext = this.str.split('.')
return ext[ext.length - 1]
}
return ''
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/cli/cms/editor/handlebars/compileAbe.js
Expand Up @@ -22,9 +22,11 @@ export default function compileAbe(){
key = arguments[0].hash['key'].split('-')
key = key[key.length - 1]
hash = arguments[0].hash
hash.key = hash.key.replace(/\{\{@index\}\}/, '[{{@index}}]')
// hash.key = hash.key.replace(/\{\{@index\}\}/, '[{{@index}}]')
hash.key = hash.key.replace(/\{\{@index\}\}/, `[${arguments[0].data.index}]`)
try{
value = content ? content[hash['dictionnary']][arguments[0].data.index][key] : hash.key
value = content ? eval(`content.${hash.dictionnary}[${arguments[0].data.index}].${key}`) : hash.key
// value = content ? content[hash['dictionnary']][arguments[0].data.index][key] : hash.key
}
catch(e){
console.log(e.stack)
Expand Down
13 changes: 12 additions & 1 deletion src/cli/cms/editor/handlebars/sourceAttr.js
Expand Up @@ -13,8 +13,17 @@ export default function sourceAttr(obj, params) {
var str = params.display
var selected = ''
var displayName = prepareDisplay(obj, str)
var values = params.value
if(Object.prototype.toString.call(params.value) !== '[object Array]') {
values = [params.value]
}

// if (params.key == "colors.single") {
// console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
// console.log('params', params)
// }

Array.prototype.forEach.call(params.value, (pValue) => {
Array.prototype.forEach.call(values, (pValue) => {
if (isSelected(pValue, displayName, str)) {
selected = 'selected'
}
Expand Down Expand Up @@ -53,9 +62,11 @@ export function prepareDisplay(obj, str = null) {
var keys = getKeys(str)
Array.prototype.forEach.call(keys, (key) => {
var val = get(obj, key)

var pattern = new RegExp('{{'+key+'}}|'+key, 'g')
str = str.replace(pattern, val)
})
// console.log('params.value', params.value)

if (str == null) {
str = obj
Expand Down
5 changes: 2 additions & 3 deletions src/cli/cms/operations/save.js
Expand Up @@ -10,9 +10,8 @@ import {
export function saveJson(url, json) {
mkdirp.sync(path.dirname(url))

if(typeof json.abe_source !== 'undefined' && json.abe_source !== null) {
delete json.abe_source
}
if(json.abe_source != null) delete json.abe_source
if(json.abeEditor != null) delete json.abeEditor

var eachRecursive = function (obj) {
for (var k in obj) {
Expand Down
17 changes: 8 additions & 9 deletions src/cli/cms/templates/prepare.js
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../'

export function addAbeAttrSingleTab(key, elem, htmlAttribute = null) {
var res = ""
var res = ''

var valueOfAttritube = key.replace(/\./g, '-')
key = cmsData.regex.validDataAbe(valueOfAttritube)
Expand All @@ -23,7 +23,7 @@ export function addAbeAttrSingleTab(key, elem, htmlAttribute = null) {
}

export function addAbeAttrForBlock(key, elem, htmlAttribute = null) {
var res = ""
var res = ''

var valueOfAttritube = key.split('.')
var parentKey = valueOfAttritube.shift()
Expand Down Expand Up @@ -61,12 +61,12 @@ export function addAbeDataAttrForHtmlTag(template) {
while (match = cmsData.regex.abePattern.exec(template)) {
key = cmsData.regex.getAttr(match, 'key')

if (cmsData.regex.isSingleAbe(match, newTemplate)) {
getattr = key.replace(/\./g, '-')
} else {
getattr = key.replace('.', '[index].')
if (!cmsData.regex.isSingleAbe(match, newTemplate)) {
key = key.replace('.', '{{@index}}.')
}

getattr = key.replace(/\./g, '-')

newTemplate = newTemplate.replace(
cmsData.regex.escapeTextToRegex(match[0], 'g'),
' data-abe-' + cmsData.regex.validDataAbe(getattr) + '="' + getattr + '" ' + match[0]
Expand Down Expand Up @@ -183,8 +183,7 @@ export function addAbeDataAttrForHtmlAttributes(template) {
* @param {[type]} json [description]
*/
export function addAbeSourceComment(template, json) {

// Don't know what it does...

if(typeof json.abe_source !== 'undefined' && json.abe_source !== null) {
var keys = Object.keys(json.abe_source)

Expand Down Expand Up @@ -302,7 +301,7 @@ export function indexEachBlocks(template, onlyHtml) {

var voidData = {}
voidData[key] = [{}]
var blockCompiled = Handlebars.compile(block.replace(/{{abe (.*?)["'] ?}}/g, '[[abe $1]]').replace(new RegExp(`\\.\\.\/${config.meta.name}`, 'g'), config.meta.name))
var blockCompiled = Handlebars.compile(block.replace(/{{abe (.*?["']) ?}}/g, '[[abe $1]]').replace(new RegExp(`\\.\\.\/${config.meta.name}`, 'g'), config.meta.name))
var blockHtml = blockCompiled(voidData, {data: {intl: config.intlData}}).replace(/\[\[abe (.*?)\]\]/g, '{{abe $1}}')

// je rajoute un data-abe-block avec index sur tous les tags html du bloc each
Expand Down
42 changes: 24 additions & 18 deletions src/server/controllers/editor.js
Expand Up @@ -11,24 +11,28 @@ import {

function add(obj, json, text, util) {
var value = obj.value

if(obj.key.indexOf('[') > -1) {
var key = obj.key.split('[')[0]
var index = obj.key.match(/[^\[]+?(?=\])/)[0]
var prop = obj.key.replace(/[^\.]+?\./, '')
key = getDataIdWithNoSlash(key)

if(typeof json[key] !== 'undefined' && json[key] !== null &&
typeof json[key][index] !== 'undefined' && json[key][index] !== null &&
typeof json[key][index][prop] !== 'undefined' && json[key][index][prop] !== null) {
obj.value = json[getDataIdWithNoSlash(key)][index][prop]
}else if(typeof value !== 'undefined' && value !== null && value !== '') {
if(typeof json[key] === 'undefined' || json[key] === null){
json[key] = []
}
if(typeof json[key][index] === 'undefined' || json[key][index] === null){
json[key][index] = {}
try {
obj.value = eval('json[key][index].' + prop)
} catch(e) {

try {
eval(`json[key][index].${prop} = ` + JSON.stringify(value))
}catch(e) {
// no value found inside json OKEY
}
json[key][index][prop] = value
}
}else {
try {
obj.value = eval(`json.${getDataIdWithNoSlash(obj.key)}`)
} catch(e) {
// no value found inside json OKEY
}
}

Expand Down Expand Up @@ -79,7 +83,8 @@ function addToForm(match, text, json, util, arrayBlock, keyArray = null, i = 0)
}catch(e) {
obj.value = null
}
json[getDataIdWithNoSlash(obj.key)] = add(obj, json, text, util)
// json[getDataIdWithNoSlash(obj.key)] =
add(obj, json, text, util)
}
}

Expand Down Expand Up @@ -109,7 +114,7 @@ function insertAbeEach (obj, text, json, util, arrayBlock) {

function each(text, json, util, arrayBlock) {
let pattEach = /(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g
let patt = /abe [^{{}}]+?(?=\}})/g
let patt = /{{(abe .*?["']) ?}}/g
var textEach, match

while (textEach = pattEach.exec(text)) {
Expand Down Expand Up @@ -153,6 +158,10 @@ function each(text, json, util, arrayBlock) {
}

function addSource(text, json, util) {
// removing each blocks potentially containing abe data type
let pattEach = /(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g
text = text.replace(pattEach, '')

var listReg = /({{abe.*type=[\'|\"]data.*}})/g
var match

Expand Down Expand Up @@ -246,8 +255,6 @@ export function editor(text, json, documentLink, precontrib = false) {
.then(() => {
addSource(text, json, util)

text = cmsData.source.removeDataList(text)

if (!precontrib) {
text = cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist(text)
text = cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist(text)
Expand All @@ -257,6 +264,7 @@ export function editor(text, json, documentLink, precontrib = false) {
arrayBlock = []
each(text, json, util, arrayBlock)

text = cmsData.source.removeDataList(text)
if(typeof json.abe_meta !== 'undefined' && json.abe_meta !== null) {
var links = json.abe_meta.link.split('/')
var link = links.pop()
Expand All @@ -271,14 +279,12 @@ export function editor(text, json, documentLink, precontrib = false) {

var blocks = orderBlock(util)


if (!precontrib) {
// HOOKS afterEditorFormBlocks
blocks = abeExtend.hooks.instance.trigger('afterEditorFormBlocks', blocks, json, text)
}

abeEngine.instance.content = json

resolve({
text: text,
form: blocks,
Expand Down
25 changes: 13 additions & 12 deletions src/server/public/abejs/scripts/modules/EditorAutocomplete.js
Expand Up @@ -24,10 +24,6 @@ export default class EditorAutocomplete {
this._handleSelectValue = this._selectValue.bind(this)
this._handleRefresh = this._refresh.bind(this)

this._autocompletesRemove = [].slice.call(document.querySelectorAll('[data-autocomplete-remove=true]'))
this._autocompletes = [].slice.call(document.querySelectorAll('[data-autocomplete=true]'))
this._autocompletesRefresh = [].slice.call(document.querySelectorAll('[data-autocomplete-refresh=true]'))

this._currentInput = null
this._divWrapper = document.createElement('div')
this._divWrapper.classList.add('autocomplete-wrapper')
Expand All @@ -38,6 +34,10 @@ export default class EditorAutocomplete {
}

rebind() {
this._autocompletesRemove = [].slice.call(document.querySelectorAll('[data-autocomplete-remove=true]'))
this._autocompletes = [].slice.call(document.querySelectorAll('[data-autocomplete=true]'))
this._autocompletesRefresh = [].slice.call(document.querySelectorAll('[data-autocomplete-refresh=true]'))

document.body.removeEventListener('mouseup', this._handleDocumentClick)
document.body.addEventListener('mouseup', this._handleDocumentClick)

Expand All @@ -63,8 +63,8 @@ export default class EditorAutocomplete {
}

_saveData() {
var id = this._currentInput.getAttribute('id')
var nodeComments = IframeCommentNode('#page-template', id)
var id = this._currentInput.getAttribute('data-id')
var nodeComments = IframeCommentNode('#page-template', id.replace(/\./g, '-'))
var maxLength = this._currentInput.getAttribute('data-maxlength')

if(typeof maxLength !== 'undefined' && maxLength !== null && maxLength !== '') {
Expand All @@ -81,21 +81,21 @@ export default class EditorAutocomplete {

var results = [].slice.call(this._currentInput.parentNode.querySelectorAll('.autocomplete-result-wrapper .autocomplete-result'))
var json = this._json.data

json[id] = []
var toSave = []
Array.prototype.forEach.call(results, (result) => {
var value = result.getAttribute('value')
if(value !== '') {
if(value.indexOf('{') > -1 || value.indexOf('[') > -1) {
json[id].push(JSON.parse(value))
toSave.push(JSON.parse(value))
}else {
json[id].push(value)
toSave.push(value)
}
}
})
eval(`json.${id} = ${JSON.stringify(toSave)}`)

this._json.data = json

if(typeof nodeComments !== 'undefined' && nodeComments !== null && nodeComments.length > 0) {

try {
Expand Down Expand Up @@ -475,7 +475,8 @@ export default class EditorAutocomplete {

_remove(e) {
var target = e.currentTarget.parentNode
this._currentInput = document.querySelector(`#${target.getAttribute('data-parent-id')}`)
var escapedSelector = target.getAttribute('data-parent-id').replace(/(:|\.|\[|\])/g,'\\$1')
this._currentInput = document.querySelector(`#${escapedSelector}`)
target.parentNode.removeChild(target)
this._saveData()
this._currentInput = null
Expand Down
7 changes: 7 additions & 0 deletions src/server/public/abejs/scripts/modules/EditorBlock.js
Expand Up @@ -232,6 +232,7 @@ export default class EditorBlock {
var blockHtml = unescape(blockContent.textContent.replace(/\[\[([\S\s]*?)\]\]/, ''))
.replace(new RegExp(`-${dataBlock}0`, 'g'), `-${dataBlock}${newNumber}`)
.replace(/\[0\]-/g, '' + newNumber + '-')
.replace(/({{abe.*type=[\'|\"]data.*}})/g, '')
var newBlock = document.createElement('abe')
newBlock.innerHTML = blockHtml

Expand Down Expand Up @@ -306,6 +307,12 @@ export default class EditorBlock {
prevListItem.parentNode.insertBefore(newBlock, next)
this._unValueForm(newBlock)

var autocompletes = [].slice.call(newBlock.querySelectorAll('.autocomplete-result-wrapper'))
if(typeof autocompletes !== 'undefined' && autocompletes !== null && autocompletes.length > 0) {
Array.prototype.forEach.call(autocompletes, (autocomplete) => {
autocomplete.innerHTML = ''
})
}
var richs = [].slice.call(newBlock.querySelectorAll('[contenteditable]'))
if(typeof richs !== 'undefined' && richs !== null && richs.length > 0) {
Array.prototype.forEach.call(richs, (rich) => {
Expand Down

0 comments on commit 98fb9dc

Please sign in to comment.