Skip to content

Commit

Permalink
build refactor request
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Sep 22, 2016
1 parent c44d23e commit d68a2ac
Show file tree
Hide file tree
Showing 15 changed files with 695 additions and 745 deletions.
16 changes: 8 additions & 8 deletions dist/cli/handlebars/abe/listPage.js
Expand Up @@ -29,10 +29,10 @@ function listPage(file, index, text) {
file = _.Hooks.instance.trigger('beforeListPage', file, index, text);

res += '<tr>';
res += '<td>' + (0, _math2.default)(index, '+', 1) + '</td>\n <td>\n <a href="/abe/' + file.template + '?filePath=' + file.fileUrl + '" class="file-path">\n ' + file.fileUrl + '\n </a>\n </td>';
res += '<td>' + (0, _math2.default)(index, '+', 1) + '</td>\n <td>\n <a href="/abe/' + file.abe_meta.template + '?filePath=' + file.abe_meta.link + '" class="file-path">\n ' + file.abe_meta.link + '\n </a>\n </td>';

if (file.template) {
res += '<td align="center">\n ' + file.template + '\n </td>';
if (file.abe_meta.template) {
res += '<td align="center">\n ' + file.abe_meta.template + '\n </td>';
} else {
res += '<td align="center"></td>';
}
Expand All @@ -49,16 +49,16 @@ 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.abe_meta.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.abe_meta.template + '?filePath=' + file.abe_meta.link + '" class="hidden label label-default label-draft"></a>';
}

workflow += '</td>';
workflow += '<td align="center" class="publish">';

if (file.published) {
workflow += '<a href="/abe/' + file.template + '?filePath=' + file.published.filePath + '" class="checkmark label-published">&#10004;</a>';
workflow += '<a href="/abe/' + file.abe_meta.template + '?filePath=' + file.published.filePath + '" class="checkmark label-published">&#10004;</a>';
}
workflow += '</td>';

Expand All @@ -68,10 +68,10 @@ function listPage(file, index, text) {
res += '<td align="center">\n <div class="row icons-action">';

if (this.published) {
res += '<a href="/unpublish/?filePath=' + file.fileUrl + '"\n title="' + text.unpublish + '"\n class="icon" data-unpublish="true" data-text="' + text.confirmUnpublish + ' ' + file.fileUrl + '">\n <span class="glyphicon glyphicon-eye-close"></span>\n </a>';
res += '<a href="/unpublish/?filePath=' + file.abe_meta.link + '"\n title="' + text.unpublish + '"\n class="icon" data-unpublish="true" data-text="' + text.confirmUnpublish + ' ' + file.abe_meta.link + '">\n <span class="glyphicon glyphicon-eye-close"></span>\n </a>';
}

res += '<a href="/delete/?filePath=' + this.path + '"\n title="' + text.delete + '"\n class="icon"\n data-delete="true"\n data-text="' + text.confirmDelete + ' ' + file.fileUrl + '">\n <span class="glyphicon glyphicon-trash"></span>\n </a>';
res += '<a href="/delete/?filePath=' + this.path + '"\n title="' + text.delete + '"\n class="icon"\n data-delete="true"\n data-text="' + text.confirmDelete + ' ' + file.abe_meta.link + '">\n <span class="glyphicon glyphicon-trash"></span>\n </a>';

res += '\n </div>\n </td>\n </tr>';

Expand Down
31 changes: 20 additions & 11 deletions dist/cli/handlebars/abe/sourceAttr.js
Expand Up @@ -17,27 +17,36 @@ function sourceAttr(val, params) {
if ((typeof hiddenVal === 'undefined' ? 'undefined' : _typeof(hiddenVal)) === 'object' && Object.prototype.toString.call(hiddenVal) === '[object Object]') {
hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, "&apos;");

var displayVal = _.Sql.deep_value_array(val, params.display);
if (typeof params.display !== 'undefined' && params.display !== null && typeof displayVal !== 'undefined' && displayVal !== null) {
val = displayVal;
} else {
try {
var displayVal = eval('val.' + params.display);
if (typeof params.display !== 'undefined' && params.display !== null && typeof displayVal !== 'undefined' && displayVal !== null) {
val = displayVal;
} else {
val = val[Object.keys(val)[0]];
}
} catch (e) {
val = val[Object.keys(val)[0]];
}
}

if (_typeof(params.value) === 'object' && Object.prototype.toString.call(params.value) === '[object Array]') {
Array.prototype.forEach.call(params.value, function (v) {
var item = v;
var displayV = _.Sql.deep_value_array(item, params.display);
if (typeof params.display !== 'undefined' && params.display !== null && typeof displayV !== 'undefined' && displayV !== null) {
item = displayV;
} else {
if (typeof v === 'string') {
item = v;
try {
var displayV = eval('item.' + params.display);
if (typeof params.display !== 'undefined' && params.display !== null && typeof displayV !== 'undefined' && displayV !== null) {
item = displayV;
} else {
item = v[Object.keys(v)[0]];
if (typeof v === 'string') {
item = v;
} else {
item = v[Object.keys(v)[0]];
}
}
} catch (e) {
item = v[Object.keys(v)[0]];
}

if ((typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && Object.prototype.toString.call(val) === '[object Array]' && (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' && Object.prototype.toString.call(item) === '[object Array]') {

Array.prototype.forEach.call(item, function (i) {
Expand Down
52 changes: 34 additions & 18 deletions dist/cli/helpers/abe-get-select-template-keys.js
Expand Up @@ -18,32 +18,48 @@ var _cli = require('../../cli');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var traverseFileSystem = function traverseFileSystem(currentPath, arr) {
var res = [];
var files = _fsExtra2.default.readdirSync(currentPath);
for (var i in files) {
var currentFile = currentPath + '/' + files[i];
var stats = _fsExtra2.default.statSync(currentFile);
if (stats.isFile()) {
if (currentFile.indexOf(_cli.config.files.templates.extension) > -1) {
res.push(currentFile);
}
} else if (stats.isDirectory()) {
res = res.concat(traverseFileSystem(currentFile));
}
}
return res;
};

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

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

resolve(file_list_with_extention);
});
var templatesList = traverseFileSystem(templatesPath);
resolve(templatesList);
});

return p;
};

var findRequestKeys = function findRequestKeys(file_list_with_extention) {
/**
* Get columns and where.left ids of a select statement
*
* select title, image from ../ where template=""
*
* return [title, image, template]
*
* @param {Array} templatesList ["article.html", "other.html"]
* @return {Promise}
*/
var findRequestColumns = function findRequestColumns(templatesList) {
var whereKeysCheck = {};
var whereKeys = [];
var p = new Promise(function (resolve, reject) {
var util = new _cli.Util();
Array.prototype.forEach.call(file_list_with_extention, function (file) {
Array.prototype.forEach.call(templatesList, function (file) {
var template = _fsExtra2.default.readFileSync(file, 'utf8');
var matches = util.dataRequest(template);

Expand Down Expand Up @@ -79,13 +95,13 @@ var findRequestKeys = function findRequestKeys(file_list_with_extention) {

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

findRequestKeys(file_list_with_extention).then(function (whereKeys) {
findRequestColumns(templatesList).then(function (whereKeys) {

resolve(whereKeys);
}, function () {
console.log('findRequestKeys reject');
console.log('findRequestColumns reject');
reject();
}).catch(function (e) {
console.error('getSelectTemplateKeys', e);
Expand Down
144 changes: 51 additions & 93 deletions dist/cli/helpers/abe-sql.js
Expand Up @@ -109,12 +109,15 @@ var Sql = function () {
var toReplace = matchFromExec[1];
while (fromMatch = matchVariable.exec(toReplace)) {
if (typeof fromMatch !== 'undefined' && fromMatch !== null && typeof fromMatch[1] !== 'undefined' && fromMatch[1] !== null) {
var value = Sql.deep_value_array(jsonPage, fromMatch[1]);
if (typeof value !== 'undefined' && value !== null) {
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', value);
} else {
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', '');
}

try {
var value = eval('jsonPage.' + fromMatch[1]);
if (typeof value !== 'undefined' && value !== null) {
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', value);
} else {
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', '');
}
} catch (e) {}
}
}

Expand Down Expand Up @@ -358,10 +361,12 @@ var Sql = function () {

var fromDirectory = Sql.getFromDirectory(from, pathFromClause);

var dateStart = new Date();

var list = _.Manager.instance.getList();
var files_array = list[0].files.filter(function (element, index, arr) {
if (typeof element.published !== 'undefined' && element.published !== null) {
if (element.published.path.indexOf(fromDirectory) > -1) {
var files_array = list.filter(function (element, index, arr) {
if (element.published) {
if (element.path.indexOf(fromDirectory) > -1) {
return true;
}
}
Expand Down Expand Up @@ -429,59 +434,6 @@ var Sql = function () {

return 'other';
}
}, {
key: 'deep_value',
value: function deep_value(obj, pathDeep) {

if (pathDeep.indexOf('.') === -1) {
return typeof obj[pathDeep] !== 'undefined' && obj[pathDeep] !== null ? obj[pathDeep] : null;
}

var pathSplit = pathDeep.split('.');
var res = JSON.parse(JSON.stringify(obj));
for (var i = 0; i < pathSplit.length; i++) {
if (typeof res[pathSplit[i]] !== 'undefined' && res[pathSplit[i]] !== null) {
res = res[pathSplit[i]];
} else {
return null;
}
}

return res;
}
}, {
key: 'deep_value_array',
value: function deep_value_array(obj, pathDeep) {

if (pathDeep.indexOf('.') === -1) {
return typeof obj[pathDeep] !== 'undefined' && obj[pathDeep] !== null ? obj[pathDeep] : null;
}

var pathSplit = pathDeep.split('.');
var res = JSON.parse(JSON.stringify(obj));

while (pathSplit.length > 0) {

if (typeof res[pathSplit[0]] !== 'undefined' && res[pathSplit[0]] !== null) {
if (_typeof(res[pathSplit[0]]) === 'object' && Object.prototype.toString.call(res[pathSplit[0]]) === '[object Array]') {
var resArray = [];

Array.prototype.forEach.call(res[pathSplit[0]], function (item) {
resArray.push(Sql.deep_value_array(item, pathSplit.join('.').replace(pathSplit[0] + '.', '')));
});
res = resArray;
pathSplit.shift();
} else {
res = res[pathSplit[0]];
}
} else {
return null;
}
pathSplit.shift();
}

return res;
}
}, {
key: 'executeWhereClause',
value: function executeWhereClause(files, wheres, maxLimit, columns, jsonPage) {
Expand All @@ -497,26 +449,28 @@ var Sql = function () {
var file = _step.value;

if (limit < maxLimit || maxLimit === -1) {
var doc = Sql.executeWhereClauseToFile(file.published, wheres, jsonPage);
if (file.published === true) {
var doc = Sql.executeWhereClauseOnDocument(file, wheres, jsonPage);

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

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

Array.prototype.forEach.call(columns, function (column) {
if (typeof json[column] !== 'undefined' && json[column] !== null) {
jsonValues[column] = json[column];
}
});
jsonValues[_.config.meta.name] = json[_.config.meta.name];
} else {
jsonValues = json;
}
Array.prototype.forEach.call(columns, function (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 Expand Up @@ -683,37 +637,41 @@ var Sql = function () {
return shouldAdd;
}
}, {
key: 'executeWhereClauseToFile',
value: function executeWhereClauseToFile(file, wheres, jsonPage) {
var json = file;
// if (fileUtils.isFile(file.path)) {
// json = fse.readJsonSync(file.path)
// }
//
var shouldAdd = json;
key: 'executeWhereClauseOnDocument',
value: function executeWhereClauseOnDocument(jsonDoc, wheres, jsonOriginalDoc) {
var shouldAdd = jsonDoc;

if (typeof wheres !== 'undefined' && wheres !== null) {
(function () {
var meta = _.config.meta.name;
if (typeof json[meta] !== 'undefined' && json[meta] !== null) {
if (typeof jsonDoc[meta] !== 'undefined' && jsonDoc[meta] !== null) {
Array.prototype.forEach.call(wheres, function (where) {
var value;
var compare;

if (where.left === 'template' || where.left === 'abe_meta.template') {
value = _.FileParser.getTemplate(json[meta].template);
value = _.FileParser.getTemplate(jsonDoc[meta].template);
} else {
value = Sql.deep_value_array(json, where.left);
try {
value = eval('jsonDoc.' + where.left);
} catch (e) {
// console.log('e', e)
}
}
compare = where.right;

var matchVariable = /^{{(.*)}}$/.exec(compare);
if (typeof matchVariable !== 'undefined' && matchVariable !== null && matchVariable.length > 0) {
var shouldCompare = Sql.deep_value_array(jsonPage, matchVariable[1]);
if (typeof shouldCompare !== 'undefined' && shouldCompare !== null) {
compare = shouldCompare;
} else {
try {
var shouldCompare = eval('jsonOriginalDoc.' + matchVariable[1]);
if (typeof shouldCompare !== 'undefined' && shouldCompare !== null) {
compare = shouldCompare;
} else {
shouldAdd = false;
}
} catch (e) {
shouldAdd = false;
// console.log('e', e)
}
}

Expand Down

0 comments on commit d68a2ac

Please sign in to comment.