2014-10-10 18:54:07 +04:00
|
|
|
// ### Pagination Helper
|
|
|
|
// `{{pagination}}`
|
|
|
|
// Outputs previous and next buttons, along with info about the current page
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
errors = proxy.errors,
|
|
|
|
i18n = proxy.i18n,
|
|
|
|
templates = proxy.templates,
|
2014-10-10 18:54:07 +04:00
|
|
|
pagination;
|
|
|
|
|
|
|
|
pagination = function (options) {
|
|
|
|
if (!_.isObject(this.pagination) || _.isFunction(this.pagination)) {
|
2016-10-06 15:27:35 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 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)) {
|
2016-10-06 15:27:35 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 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))) {
|
2016-10-06 15:27:35 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 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)) {
|
2016-10-06 15:27:35 +03:00
|
|
|
throw new errors.IncorrectUsageError({message: 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
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
return templates.execute('pagination', data, options);
|
2014-10-10 18:54:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = pagination;
|