From 36ddb1787e70d71f55ec0917bed639aac4c85513 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 27 Jul 2016 17:50:13 +0800 Subject: [PATCH] FEATURE: Add toggle topic visibility button in popup menu. --- .../discourse/controllers/composer.js.es6 | 45 ++++++++++++++-- .../discourse/models/composer.js.es6 | 3 ++ .../discourse/templates/composer.hbs | 8 +-- app/controllers/posts_controller.rb | 2 + config/locales/client.en.yml | 2 + lib/topic_creator.rb | 5 +- .../acceptance/composer-test.js.es6 | 53 ++++++++++++++++++- 7 files changed, 107 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6 index f896464a1..b89d61a98 100644 --- a/app/assets/javascripts/discourse/controllers/composer.js.es6 +++ b/app/assets/javascripts/discourse/controllers/composer.js.es6 @@ -63,6 +63,7 @@ export default Ember.Controller.extend({ isUploading: false, topic: null, linkLookup: null, + whisperOrUnlistTopic: Ember.computed.or('model.whisper', 'model.unlistTopic'), @computed('model.replyingToTopic', 'model.creatingPrivateMessage', 'model.targetUsernames') focusTarget(replyingToTopic, creatingPM, usernames) { @@ -109,10 +110,26 @@ export default Ember.Controller.extend({ !creatingPrivateMessage; }, - @computed('model.action') - canWhisper(action) { + @computed('model.whisper', 'model.unlistTopic') + whisperOrUnlistTopicText(whisper, unlistTopic) { + if (whisper) { + return I18n.t("composer.whisper"); + } else if (unlistTopic) { + return I18n.t("composer.unlist"); + } + }, + + @computed + isStaffUser() { const currentUser = this.currentUser; - return currentUser && currentUser.get('staff') && this.siteSettings.enable_whispers && action === Composer.REPLY; + return currentUser && currentUser.get('staff'); + }, + + canUnlistTopic: Em.computed.and('model.creatingTopic', 'isStaffUser'), + + @computed('model.action', 'isStaffUser') + canWhisper(action, isStaffUser) { + return isStaffUser && this.siteSettings.enable_whispers && action === Composer.REPLY; }, @computed("popupMenuOptions") @@ -132,11 +149,20 @@ export default Ember.Controller.extend({ return option; }, - @computed("model.composeState") + @computed("model.composeState", "model.creatingTopic") popupMenuOptions(composeState) { if (composeState === 'open') { let options = []; + options.push(this._setupPopupMenuOption(() => { + return { + action: 'toggleInvisible', + icon: 'eye-slash', + label: 'composer.toggle_unlisted', + condition: "canUnlistTopic" + }; + })); + options.push(this._setupPopupMenuOption(() => { return { action: 'toggleWhisper', @@ -210,6 +236,10 @@ export default Ember.Controller.extend({ this.toggleProperty('model.whisper'); }, + toggleInvisible() { + this.toggleProperty('model.unlistTopic'); + }, + toggleToolbar() { this.toggleProperty('showToolbar'); }, @@ -503,7 +533,6 @@ export default Ember.Controller.extend({ this.set('scopedCategoryId', opts.categoryId); } - this.setProperties({ showEditReason: false, editReason: null }); // If we want a different draft than the current composer, close it and clear our model. @@ -545,6 +574,12 @@ export default Ember.Controller.extend({ }).then(resolve, reject); } + if (composerModel) { + if (composerModel.get('action') !== opts.action) { + composerModel.setProperties({ unlistTopic: false, whisper: false }); + } + } + self._setModel(composerModel, opts); resolve(); }); diff --git a/app/assets/javascripts/discourse/models/composer.js.es6 b/app/assets/javascripts/discourse/models/composer.js.es6 index 9ecc0beef..738ec9d95 100644 --- a/app/assets/javascripts/discourse/models/composer.js.es6 +++ b/app/assets/javascripts/discourse/models/composer.js.es6 @@ -22,6 +22,7 @@ const CLOSED = 'closed', _create_serializer = { raw: 'reply', title: 'title', + unlist_topic: 'unlistTopic', category: 'categoryId', topic_id: 'topic.id', is_warning: 'isWarning', @@ -41,6 +42,7 @@ const CLOSED = 'closed', const Composer = RestModel.extend({ _categoryId: null, + unlistTopic: false, archetypes: function() { return this.site.get('archetypes'); @@ -504,6 +506,7 @@ const Composer = RestModel.extend({ reply: null, post: null, title: null, + unlistTopic: false, editReason: null, stagedPost: false, typingTime: 0, diff --git a/app/assets/javascripts/discourse/templates/composer.hbs b/app/assets/javascripts/discourse/templates/composer.hbs index acb26b185..2551444d5 100644 --- a/app/assets/javascripts/discourse/templates/composer.hbs +++ b/app/assets/javascripts/discourse/templates/composer.hbs @@ -29,8 +29,8 @@
{{{model.actionTitle}}} {{#unless site.mobileView}} - {{#if model.whisper}} - ({{i18n "composer.whisper"}}) + {{#if whisperOrUnlistTopicText}} + ({{whisperOrUnlistTopicText}}) {{/if}} {{/unless}} @@ -106,8 +106,8 @@ {{i18n 'cancel'}} {{#if site.mobileView}} - {{#if model.whisper}} - + {{#if whisperOrUnlistTopic}} + {{/if}} {{/if}}
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 4caabb1ab..229015e1e 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -554,10 +554,12 @@ class PostsController < ApplicationController :auto_track, :typing_duration_msecs, :composer_open_duration_msecs, + :visible ] # param munging for WordPress params[:auto_track] = !(params[:auto_track].to_s == "false") if params[:auto_track] + params[:visible] = (params[:unlist_topic].to_s == "false") if params[:unlist_topic] if api_key_valid? # php seems to be sending this incorrectly, don't fight with it diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 4db4f7f2c..1609e9ba6 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1016,9 +1016,11 @@ en: more_emoji: "more..." options: "Options" whisper: "whisper" + unlist: "unlisted" add_warning: "This is an official warning." toggle_whisper: "Toggle Whisper" + toggle_unlisted: "Toggle Unlisted" posting_not_on_topic: "Which topic do you want to reply to?" saving_draft_tip: "saving..." saved_draft_tip: "saved" diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index da0f7dcc1..9e76327a5 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -93,10 +93,13 @@ class TopicCreator end def setup_topic_params + @opts[:visible] = true if @opts[:visible].nil? + topic_params = { title: @opts[:title], user_id: @user.id, - last_post_user_id: @user.id + last_post_user_id: @user.id, + visible: @opts[:visible] } [:subtype, :archetype, :meta_data, :import_mode].each do |key| diff --git a/test/javascripts/acceptance/composer-test.js.es6 b/test/javascripts/acceptance/composer-test.js.es6 index 38789579c..fe086a680 100644 --- a/test/javascripts/acceptance/composer-test.js.es6 +++ b/test/javascripts/acceptance/composer-test.js.es6 @@ -1,6 +1,11 @@ import { acceptance } from "helpers/qunit-helpers"; -acceptance("Composer", { loggedIn: true }); +acceptance("Composer", { + loggedIn: true, + settings: { + enable_whispers: true + } +}); test("Tests the Composer controls", () => { visit("/"); @@ -254,6 +259,52 @@ test("Composer can toggle between edit and reply", () => { }); }); +test("Composer can toggle between reply and createTopic", () => { + visit("/t/this-is-a-test-topic/9"); + + click('.topic-post:eq(0) button.reply'); + click('button.options'); + click('.popup-menu .fa-eye-slash'); + andThen(() => { + ok( + find('.composer-fields .whisper').text().indexOf(I18n.t("composer.whisper")) > 0, + 'it sets the post type to whisper' + ); + }); + + visit("/"); + andThen(() => { + ok(exists('#create-topic'), 'the create topic button is visible'); + }); + + click('#create-topic'); + andThen(() => { + ok( + find('.composer-fields .whisper').text().indexOf(I18n.t("composer.whisper")) === -1, + "it should reset the state of the composer's model" + ); + }); + + click('button.options'); + click('.popup-menu .fa-eye-slash'); + andThen(() => { + ok( + find('.composer-fields .whisper').text().indexOf(I18n.t("composer.unlist")) > 0, + 'it sets the topic to unlisted' + ); + }); + + visit("/t/this-is-a-test-topic/9"); + + click('.topic-post:eq(0) button.reply'); + andThen(() => { + ok( + find('.composer-fields .whisper').text().indexOf(I18n.t("composer.unlist")) === -1, + "it should reset the state of the composer's model" + ); + }); +}); + test("Composer with dirty reply can toggle to edit", () => { visit("/t/this-is-a-test-topic/9");