2013-06-25 15:43:15 +04:00
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
handlebars = require('express-hbs').handlebars,
|
|
|
|
nodefn = require('when/node/function'),
|
2013-07-09 19:55:23 +04:00
|
|
|
|
2013-06-26 01:44:34 +04:00
|
|
|
NavHelper;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-06-26 01:44:34 +04:00
|
|
|
NavHelper = function (navTemplate) {
|
2013-06-25 15:43:15 +04:00
|
|
|
// Bind the context for our methods.
|
|
|
|
_.bindAll(this, 'compileTemplate', 'renderNavItems');
|
|
|
|
|
|
|
|
if (_.isFunction(navTemplate)) {
|
|
|
|
this.navTemplateFunc = navTemplate;
|
|
|
|
} else {
|
|
|
|
this.navTemplatePath = navTemplate;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-26 01:44:34 +04:00
|
|
|
NavHelper.prototype.compileTemplate = function (templatePath) {
|
2013-06-25 15:43:15 +04:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Allow people to overwrite the navTemplatePath
|
|
|
|
templatePath = templatePath || this.navTemplatePath;
|
|
|
|
|
|
|
|
return nodefn.call(fs.readFile, templatePath).then(function (navTemplateContents) {
|
|
|
|
// TODO: Can handlebars compile async?
|
|
|
|
self.navTemplateFunc = handlebars.compile(navTemplateContents.toString());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-06-26 01:44:34 +04:00
|
|
|
NavHelper.prototype.renderNavItems = function (navItems) {
|
2013-07-09 19:55:23 +04:00
|
|
|
return new handlebars.SafeString(this.navTemplateFunc({links: navItems}));
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// A static helper method for registering with ghost
|
2013-06-26 01:44:34 +04:00
|
|
|
NavHelper.registerWithGhost = function (ghost) {
|
2013-06-25 15:43:15 +04:00
|
|
|
var templatePath = path.join(ghost.paths().frontendViews, 'nav.hbs'),
|
2013-06-26 01:44:34 +04:00
|
|
|
ghostNavHelper = new NavHelper(templatePath);
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
return ghostNavHelper.compileTemplate().then(function () {
|
2013-06-26 01:44:34 +04:00
|
|
|
ghost.registerThemeHelper("nav", ghostNavHelper.renderNavItems);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-06-26 01:44:34 +04:00
|
|
|
module.exports = NavHelper;
|