2015-01-21 10:00:38 +03:00
|
|
|
// ### Navigation Helper
|
|
|
|
// `{{navigation}}`
|
|
|
|
// Outputs navigation menu of static urls
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
SafeString = proxy.SafeString,
|
|
|
|
i18n = proxy.i18n,
|
|
|
|
errors = proxy.errors,
|
|
|
|
templates = proxy.templates;
|
2015-01-21 10:00:38 +03:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function navigation(options) {
|
2015-02-28 15:53:00 +03:00
|
|
|
var navigationData = options.data.blog.navigation,
|
|
|
|
currentUrl = options.data.root.relativeUrl,
|
2016-01-05 21:04:39 +03:00
|
|
|
self = this,
|
2015-02-28 15:53:00 +03:00
|
|
|
output,
|
2016-02-21 21:48:44 +03:00
|
|
|
data;
|
2015-01-21 10:00:38 +03:00
|
|
|
|
2015-02-28 15:53:00 +03:00
|
|
|
if (!_.isObject(navigationData) || _.isFunction(navigationData)) {
|
2016-10-06 15:27:35 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: i18n.t('warnings.helpers.navigation.invalidData')
|
|
|
|
});
|
2015-01-21 10:00:38 +03:00
|
|
|
}
|
|
|
|
|
2015-02-28 15:53:00 +03:00
|
|
|
if (navigationData.filter(function (e) {
|
2015-02-16 00:44:07 +03:00
|
|
|
return (_.isUndefined(e.label) || _.isUndefined(e.url));
|
|
|
|
}).length > 0) {
|
2016-10-06 15:27:35 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: i18n.t('warnings.helpers.navigation.valuesMustBeDefined')
|
|
|
|
});
|
2015-02-16 00:44:07 +03:00
|
|
|
}
|
2015-01-21 10:00:38 +03:00
|
|
|
|
|
|
|
// check for non-null string values
|
2015-02-28 15:53:00 +03:00
|
|
|
if (navigationData.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) {
|
2016-10-06 15:27:35 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: i18n.t('warnings.helpers.navigation.valuesMustBeString')
|
|
|
|
});
|
2015-01-21 10:00:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function _slugify(label) {
|
|
|
|
return label.toLowerCase().replace(/[^\w ]+/g, '').replace(/ +/g, '-');
|
|
|
|
}
|
|
|
|
|
2016-02-02 13:29:05 +03:00
|
|
|
// strips trailing slashes and compares urls
|
|
|
|
function _isCurrentUrl(href, currentUrl) {
|
2016-06-07 22:10:20 +03:00
|
|
|
if (!currentUrl) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-02 13:29:05 +03:00
|
|
|
var strippedHref = href.replace(/\/+$/, ''),
|
|
|
|
strippedCurrentUrl = currentUrl.replace(/\/+$/, '');
|
|
|
|
return strippedHref === strippedCurrentUrl;
|
|
|
|
}
|
|
|
|
|
2015-01-21 10:00:38 +03:00
|
|
|
// {{navigation}} should no-op if no data passed in
|
2015-02-28 15:53:00 +03:00
|
|
|
if (navigationData.length === 0) {
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString('');
|
2015-01-21 10:00:38 +03:00
|
|
|
}
|
|
|
|
|
2015-02-28 15:53:00 +03:00
|
|
|
output = navigationData.map(function (e) {
|
2015-01-21 10:00:38 +03:00
|
|
|
var out = {};
|
2016-02-02 13:29:05 +03:00
|
|
|
out.current = _isCurrentUrl(e.url, currentUrl);
|
2015-01-21 10:00:38 +03:00
|
|
|
out.label = e.label;
|
|
|
|
out.slug = _slugify(e.label);
|
2017-01-11 13:45:56 +03:00
|
|
|
out.url = e.url;
|
2016-01-05 21:04:39 +03:00
|
|
|
out.secure = self.secure;
|
2015-01-21 10:00:38 +03:00
|
|
|
return out;
|
|
|
|
});
|
|
|
|
|
2016-02-21 21:48:44 +03:00
|
|
|
data = _.merge({}, {navigation: output});
|
2015-01-21 10:00:38 +03:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
return templates.execute('navigation', data, options);
|
2015-01-21 10:00:38 +03:00
|
|
|
};
|
|
|
|
|