diff --git a/lib/data/index.js b/lib/data/index.js new file mode 100644 index 0000000..f6ccdfa --- /dev/null +++ b/lib/data/index.js @@ -0,0 +1,9 @@ + +exports = module.exports = { + tbl: require('./tbl'), + otm: require('./otm'), + mtm: require('./mtm'), + stc: require('./stc'), + list: require('./list'), + pagination: require('./pagination') +} \ No newline at end of file diff --git a/lib/listview/data.js b/lib/data/list.js similarity index 80% rename from lib/listview/data.js rename to lib/data/list.js index fcf6e59..8169af0 100644 --- a/lib/listview/data.js +++ b/lib/data/list.js @@ -1,9 +1,9 @@ var qb = require('../qb'), - format = require('./format'); + format = require('../format'); -exports = module.exports = function (args, cb) { +exports.get = function (args, done) { var columns = args.config.columns; var visible = [], @@ -18,7 +18,7 @@ exports = module.exports = function (args, cb) { qb.lst.create(args); args.db.client.query(args.query, function (err, rows) { - if (err) return cb(err); + if (err) return done(err); var records = []; for (var i=0; i < rows.length; i++) { var pk = { @@ -28,12 +28,12 @@ exports = module.exports = function (args, cb) { var values = []; for (var j=1; j < columns.length; j++) { var value = rows[i][columns[j].name]; - value = format.value(columns[j], value); + value = format.list.value(columns[j], value); if (columns[j].manyToMany && value) value = {mtm: value.split(',')} values.push(value); } records.push({pk: pk, values: values}); } - cb(null, {columns: names, records: records}); + done(null, {columns: names, records: records}); }); } diff --git a/lib/data/mtm.js b/lib/data/mtm.js new file mode 100644 index 0000000..b828777 --- /dev/null +++ b/lib/data/mtm.js @@ -0,0 +1,52 @@ + +var async = require('async'); +var qb = require('../qb'); + + +// modifies `rows.columns` with manyToMany columns on select +// modifies `rows` with ids on post + +// return an array of all child ids linked to this record's pk +function getIds (args, column, pk, done) { + var str = qb.mtm.select(args, column, pk); + args.db.client.query(str, function (err, rows) { + if (err) return done(err); + var result = []; + for (var i=0; i < rows.length; i++) { + result.push(rows[i][Object.keys(rows[i])[0]]); + } + done(err, result); + }); +} + +function loopColumns (args, row, done) { + row.ids = {}; + async.each(args.config.columns, function (column, done) { + if (!column.manyToMany) return done(); + + // after select this column practically doesn't exists + if (row.columns[column.name] === undefined) + row.columns[column.name] = []; + + var pk = row.pk || row.columns['__pk']; + if (!pk) return done(); + + getIds(args, column, pk, function (err, ids) { + if (err) return done(err); + args.post + // save the ids from the database + ? row.ids[column.name] = ids + // selecting + : row.columns[column.name] = ids; + + done(); + }); + }, done); +} + +// loopRecords +exports.get = function (args, rows, done) { + async.each(rows, function (row, done) { + loopColumns(args, row, done); + }, done); +} diff --git a/lib/data/otm.js b/lib/data/otm.js new file mode 100644 index 0000000..32ed7a2 --- /dev/null +++ b/lib/data/otm.js @@ -0,0 +1,27 @@ + +var async = require('async'); +var qb = require('../qb'); + + +// modifies `args.config.columns` with `value` + +function getData (args, ref, done) { + var str = qb.otm.select(args, ref); + args.db.client.query(str, function (err, rows) { + if (err) return done(err); + done(null, rows); + }); +} + +exports.get = function (args, done) { + async.each(args.config.columns, function (column, done) { + if ((!column.oneToMany && !column.manyToMany) || !column.control.select) + return done(); + var ref = column.oneToMany ? column.oneToMany : column.manyToMany.ref; + getData(args, ref, function (err, rows) { + if (err) return done(err); + column.value = rows; + done(); + }); + }, done); +} diff --git a/lib/listview/pagination.js b/lib/data/pagination.js similarity index 66% rename from lib/listview/pagination.js rename to lib/data/pagination.js index c8fffc9..1281724 100644 --- a/lib/listview/pagination.js +++ b/lib/data/pagination.js @@ -3,13 +3,13 @@ var pagination = require('sr-pagination'); var qb = require('../qb') -exports.get = function (args, cb) { +exports.get = function (args, done) { var str = qb.lst.pagination(args); args.db.client.query(str, function (err, rows) { - if (err) return cb(err); + if (err) return done(err); var total = parseInt(rows[0].count), rows = args.config.listview.page, page = parseInt(args.page || 1); - cb(null, pagination({page: page, links: 9, rows: rows, total: total})); + done(null, pagination({page: page, links: 9, rows: rows, total: total})); }); } diff --git a/lib/data/stc.js b/lib/data/stc.js new file mode 100644 index 0000000..5e0413d --- /dev/null +++ b/lib/data/stc.js @@ -0,0 +1,40 @@ + +// load static values from settings +// modifies `args.config.columns` with `value` + +function getSelect (options) { + var values = []; + for (var i=0; i < options.length; i++) { + if ('string' === typeof options[i]) { + values.push({__pk: options[i], __text: options[i]}); + } + else if ('object' === typeof options[i]) { + var value = Object.keys(options[i])[0], + text = options[i][value]; + values.push({__pk: value, __text: text}); + } + } + return values; +} + +function getRadio (options) { + return [ + {text: options[0], value: 1}, + {text: options[1], value: 0} + ]; +} + +exports.get = function (args) { + var columns = args.config.columns; + for (var i=0; i < columns.length; i++) { + var control = columns[i].control; + if (!(control.options instanceof Array)) continue; + + if (control.select) { + columns[i].value = getSelect(control.options); + } + else if (control.radio) { + columns[i].value = getRadio(control.options); + } + } +} diff --git a/lib/data/tbl.js b/lib/data/tbl.js new file mode 100644 index 0000000..39732a9 --- /dev/null +++ b/lib/data/tbl.js @@ -0,0 +1,23 @@ + +var qb = require('../qb'); + + +// get table records + +exports.get = function (args, done) { + if (args.post) { + var table = args.config.table, + rows = args.post[table.name].records || []; + return done(null, rows); + } + + var str = qb.tbl.select(args); + args.db.client.query(str, function (err, rows) { + if (err) return done(err); + var result = []; + for (var i=0; i < rows.length; i++) { + result.push({columns: rows[i]}); + } + done(null, result); + }); +} diff --git a/lib/editview/data.js b/lib/editview/data.js deleted file mode 100644 index e201f5c..0000000 --- a/lib/editview/data.js +++ /dev/null @@ -1,137 +0,0 @@ - -var async = require('async'); -var qb = require('../qb'); - - -// modifies `args.config.columns` with `value` -var otm = { - _getData: function (args, ref, done) { - var str = qb.otm.select(args, ref); - args.db.client.query(str, function (err, rows) { - if (err) return done(err); - done(null, rows); - }); - }, - get: function (args, done) { - async.each(args.config.columns, function (column, done) { - if ((!column.oneToMany && !column.manyToMany) || !column.control.select) - return done(); - var ref = column.oneToMany ? column.oneToMany : column.manyToMany.ref; - otm._getData(args, ref, function (err, rows) { - if (err) return done(err); - column.value = rows; - done(); - }); - }, done); - } -}; - -// load static values from settings -// modifies `args.config.columns` with `value` -var stc = { - getSelect: function (options) { - var values = []; - for (var i=0; i < options.length; i++) { - if ('string' === typeof options[i]) { - values.push({__pk: options[i], __text: options[i]}); - } - else if ('object' === typeof options[i]) { - var value = Object.keys(options[i])[0], - text = options[i][value]; - values.push({__pk: value, __text: text}); - } - } - return values; - }, - getRadio: function (options) { - return [ - {text: options[0], value: 1}, - {text: options[1], value: 0} - ]; - }, - // loopColumns - get: function (args) { - var columns = args.config.columns; - for (var i=0; i < columns.length; i++) { - var control = columns[i].control; - if (!(control.options instanceof Array)) continue; - - if (control.select) { - columns[i].value = stc.getSelect(control.options); - } - else if (control.radio) { - columns[i].value = stc.getRadio(control.options); - } - } - } -}; - -// get table records -var tbl = { - get: function (args, done) { - if (args.post) { - var table = args.config.table, - rows = args.post[table.name].records || []; - return done(null, rows); - } - - var str = qb.tbl.select(args); - args.db.client.query(str, function (err, rows) { - if (err) return done(err); - var result = []; - for (var i=0; i < rows.length; i++) { - result.push({columns: rows[i]}); - } - done(null, result); - }); - } -}; - -// modifies `rows.columns` with manyToMany columns on select -// modifies `rows` with ids on post -var mtm = { - // return an array of all child ids linked to this record's pk - getIds: function (args, column, pk, done) { - var str = qb.mtm.select(args, column, pk); - args.db.client.query(str, function (err, rows) { - if (err) return done(err); - var result = []; - for (var i=0; i < rows.length; i++) { - result.push(rows[i][Object.keys(rows[i])[0]]); - } - done(err, result); - }); - }, - loopColumns: function (args, row, done) { - row.ids = {}; - async.each(args.config.columns, function (column, done) { - if (!column.manyToMany) return done(); - - // after select this column practically doesn't exists - if (row.columns[column.name] === undefined) - row.columns[column.name] = []; - - var pk = row.pk || row.columns['__pk']; - if (!pk) return done(); - - mtm.getIds(args, column, pk, function (err, ids) { - if (err) return done(err); - args.post - // save the ids from the database - ? row.ids[column.name] = ids - // selecting - : row.columns[column.name] = ids; - - done(); - }); - }, done); - }, - // loopRecords - get: function (args, rows, done) { - async.each(rows, function (row, done) { - mtm.loopColumns(args, row, done); - }, done); - } -}; - -exports = module.exports = {tbl: tbl, otm: otm, mtm: mtm, stc: stc}; diff --git a/lib/editview/index.js b/lib/editview/index.js index d2288b4..054c829 100644 --- a/lib/editview/index.js +++ b/lib/editview/index.js @@ -2,8 +2,8 @@ var async = require('async'), dcopy = require('deep-copy'); var upload = require('./upload'), - data = require('./data'), - params = require('./params'); + data = require('../data'), + template = require('../template'); function removeHidden (columns) { @@ -27,7 +27,7 @@ function getData (args, done) { if (err) return done(err); upload.loopRecords(args, function (err) { if (err) return done(err); - done(null, params.get(args, rows)); + done(null, template.table.get(args, rows)); }); }); }); diff --git a/lib/editview/format.js b/lib/format/form.js similarity index 100% rename from lib/editview/format.js rename to lib/format/form.js diff --git a/lib/format/index.js b/lib/format/index.js new file mode 100644 index 0000000..ddaaec9 --- /dev/null +++ b/lib/format/index.js @@ -0,0 +1,5 @@ + +exports = module.exports = { + list: require('./list'), + form: require('./form') +} diff --git a/lib/listview/format.js b/lib/format/list.js similarity index 100% rename from lib/listview/format.js rename to lib/format/list.js diff --git a/lib/template/index.js b/lib/template/index.js new file mode 100644 index 0000000..7ca8479 --- /dev/null +++ b/lib/template/index.js @@ -0,0 +1,4 @@ + +exports = module.exports = { + table: require('./table') +} diff --git a/lib/editview/params.js b/lib/template/table.js similarity index 93% rename from lib/editview/params.js rename to lib/template/table.js index 968f098..ed86732 100644 --- a/lib/editview/params.js +++ b/lib/template/table.js @@ -1,7 +1,7 @@ var dcopy = require('deep-copy'); -var validate = require('./validate'), - format = require('./format'); +var validate = require('../editview/validate'), + format = require('../format'); exports.blank = function (args) { @@ -65,7 +65,7 @@ exports.records = function (args, rows) { var value = values[column.name]; column.error = validate.value(column, value); - column.value = format.value(column, value); + column.value = format.form.value(column, value); args.error = column.error ? true : args.error; } records.push({pk: pk, columns: columns, insert: insert, remove: remove}); diff --git a/routes/listview.js b/routes/listview.js index a05b3f1..3b870c5 100644 --- a/routes/listview.js +++ b/routes/listview.js @@ -1,17 +1,9 @@ var dcopy = require('deep-copy'); -var qb = require('../lib/qb'); -// listview -var listview = { - data: require('../lib/listview/data')}, - pagination = require('../lib/listview/pagination'), - filter = require('../lib/listview/filter'); -// editview -var editview = { - otm: require('../lib/editview/data').otm, - stc: require('../lib/editview/data').stc, - format: require('../lib/editview/format') -}; +var qb = require('../lib/qb'), + data = require('../lib/data'), + format = require('../lib/format'); +var filter = require('../lib/listview/filter'); function getArgs (req, res) { @@ -30,14 +22,14 @@ function getArgs (req, res) { } exports.get = function (req, res, next) { - data(req, res, next); + _data(req, res, next); } exports.post = function (req, res, next) { - data(req, res, next); + _data(req, res, next); } -function data (req, res, next) { +function _data (req, res, next) { var args = getArgs(req, res), events = res.locals._admin.events; @@ -45,33 +37,33 @@ function data (req, res, next) { qb.lst.select(args); events.preList(req, res, args, function () { - listview.data(args, function (err, data) { + data.list.get(args, function (err, ddata) { if (err) return next(err); - pagination.get(args, function (err, pager) { + data.pagination.get(args, function (err, pager) { if (err) return next(err); // always should be in front of filter.getColumns // as it may reduce args.config.columns var order = filter.getOrderColumns(req, args); args.config.columns = filter.getColumns(args); - editview.otm.get(args, function (err) { + data.otm.get(args, function (err) { if (err) return next(err); - editview.stc.get(args); + data.stc.get(args); - render(req, res, args, data, pager, order, next); + render(req, res, args, ddata, pager, order, next); }); }); }); }); } -function render (req, res, args, data, pager, order, next) { +function render (req, res, args, ddata, pager, order, next) { // set filter active items for (var i=0; i < args.config.columns.length; i++) { var column = args.config.columns[i], value = args.filter.columns[column.name]; column.defaultValue = null; - column.value = editview.format.value(column, value); + column.value = format.form.value(column, value); } res.locals.view = { @@ -110,8 +102,8 @@ function render (req, res, args, data, pager, order, next) { res.locals.collapsed = args.filter.show; res.locals.or = args.filter.or; - res.locals.columns = data.columns; - res.locals.records = data.records; + res.locals.columns = ddata.columns; + res.locals.records = ddata.records; res.locals.pagination = pager; res.locals.partials = {