Ghost/core/ghost.js

184 lines
5.2 KiB
JavaScript
Raw Normal View History

2013-05-11 20:44:25 +04:00
// # Ghost Module
// Defines core methods required to build the frontend
2013-05-16 14:29:02 +04:00
/*global module, require, __dirname */
2013-05-11 20:44:25 +04:00
(function () {
"use strict";
// ## Setup Prerequisites
var config = require('./../config'),
when = require('when'),
2013-05-11 20:44:25 +04:00
express = require('express'),
path = require('path'),
hbs = require('express-hbs'),
_ = require('underscore'),
Polyglot = require('node-polyglot'),
2013-06-01 18:47:41 +04:00
models = require('./shared/models'),
2013-05-29 04:10:39 +04:00
ExampleFilter = require('../content/plugins/exampleFilters'),
2013-05-11 20:44:25 +04:00
Ghost,
2013-05-29 04:10:39 +04:00
instance,
2013-05-11 20:44:25 +04:00
statuses;
2013-05-30 02:49:49 +04:00
2013-05-11 20:44:25 +04:00
// ## Article Statuses
/**
* A list of atricle status types
* @type {Object}
*/
statuses = {
'draft': 'draft',
'complete': 'complete',
'approved': 'approved',
'scheduled': 'scheduled',
'published': 'published'
};
// ## Module Methods
/**
* @method Ghost
* @returns {*}
* @constructor
*/
Ghost = function () {
var app,
2013-05-29 04:10:39 +04:00
plugin,
2013-05-11 20:44:25 +04:00
polyglot;
if (!instance) {
instance = this;
2013-06-01 18:47:41 +04:00
// Holds the filters
this.filterCallbacks = [];
// Holds the filter hooks (that are built in to Ghost Core)
this.filters = [];
2013-05-29 04:10:39 +04:00
plugin = new ExampleFilter(instance).init();
2013-05-11 20:44:25 +04:00
app = express();
polyglot = new Polyglot();
// functionality
// load Plugins...
// var f = new FancyFirstChar(ghost).init();
_.extend(instance, {
app: function () { return app; },
config: function () { return config; },
2013-06-01 18:47:41 +04:00
globals: function () { return instance.globalConfig; }, // there's no management here to be sure this has loaded
dataProvider: models,
2013-05-11 20:44:25 +04:00
statuses: function () { return statuses; },
polyglot: function () { return polyglot; },
plugin: function () { return plugin; },
2013-05-11 20:44:25 +04:00
paths: function () {
return {
'activeTheme': __dirname + '/../content/' + config.themeDir + '/' + config.activeTheme + '/',
'adminViews': __dirname + '/admin/views/',
'frontendViews': __dirname + '/frontend/views/',
'lang': __dirname + '/lang/'
2013-05-11 20:44:25 +04:00
};
}
2013-05-30 13:41:25 +04:00
2013-05-11 20:44:25 +04:00
});
2013-05-30 13:41:25 +04:00
2013-05-11 20:44:25 +04:00
}
return instance;
};
2013-06-01 18:47:41 +04:00
Ghost.prototype.init = function () {
this.globalConfig = config.blogData;
return when.all([instance.dataProvider.init()]);
2013-05-30 13:41:25 +04:00
};
2013-05-11 20:44:25 +04:00
/**
* @param {string} name
* @param {Function} fn
* @return {method} hbs.registerHelper
*/
Ghost.prototype.registerThemeHelper = function (name, fn) {
hbs.registerHelper(name, fn);
};
/**
* @param {string} name
* @param {Function} fn
* @return {*}
*/
Ghost.prototype.registerTheme = function (name, fn) {
return this;
};
2013-05-11 20:44:25 +04:00
/**
* @param {string} name
* @param {Function} fn
* @return {*}
*/
Ghost.prototype.registerPlugin = function (name, fn) {
return this;
};
2013-05-11 20:44:25 +04:00
/**
* @param {string} name
* @param {Function} fn
*/
Ghost.prototype.registerFilter = function (name, fn) {
2013-05-29 04:10:39 +04:00
if (!this.filterCallbacks.hasOwnProperty(name)) {
this.filterCallbacks[name] = [];
2013-05-11 20:44:25 +04:00
}
console.log('registering filter for ', name);
2013-05-29 04:10:39 +04:00
this.filterCallbacks[name].push(fn);
2013-05-11 20:44:25 +04:00
};
/**
* @param {string} name [description]
* @param {*} args
* @param {Function} callback
* @return {method} callback
*/
Ghost.prototype.doFilter = function (name, args, callback) {
var fn;
2013-05-29 04:10:39 +04:00
if (this.filterCallbacks.hasOwnProperty(name)) {
for (fn in this.filterCallbacks[name]) {
if (this.filterCallbacks[name].hasOwnProperty(fn)) {
2013-05-11 20:44:25 +04:00
console.log('doing filter for ', name);
2013-05-29 04:10:39 +04:00
args = this.filterCallbacks[name][fn](args);
2013-05-11 20:44:25 +04:00
}
}
}
2013-05-11 20:44:25 +04:00
callback(args);
};
/**
* Initialise Theme
*
* @todo Tod (?) Old comment
* @param {Object} app
*/
Ghost.prototype.initTheme = function (app) {
var self = this;
return function initTheme(req, res, next) {
app.set('view engine', 'hbs');
if (/(^\/ghost$|^\/ghost\/)/.test(req.url) === false) {
app.engine('hbs', hbs.express3(
{partialsDir: self.paths().activeTheme + 'partials'}
));
app.set('views', self.paths().activeTheme);
} else {
2013-05-12 17:40:59 +04:00
app.engine('hbs', hbs.express3({partialsDir: self.paths().adminViews + 'partials'}));
2013-05-11 20:44:25 +04:00
app.set('views', self.paths().adminViews);
app.use('/core/admin/assets', express['static'](path.join(__dirname, '/admin/assets')));
}
app.use(express['static'](self.paths().activeTheme));
app.use('/content/images', express['static'](path.join(__dirname, '/../content/images')));
next();
};
};
module.exports = Ghost;
}());