2013-05-27 23:27:41 +04:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
handlebars = require('express-hbs').handlebars,
|
|
|
|
nodefn = require('when/node/function'),
|
|
|
|
GhostNavHelper;
|
|
|
|
|
|
|
|
GhostNavHelper = function (navTemplate) {
|
|
|
|
// Bind the context for our methods.
|
|
|
|
_.bindAll(this, 'compileTemplate', 'renderNavItems');
|
|
|
|
|
|
|
|
if (_.isFunction(navTemplate)) {
|
|
|
|
this.navTemplateFunc = navTemplate;
|
|
|
|
} else {
|
|
|
|
this.navTemplatePath = navTemplate;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GhostNavHelper.prototype.compileTemplate = function (templatePath) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Allow people to overwrite the navTemplatePath
|
|
|
|
templatePath = templatePath || this.navTemplatePath;
|
|
|
|
|
2013-05-31 09:58:20 +04:00
|
|
|
return nodefn.call(fs.readFile, templatePath).then(function (navTemplateContents) {
|
2013-05-27 23:27:41 +04:00
|
|
|
// TODO: Can handlebars compile async?
|
|
|
|
self.navTemplateFunc = handlebars.compile(navTemplateContents.toString());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
GhostNavHelper.prototype.renderNavItems = function (navItems) {
|
|
|
|
var output;
|
|
|
|
|
|
|
|
output = this.navTemplateFunc({links: navItems});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A static helper method for registering with ghost
|
2013-05-31 09:58:20 +04:00
|
|
|
GhostNavHelper.registerWithGhost = function (ghost) {
|
2013-05-27 23:27:41 +04:00
|
|
|
var templatePath = path.join(ghost.paths().frontendViews, 'nav.hbs'),
|
|
|
|
ghostNavHelper = new GhostNavHelper(templatePath);
|
|
|
|
|
2013-05-31 09:58:20 +04:00
|
|
|
return ghostNavHelper.compileTemplate().then(function () {
|
2013-05-27 23:27:41 +04:00
|
|
|
ghost.registerThemeHelper("ghostNav", ghostNavHelper.renderNavItems);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GhostNavHelper;
|
|
|
|
}());
|