Permalink
Browse files

implement menu listing function

  • Loading branch information...
1 parent 95dcac2 commit 4927c2b2ad45960581136b19f999d9fd62bf3851 @yssk22 committed May 7, 2011
Showing with 47 additions and 13 deletions.
  1. +4 −0 apps/posts/app.js
  2. +4 −0 apps/sunrise/app.js
  3. +32 −11 lib/site.js
  4. +3 −0 template/_attachments/templates/layout.ejs
  5. +4 −2 template/app.js
View
4 apps/posts/app.js
@@ -8,6 +8,10 @@ var raiseError = require('sunrise').raiseError;
var ddoc = {
_id: "_design/posts" ,
+ name: 'Posts',
+ version: '0.1.0',
+ description: 'A simple blog application for sunrise',
+ author: 'Yohei Sasaki',
views: {},
shows: {},
lists: {},
View
4 apps/sunrise/app.js
@@ -6,6 +6,10 @@ var auth = require('sunrise').middleware.auth;
var ddoc = {
_id: "_design/sunrise" ,
+ version: require('sunrise').version,
+ name: 'System',
+ description: 'Sunrise system application',
+ author: 'Yohei Sasaki',
views: {},
shows: {},
lists: {}
View
43 lib/site.js
@@ -65,31 +65,56 @@ exports.createSite = function(sitepath, options){
var site = createApp(abspath(sitepath), {
middlewares: middleware
});
+ site._installedApps = {};
+ site.error(require('./middleware/error')(site.set('errors')));
+ site.helpers(require('./helpers'));
+ site.dynamicHelpers(require('./dynamicHelpers'));
/**
* Install application on the given url.
*/
site.install = function(apppath, rootUrl, options){
- site._installedApps = site._installedApps || {};
options = merge({
- rootUrl: rootUrl
+ rootUrl: rootUrl,
+ menuOrder: Object.keys(site._installedApps).length
}, options);
var app = createApp(apppath, options);
+ app._installOptions = options;
app.init();
site.use(rootUrl, app);
- // merge site level helpers
- app.helpers(require('./helpers'));
- app.dynamicHelpers(require('./dynamicHelpers'));
// merge helpers
site._installedApps[app.set('home')] = app;
+ // update installedApps helper for view.
+ site.helpers({
+ installedApps: Object.keys(site._installedApps).map(function(home){
+ var app = site._installedApps[home];
+ var ddoc = app.ddoc;
+ return {
+ name: ddoc.name || ddoc._id.split('/')[1],
+ description: ddoc.description,
+ version: ddoc.version,
+ author: ddoc.author,
+ home: app._installOptions.home || home,
+ menuOrder: app._installOptions.menuOrder,
+ menuName: app._installOptions.menuName || ddoc.name || ddoc._id.split('/')[1]
+ };
+ }).sort(function(a, b){ return a.menuOrder > b.menuOrder; })
+ });
+
+ // update helpers in all applicaitons
+ Object.keys(site._installedApps).forEach(function(k){
+ var app = site._installedApps[k];
+ app.helpers(site.viewHelpers);
+ app.dynamicHelpers(site.dynamicViewHelpers);
+ });
return app;
};
/**
- * Deploy all installed applicationsx
+ * Deploy all installed applications
*/
site.deployAll = function(cb){
- var apps = Object.keys(site._installedApps || {}).map(function(k){
+ var apps = Object.keys(site._installedApps).map(function(k){
return site._installedApps[k];
});
apps.unshift(site);
@@ -98,10 +123,6 @@ exports.createSite = function(sitepath, options){
parallel.apply(this, funs);
};
- site.error(require('./middleware/error')(site.set('errors')));
- site.helpers(require('./helpers'));
- site.dynamicHelpers(require('./dynamicHelpers'));
-
site.init();
return site;
};
View
3 template/_attachments/templates/layout.ejs
@@ -35,6 +35,9 @@
<nav id="siteNavigation" class="clear">
<ul>
<li><a href="/">Home</a></li>
+ <% installedApps.forEach(function(appInfo){ %>
+ <li><a href="<%= appInfo.home %>"><%= appInfo.menuName %></a></li>
+ <% }) %>
</ul>
</nav>
</header>
View
6 template/app.js
@@ -20,11 +20,13 @@ ddoc.init = function(site){
});
+ // install applications as you like
+ // site.install('posts', '/posts/', {database: 'posts'});
+ site.install('posts', '/posts/', {database: 'posts'});
+
// install system application
site.install('sunrise', '/sunrise/');
- // install applications as you like
- // site.install('posts', '/posts/', {database: 'posts'});
};
//

0 comments on commit 4927c2b

Please sign in to comment.