2015-01-21 10:00:38 +03:00
|
|
|
// ### Navigation Helper
|
|
|
|
// `{{navigation}}`
|
|
|
|
// Outputs navigation menu of static urls
|
|
|
|
|
|
|
|
var _ = require('lodash'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
template = require('./template'),
|
|
|
|
navigation;
|
|
|
|
|
|
|
|
navigation = function (options) {
|
|
|
|
/*jshint unused:false*/
|
2015-02-16 00:44:07 +03:00
|
|
|
var navigation,
|
2015-01-21 10:00:38 +03:00
|
|
|
context,
|
|
|
|
currentUrl = this.relativeUrl;
|
|
|
|
|
2015-02-16 00:44:07 +03:00
|
|
|
if (!_.isObject(this.navigation) || _.isFunction(this.navigation)) {
|
2015-01-21 10:00:38 +03:00
|
|
|
return errors.logAndThrowError('navigation data is not an object or is a function');
|
|
|
|
}
|
|
|
|
|
2015-02-16 00:44:07 +03:00
|
|
|
if (this.navigation.filter(function (e) {
|
|
|
|
return (_.isUndefined(e.label) || _.isUndefined(e.url));
|
|
|
|
}).length > 0) {
|
|
|
|
return errors.logAndThrowError('All values must be defined for label, url and current');
|
|
|
|
}
|
2015-01-21 10:00:38 +03:00
|
|
|
|
|
|
|
// check for non-null string values
|
2015-02-16 00:44:07 +03:00
|
|
|
if (this.navigation.filter(function (e) {
|
2015-01-21 10:00:38 +03:00
|
|
|
return ((!_.isNull(e.label) && !_.isString(e.label)) ||
|
|
|
|
(!_.isNull(e.url) && !_.isString(e.url)));
|
|
|
|
}).length > 0) {
|
|
|
|
return errors.logAndThrowError('Invalid value, Url and Label must be strings');
|
|
|
|
}
|
|
|
|
|
|
|
|
function _slugify(label) {
|
|
|
|
return label.toLowerCase().replace(/[^\w ]+/g, '').replace(/ +/g, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
// {{navigation}} should no-op if no data passed in
|
2015-02-16 00:44:07 +03:00
|
|
|
if (this.navigation.length === 0) {
|
2015-01-21 10:00:38 +03:00
|
|
|
return new hbs.SafeString('');
|
|
|
|
}
|
|
|
|
|
2015-02-16 00:44:07 +03:00
|
|
|
navigation = this.navigation.map(function (e) {
|
2015-01-21 10:00:38 +03:00
|
|
|
var out = {};
|
|
|
|
out.current = e.url === currentUrl;
|
|
|
|
out.label = e.label;
|
|
|
|
out.slug = _slugify(e.label);
|
|
|
|
out.url = hbs.handlebars.Utils.escapeExpression(e.url);
|
|
|
|
return out;
|
|
|
|
});
|
|
|
|
|
2015-02-16 00:44:07 +03:00
|
|
|
context = _.merge({}, {navigation: navigation});
|
2015-01-21 10:00:38 +03:00
|
|
|
|
|
|
|
return template.execute('navigation', context);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = navigation;
|