FEATURE: prefill topic title, body and category via URL

This commit is contained in:
Arpit Jalan 2015-04-28 00:06:02 +05:30
parent 10270593a4
commit cff6e49eb7
6 changed files with 54 additions and 2 deletions

View File

@ -435,6 +435,21 @@ export default DiscourseController.extend({
composerModel.set('composeState', Discourse.Composer.OPEN);
composerModel.set('isWarning', false);
if (opts.topicTitle && opts.topicTitle.length <= this.get('maxTitleLength')) {
this.set('model.title', opts.topicTitle);
}
if (opts.topicCategory) {
var category = Discourse.Category.list().findProperty('name', opts.topicCategory);
if (category && category.id) {
this.set('model.categoryId', category.id);
}
}
if (opts.topicBody) {
this.set('model.reply', opts.topicBody);
}
this.get('controllers.composer-messages').queryFor(composerModel);
},

View File

@ -15,7 +15,17 @@ Discourse.OpenComposer = Em.Mixin.create({
draftKey: controller.get('draft_key'),
draftSequence: controller.get('draft_sequence')
});
},
openComposerWithParams: function(controller, title, body, category) {
this.controllerFor('composer').open({
action: Discourse.Composer.CREATE_TOPIC,
topicTitle: title,
topicBody: body,
topicCategory: category,
draftKey: controller.get('draft_key'),
draftSequence: controller.get('draft_sequence')
});
}
});

View File

@ -90,6 +90,8 @@ export default function() {
this.route('privacy', {path: '/privacy'});
this.route('guidelines', {path: '/guidelines'});
this.route('new-topic', {path: '/new-topic'});
this.resource('badges', function() {
this.route('show', {path: '/:id/:slug'});
});

View File

@ -10,7 +10,7 @@ function unlessReadOnly(method) {
};
}
const ApplicationRoute = Discourse.Route.extend({
const ApplicationRoute = Discourse.Route.extend(Discourse.OpenComposer, {
siteTitle: Discourse.computed.setting('title'),
@ -145,6 +145,10 @@ const ApplicationRoute = Discourse.Route.extend({
factory = this.container.lookupFactory('controller:' + controllerName);
this.render(w, {into: 'modal/topic-bulk-actions', outlet: 'bulkOutlet', controller: factory ? controllerName : 'topic-bulk-actions'});
},
createNewTopicViaParams: function(title, body, category) {
this.openComposerWithParams(this.controllerFor('discovery/topics'), title, body, category);
}
},

View File

@ -0,0 +1,19 @@
export default Discourse.Route.extend({
beforeModel: function(transition) {
const self = this;
if (Discourse.User.current()) {
// User is logged in
self.replaceWith('discovery.latest').then(function(e) {
if (self.controllerFor('navigation/default').get('canCreateTopic')) {
// User can create topic
Ember.run.next(function() {
e.send('createNewTopicViaParams', transition.queryParams.title, transition.queryParams.body, transition.queryParams.category);
});
}
});
} else {
// User is not logged in
self.replaceWith('login');
}
}
});

View File

@ -411,6 +411,8 @@ Discourse::Application.routes.draw do
get 'embed/comments' => 'embed#comments'
get 'embed/count' => 'embed#count'
get "new-topic" => "list#latest"
# Topic routes
get "t/id_for/:slug" => "topics#id_for_slug"
get "t/:slug/:topic_id/wordpress" => "topics#wordpress", constraints: {topic_id: /\d+/}