2014-10-10 18:54:07 +04:00
|
|
|
// ### Pagination Helper
|
|
|
|
// `{{pagination}}`
|
|
|
|
// Outputs previous and next buttons, along with info about the current page
|
|
|
|
|
|
|
|
var _ = require('lodash'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
template = require('./template'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2014-10-10 18:54:07 +04:00
|
|
|
pagination;
|
|
|
|
|
|
|
|
pagination = function (options) {
|
|
|
|
/*jshint unused:false*/
|
|
|
|
if (!_.isObject(this.pagination) || _.isFunction(this.pagination)) {
|
2015-11-12 15:29:45 +03:00
|
|
|
return errors.logAndThrowError(i18n.t('warnings.helpers.pagination.invalidData'));
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isUndefined(this.pagination.page) || _.isUndefined(this.pagination.pages) ||
|
|
|
|
_.isUndefined(this.pagination.total) || _.isUndefined(this.pagination.limit)) {
|
2015-11-12 15:29:45 +03:00
|
|
|
return errors.logAndThrowError(i18n.t('warnings.helpers.pagination.valuesMustBeDefined'));
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((!_.isNull(this.pagination.next) && !_.isNumber(this.pagination.next)) ||
|
|
|
|
(!_.isNull(this.pagination.prev) && !_.isNumber(this.pagination.prev))) {
|
2015-11-12 15:29:45 +03:00
|
|
|
return errors.logAndThrowError(i18n.t('warnings.helpers.pagination.nextPrevValuesMustBeNumeric'));
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isNumber(this.pagination.page) || !_.isNumber(this.pagination.pages) ||
|
|
|
|
!_.isNumber(this.pagination.total) || !_.isNumber(this.pagination.limit)) {
|
2015-11-12 15:29:45 +03:00
|
|
|
return errors.logAndThrowError(i18n.t('warnings.helpers.pagination.valuesMustBeNumeric'));
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
2016-02-21 21:48:44 +03:00
|
|
|
var data = _.merge({}, this.pagination);
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2016-02-21 21:48:44 +03:00
|
|
|
return template.execute('pagination', data, options);
|
2014-10-10 18:54:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = pagination;
|