Vulcan/packages/telescope-posts/lib/routes.js

263 lines
5.7 KiB
JavaScript
Raw Normal View History

/**
* The Posts.controllers namespace
* @namespace Posts.controllers
*/
Posts.controllers = {};
/**
* Controller for all posts lists
*/
Posts.controllers.list = RouteController.extend({
template: "posts_list_controller",
2015-03-22 09:42:58 +09:00
onBeforeAction: function () {
var showViewsNav = (typeof this.showViewsNav === 'undefined') ? true : this.showViewsNav;
if (showViewsNav) {
this.render('posts_list_top', {to: 'postsListTop'});
}
2015-03-22 09:42:58 +09:00
this.next();
},
2015-04-29 14:38:14 +09:00
data: function () {
var terms = {
view: this.view,
limit: this.params.limit || Settings.get('postsPerPage', 10)
};
// console.log('----------------- router running');
2015-04-29 14:38:14 +09:00
// note: the post list controller template will handle all subscriptions, so we just need to pass in the terms
return {
2015-04-29 14:38:14 +09:00
terms: terms
};
},
getTitle: function () {
return i18n.t(this.view);
},
getDescription: function () {
if (Router.current().route.getName() === 'posts_default') { // return site description on root path
2015-03-28 18:30:26 +09:00
return Settings.get('description');
} else {
2015-05-18 11:11:14 +09:00
return i18n.t(_.findWhere(Telescope.menuItems.get("viewsMenu"), {label: this.view}).description);
}
},
2014-11-27 17:25:01 +05:30
fastRender: true
});
2015-02-12 18:57:37 +09:00
var getDefaultViewController = function () {
2015-03-28 18:30:26 +09:00
var defaultView = Settings.get('defaultView', 'top');
// if view we got from settings is available in Posts.views object, use it
if (!!Posts.controllers[defaultView]) {
return Posts.controllers[defaultView];
} else {
return Posts.controllers.top;
}
2015-02-12 18:57:37 +09:00
};
// wrap in startup block to make sure Settings collection is defined
Meteor.startup(function () {
Posts.controllers.default = getDefaultViewController().extend({
getTitle: function () {
2015-03-28 18:30:26 +09:00
var title = Settings.get('title', 'Telescope');
var tagline = Settings.get('tagline');
var fullTitle = !!tagline ? title + ' ' + tagline : title ;
return fullTitle;
2015-02-12 18:57:37 +09:00
}
});
});
/**
* Controller for top view
*/
Posts.controllers.top = Posts.controllers.list.extend({
view: 'top'
});
/**
* Controller for new view
*/
Posts.controllers.new = Posts.controllers.list.extend({
view: 'new'
});
/**
* Controller for best view
*/
Posts.controllers.best = Posts.controllers.list.extend({
view: 'best'
});
/**
* Controller for pending view
*/
Posts.controllers.pending = Posts.controllers.list.extend({
view: 'pending'
});
/**
* Controller for scheduled view
*/
Posts.controllers.scheduled = Posts.controllers.list.extend({
view: 'scheduled'
});
/**
* Controller for single post page
*/
Posts.controllers.page = RouteController.extend({
2015-04-13 16:29:33 +09:00
template: 'post_page',
waitOn: function() {
this.postSubscription = coreSubscriptions.subscribe('singlePost', this.params._id);
this.postUsersSubscription = coreSubscriptions.subscribe('postUsers', this.params._id);
this.commentSubscription = coreSubscriptions.subscribe('commentsList', {view: 'postComments', postId: this.params._id});
},
post: function() {
return Posts.findOne(this.params._id);
},
getTitle: function () {
2014-12-18 15:09:50 +09:00
if (!!this.post())
return this.post().title;
},
2015-06-18 13:04:38 +09:00
onBeforeAction: function () {
if (! this.post()) {
if (this.postSubscription.ready()) {
2015-04-13 16:29:33 +09:00
this.render('not_found');
} else {
2015-04-13 16:29:33 +09:00
this.render('loading');
}
} else {
this.next();
}
},
onRun: function() {
var sessionId = Meteor.default_connection && Meteor.default_connection._lastSessionId ? Meteor.default_connection._lastSessionId : null;
Meteor.call('increasePostViews', this.params._id, sessionId);
2015-01-10 12:46:44 +09:00
this.next();
},
data: function() {
return this.post();
2014-11-27 17:25:01 +05:30
},
2015-06-18 13:04:38 +09:00
onAfterAction: function () {
var post = this.post();
if (post && post.slug !== this.params.slug) {
window.history.replaceState({}, "", post.getPageUrl());
2015-06-18 13:04:38 +09:00
}
},
2014-11-27 17:25:01 +05:30
fastRender: true
});
Meteor.startup(function () {
Router.route('/', {
name: 'posts_default',
controller: Posts.controllers.default
});
Router.route('/top/:limit?', {
name: 'posts_top',
controller: Posts.controllers.top
});
// New
Router.route('/new/:limit?', {
name: 'posts_new',
controller: Posts.controllers.new
});
// Best
Router.route('/best/:limit?', {
name: 'posts_best',
controller: Posts.controllers.best
});
// Pending
Router.route('/pending/:limit?', {
name: 'posts_pending',
controller: Posts.controllers.pending
});
// Scheduled
Router.route('/scheduled/:limit?', {
name: 'posts_scheduled',
controller: Posts.controllers.scheduled
});
// Post Page
2015-06-18 13:04:38 +09:00
// legacy route
Router.route('/posts/:_id', {
2015-06-18 13:04:38 +09:00
name: 'post_page_id',
onBeforeAction: function () {
var post = {
slug: '_',
_id: this.params._id
};
Router.go("post_page", post);
}
});
Router.route('/p/:slug/:_id', {
name: 'post_page',
controller: Posts.controllers.page
});
Router.route('/posts/:_id/comment/:commentId', {
name: 'post_page_comment',
controller: Posts.controllers.page,
onAfterAction: function () {
// TODO: scroll to comment position
}
});
// Post Edit
Router.route('/posts/:_id/edit', {
name: 'post_edit',
2015-04-13 16:29:33 +09:00
template: 'post_edit',
waitOn: function () {
return [
coreSubscriptions.subscribe('singlePost', this.params._id),
coreSubscriptions.subscribe('allUsersAdmin')
];
},
data: function() {
return {
postId: this.params._id,
post: Posts.findOne(this.params._id)
};
},
fastRender: true
});
// Post Submit
Router.route('/submit', {
name: 'post_submit',
2015-04-13 16:29:33 +09:00
template: 'post_submit',
waitOn: function () {
return coreSubscriptions.subscribe('allUsersAdmin');
}
});
2015-03-28 18:30:26 +09:00
});