Skip to content

Commit

Permalink
*rename
Browse files Browse the repository at this point in the history
  • Loading branch information
jspears committed Apr 3, 2012
1 parent 233e446 commit fdc7690
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 133 deletions.
78 changes: 6 additions & 72 deletions lib/bobamo-express.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
var generate = require('./generate'), _u = require('underscore'), mutil = require('mers/lib/util'), mers = require('mers'), jqtpl = require('jqtpl'),
App = require('./display-model'),
mongooseApp = require('./mongoose-adapter'),
packageApp = require('./package-adapter')
adminPlugin = require('./admin-plugin')
;
var _u = require('underscore'), jqtpl = require('jqtpl'), PluginManager = require('./plugin-manager');

function bobamo(options, express) {
var options = _u.extend(this, options);
if (options.uri) {
Expand All @@ -22,80 +18,18 @@ function bobamo(options, express) {
var dir = __dirname + "/..";
var base = options.basepath || (options.basepath = '');
if (base[base.length - 1] != '/')
base +='/'
if (base.length > 1 && base[0] !== '/'){
base = '/'+base;
}
if (!options.displayFactory){
options.displayFactory = new App(mongooseApp(options), packageApp);
base += '/'
if (base.length > 1 && base[0] !== '/') {
base = '/' + base;
}

// app.use(options.basepath || '/', express.static(dir + '/public'));
app.set('views', dir + '/views');
app.set('view engine', "html");
app.register('.html', jqtpl.express);
app.register('.js', jqtpl.express);
app.use(express.bodyParser());
app.get(base + '*', function(req,res,next){

req._url =req.url;
req.url = req.url.substring(base.length - 1);
next();

}, express.static(dir + '/public'), function(req,res,next){
req.url = req._url;
next();
});
adminPlugin(app, base, options.displayFactory);
require('./less-plugin')(app,base, options.displayFactory);
app.all(new RegExp(base + 'api/*'), function (req, res, next) {
req.query.transform = mutil.split(req.query.transform, ',', ['_idToId']);
next();
});

app.use(base + 'api', mers(_u.extend({
}, options, {

transformers:{
labelval:function (m) {
var model = options.displayFactory.modelFor(m.modelName);
var labelAttr = model && model.labelAttr;
return function (obj) {
return {
val:obj._id || obj.id,
label:labelAttr && obj[labelAttr] ? obj[labelAttr] : m.modelName + '[' + (obj.id || obj._id)+']'
}
}
},
_idToId:function (M) {
return function (obj) {
if (!obj) return null;
var o = obj.toObject ? obj.toObject() : obj;
o.id = obj._id;
delete o._id;
delete o.id_;
delete o.managerId_;
if (M && M.modelName == 'user')
o.password = '';
var manager = obj.manager;
o.managerFirstName = manager ? manager.firstName : '';
o.managerLastName = manager ? manager.lastName : '';
o.managerId = manager && manager._id ? manager._id.toString() : manager || '';
delete o.reports;
delete o.manager;
return o;
}
}
}

})).rest());


app.get(base, function (req, res, next) {
res.redirect((base || '/') + 'index.html');
});

generate(options, app)
new PluginManager(options, app)
console.log('mounted bobamo on ', base, dir);
},
handle:function (req, res, next) {
Expand Down
4 changes: 3 additions & 1 deletion lib/display-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function App(options) {
this.__defineGetter__('modelPaths', function () {
var ret = {};
_u.each(this._modelPaths, function onModelPaths(v, k) {
var model = ret[k] = new Model(k, v);
ret[k] = new Model(k, v);
}, this);
return ret;
});
Expand All @@ -55,6 +55,7 @@ function App(options) {
});
}
App.prototype.add = function (App) {
if (!App) return this;
this._args.shift(App);
this._args.forEach(function (k, v) {
_u.each(k.modelPaths, function onAppArgs(vv, kk) {
Expand All @@ -63,6 +64,7 @@ App.prototype.add = function (App) {
}, this);

}, this);
return this;
}
App.prototype.modelFor = function (model) {
var Model = model;
Expand Down
13 changes: 11 additions & 2 deletions lib/plugin-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ var Plugin = function (options, app, name) {
this.app = app;
}
//Returns a display-model if it can.
Plugin.prototype.app = function(){
Plugin.prototype.appModel = function(){

}
/**
* Adds filters before routes, in gives a shot
* to filtering things before swallowing the request.
* @param options
*/
Plugin.prototype.filters = function(options){

}
/**
Expand Down Expand Up @@ -113,6 +121,7 @@ Plugin.prototype.admin = function (admin) {
* @param options
*/
Plugin.prototype.generate = function (res, view, options) {
res.render(view, _u.extend({}, options, {root:path.join(this.name, '/views/', view), layout:false}));
var search = path.join(path.dirname(module.parent.filename), '/views/', view);
res.render(search, _u.extend({relative:false}, options, {layout:false}));
}
module.exports = Plugin;
37 changes: 24 additions & 13 deletions lib/plugin-manager.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
var fs = require('fs'), path = require('path'), _u = require('underscore'), util = require('./util'), App = require('./display-model');
var Options = function(options, pluginManager){
var app = options.app;
delete options.app;
_u.extend(this, options);
this.__defineGetter__('app', function(){

});

}
var PluginManager = function (options, express) {
this.options = options;
this.plugins = options.plugins || this.loadPlugins(this.options, express);
this.app = new App(this.options, [this.loadMongoModels()])
this.options.appModel = this.appModel = new App(this.options, [this.loadMongoModels()])
this.loadAppModels();
this.loadFilters();
this.loadRoutes();
}
PluginManager.prototype.loadFilters = function () {
this.plugins.forEach(function (plugin) {
plugin.filters(this.options);
}, this);
}

PluginManager.prototype.app = function () {
return this.app;
PluginManager.prototype.loadAppModels = function () {
this.plugins.forEach(function (plugin) {
this.appModel.add(plugin.appModel(this.options))
}, this);

}

PluginManager.prototype.loadRoutes = function () {
this.plugins.forEach(function (plugin) {
plugin.routes(this.options);
}, this);

}
PluginManager.prototype.loadMongoModels = function () {
var self = this;
var mongoose = this.options.mongoose;
Expand Down Expand Up @@ -48,7 +57,9 @@ PluginManager.prototype.loadPlugins = function (options, express) {
if (path.existsSync(fpath)) {
try {
var Plugin = require(fpath);
plugins.push(new Plugin(options, express, name));
var plugin = new Plugin(options, express, name);
plugins.push(plugin);

} catch (e) {
console.warn('error loading plugin [' + fpath + ']', e);
}
Expand Down Expand Up @@ -77,7 +88,7 @@ PluginManager.prototype.pluginFor = function (path, property, object) {
}

function MModel(m, manager) {
this.__defineGetter__('modelName', function () {
this.__defineGetter__('modelName', function () {
return m.modelName;
});
this.__defineGetter__('plural', function () {
Expand Down
3 changes: 2 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ module.exports = {
defaultOrSet:_defaultOrSet,
depth:_depth,
value:_value,
options:_options
options:_options,
inherits:require('util').inherits
}
44 changes: 30 additions & 14 deletions plugins/bobamo/bobamo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var BobamoPlugin = function (options) {
}
sutil.inherits(BobamoPlugin, Plugin);
module.exports = BobamoPlugin;
BobamoPlugin.prototype.app = function(){
BobamoPlugin.prototype.appModel = function () {
var self = this;
var mongoose = this.options.mongoose;
return new function () {
Expand All @@ -20,47 +20,63 @@ BobamoPlugin.prototype.app = function(){
}
}

BobamoPlugin.prototype.routes = function () {
BobamoPlugin.prototype.routes = function (options) {
var appModel = options.appModel;

function makeOptions(req) {
var type = req.params.type;
var opts = {};
if (type) {
opts.schema = appModel.schemaFor(type);
opts.model = appModel.modelFor(type)

}
return opts;
}

var apiPath = this.options.apiUri || this.pluginUrl + '/rest/';
var app = this.app;
var base = this.pluginUrl;
app.get(base+'/*', function(req,res,next){

app.get(base + '/*', function (req, res, next) {
var useAuth = req.isAuthenticated ? true : false;
res.local('useAuthentication', useAuth )
res.local('isAuthenticated', useAuth && req.isAuthenicated() : null);
res.local('useAuthentication', useAuth)
res.local('isAuthenticated', useAuth ? req.isAuthenicated() : false);
res.local('api', apiPath);
res.local('baseUrl', this.baseUrl);
res.local('params', req.params);
res.local('query', req.query);
res.local('appModel', appModel);
res.local('options', options);
next();
});
app.get(base + '/:view', function (req, res, next) {
this.generate(res, 'views/'+req.params.view, makeOptions(req));
});
this.generate(res, req.params.view, makeOptions(req));
}.bind(this));
app.get(base + '/js/:view', function (req, res, next) {
this.generate(res, 'views/'+req.params.view, makeOptions(req));
this.generate(res, req.params.view, makeOptions(req));
});
app.get(base + '/js/:super?/views/:view', function (req, res, next) {
this.generate(res, 'views/'+req.params.view, makeOptions(req));
this.generate(res, req.params.view, makeOptions(req));
});
app.get(base + '/js/:super?/views/:type/:view', function (req, res, next) {
this.generate(res, 'views/'+req.params.view, makeOptions(req));
this.generate(res, 'views/' + req.params.view, makeOptions(req));
});

app.get(base + '/js/:super?/:clazz/:type', function (req, res, next) {
this.generate(res, 'views/'+req.params.view, makeOptions(req));
this.generate(res, req.params.view, makeOptions(req));
});

app.get(base + '/js/:super?/:view', function (req, res, next) {
this.generate(res, 'views/'+req.params.view, makeOptions(req));
this.generate(res, req.params.view, makeOptions(req));

});
app.get(base + '/templates/:super?/:type/:view', function (req, res, next) {
this.generate(res, 'views/'+req.params.view, makeOptions(req));
this.generate(res, req.params.view, makeOptions(req));

});
app.get(base + '/tpl/:super?/:view', function (req, res, next) {
this.generate(res, 'views/templates/'+req.params.view, makeOptions(req));
this.generate(res, 'templates/' + req.params.view, makeOptions(req));

});
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/bobamo/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>${_app(true).title} ${_app(true).version}</title>
<title>${appModel.title} ${appModel.version}</title>
<meta name="description" content="">
<meta name="author" content="">

Expand All @@ -19,7 +19,7 @@
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link href="less/${query.checksum || lessFactory.checksum || ''}" rel="stylesheet">
<link href="less/${query.checksum || lessFactory && lessFactory.checksum || ''}" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">
<link href="js/libs/table/jquery.sorter.css" rel="stylesheet">
<link href="js/libs/backbone-forms/src/backbone-forms.css" rel="stylesheet">
Expand Down
28 changes: 15 additions & 13 deletions plugins/less/less-factory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var fs = require('fs'), path = require('path'), _u = require('underscore'), inflection = require('./inflection'), crypto = require('crypto'), less = require('less');
var fs = require('fs'), path = require('path'), _u = require('underscore'), inflection = require('../../lib/inflection'), crypto = require('crypto'), less = require('less');
/**
* CssFactory for Less
*/
Expand Down Expand Up @@ -126,7 +126,9 @@ CssFactory.prototype.schemaFor = function (vars) {
*/
CssFactory.prototype.fieldsets = function (vars) {
vars = vars || this.variables;
var fieldsets = [{legend:'Imports', fields:['imports','paths']}];
var fieldsets = [
{legend:'Imports', fields:['imports', 'paths']}
];
_u(vars).each(function (v, k) {
fieldsets.push({legend:inflection.titleize(inflection.humanize(k)), fields:Object.keys(v)})
}, this)
Expand Down Expand Up @@ -182,25 +184,25 @@ CssFactory.prototype.createCache = function (onCreate, variables, isDefault) {
return onCreate(err);

if (isDefault)
self.checksum = id || obj.checksum ;
self.checksum = id || obj.checksum;
obj.id = id || obj.checksum;
var c = self.cache(obj);
onCreate(null, c);
}, variables);

}
CssFactory.prototype.current = function(onSend, cacheId){
CssFactory.prototype.current = function (onSend, cacheId) {
var c = this._cache[cacheId || this.checksum];
if (c){
c.lastAccess = Date.now();
onSend(null, c);
}else{
if (c) {
c.lastAccess = Date.now();
onSend(null, c);
} else {
this.createCache(onSend, {}, true);
}
return this;
}

CssFactory.prototype.getCache = function(id){
CssFactory.prototype.getCache = function (id) {
return this._cache[id];
}

Expand All @@ -216,17 +218,17 @@ CssFactory.prototype.cache = function (obj) {
var valRe = /^(#|px|%|em|in|cm|mm|ex|pt|pc|px)$/;
CssFactory.prototype.createCss = function (onCreate, variables, imports) {
variables = variables || {};
if (!variables.imports){
variables.imports = this.default_imports;
if (!variables.imports) {
variables.imports = this.default_imports;
}
if (!variables.paths){
if (!variables.paths) {
variables.paths = this.paths;
}

var str = this._imports(variables.imports);
_u(variables).each(function onBodyPost(k, v) {
if (k == 'imports' || k == 'paths') return;
if (v && k && _u.isString(v) && _u.isString(k) &! valRe.test(k))
if (v && k && _u.isString(v) && _u.isString(k) & !valRe.test(k))
str.push('@' + v + ': ' + k + ';\n');
});

Expand Down
Loading

0 comments on commit fdc7690

Please sign in to comment.