Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/AdFabConnect/abejs into r…
Browse files Browse the repository at this point in the history
…eference-editor

# Conflicts:
#	src/cli/core/manager/Manager.js
  • Loading branch information
wonknu committed Oct 31, 2016
2 parents 7482d1c + 429a0d5 commit 2d0a061
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 65 deletions.
23 changes: 18 additions & 5 deletions src/cli/cms/data/file.js
Expand Up @@ -11,12 +11,19 @@ import {
} from '../../'

export function getAbeMeta(fileObject, json) {
const fileStatusIsPublish = cmsData.fileAttr.get(fileObject.cleanPath)

if(json.abe_meta.latest.date != null) {
fileObject.date = json.abe_meta.latest.date
fileObject.cleanDate = moment(json.abe_meta.latest.date).format('YYYY/MM/DD HH:MM:ss')
}

if(json.abe_meta != null) {
if (json.abe_meta.status === 'publish') {
fileObject.htmlPath = path.join(config.root, config.publish.url, path.join('/', fileObject.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))
}else {
fileObject.htmlPath = path.join(config.root, config.draft.url, path.join('/', fileObject.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))
}
var date = null
if (json.abe_meta.latest.date !== null) {
date = json.abe_meta.latest.date
Expand All @@ -28,9 +35,12 @@ export function getAbeMeta(fileObject, json) {
type: (json.abe_meta.type != null) ? json.abe_meta.type : null,
link: (json.abe_meta.link != null) ? json.abe_meta.link : null,
template: (json.abe_meta.template != null) ? json.abe_meta.template : null,
status: (json.abe_meta.status != null) ? json.abe_meta.status : null,
cleanName: (json.abe_meta.cleanName != null) ? json.abe_meta.cleanName : null,
cleanFilename: (json.abe_meta.cleanFilename != null) ? json.abe_meta.cleanFilename : null
cleanFilename: (json.abe_meta.cleanFilename != null) ? json.abe_meta.cleanFilename : null,
status: (json.abe_meta.status != null) ? json.abe_meta.status : null
}
if(fileStatusIsPublish.s != null && json.abe_meta.status === 'publish') {
fileObject.abe_meta.status = 'draft'
}
}

Expand All @@ -45,9 +55,8 @@ export function getAllWithKeys(withKeys) {
let filesArr = []

Array.prototype.forEach.call(files, (pathFile) => {
let fileObject = cmsData.file.getFileObject(pathFile)
const json = cmsData.file.get(pathFile)

let fileObject = cmsData.file.getFileObject(pathFile)
fileObject = cmsData.file.getAbeMeta(fileObject, json)

Array.prototype.forEach.call(withKeys, (key) => {
Expand Down Expand Up @@ -163,6 +172,7 @@ export function getFilesByType(pathFile, type = null) {
export function getFileObject(pathFile) {
const pathData = path.join(config.root, config.data.url)
const extension = '.json'
const templateExtension = '.' + config.files.templates.extension

const name = path.basename(pathFile)
const relativePath = pathFile.replace(pathData + '/', '')
Expand All @@ -182,6 +192,8 @@ export function getFileObject(pathFile) {

const fileDate = moment(date)
const duration = moment.duration(moment(fileDate).diff(new Date())).humanize(true)

const htmlUrl = path.join('/', relativePath.replace(extension, templateExtension))

let fileObject = {
'name': name,
Expand All @@ -192,7 +204,8 @@ export function getFileObject(pathFile) {
'cleanDate': fileDate.format('YYYY/MM/DD HH:MM:ss'),
'duration': duration,
'cleanName': parentName,
'parentRelativePath': parentRelativePath
'parentRelativePath': parentRelativePath,
'html': htmlUrl
}

return fileObject
Expand Down
38 changes: 18 additions & 20 deletions src/cli/cms/data/revision.js
Expand Up @@ -103,6 +103,7 @@ export function getDocumentRevision(docPath) {
result = revisions[0]
}
}

return result
}

Expand Down Expand Up @@ -135,24 +136,11 @@ export function filePathInfos(pathFolder) {
}
}

export function getFilesMerged(files) {
var merged = {}
var arMerged = []
export function mergeRevisions(files) {
let merged = {}

Array.prototype.forEach.call(files, (file) => {
var parentRelativePath = file.parentRelativePath

var fileStatusIsPublish = cmsData.fileAttr.get(file.cleanPath)
if(fileStatusIsPublish.s != null && file.abe_meta.status === 'publish') {
file.abe_meta.status = 'draft'
}

file.html = path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`))
if (file.abe_meta.status === 'publish') {
file.htmlPath = path.join(config.root, config.publish.url, path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))
}else {
file.htmlPath = path.join(config.root, config.draft.url, path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))
}
const parentRelativePath = file.parentRelativePath

if(merged[parentRelativePath] == null) {
merged[parentRelativePath] = {
Expand All @@ -167,11 +155,15 @@ export function getFilesMerged(files) {
revisions: []
}
}

merged[parentRelativePath].revisions.push(JSON.parse(JSON.stringify(file)))
})

// return merged
return merged
}

export function sortRevisions(merged) {
let sortedArray = []

Array.prototype.forEach.call(Object.keys(merged), (key) => {
var revisions = merged[key].revisions

Expand Down Expand Up @@ -203,8 +195,14 @@ export function getFilesMerged(files) {
merged[key].duration = revisions[0].duration
merged[key].abe_meta = revisions[0].abe_meta

arMerged.push(merged[key])
sortedArray.push(merged[key])
})

return arMerged
return sortedArray
}

export function getFilesMerged(files) {
const revisions = cmsData.revision.mergeRevisions(files)

return cmsData.revision.sortRevisions(revisions)
}
21 changes: 10 additions & 11 deletions src/cli/cms/operations/create.js
Expand Up @@ -13,11 +13,10 @@ var create = function(template, pathCreate, name, req, forceJson = {}, duplicate
var p = new Promise((resolve, reject) => {
abeExtend.hooks.instance.trigger('beforeCreate', template, pathCreate, name, req, forceJson)

var templatePath = path.join(template.replace(config.root, ''), config.templates.url, `${template}.${config.files.templates.extension}`)
var templatePath = path.join(config.root, config.templates.url, `${template}.${config.files.templates.extension}`)
var filePath = path.join(pathCreate, name)
filePath = coreUtils.slug.clean(filePath)
filePath = path.join(config.root, config.draft.url, filePath.replace(config.root))

if(templatePath !== null && filePath !== null) {
var tplUrl = cmsData.file.fromUrl(filePath)

Expand All @@ -36,15 +35,15 @@ var create = function(template, pathCreate, name, req, forceJson = {}, duplicate

abeExtend.hooks.instance.trigger('afterCreate', json, text, pathCreate, name, req, forceJson)
cmsOperations.save.save(filePath, template, json, text, 'draft', null, 'draft')
.then((resSave) => {
Manager.instance.updateList()
filePath = resSave.htmlPath
tplUrl = cmsData.file.fromUrl(filePath)
resolve(resSave.json)
}).catch(function(e) {
reject()
console.error(e)
})
.then((resSave) => {
Manager.instance.addPostInList(resSave.jsonPath)
filePath = resSave.htmlPath
tplUrl = cmsData.file.fromUrl(filePath)
resolve(resSave.json)
}).catch(function(e) {
reject()
console.error(e)
})
}else {
json = cmsData.file.get(tplUrl.json.path)
resolve(json, tplUrl.json.path)
Expand Down
10 changes: 5 additions & 5 deletions src/cli/cms/operations/post.js
Expand Up @@ -27,7 +27,7 @@ export function draft(filePath, tplPath, json, workflow = 'draft', type = 'draft
}else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){
result = resSave
}else if(typeof resSave.json !== 'undefined' && resSave.json !== null){
Manager.instance.updateList()
Manager.instance.updatePostInList(resSave.jsonPath)
result = {
success: 1,
json: resSave.json
Expand Down Expand Up @@ -84,7 +84,7 @@ export function publish(filePath, tplPath, json) {
}
}
abeExtend.hooks.instance.trigger('afterPublish', result)
Manager.instance.updateList()
Manager.instance.updatePostInList(resSave.jsonPath)
resolve(result)
}).catch(function(e) {
console.error('post.js', e)
Expand Down Expand Up @@ -128,10 +128,10 @@ export function unpublish(filePath) {
null,
'reject'
)
.then(() => {
.then((resSave) => {
cmsOperations.remove.removeFile(tplUrl.publish.path, tplUrl.publish.json)
abeExtend.hooks.instance.trigger('afterUnpublish', tplUrl.publish.path, tplUrl.publish.json)
Manager.instance.updateList()
Manager.instance.updatePostInList(resSave.jsonPath)
})
}
}
Expand Down Expand Up @@ -181,7 +181,7 @@ export function reject(filePath, tplPath, json) {
}
}
abeExtend.hooks.instance.trigger('afterReject', result)
Manager.instance.updateList()
Manager.instance.updatePostInList(resSave.jsonPath)
resolve(result)
})
}).catch(function(e) {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cms/operations/remove.js
Expand Up @@ -21,7 +21,7 @@ export function remove(filePath) {
cmsOperations.remove.removeFile(revision.path, revision.htmlPath)
})

Manager.instance.updateList()
Manager.instance.removePostInList(filePath)
}

export function removeFile(file, json) {
Expand Down
49 changes: 49 additions & 0 deletions src/cli/core/manager/Manager.js
Expand Up @@ -36,6 +36,7 @@ class Manager {
this._pathTemplate = path.join(config.root, config.templates.url)
this._pathStructure = path.join(config.root, config.structure.url)
this._pathReference = path.join(config.root, config.reference.url)
this._pathData = path.join(config.root, config.data.url)
this._watchersStart()

this.updateStructureAndTemplates()
Expand Down Expand Up @@ -195,6 +196,54 @@ class Manager {
return this
}

addPostInList(pathFile){
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(config.root, '').replace(/^\/?.+?\//, '')
const json = cmsData.file.get(pathFile)
let merged = {}
let rev = []
let revision = cmsData.file.getFileObject(pathFile)

revision = cmsData.file.getAbeMeta(revision, json)
Array.prototype.forEach.call(this._whereKeys, (key) => {
var keyFirst = key.split('.')[0]
revision[keyFirst] = json[keyFirst]
})
rev.push(revision)
merged = cmsData.revision.mergeRevisions(rev)
const sortedResult = cmsData.revision.sortRevisions(merged)
this._list.push(sortedResult[0])
this._list.sort(coreUtils.sort.predicatBy('date', -1))
}

updatePostInList(pathFile){
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(config.root, '').replace(/^\/?.+?\//, '')
const found = coreUtils.array.find(this._list, 'parentRelativePath', parentRelativePath)
if(found.length > 0){
const index = found[0]
const json = cmsData.file.get(pathFile)
let merged = {}
merged[parentRelativePath] = this._list[index]
let revision = cmsData.file.getFileObject(pathFile)
revision = cmsData.file.getAbeMeta(revision, json)
Array.prototype.forEach.call(this._whereKeys, (key) => {
var keyFirst = key.split('.')[0]
revision[keyFirst] = json[keyFirst]
})
merged[parentRelativePath].revisions.push(JSON.parse(JSON.stringify(revision)))
const sortedResult = cmsData.revision.sortRevisions(merged)
this._list[index] = sortedResult[0]
this._list.sort(coreUtils.sort.predicatBy('date', -1))
}
}

removePostInList(pathFile){
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(config.root, '').replace(/^\/?.+?\//, '')
const found = coreUtils.array.find(this._list, 'parentRelativePath', parentRelativePath)
if(found.length > 0){
console.log('ok found')
}
}

updateList() {
this._list = cmsData.file.getAllWithKeys(this._whereKeys)
this._list.sort(coreUtils.sort.predicatBy('date', -1))
Expand Down
43 changes: 43 additions & 0 deletions src/cli/core/utils/array.js
@@ -0,0 +1,43 @@
/**
* Highly efficient filter on a value in an object
* @param {Array} arr the array of objects
* @param {string} key the key to filter on
* @param {mixed} value the value to compare
* @return {Array} the filtered array
*/
export function filter(arr, key, value) {
var result = []
var i = 0
var len = arr.length

for (; i < len; i += 1) {
var elt = arr[i]

if (elt[key] == value) {
result.push(element)
}
}
return result
}

/**
* Highly efficient find indexes on a value in an property of an object
* @param {Array} arr the array of objects
* @param {string} key the key to filter on
* @param {mixed} value the value to compare
* @return {Array} the filtered array of indexes
*/
export function find(arr, key, value) {
var result = []
var i = 0
var len = arr.length

for (; i < len; i += 1) {
var elt = arr[i]

if (elt[key] == value) {
result.push(i)
}
}
return result
}
12 changes: 7 additions & 5 deletions src/cli/core/utils/index.js
@@ -1,13 +1,15 @@
import * as array from './array'
import * as sort from './sort'
import * as text from './text'
import locales from './locales'
import * as slug from './slug'
import * as file from './file'

export {
sort
,text
,locales
,slug
,file
array,
sort,
text,
locales,
slug,
file
}
13 changes: 0 additions & 13 deletions src/cli/core/utils/object.json

This file was deleted.

6 changes: 3 additions & 3 deletions src/server/views/partials/create-form-template.html
Expand Up @@ -5,12 +5,12 @@
<option></option>
{{#each manager.list.templates}}
{{#if isHome}}
<option value="{{path}}" clean-value="{{website}}">{{name}}</option>
<option value="{{name}}" clean-value="{{website}}">{{name}}</option>
{{else}}
{{#ifCond @root.json.abe_meta.template name}}
<option value="{{path}}" clean-value="{{website}}" selected="selected">{{name}}</option>
<option value="{{name}}" clean-value="{{website}}" selected="selected">{{name}}</option>
{{else}}
<option value="{{path}}" clean-value="{{website}}">{{name}}</option>
<option value="{{name}}" clean-value="{{website}}">{{name}}</option>
{{/ifCond}}
{{/if}}
{{/each}}
Expand Down

0 comments on commit 2d0a061

Please sign in to comment.