From c681b353f23522c71fdea1eb2f968d0d84fde9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 12 Jan 2015 12:10:15 +0100 Subject: [PATCH] FEATURE: bookmark topic button --- .../discourse/controllers/topic.js.es6 | 13 ++++--- .../discourse/lib/keyboard_shortcuts.js | 14 +++---- .../javascripts/discourse/models/_post.js | 37 +++++++++++-------- .../javascripts/discourse/models/topic.js | 15 +++++--- .../modal/keyboard_shortcuts_help.hbs | 2 +- .../discourse/views/bookmark-button.js.es6 | 19 ++++++++++ .../views/topic-footer-buttons.js.es6 | 2 + app/controllers/posts_controller.rb | 12 +++--- app/controllers/topics_controller.rb | 16 +++++++- app/models/post_action_type.rb | 10 ++++- app/models/topic_user.rb | 1 - app/serializers/topic_view_serializer.rb | 7 +++- config/locales/client.en.yml | 2 +- config/routes.rb | 1 + 14 files changed, 103 insertions(+), 48 deletions(-) create mode 100644 app/assets/javascripts/discourse/views/bookmark-button.js.es6 diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6 index 672ea3313..0bb6ed3e0 100644 --- a/app/assets/javascripts/discourse/controllers/topic.js.es6 +++ b/app/assets/javascripts/discourse/controllers/topic.js.es6 @@ -213,8 +213,11 @@ export default ObjectController.extend(Discourse.SelectedPostsCount, BufferedCon alert(I18n.t("bookmarks.not_bookmarked")); return; } - post.toggleProperty('bookmarked'); - return false; + if (post) { + return post.toggleBookmark(); + } else { + return this.get("model").toggleBookmark(); + } }, jumpTop: function() { @@ -547,15 +550,13 @@ export default ObjectController.extend(Discourse.SelectedPostsCount, BufferedCon // Receive notifications for this topic subscribe: function() { - // Unsubscribe before subscribing again this.unsubscribe(); - var bus = Discourse.MessageBus; - var topicController = this; - bus.subscribe("/topic/" + (this.get('id')), function(data) { + Discourse.MessageBus.subscribe("/topic/" + this.get('id'), function(data) { var topic = topicController.get('model'); + if (data.notification_level_change) { topic.set('details.notification_level', data.notification_level_change); topic.set('details.notifications_reason_id', data.notifications_reason_id); diff --git a/app/assets/javascripts/discourse/lib/keyboard_shortcuts.js b/app/assets/javascripts/discourse/lib/keyboard_shortcuts.js index d7c6d0c14..f96e7fd33 100644 --- a/app/assets/javascripts/discourse/lib/keyboard_shortcuts.js +++ b/app/assets/javascripts/discourse/lib/keyboard_shortcuts.js @@ -18,15 +18,13 @@ var PATH_BINDINGS = { }, CLICK_BINDINGS = { - // star topic - 'f': '#topic-footer-buttons button.star, .topic-list tr.topic-list-item.selected a.star', - + 'f': '#topic-footer-buttons button.bookmark', // bookmark topic 'm m': 'div.notification-options li[data-id="0"] a', // mark topic as muted 'm r': 'div.notification-options li[data-id="1"] a', // mark topic as regular 'm t': 'div.notification-options li[data-id="2"] a', // mark topic as tracking 'm w': 'div.notification-options li[data-id="3"] a', // mark topic as watching 'x r': '#dismiss-new,#dismiss-new-top,#dismiss-posts,#dismiss-posts-top', // dismiss new/posts - 'x t': '#dismiss-topics,#dismiss-topics-top', //dismiss topics + 'x t': '#dismiss-topics,#dismiss-topics-top', // dismiss topics '.': '.alert.alert-info.clickable', // show incoming/updated topics 'n': '#user-notifications', // open notifications menu 'o,enter': '.topic-list tr.selected a.title', // open selected topic @@ -36,7 +34,7 @@ var PATH_BINDINGS = { }, FUNCTION_BINDINGS = { - 'c': 'createTopic', // create new topic + 'c': 'createTopic', // create new topic 'home': 'goToFirstPost', '#': 'toggleProgress', 'end': 'goToLastPost', @@ -47,11 +45,11 @@ var PATH_BINDINGS = { 'k': 'selectUp', 'u': 'goBack', '/': 'showSearch', - '=': 'showSiteMap', // open site map menu - 'p': 'showCurrentUser', // open current user menu + '=': 'showSiteMap', // open site map menu + 'p': 'showCurrentUser', // open current user menu 'ctrl+f': 'showBuiltinSearch', 'command+f': 'showBuiltinSearch', - '?': 'showHelpModal', // open keyboard shortcut help + '?': 'showHelpModal', // open keyboard shortcut help 'q': 'quoteReply' }; diff --git a/app/assets/javascripts/discourse/models/_post.js b/app/assets/javascripts/discourse/models/_post.js index 686e8230f..9df35ab8d 100644 --- a/app/assets/javascripts/discourse/models/_post.js +++ b/app/assets/javascripts/discourse/models/_post.js @@ -68,17 +68,6 @@ Discourse.Post = Discourse.Model.extend({ return this.get("user_id") === Discourse.User.currentProp("id") || Discourse.User.currentProp('staff'); }.property("user_id"), - bookmarkedChanged: function() { - Discourse.Post.bookmark(this.get('id'), this.get('bookmarked')) - .then(null, function (error) { - if (error && error.responseText) { - bootbox.alert($.parseJSON(error.responseText).errors[0]); - } else { - bootbox.alert(I18n.t('generic_error')); - } - }); - }.observes('bookmarked'), - wikiChanged: function() { var data = { wiki: this.get("wiki") }; this._updatePost("wiki", data); @@ -421,6 +410,28 @@ Discourse.Post = Discourse.Model.extend({ unhide: function () { return Discourse.ajax("/posts/" + this.get("id") + "/unhide", { type: "PUT" }); + }, + + toggleBookmark: function() { + var self = this; + + this.toggleProperty("bookmarked"); + if (this.get("post_number") === 1) { this.toggleProperty("topic.bookmarked"); } + + return Discourse.ajax("/posts/" + this.get("id") + "/bookmark", { + type: 'PUT', + data: { bookmarked: this.get("bookmarked") } + }).then(null, function (error) { + + self.toggleProperty("bookmarked"); + if (this.get("post_number") === 1) { this.toggleProperty("topic.bookmarked"); } + + if (error && error.responseText) { + bootbox.alert($.parseJSON(error.responseText).errors[0]); + } else { + bootbox.alert(I18n.t('generic_error')); + } + }); } }); @@ -491,10 +502,6 @@ Discourse.Post.reopenClass({ return Discourse.ajax("/posts/" + postId + ".json").then(function (result) { return Discourse.Post.create(result); }); - }, - - bookmark: function(postId, bookmarked) { - return Discourse.ajax("/posts/" + postId + "/bookmark", { type: 'PUT', data: { bookmarked: bookmarked } }); } }); diff --git a/app/assets/javascripts/discourse/models/topic.js b/app/assets/javascripts/discourse/models/topic.js index f4b13f818..dc20e4430 100644 --- a/app/assets/javascripts/discourse/models/topic.js +++ b/app/assets/javascripts/discourse/models/topic.js @@ -181,14 +181,17 @@ Discourse.Topic = Discourse.Model.extend({ }.property('word_count'), toggleBookmark: function() { - var topic = this; - topic.toggleProperty('bookmarked'); - return Discourse.ajax({ - url: "" + (this.get('url')) + "/bookmark", + var self = this, firstPost = this.get("postStream.posts")[0]; + + this.toggleProperty('bookmarked'); + if (this.get("postStream.firstPostPresent")) { firstPost.toggleProperty("bookmarked"); } + + return Discourse.ajax('/t/' + this.get('id') + '/bookmark', { type: 'PUT', - data: { bookmarked: topic.get('bookmarked') ? true : false } + data: { bookmarked: self.get('bookmarked') } }).then(null, function (error) { - topic.toggleProperty('bookmarked'); + self.toggleProperty('bookmarked'); + if (self.get("postStream.firstPostPresent")) { firstPost.toggleProperty('bookmarked'); } if (error && error.responseText) { bootbox.alert($.parseJSON(error.responseText).errors); diff --git a/app/assets/javascripts/discourse/templates/modal/keyboard_shortcuts_help.hbs b/app/assets/javascripts/discourse/templates/modal/keyboard_shortcuts_help.hbs index c13311cea..2b6ec44f5 100644 --- a/app/assets/javascripts/discourse/templates/modal/keyboard_shortcuts_help.hbs +++ b/app/assets/javascripts/discourse/templates/modal/keyboard_shortcuts_help.hbs @@ -36,7 +36,7 @@

{{i18n 'keyboard_shortcuts_help.actions.title'}}