A platform for community discussion. Free, open, simple.
REFACTOR: move the pinned button to DMenu (#33869)
Kris57bc5466f496
diff --git a/app/assets/javascripts/discourse/app/components/pinned-button.gjs b/app/assets/javascripts/discourse/app/components/pinned-button.gjsnew file mode 100644index 00000000000..49589565e81--- /dev/null+++ b/app/assets/javascripts/discourse/app/components/pinned-button.gjs@@ -0,0 +1,37 @@+import Component from "@glimmer/component";+import { htmlSafe } from "@ember/template";+import PinnedOptions from "discourse/components/pinned-options";+import { i18n } from "discourse-i18n";++export default class PinnedButton extends Component {+ get reasonText() {+ const pinnedGlobally = this.args.topic?.pinned_globally;+ const pinned = this.args.pinned;+ const globally = pinnedGlobally ? "_globally" : "";+ const pinnedKey = pinned ? `pinned${globally}` : "unpinned";+ const key = `topic_statuses.${pinnedKey}.help`;+ return i18n(key);+ }++ get isHidden() {+ const pinned = this.args.pinned;+ const deleted = this.args.topic?.deleted;+ const unpinned = this.args.topic?.unpinned;+ return deleted || (!pinned && !unpinned);+ }++ <template>+ {{#unless this.isHidden}}+ <div class="pinned-button" ...attributes>+ {{#if @appendReason}}+ <p class="reason">+ <PinnedOptions @value={{@pinned}} @topic={{@topic}} />+ <span class="text">{{htmlSafe this.reasonText}}</span>+ </p>+ {{else}}+ <PinnedOptions @value={{@pinned}} @topic={{@topic}} />+ {{/if}}+ </div>+ {{/unless}}+ </template>+}diff --git a/app/assets/javascripts/discourse/app/components/pinned-options.gjs b/app/assets/javascripts/discourse/app/components/pinned-options.gjsnew file mode 100644index 00000000000..183fd59f7a3--- /dev/null+++ b/app/assets/javascripts/discourse/app/components/pinned-options.gjs@@ -0,0 +1,168 @@+import Component from "@glimmer/component";+import { fn } from "@ember/helper";+import { action } from "@ember/object";+import { service } from "@ember/service";+import DButton from "discourse/components/d-button";+import DropdownMenu from "discourse/components/dropdown-menu";+import concatClass from "discourse/helpers/concat-class";+import icon from "discourse/helpers/d-icon";+import { i18n } from "discourse-i18n";+import DMenu from "float-kit/components/d-menu";++const UNPINNED = "unpinned";+const PINNED = "pinned";+const GLOBALLY = "_globally";++class PinnedOptionsTrigger extends Component {+ @service site;++ get showFullTitle() {+ return this.args.showFullTitle ?? true;+ }++ get showCaret() {+ return this.site.desktopView && (this.args.showCaret ?? true);+ }++ get pinnedKey() {+ if (!this.args.value) {+ return UNPINNED;+ }++ if (this.args.topic?.pinned_globally) {+ return `${PINNED}${GLOBALLY}`;+ }++ return PINNED;+ }++ get title() {+ return i18n(`topic_statuses.${this.pinnedKey}.title`);+ }++ get iconName() {+ return this.pinnedKey === UNPINNED ? "thumbtack unpinned" : "thumbtack";+ }++ <template>+ <button+ class={{concatClass+ "btn btn-default"+ (if this.showFullTitle "btn-icon-text" "no-text")+ }}+ ...attributes+ >+ {{icon this.iconName}}++ {{#if this.showFullTitle}}+ <span class="d-button-label">+ {{this.title}}+ </span>+ {{/if}}++ {{#if this.showCaret}}+ {{icon "angle-down" class="pinned-options-btn__caret"}}+ {{/if}}+ </button>+ </template>+}++export default class PinnedOptions extends Component {+ @service site;++ @action+ registerDmenuApi(api) {+ this.dmenuApi = api;+ }++ @action+ async setPinnedState(value) {+ await this.dmenuApi.close({ focusTrigger: true });++ const topic = this.args.topic;+ if (value === UNPINNED) {+ await topic.clearPin();+ } else {+ await topic.rePin();+ }+ }++ @action+ isSelectedClass(optionId) {+ const currentValue = this.args.value ? PINNED : UNPINNED;+ return currentValue === optionId ? "-selected" : "";+ }++ get options() {+ const globally = this.args.topic?.pinned_globally ? GLOBALLY : "";++ return [+ {+ id: PINNED,+ title: i18n(`topic_statuses.pinned${globally}.title`),+ description: i18n(`topic_statuses.pinned${globally}.help`),+ icon: "thumbtack",+ },+ {+ id: UNPINNED,+ title: i18n("topic_statuses.unpinned.title"),+ description: i18n("topic_statuses.unpinned.help"),+ icon: "thumbtack unpinned",+ },+ ];+ }++ <template>+ <DMenu+ @identifier="pinned-options"+ @modalForMobile={{true}}+ @triggerClass={{concatClass+ "btn-default"+ "pinned-options-trigger-btn"+ @triggerClass+ }}+ @contentClass={{@contentClass}}+ @onRegisterApi={{this.registerDmenuApi}}+ @autofocus={{false}}+ @triggerComponent={{component+ PinnedOptionsTrigger+ showFullTitle=@showFullTitle+ showCaret=@showCaret+ value=@value+ topic=@topic+ }}+ ...attributes+ >+ <:content>+ <DropdownMenu as |dropdown|>+ {{#each this.options as |option|}}+ <dropdown.item>+ <DButton+ class={{concatClass+ "pinned-options-btn"+ (this.isSelectedClass option.id)+ }}+ @action={{fn this.setPinnedState option.id}}+ data-pinned-state={{option.id}}+ >+ <div class="pinned-options-btn__icons">+ {{icon option.icon}}+ </div>+ <div class="pinned-options-btn__texts">+ <span class="pinned-options-btn__label">+ {{option.title}}+ </span>+ {{#if option.description}}+ <span class="pinned-options-btn__description">+ {{option.description}}+ </span>+ {{/if}}+ </div>+ </DButton>+ </dropdown.item>+ {{/each}}+ </DropdownMenu>+ </:content>+ </DMenu>+ </template>+}diff --git a/app/assets/javascripts/discourse/app/components/topic-footer-buttons.gjs b/app/assets/javascripts/discourse/app/components/topic-footer-buttons.gjsindex cf89ad15318..128b4599918 100644--- a/app/assets/javascripts/discourse/app/components/topic-footer-buttons.gjs+++ b/app/assets/javascripts/discourse/app/components/topic-footer-buttons.gjs@@ -9,6 +9,7 @@ import { eq, gt } from "truth-helpers";import BookmarkMenu from "discourse/components/bookmark-menu";import DButton from "discourse/components/d-button";import DropdownMenu from "discourse/components/dropdown-menu";+import PinnedButton from "discourse/components/pinned-button";import PluginOutlet from "discourse/components/plugin-outlet";import TopicAdminMenu from "discourse/components/topic-admin-menu";import UserTip from "discourse/components/user-tip";@@ -22,7 +23,6 @@ import { getTopicFooterDropdowns } from "discourse/lib/register-topic-footer-droimport TopicBookmarkManager from "discourse/lib/topic-bookmark-manager";import { i18n } from "discourse-i18n";import DropdownSelectBox from "select-kit/components/dropdown-select-box";-import PinnedButton from "select-kit/components/pinned-button";import TopicNotificationsButton from "select-kit/components/topic-notifications-button";import DMenu from "float-kit/components/d-menu";diff --git a/app/assets/javascripts/discourse/tests/integration/components/select-kit/pinned-options-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/pinned-options-test.gjssimilarity index 59%rename from app/assets/javascripts/discourse/tests/integration/components/select-kit/pinned-options-test.gjsrename to app/assets/javascripts/discourse/tests/integration/components/pinned-options-test.gjsindex 2036c71b251..46295a8e6d6 100644--- a/app/assets/javascripts/discourse/tests/integration/components/select-kit/pinned-options-test.gjs+++ b/app/assets/javascripts/discourse/tests/integration/components/pinned-options-test.gjs@@ -1,18 +1,16 @@import { getOwner } from "@ember/owner";-import { render } from "@ember/test-helpers";+import { click, render } from "@ember/test-helpers";import { module, test } from "qunit";+import PinnedOptions from "discourse/components/pinned-options";import { setupRenderingTest } from "discourse/tests/helpers/component-test";-import selectKit from "discourse/tests/helpers/select-kit-helper";-import PinnedOptions from "select-kit/components/pinned-options";-module("Integration | Component | select-kit/pinned-options", function (hooks) {+module("Integration | Component | pinned-options", function (hooks) {setupRenderingTest(hooks);test("unpinning", async function (assert) {const self = this;this.siteSettings.automatically_unpin_topics = false;- this.set("subject", selectKit());const store = getOwner(this).lookup("service:store");this.set(@@ -31,19 +29,21 @@ module("Integration | Component | select-kit/pinned-options", function (hooks) {</template>);- assert.strictEqual(this.subject.header().name(), "pinned");+ assert.dom(".pinned-options-trigger-btn .d-button-label").hasText("Pinned");- await this.subject.expand();- await this.subject.selectRowByValue("unpinned");+ await click(".pinned-options-trigger-btn");- assert.strictEqual(this.subject.header().name(), "unpinned");+ await click('[data-pinned-state="unpinned"]');++ assert.false(this.topic.pinned, "topic should be unpinned");+ assert.true(this.topic.unpinned, "topic should be marked as unpinned");});test("pinning", async function (assert) {const self = this;this.siteSettings.automatically_unpin_topics = false;- this.set("subject", selectKit());+const store = getOwner(this).lookup("service:store");this.set("topic",@@ -52,6 +52,7 @@ module("Integration | Component | select-kit/pinned-options", function (hooks) {title: "Qunit Test Topic",deleted_at: new Date(),pinned: false,+ unpinned: true,}));@@ -61,11 +62,15 @@ module("Integration | Component | select-kit/pinned-options", function (hooks) {</template>);- assert.strictEqual(this.subject.header().name(), "unpinned");+ assert+ .dom(".pinned-options-trigger-btn .d-button-label")+ .hasText("Unpinned");++ await click(".pinned-options-trigger-btn");- await this.subject.expand();- await this.subject.selectRowByValue("pinned");+ await click('[data-pinned-state="pinned"]');- assert.strictEqual(this.subject.header().name(), "pinned");+ assert.true(this.topic.pinned, "topic is pinned");+ assert.false(this.topic.unpinned, "topic isn't unpinned");});});diff --git a/app/assets/javascripts/select-kit/addon/components/pinned-button.gjs b/app/assets/javascripts/select-kit/addon/components/pinned-button.gjsdeleted file mode 100644index a70603cb44f..00000000000--- a/app/assets/javascripts/select-kit/addon/components/pinned-button.gjs+++ /dev/null@@ -1,40 +0,0 @@-/* eslint-disable ember/no-classic-components */-import Component from "@ember/component";-import { htmlSafe } from "@ember/template";-import { classNameBindings, classNames } from "@ember-decorators/component";-import discourseComputed from "discourse/lib/decorators";-import { i18n } from "discourse-i18n";-import PinnedOptions from "select-kit/components/pinned-options";-import { pluginApiIdentifiers } from "select-kit/components/select-kit";--@classNames("pinned-button")-@classNameBindings("isHidden")-@pluginApiIdentifiers("pinned-button")-export default class PinnedButton extends Component {- descriptionKey = "help";- appendReason = true;-- @discourseComputed("topic.pinned_globally", "pinned")- reasonText(pinnedGlobally, pinned) {- const globally = pinnedGlobally ? "_globally" : "";- const pinnedKey = pinned ? `pinned${globally}` : "unpinned";- const key = `topic_statuses.${pinnedKey}.help`;- return i18n(key);- }-- @discourseComputed("pinned", "topic.deleted", "topic.unpinned")- isHidden(pinned, deleted, unpinned) {- return deleted || (!pinned && !unpinned);- }-- <template>- {{#if this.appendReason}}- <p class="reason">- <PinnedOptions @value={{this.pinned}} @topic={{this.topic}} />- <span class="text">{{htmlSafe this.reasonText}}</span>- </p>- {{else}}- <PinnedOptions @value={{this.pinned}} @topic={{this.topic}} />- {{/if}}- </template>-}diff --git a/app/assets/javascripts/select-kit/addon/components/pinned-options.js b/app/assets/javascripts/select-kit/addon/components/pinned-options.jsdeleted file mode 100644index c0ff7be80c1..00000000000--- a/app/assets/javascripts/select-kit/addon/components/pinned-options.js+++ /dev/null@@ -1,68 +0,0 @@-import { action, computed } from "@ember/object";-import { htmlSafe } from "@ember/template";-import { classNames } from "@ember-decorators/component";-import { i18n } from "discourse-i18n";-import DropdownSelectBoxComponent from "select-kit/components/dropdown-select-box";-import {- pluginApiIdentifiers,- selectKitOptions,-} from "select-kit/components/select-kit";--const UNPINNED = "unpinned";-const PINNED = "pinned";--@classNames("pinned-options")-@selectKitOptions({- showCaret: true,-})-@pluginApiIdentifiers("pinned-options")-export default class PinnedOptions extends DropdownSelectBoxComponent {- modifySelection(content) {- const pinnedGlobally = this.get("topic.pinned_globally");- const pinned = this.value;- const globally = pinnedGlobally ? "_globally" : "";- const state = pinned ? `pinned${globally}` : UNPINNED;- const title = i18n(`topic_statuses.${state}.title`);-- content.label = htmlSafe(`<span>${title}</span>`);- content.title = title;- content.name = state;- content.icon = `thumbtack${state === UNPINNED ? " unpinned" : ""}`;- return content;- }-- @computed- get content() {- const globally = this.topic.pinned_globally ? "_globally" : "";-- return [- {- id: PINNED,- name: i18n(`topic_statuses.pinned${globally}.title`),- description: this.site.mobileView- ? null- : i18n(`topic_statuses.pinned${globally}.help`),- icon: "thumbtack",- },- {- id: UNPINNED,- name: i18n("topic_statuses.unpinned.title"),- icon: "thumbtack unpinned",- description: this.site.mobileView- ? null- : i18n("topic_statuses.unpinned.help"),- },- ];- }-- @action- onChange(value) {- const topic = this.topic;-- if (value === UNPINNED) {- return topic.clearPin();- } else {- return topic.rePin();- }- }-}diff --git a/app/assets/stylesheets/common/components/notifications-tracking.scss b/app/assets/stylesheets/common/components/notifications-tracking.scssindex 3e7ce0f839a..5c2abfd4649 100644--- a/app/assets/stylesheets/common/components/notifications-tracking.scss+++ b/app/assets/stylesheets/common/components/notifications-tracking.scss@@ -1,3 +1,4 @@+.pinned-options-btn,.notifications-tracking-btn {display: flex;flex: 1 0 auto;diff --git a/app/assets/stylesheets/common/select-kit/_index.scss b/app/assets/stylesheets/common/select-kit/_index.scssindex de03e0e8c49..d08a15d42d5 100644--- a/app/assets/stylesheets/common/select-kit/_index.scss+++ b/app/assets/stylesheets/common/select-kit/_index.scss@@ -17,7 +17,6 @@@import "notifications-button";@import "notifications-filter";@import "period-chooser";-@import "pinned-button";@import "select-kit";@import "single-select";@import "tag-chooser";diff --git a/app/assets/stylesheets/common/select-kit/pinned-button.scss b/app/assets/stylesheets/common/select-kit/pinned-button.scssdeleted file mode 100644index 3e51092ae39..00000000000--- a/app/assets/stylesheets/common/select-kit/pinned-button.scss+++ /dev/null@@ -1,7 +0,0 @@-#topic-footer-buttons {- .pinned-button {- &.is-hidden {- display: none;- }- }-}diff --git a/plugins/styleguide/assets/javascripts/discourse/components/sections/atoms/dropdowns.gjs b/plugins/styleguide/assets/javascripts/discourse/components/sections/atoms/dropdowns.gjsindex d4a4ddbffc0..b0a11851afa 100644--- a/plugins/styleguide/assets/javascripts/discourse/components/sections/atoms/dropdowns.gjs+++ b/plugins/styleguide/assets/javascripts/discourse/components/sections/atoms/dropdowns.gjs@@ -1,5 +1,7 @@import { fn, get, hash } from "@ember/helper";import CategoryNotificationsTracking from "discourse/components/category-notifications-tracking";+import PinnedButton from "discourse/components/pinned-button";+import PinnedOptions from "discourse/components/pinned-options";import TopicNotificationsTracking from "discourse/components/topic-notifications-tracking";import CategoriesAdminDropdown from "select-kit/components/categories-admin-dropdown";import CategoryChooser from "select-kit/components/category-chooser";@@ -11,8 +13,6 @@ import IconPicker from "select-kit/components/icon-picker";import ListSetting from "select-kit/components/list-setting";import MiniTagChooser from "select-kit/components/mini-tag-chooser";import MultiSelect from "select-kit/components/multi-select";-import PinnedButton from "select-kit/components/pinned-button";-import PinnedOptions from "select-kit/components/pinned-options";import UserNotificationsDropdown from "select-kit/components/user-notifications-dropdown";import StyleguideExample from "discourse/plugins/styleguide/discourse/components/styleguide-example";