Skip to content

Commit

Permalink
refactor data select
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Sep 21, 2016
1 parent 2777c53 commit 1966811
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 108 deletions.
14 changes: 7 additions & 7 deletions src/cli/handlebars/abe/listPage.js
Expand Up @@ -10,8 +10,8 @@ export default function listPage(file, index, text) {
res += `<tr>`
res += `<td>${math(index, '+', 1)}</td>
<td>
<a href="/abe/${file.template}?filePath=${file.fileUrl}" class="file-path">
${file.fileUrl}
<a href="/abe/${file.template}?filePath=${file.abe_meta.link}" class="file-path">
${file.abe_meta.link}
</a>
</td>`

Expand All @@ -37,9 +37,9 @@ export default function listPage(file, index, text) {

workflow += `<td align="center" class="draft">`
if((typeof file.published !== undefined && file.published !== null && !file.published) || (file.published && file.draft && file.published.date < file.draft.date)) {
workflow += `<a href="/abe/${file.template}?filePath=${file.fileUrl}" class="label label-default label-draft">draft</a>`
workflow += `<a href="/abe/${file.template}?filePath=${file.abe_meta.link}" class="label label-default label-draft">draft</a>`
}else {
workflow += `<a href="/abe/${file.template}?filePath=${file.fileUrl}" class="hidden label label-default label-draft"></a>`
workflow += `<a href="/abe/${file.template}?filePath=${file.abe_meta.link}" class="hidden label label-default label-draft"></a>`
}

workflow += `</td>`
Expand All @@ -57,9 +57,9 @@ export default function listPage(file, index, text) {
<div class="row icons-action">`

if (this.published){
res += `<a href="/unpublish/?filePath=${file.fileUrl}"
res += `<a href="/unpublish/?filePath=${file.abe_meta.link}"
title="${text.unpublish}"
class="icon" data-unpublish="true" data-text="${text.confirmUnpublish} ${file.fileUrl}">
class="icon" data-unpublish="true" data-text="${text.confirmUnpublish} ${file.abe_meta.link}">
<span class="glyphicon glyphicon-eye-close"></span>
</a>`
}
Expand All @@ -68,7 +68,7 @@ export default function listPage(file, index, text) {
title="${text.delete}"
class="icon"
data-delete="true"
data-text="${text.confirmDelete} ${file.fileUrl}">
data-text="${text.confirmDelete} ${file.abe_meta.link}">
<span class="glyphicon glyphicon-trash"></span>
</a>`

Expand Down
20 changes: 12 additions & 8 deletions src/cli/helpers/abe-set-deep-value.js
@@ -1,12 +1,16 @@
var set_deep_value = function(obj, is, value) {
if (typeof is == 'string')
return set_deep_value(obj,is.split('.'), value);
else if (is.length==1 && value!==undefined)
return obj[is[0]] = value;
else if (is.length==0)
return obj;
else
return set_deep_value(obj[is[0]],is.slice(1), value);
if (typeof is == 'string'){
return set_deep_value(obj,is.split('.'), value);
}else if (is.length == 1 && value !== undefined){
return obj[is[0]] = value;
}else if (is.length==0){
return obj;
}else {
if(typeof obj[is[0]] === 'undefined' || obj[is[0]] === null) {
obj[is[0]] = {}
}
return set_deep_value(obj[is[0]], is.slice(1), value);
}
}

export default set_deep_value
46 changes: 24 additions & 22 deletions src/cli/helpers/abe-sql.js
Expand Up @@ -322,9 +322,9 @@ export default class Sql {
var fromDirectory = Sql.getFromDirectory(from, pathFromClause)

var list = Manager.instance.getList()
var files_array = list[0].files.filter((element, index, arr) => {
if(typeof element.published !== 'undefined' && element.published !== null) {
if (element.published.path.indexOf(fromDirectory) > -1) {
var files_array = list.filter((element, index, arr) => {
if(element.published) {
if (element.path.indexOf(fromDirectory) > -1) {
return true
}
}
Expand Down Expand Up @@ -446,26 +446,28 @@ export default class Sql {

for(let file of files) {
if(limit < maxLimit || maxLimit === -1) {
var doc = Sql.executeWhereClauseToFile(file.published, wheres, jsonPage)

if(doc) {
var json = JSON.parse(JSON.stringify(doc))
var jsonValues = {}

if(typeof columns !== 'undefined' && columns !== null && columns.length > 0 && columns[0] !== '*') {

Array.prototype.forEach.call(columns, (column) => {
if(typeof json[column] !== 'undefined' && json[column] !== null) {
jsonValues[column] = json[column]
}
})
jsonValues[config.meta.name] = json[config.meta.name]
}else {
jsonValues = json
}
if (file.published === true) {
var doc = Sql.executeWhereClauseToFile(file, wheres, jsonPage)

if(doc) {
var json = JSON.parse(JSON.stringify(doc))
var jsonValues = {}

if(typeof columns !== 'undefined' && columns !== null && columns.length > 0 && columns[0] !== '*') {

Array.prototype.forEach.call(columns, (column) => {
if(typeof json[column] !== 'undefined' && json[column] !== null) {
jsonValues[column] = json[column]
}
})
jsonValues[config.meta.name] = json[config.meta.name]
}else {
jsonValues = json
}

res.push(jsonValues)
limit++
res.push(jsonValues)
limit++
}
}
} else {
break
Expand Down
1 change: 0 additions & 1 deletion src/cli/helpers/abe-utils.js
Expand Up @@ -304,7 +304,6 @@ export default class Utils {
}

static getDataList(tplPath, text, jsonPage) {

var p = new Promise((resolve, reject) => {
var sourceAttr = config.source.name

Expand Down
21 changes: 9 additions & 12 deletions src/cli/helpers/file-parser.js
Expand Up @@ -17,6 +17,7 @@ import {
,Plugins
,Manager
,set_deep_value
,deep_value
} from '../'

export default class FileParser {
Expand All @@ -43,8 +44,6 @@ export default class FileParser {
}

static read(base, dirName, type, flatten, extensions = /(.*?)/, max = 99, current = 0, inversePattern = false) {
var website = config.root.split('/')
website = website[website.length - 1]
var arr = []
var level = fse.readdirSync(dirName)
var fileCurrentLevel = []
Expand Down Expand Up @@ -82,7 +81,6 @@ export default class FileParser {
var item = {
'name': level[i],
'path': path,
'website': website,
'cleanPathName': fileAttr.delete(path),
'cleanPath': path.replace(base + '/', ''),
date: date,
Expand All @@ -106,13 +104,13 @@ export default class FileParser {
if(!fileCurrentLevel.includes(level[i]) && match) {
if(isFolder) {
if(!flatten) {
var index = arr.push({'name': level[i], 'path': path, 'website': website, 'cleanPath': path.replace(base + '/', ''), 'folders': [], 'type': 'folder'}) - 1
var index = arr.push({'name': level[i], 'path': path, 'cleanPath': path.replace(base + '/', ''), 'folders': [], 'type': 'folder'}) - 1
if(current < max){
arr[index].folders = FileParser.read(base, path, type, flatten, extensions, max, current + 1, inversePattern)
}
}else {
if(type === 'folders' || type === null) {
arr.push({'name': level[i], 'path': path, 'website': website, 'cleanPath': path.replace(base + '/', ''), 'type': 'folder'})
arr.push({'name': level[i], 'path': path, 'cleanPath': path.replace(base + '/', ''), 'type': 'folder'})
}
if(current < max){
Array.prototype.forEach.call(FileParser.read(base, path, type, flatten, extensions, max, current + 1, inversePattern), (files) => {
Expand Down Expand Up @@ -409,20 +407,19 @@ export default class FileParser {
Array.prototype.forEach.call(withKeys, (key) => {
// console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
// console.log(key, key.split('.').reduce((o,i)=>o[i], json))
var keys = key.split('.')
var firstKey = keys[0]
if(typeof json !== 'undefined' && json !== null
&& typeof json[key.split('.')[0]] !== 'undefined' && json[key.split('.')[0]] !== null) {
set_deep_value(cleanFile, key, key.split('.').reduce((o,i)=>o[i], json))
&& typeof json[firstKey] !== 'undefined' && json[firstKey] !== null) {
set_deep_value(cleanFile, key, deep_value(json, key))
}
// if(typeof json[key] !== 'undefined' && json[key] !== null) {
// cleanFile[key] = json[key]
// }
})
filesArr.push(cleanFile)
})
var merged = fileUtils.getFilesMerged(filesArr)

site.files = Hooks.instance.trigger('afterGetAllFiles', merged)
return [site]
Hooks.instance.trigger('afterGetAllFiles', merged)
return merged
}

// static getAllFiles(withKeys) {
Expand Down
143 changes: 90 additions & 53 deletions src/cli/helpers/file-utils.js
Expand Up @@ -219,43 +219,43 @@ export default class FileUtils {
}

/* TODO: put this method in its right helper */
static checkMergedFile(file, merged) {

var cleanFilePath = file.cleanFilePath
var revision = file
revision.status = file.status
revision.filePath = file.filePath
revision.date = file.abe_meta && file.abe_meta.latest ? file.abe_meta.latest.date : ''
revision.template = file[config.meta.name].template ? file[config.meta.name].template.replace(/^\/+/, '') : ''
revision.cleanFilePath = cleanFilePath

revision[config.meta.name] = file[config.meta.name]

if(typeof merged[cleanFilePath] === 'undefined' || merged[cleanFilePath] === null) {
merged[cleanFilePath] = {}

merged[cleanFilePath].cleanFilePath = revision.cleanFilePath
merged[cleanFilePath].date = revision.date
merged[cleanFilePath].template = revision.template
merged[cleanFilePath][config.meta.name] = revision[config.meta.name]
merged[cleanFilePath][file.status] = revision
}else {
var oldDate = new Date(merged[cleanFilePath].date)
var newDate = new Date(revision.date)
var oldStatus = ''
if(typeof merged[cleanFilePath][revision.status] !== 'undefined' && merged[cleanFilePath][revision.status] !== null) {
oldStatus = merged[cleanFilePath][revision.status].status
}
var newStatus = revision.status

// if draft > publish
if(typeof merged[cleanFilePath][newStatus] === 'undefined' || merged[cleanFilePath][newStatus] === null) {
merged[cleanFilePath][newStatus] = revision
}else if(newDate > oldDate && oldStatus === newStatus) {
merged[cleanFilePath][file.status] = revision
}
}
}
// static checkMergedFile(file, merged) {

// var cleanFilePath = file.cleanFilePath
// var revision = file
// revision.status = file.status
// revision.filePath = file.filePath
// revision.date = file.abe_meta && file.abe_meta.latest ? file.abe_meta.latest.date : ''
// revision.template = file[config.meta.name].template ? file[config.meta.name].template.replace(/^\/+/, '') : ''
// revision.cleanFilePath = cleanFilePath

// revision[config.meta.name] = file[config.meta.name]

// if(typeof merged[cleanFilePath] === 'undefined' || merged[cleanFilePath] === null) {
// merged[cleanFilePath] = {}

// merged[cleanFilePath].cleanFilePath = revision.cleanFilePath
// merged[cleanFilePath].date = revision.date
// merged[cleanFilePath].template = revision.template
// merged[cleanFilePath][config.meta.name] = revision[config.meta.name]
// merged[cleanFilePath][file.status] = revision
// }else {
// var oldDate = new Date(merged[cleanFilePath].date)
// var newDate = new Date(revision.date)
// var oldStatus = ''
// if(typeof merged[cleanFilePath][revision.status] !== 'undefined' && merged[cleanFilePath][revision.status] !== null) {
// oldStatus = merged[cleanFilePath][revision.status].status
// }
// var newStatus = revision.status

// // if draft > publish
// if(typeof merged[cleanFilePath][newStatus] === 'undefined' || merged[cleanFilePath][newStatus] === null) {
// merged[cleanFilePath][newStatus] = revision
// }else if(newDate > oldDate && oldStatus === newStatus) {
// merged[cleanFilePath][file.status] = revision
// }
// }
// }

/* TODO: put this method in its right helper */
// static mergeFiles(files1, files2) {
Expand Down Expand Up @@ -297,26 +297,63 @@ export default class FileUtils {
var arMerged = []

Array.prototype.forEach.call(files, (file) => {
FileUtils.checkMergedFile(file, merged)
var cleanFilePath = file.cleanFilePath
var revision = file
revision.status = file.status
revision.filePath = file.filePath
revision.date = file.abe_meta && file.abe_meta.latest ? file.abe_meta.latest.date : ''
revision.template = file[config.meta.name].template ? file[config.meta.name].template.replace(/^\/+/, '') : ''
revision.cleanFilePath = cleanFilePath

revision[config.meta.name] = file[config.meta.name]

if(typeof merged[cleanFilePath] === 'undefined' || merged[cleanFilePath] === null) {
merged[cleanFilePath] = file
// merged[cleanFilePath] = {}

// merged[cleanFilePath].cleanFilePath = revision.cleanFilePath
// merged[cleanFilePath].date = revision.date
// merged[cleanFilePath].template = revision.template
// merged[cleanFilePath][config.meta.name] = revision[config.meta.name]
merged[cleanFilePath][file.status] = true
}else {
var oldDate = new Date(merged[cleanFilePath].date)
var newDate = new Date(file.date)
var oldStatus = ''
if(merged[cleanFilePath][file.status]) {
oldStatus = file.status
}
// var newStatus = revision.status

// if draft > publish
if(typeof merged[cleanFilePath][file.status] === 'undefined' || merged[cleanFilePath][file.status] === null) {
merged[cleanFilePath][file.status] = true
}else if(newDate > oldDate && oldStatus === file.status) {
merged[cleanFilePath][file.status] = true
}
}
})

// return merged
Array.prototype.forEach.call(Object.keys(merged), (key) => {
var merge = merged[key]
var publishedDate = (typeof merge.published !== 'undefined' && merge.published !== null) ? new Date(merge.published.date) : null
var draftDate = (typeof merge.draft !== 'undefined' && merge.draft !== null) ? new Date(merge.draft.date) : null

if(publishedDate !== null && draftDate !== null && publishedDate >= draftDate) {
merge.draft = null
}
var revision = {
fileUrl: path.join(merge.cleanFilePath.replace(/\.json/, `.${config.files.templates.extension}`)),
path: merge.cleanFilePath,
template: merge.template,
published: merge.published,
date: merge.date,
draft: merge.draft
}
arMerged.push(revision)
arMerged.push(merge)

// var publishedDate = (typeof merge.published !== 'undefined' && merge.published !== null) ? new Date(merge.published.date) : null
// var draftDate = (typeof merge.draft !== 'undefined' && merge.draft !== null) ? new Date(merge.draft.date) : null

// if(publishedDate !== null && draftDate !== null && publishedDate >= draftDate) {
// merge.draft = null
// }
// var revision = {
// fileUrl: path.join(merge.cleanFilePath.replace(/\.json/, `.${config.files.templates.extension}`)),
// path: merge.cleanFilePath,
// template: merge.template,
// published: merge.published,
// date: merge.date,
// draft: merge.draft
// }
// arMerged.push(revision)
})

return arMerged
Expand Down
4 changes: 2 additions & 2 deletions src/cli/models/Manager.js
Expand Up @@ -50,11 +50,11 @@ class Manager {

updateList() {
this._list = FileParser.getAllFilesWithMeta(this._whereKeys)
// console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
// console.log('this._list[0]', this._list[0])

// this._list = FileParser.getAllFiles(useKeys)
this._list.sort(FileParser.predicatBy('date'))
// console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
// console.log('this._list[0].files', this._list[0].files)

return this
}
Expand Down
4 changes: 1 addition & 3 deletions src/server/views/partials/right-file-list.html
Expand Up @@ -28,12 +28,10 @@
<th align="center">actions</th>
</tr>
</thead>
{{#each manager.home.files}}
<tbody>
{{#each this.files}}
{{#each manager.home.files}}
{{listPage this @index @root.text}}
{{/each}}
{{/each}}
</tbody>
</table>
<br />
Expand Down

0 comments on commit 1966811

Please sign in to comment.