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 20, 2016
1 parent a0a74eb commit 19d37ff
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 188 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.path}" class="file-path">
${file.path}
<a href="/abe/${file.template}?filePath=${file.fileUrl}" class="file-path">
${file.fileUrl}
</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.path}" class="label label-default label-draft">draft</a>`
workflow += `<a href="/abe/${file.template}?filePath=${file.fileUrl}" class="label label-default label-draft">draft</a>`
}else {
workflow += `<a href="/abe/${file.template}?filePath=${file.path}" class="hidden label label-default label-draft"></a>`
workflow += `<a href="/abe/${file.template}?filePath=${file.fileUrl}" 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.path}"
res += `<a href="/unpublish/?filePath=${file.fileUrl}"
title="${text.unpublish}"
class="icon" data-unpublish="true" data-text="${text.confirmUnpublish} ${file.path}">
class="icon" data-unpublish="true" data-text="${text.confirmUnpublish} ${file.fileUrl}">
<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.path}">
data-text="${text.confirmDelete} ${file.fileUrl}">
<span class="glyphicon glyphicon-trash"></span>
</a>`

Expand Down
111 changes: 111 additions & 0 deletions src/cli/helpers/abe-get-select-template-keys.js
@@ -0,0 +1,111 @@
import path from 'path'
import fse from 'fs-extra'
import {execFile} from 'child_process'
import {
fileUtils,
FileParser,
Util,
Sql,
cleanSlug,
getTemplate,
save,
config,
log,
Hooks,
removeDuplicateAttr,
Manager
} from '../../cli'

var findTemplates = function(templatesPath) {
var p = new Promise((resolve, reject) => {
execFile('find', [ templatesPath ], (err, stdout, stderr) => {
if (err) reject(err)

var file_list = stdout.split('\n')
var file_list_with_extention = []
Array.prototype.forEach.call(file_list, (file) => {
if (file.indexOf(config.files.templates.extension) > -1) {
file_list_with_extention.push(file)
}
})

resolve(file_list_with_extention)
});
})

return p
}

var findRequestKeys = function(file_list_with_extention) {
var whereKeysCheck = {}
var whereKeys = []
var p = new Promise((resolve, reject) => {
let util = new Util()
Array.prototype.forEach.call(file_list_with_extention, (file) => {
var template = fse.readFileSync(file, 'utf8')
var matches = util.dataRequest(template)

Array.prototype.forEach.call(matches, (match) => {
var obj = Util.getAllAttributes(match[0], {})
obj = Util.sanitizeSourceAttribute(obj, {})

var type = Sql.getSourceType(obj.sourceString)

switch (type) {
case 'request':
var request = Sql.handleSqlRequest(obj.sourceString, {})
Array.prototype.forEach.call(request.columns, (column) => {
if(typeof whereKeysCheck[column] === 'undefined' || whereKeysCheck[column] === null) {
whereKeysCheck[column] = true
whereKeys.push(column)
}
})
Array.prototype.forEach.call(request.where, (where) => {
if(typeof whereKeysCheck[where.left] === 'undefined' || whereKeysCheck[where.left] === null) {
whereKeysCheck[where.left] = true
whereKeys.push(where.left)
}
})
}
resolve(whereKeys)
})
})
})

return p
}

var getSelectTemplateKeys = function(templatesPath) {
var p = new Promise((resolve, reject) => {
findTemplates(templatesPath)
.then((file_list_with_extention) => {

findRequestKeys(file_list_with_extention)
.then((whereKeys) => {

resolve(whereKeys)
},
() => {
console.log('findRequestKeys reject')
reject()
})
.catch((e) => {
console.error('getSelectTemplateKeys', e)
reject()
})
},
() => {
console.log('findTemplates reject')
reject()
})
.catch((e) => {
console.error('getSelectTemplateKeys', e)
reject()
})

})

return p
}

export default getSelectTemplateKeys
12 changes: 12 additions & 0 deletions src/cli/helpers/abe-set-deep-value.js
@@ -0,0 +1,12 @@
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);
}

export default set_deep_value

0 comments on commit 19d37ff

Please sign in to comment.