Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: restyle topic voting component, add voting to docked header (#39234)

Krisf5a29c690567
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs
index 38b82c03f7e..963dff45acc 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs
@@ -1,21 +1,14 @@
import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import { service } from "@ember/service";
-import concatClass from "discourse/helpers/concat-class";
-import routeAction from "discourse/helpers/route-action";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import VoteButton from "./vote-button";
import VoteCount from "./vote-count";
export default class VoteBox extends Component {
- @service siteSettings;
@service currentUser;
- @tracked votesAlert;
- @tracked allowClick = true;
-
@action
addVote() {
let topic = this.args.topic;
@@ -31,8 +24,6 @@ export default class VoteBox extends Component {
this.currentUser.votes_exceeded = !result.can_vote;
this.currentUser.vote_limit = result.vote_limit;
this.currentUser.votes_left = result.votes_left;
- this.votesAlert = result.alert;
- this.allowClick = true;
})
.catch(popupAjaxError);
}
@@ -53,33 +44,18 @@ export default class VoteBox extends Component {
this.currentUser.votes_exceeded = !result.can_vote;
this.currentUser.vote_limit = result.vote_limit;
this.currentUser.votes_left = result.votes_left;
- this.allowClick = true;
})
.catch(popupAjaxError);
}
- @action
- closeVotesAlert() {
- this.votesAlert = null;
- }
-
<template>
- <div
- class={{concatClass
- "voting-wrapper"
- (if this.siteSettings.topic_voting_show_who_voted "show-pointer")
- }}
- >
- <VoteCount @topic={{@topic}} @showLogin={{routeAction "showLogin"}} />
+ <div class="voting-wrapper">
<VoteButton
@topic={{@topic}}
- @allowClick={{this.allowClick}}
- @showVoteOptions={{this.showVoteOptions}}
@addVote={{this.addVote}}
- @showLogin={{routeAction "showLogin"}}
@removeVote={{this.removeVote}}
/>
-
+ <VoteCount @topic={{@topic}} />
</div>
</template>
}
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs
index 99906876c20..2943c18e1b0 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs
@@ -5,6 +5,7 @@ import { service } from "@ember/service";
import DButton from "discourse/components/d-button";
import DropdownMenu from "discourse/components/dropdown-menu";
import DMenu from "discourse/float-kit/components/d-menu";
+import DTooltip from "discourse/float-kit/components/d-tooltip";
import icon from "discourse/helpers/d-icon";
import { applyBehaviorTransformer } from "discourse/lib/transformer";
import { and, eq, not } from "discourse/truth-helpers";
@@ -12,6 +13,7 @@ import { i18n } from "discourse-i18n";
export default class VoteBox extends Component {
@service currentUser;
+ @service router;
@tracked hasVoted = false;
@tracked hasSeenSuccessMenu = false;
@@ -20,31 +22,8 @@ export default class VoteBox extends Component {
return this.args.topic;
}
- get buttonContent() {
- const content = {};
- if (this.currentUser) {
- if (this.topic.closed) {
- content.label = i18n("topic_voting.voting_closed_title");
- content.title = i18n("topic_voting.voting_closed_title");
- } else if (this.topic.user_voted) {
- content.label = i18n("topic_voting.voted_title");
- content.title = i18n("topic_voting.voted_title");
- } else if (this.currentUser.vote_limit === 0) {
- content.label = i18n("topic_voting.locked");
- content.title = i18n("topic_voting.locked_description");
- } else if (this.currentUser.votes_exceeded) {
- content.label = i18n("topic_voting.voting_limit");
- content.title = i18n("topic_voting.reached_limit");
- } else {
- content.label = i18n("topic_voting.vote_title");
- content.title = i18n("topic_voting.vote_title");
- }
- } else {
- content.label = i18n("topic_voting.anonymous_button", { count: 1 });
- content.title = i18n("topic_voting.anonymous_button", { count: 1 });
- }
-
- return content;
+ get buttonIcon() {
+ return this.topic.user_voted ? "vote-up-filled" : "vote-up";
}
get limitsEnabled() {
@@ -56,9 +35,25 @@ export default class VoteBox extends Component {
}
get buttonClasses() {
- return this.currentUser?.vote_limit === 0
- ? "btn-default vote-button"
- : "btn-primary vote-button";
+ if (this.currentUser?.vote_limit === 0) {
+ return "btn-default btn-small voting-wrapper__button";
+ }
+
+ return this.topic.user_voted
+ ? "btn-success btn-small voting-wrapper__button"
+ : "btn-default btn-small voting-wrapper__button";
+ }
+
+ get ariaLabel() {
+ if (this.topic.closed) {
+ return i18n("topic_voting.voting_closed_description");
+ }
+ if (this.currentUser?.vote_limit === 0) {
+ return i18n("topic_voting.locked_description");
+ }
+ return this.topic.user_voted
+ ? i18n("topic_voting.remove_vote")
+ : i18n("topic_voting.vote_title");
}
@action
@@ -70,26 +65,39 @@ export default class VoteBox extends Component {
applyBehaviorTransformer("topic-vote-button-click", () => {
if (!this.currentUser) {
- return this.args.showLogin();
+ return this.router.transitionTo("login");
}
if (this.currentUser.vote_limit === 0) {
return;
}
+ // When limits are disabled and user has voted, toggle off
+ if (!this.limitsEnabled && this.topic.user_voted) {
+ this.args.removeVote();
+ this.hasVoted = false;
+ this.hasSeenSuccessMenu = false;
+ return;
+ }
+
// If user has already voted and seen the success menu, don't do anything
// The menu will show with the "remove vote" option
if (this.topic.user_voted && this.hasSeenSuccessMenu) {
return;
}
- // If user hasn't voted yet, add vote and show success menu
if (
!this.topic.closed &&
!this.topic.user_voted &&
!this.currentUser.votes_exceeded
) {
this.args.addVote();
+
+ // When limits are disabled, no menu needed
+ if (!this.limitsEnabled) {
+ return;
+ }
+
this.hasVoted = true;
// Don't set hasSeenSuccessMenu yet - it will be set when menu closes
}
@@ -123,60 +131,62 @@ export default class VoteBox extends Component {
}
<template>
- <DMenu
- @identifier="topic-voting-menu"
- @title={{this.buttonContent.title}}
- @label={{this.buttonContent.label}}
- @onShow={{this.onShowMenu}}
- @onClose={{this.onCloseMenu}}
- class={{this.buttonClasses}}
- @onRegisterApi={{this.onRegisterApi}}
- >
- <:content>
- <DropdownMenu as |dropdown|>
- {{#if this.showVotedMenu}}
- <dropdown.item class="topic-voting-menu__title">
- {{icon "circle-check"}}
- <span>{{i18n "topic_voting.voted_title"}}</span>
- </dropdown.item>
- <dropdown.item class="topic-voting-menu__votes-left">
- <DButton
- @translatedLabel={{if
- this.limitsEnabled
- (i18n
+ {{#if this.topic.closed}}
+ <DTooltip @identifier="vote-closed-tooltip" @placement="right">
+ <:trigger>
+ <DButton
+ @icon={{this.buttonIcon}}
+ @disabled={{true}}
+ @ariaLabel={{this.ariaLabel}}
+ class={{this.buttonClasses}}
+ />
+ </:trigger>
+ <:content>
+ {{i18n "topic_voting.voting_closed_description"}}
+ </:content>
+ </DTooltip>
+ {{else if this.limitsEnabled}}
+ <DMenu
+ @identifier="topic-voting-menu"
+ @icon={{this.buttonIcon}}
+ @onShow={{this.onShowMenu}}
+ @onClose={{this.onCloseMenu}}
+ @ariaLabel={{this.ariaLabel}}
+ class={{this.buttonClasses}}
+ @onRegisterApi={{this.onRegisterApi}}
+ @placement="right"
+ >
+ <:content>
+ <DropdownMenu as |dropdown|>
+ {{#if this.showVotedMenu}}
+ <dropdown.item class="topic-voting-menu__votes-left">
+ <DButton
+ @translatedLabel={{i18n
"topic_voting.see_votes"
count=this.currentUser.votes_left
max=this.currentUser.vote_limit
- )
- (i18n "topic_voting.see_all_votes")
- }}
- @href="/my/activity/votes"
- @icon="check-to-slot"
- class="btn-transparent see-votes topic-voting-menu__row-btn"
- />
- </dropdown.item>
- {{else if (eq this.currentUser.vote_limit 0)}}
- <dropdown.item class="topic-voting-menu__title --locked">
- {{icon "lock"}}
- <span>{{i18n "topic_voting.locked_description"}}</span>
- </dropdown.item>
- {{else if
- (and this.currentUser.votes_exceeded (not this.topic.user_voted))
- }}
- <dropdown.item class="topic-voting-menu__row">
- <DButton
- @translatedLabel={{i18n
- "topic_voting.see_votes"
- count=this.currentUser.votes_left
- max=this.currentUser.vote_limit
- }}
- @href="/my/activity/votes"
- @icon="check-to-slot"
- class="btn-transparent see-votes topic-voting-menu__row-btn"
- />
- </dropdown.item>
- {{else}}
- {{#if this.limitsEnabled}}
+ }}
+ @href="/my/activity/votes"
+ @icon="check-to-slot"
+ class="btn-transparent see-votes topic-voting-menu__row-btn"
+ />
+ </dropdown.item>
+ <dropdown.item class="topic-voting-menu__row">
+ <DButton
+ @translatedLabel={{i18n "topic_voting.remove_vote"}}
+ @action={{this.removeVote}}
+ @icon="arrow-rotate-left"
+ class="btn-transparent remove-vote topic-voting-menu__row-btn"
+ />
+ </dropdown.item>
+ {{else if (eq this.currentUser.vote_limit 0)}}
+ <dropdown.item class="topic-voting-menu__title --locked">
+ {{icon "lock"}}
+ <span>{{i18n "topic_voting.locked_description"}}</span>
+ </dropdown.item>
+ {{else if
+ (and this.currentUser.votes_exceeded (not this.topic.user_voted))
+ }}
<dropdown.item class="topic-voting-menu__row">
<DButton
@translatedLabel={{i18n
@@ -189,20 +199,40 @@ export default class VoteBox extends Component {
class="btn-transparent see-votes topic-voting-menu__row-btn"
/>
</dropdown.item>
- {{/if}}
- {{#if this.topic.user_voted}}
+ {{else}}
<dropdown.item class="topic-voting-menu__row">
<DButton
- @translatedLabel={{i18n "topic_voting.remove_vote"}}
- @action={{this.removeVote}}
- @icon="arrow-rotate-left"
- class="btn-transparent remove-vote topic-voting-menu__row-btn --danger"
+ @translatedLabel={{i18n
+ "topic_voting.see_votes"
+ count=this.currentUser.votes_left
+ max=this.currentUser.vote_limit
+ }}
+ @href="/my/activity/votes"
+ @icon="check-to-slot"
+ class="btn-transparent see-votes topic-voting-menu__row-btn"
/>
</dropdown.item>
+ {{#if this.topic.user_voted}}
+ <dropdown.item class="topic-voting-menu__row">
+ <DButton
+ @translatedLabel={{i18n "topic_voting.remove_vote"}}
+ @action={{this.removeVote}}
+ @icon="arrow-rotate-left"
+ class="btn-transparent remove-vote topic-voting-menu__row-btn"
+ />
+ </dropdown.item>
+ {{/if}}
{{/if}}
- {{/if}}
- </DropdownMenu>
- </:content>
- </DMenu>
+ </DropdownMenu>
+ </:content>
+ </DMenu>
+ {{else}}
+ <DButton
+ @icon={{this.buttonIcon}}
+ @action={{this.onShowMenu}}
+ @ariaLabel={{this.ariaLabel}}
+ class={{this.buttonClasses}}
+ />
+ {{/if}}
</template>
}
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-count-trigger.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-count-trigger.gjs
new file mode 100644
index 00000000000..f47eadba1cc
--- /dev/null
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-count-trigger.gjs
@@ -0,0 +1,13 @@
+import { i18n } from "discourse-i18n";
+
+const VoteCountTrigger = <template>
+ <button
+ type="button"
+ aria-label={{i18n "topic_voting.show_voters"}}
+ ...attributes
+ >
+ {{yield}}
+ </button>
+</template>;
+
+export default VoteCountTrigger;
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-count.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-count.gjs
index ebeef6a3823..f5b37b5b76c 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-count.gjs
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-count.gjs
@@ -1,91 +1,139 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
-import { hash } from "@ember/helper";
-import { on } from "@ember/modifier";
-import { action } from "@ember/object";
import { service } from "@ember/service";
-import AsyncContent from "discourse/components/async-content";
-import SmallUserList from "discourse/components/small-user-list";
+import DMenu from "discourse/float-kit/components/d-menu";
+import avatar from "discourse/helpers/bound-avatar-template";
import concatClass from "discourse/helpers/concat-class";
import { ajax } from "discourse/lib/ajax";
+import { popupAjaxError } from "discourse/lib/ajax-error";
import { bind } from "discourse/lib/decorators";
import getURL from "discourse/lib/get-url";
-import closeOnClickOutside from "discourse/modifiers/close-on-click-outside";
-import { eq } from "discourse/truth-helpers";
+import { eq, gt } from "discourse/truth-helpers";
+import { i18n } from "discourse-i18n";
+import VoteCountTrigger from "./vote-count-trigger";
-export default class VoteBox extends Component {
+export default class VoteCount extends Component {
@service siteSettings;
@service currentUser;
- @tracked showWhoVoted = false;
+ @tracked voters = null;
+ @tracked lastVoteCount = null;
+ @tracked totalVoters = 0;
+ @tracked isLoading = false;
- @bind
- async loadWhoVoted() {
- return ajax("/voting/who", {
- type: "GET",
- data: {
- topic_id: this.args.topic.id,
- },
- }).then((users) =>
- users.map((user) => {
- return {
- template: user.avatar_template,
- username: user.username,
- post_url: user.post_url,
- url: getURL("/u/") + user.username.toLowerCase(),
- };
- })
- );
+ maxDisplayedVoters = 104;
+
+ get showVoterMenu() {
+ return this.siteSettings.topic_voting_show_who_voted && this.currentUser;
+ }
+
+ get displayCount() {
+ const count = this.args.topic.vote_count;
+ if (count >= 1000) {
+ const thousands = (count / 1000).toFixed(1).replace(/\.0$/, "");
+ return i18n("topic_voting.vote_count_thousands", { count: thousands });
+ }
+ return count;
}
- @action
- click(event) {
- event.preventDefault();
- event.stopPropagation();
+ get remainingVoters() {
+ return this.totalVoters - this.maxDisplayedVoters;
+ }
- if (!this.currentUser) {
- return this.args.showLogin();
+ @bind
+ async loadVoters() {
+ // Clear cache if vote count changed
+ if (
+ this.lastVoteCount !== null &&
+ this.lastVoteCount !== this.args.topic.vote_count
+ ) {
+ this.voters = null;
}
- if (this.showWhoVoted) {
- this.showWhoVoted = false;
- } else if (this.siteSettings.topic_voting_show_who_voted) {
- this.showWhoVoted = true;
+ if (this.voters) {
+ return;
}
- }
- @action
- clickOutside() {
- this.showWhoVoted = false;
+ this.lastVoteCount = this.args.topic.vote_count;
+ this.isLoading = true;
+
+ try {
+ const users = await ajax("/voting/who", {
+ type: "GET",
+ data: { topic_id: this.args.topic.id },
+ });
+
+ this.totalVoters = users.length;
+ this.voters = users.slice(0, this.maxDisplayedVoters).map((user) => ({
+ template: user.avatar_template,
+ username: user.username,
+ url: getURL("/u/") + user.username.toLowerCase(),
+ }));
+ } catch (error) {
+ popupAjaxError(error);
+ } finally {
+ this.isLoading = false;
+ }
}
<template>
- <div
- class={{concatClass
- "vote-count-wrapper"
- (if (eq @topic.vote_count 0) "no-votes")
- }}
- {{on "click" this.click}}
- role="button"
- >
- <div class="vote-count">
- {{@topic.vote_count}}
- </div>
- </div>
-
- {{#if this.showWhoVoted}}
+ {{#if this.showVoterMenu}}
+ <DMenu
+ @identifier="vote-count-voters"
+ @triggerComponent={{VoteCountTrigger}}
+ @onShow={{this.loadVoters}}
+ @modalForMobile={{true}}
+ @placement="right"
+ class={{concatClass
+ "voting-wrapper__count"
+ (if (eq @topic.vote_count 0) "no-votes")
+ }}
+ >
+ <:trigger>
+ <span class="voting-wrapper__count-text">{{this.displayCount}}</span>
+ </:trigger>
+ <:content>
+ {{#if this.isLoading}}
+ <div class="voting-voters__loading">
+ {{i18n "loading"}}
+ </div>
+ {{else if (eq @topic.vote_count 0)}}
+ <div class="voting-voters__empty">
+ {{i18n "topic_voting.no_votes_yet"}}
+ </div>
+ {{else if this.voters}}
+ <div class="voting-voters__list">
+ {{#each this.voters as |voter|}}
+ <a
+ class="voting-voters__avatar trigger-user-card"
+ data-user-card={{voter.username}}
+ title={{voter.username}}
+ >
+ {{avatar voter.template "small"}}
+ </a>
+ {{/each}}
+ {{#if (gt this.totalVoters this.maxDisplayedVoters)}}
+ <div class="voting-voters__overflow">
+ {{i18n
+ "topic_voting.and_more_voters"
+ count=this.remainingVoters
+ }}
+ </div>
+ {{/if}}
+ </div>
+ {{/if}}
+ </:content>
+ </DMenu>
+ {{else}}
<div
- class="who-voted popup-menu voting-popup-menu"
- {{closeOnClickOutside
- this.clickOutside
- (hash secondaryTargetSelector=".vote-count-wrapper")
+ class={{concatClass
+ "voting-wrapper__count"
+ (if (eq @topic.vote_count 0) "no-votes")
}}
>
- <AsyncContent @asyncData={{this.loadWhoVoted}}>
- <:content as |voters|>
- <SmallUserList @users={{voters}} class="regular-votes" />
- </:content>
- </AsyncContent>
+ <div class="voting-wrapper__count-text">
+ {{this.displayCount}}
+ </div>
</div>
{{/if}}
</template>
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/header-topic-info__before/header-topic-voting.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/header-topic-info__before/header-topic-voting.gjs
new file mode 100644
index 00000000000..2ef78ef5800
--- /dev/null
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/header-topic-info__before/header-topic-voting.gjs
@@ -0,0 +1,9 @@
+import VoteBox from "../../components/vote-box";
+
+<template>
+ {{#if @outletArgs.topic.can_vote}}
+ <div class="voting header-title-voting">
+ <VoteBox @topic={{@outletArgs.topic}} />
+ </div>
+ {{/if}}
+</template>
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-above-post-stream/topic-title-voting.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-above-post-stream/topic-title-voting.gjs
deleted file mode 100644
index 04c5875e5f9..00000000000
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-above-post-stream/topic-title-voting.gjs
+++ /dev/null
@@ -1,28 +0,0 @@
-/* eslint-disable ember/no-classic-components */
-import Component from "@ember/component";
-import { tagName } from "@ember-decorators/component";
-import routeAction from "discourse/helpers/route-action";
-import VoteBox from "../../components/vote-box";
-
-@tagName("")
-export default class TopicTitleVoting extends Component {
- <template>
- <div
- class="topic-above-post-stream-outlet topic-title-voting"
- ...attributes
- >
- {{#if this.model.can_vote}}
- {{#if this.model.postStream.loaded}}
- {{#if this.model.postStream.firstPostPresent}}
- <div class="voting title-voting">
- <VoteBox
- @topic={{this.model}}
- @showLogin={{routeAction "showLogin"}}
- />
- </div>
- {{/if}}
- {{/if}}
- {{/if}}
- </div>
- </template>
-}
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-title/topic-title-voting.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-title/topic-title-voting.gjs
new file mode 100644
index 00000000000..5f6c072faa6
--- /dev/null
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-title/topic-title-voting.gjs
@@ -0,0 +1,13 @@
+import VoteBox from "../../components/vote-box";
+
+<template>
+ {{#if @outletArgs.model.can_vote}}
+ {{#if @outletArgs.model.postStream.loaded}}
+ {{#if @outletArgs.model.postStream.firstPostPresent}}
+ <div class="voting title-voting">
+ <VoteBox @topic={{@outletArgs.model}} />
+ </div>
+ {{/if}}
+ {{/if}}
+ {{/if}}
+</template>
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js b/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js
index 554a09dcbbe..8243fc224ec 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js
@@ -20,7 +20,9 @@ function initialize(api) {
api.addTrackedPostProperties("can_vote");
api.addTagsHtmlCallback(
(topic) => {
- if (!topic.can_vote) {
+ const router = api.container.lookup("service:router");
+
+ if (!topic.can_vote || router.currentRouteName?.startsWith("topic.")) {
return;
}
diff --git a/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss b/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss
index d32a4cf6f9c..f8e9d7eeee2 100644
--- a/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss
+++ b/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss
@@ -1,111 +1,169 @@
-.title-voting {
- padding-right: 0.75em;
- width: 6em;
- max-width: 10%;
- box-sizing: border-box;
- float: left;
- margin-top: 0.33em;
- position: relative;
-
- .rtl & {
- float: right;
- padding-right: 0;
- padding-left: 0.75em;
- }
-}
+@use "lib/viewport";
-.vote-count-wrapper {
- border: 3px solid var(--primary-low);
- font-weight: bold;
+.voting-wrapper {
display: flex;
- align-items: center;
- justify-content: center;
-}
+ flex-direction: column;
+ align-items: stretch;
+ min-width: 2.5em;
+ margin-top: 0.1em; // minor vertical alignment tweak
+
+ .fk-d-tooltip__trigger-container,
+ .fk-d-tooltip__trigger,
+ .topic-voting-menu-trigger {
+ width: 100%;
+ }
+
+ .btn[disabled]:not(.is-loading) {
+ opacity: 1;
+ background: var(--primary-very-low);
+ border: 1px solid var(--content-border-color);
+ width: 100%;
-.voting-wrapper.show-pointer .vote-count-wrapper:not(.no-votes) {
- cursor: pointer;
+ span[aria-hidden="true"] {
+ display: none; // empty span added by tooltip component impacts height
+ }
- &:hover {
- background-color: var(--primary-low);
+ .d-icon {
+ opacity: 0.5;
+ }
}
}
-.vote-button {
- width: 100%;
- margin-top: 0.35em;
-}
+.voting-wrapper__button {
+ border-radius: var(--d-button-border-radius) var(--d-button-border-radius) 0 0;
+ padding: var(--space-1) var(--space-3);
+ background: var(--secondary);
+ border: 1px solid var(--content-border-color);
-.vote-options {
- text-align: left;
-}
+ &:focus-visible,
+ .discourse-no-touch &:hover {
+ background: var(--primary-low);
+
+ .d-icon {
+ color: var(--primary-high);
+ }
+ }
+
+ &.btn-success {
+ .d-icon {
+ color: var(--success);
+ }
-.vote-option {
- cursor: pointer;
- padding: 0.35em;
+ &:focus-visible,
+ .discourse-no-touch &:hover {
+ background: var(--primary-low);
- &:hover {
- background-color: var(--primary-low);
+ .d-icon {
+ color: var(--success);
+ }
+ }
}
}
-.vote-option.remove-vote .d-icon {
- margin-right: 0.35em;
- color: var(--danger);
+.voting-wrapper__count-text {
+ color: var(--primary-medium);
}
-.list-vote-count.voted {
- font-weight: bold;
+.voting-wrapper__count {
+ border: 1px solid var(--content-border-color);
+ border-top: 0;
+ background: var(--secondary);
+ border-radius: 0 0 var(--d-button-border-radius) var(--d-button-border-radius);
+ font-size: var(--font-down-2);
+ padding: var(--space-half) var(--space-2);
+ text-align: center;
}
-.who-voted.popup-menu {
- a {
- margin: 0.4em 0.25em;
- display: inline-block;
+// grid layout for voting in topic title
+#topic-title:has(.voting) {
+ display: grid;
+ grid-template-columns: auto 1fr;
+ gap: 0 var(--topic-body-width-padding);
+
+ > * {
+ grid-column: 2;
+ }
+
+ .voting {
+ grid-column: 1;
+ grid-row: 1 / span 10; // can't use -1 with an implicit grid
+ align-self: start;
}
}
-.topic-post.voting-post {
- button.like-count,
- button.toggle-like {
- display: none;
+// topic title
+.title-voting {
+ --topic-avatar-width: 48px;
+ width: var(--topic-avatar-width);
+
+ .voting-wrapper {
+ min-width: var(--topic-avatar-width);
+ }
+
+ .voting-wrapper__button {
+ padding-block: var(--space-2);
}
}
-.voting-popup-menu {
- position: absolute;
- left: 90px;
- top: 0;
- padding: 0.5em;
- line-height: var(--line-height-medium);
- z-index: z("usercard") - 1;
- cursor: initial;
-
- // voter avatars
- .regular-votes {
- display: flex;
- flex-wrap: wrap;
- align-items: center;
- justify-content: center;
+// docked header
+.header-title-voting {
+ --secondary: var(--header_background);
+ --primary-high: var(--header_primary-high);
+ --primary-medium: var(--header_primary-medium);
+ --primary-low: var(--header_primary-low);
+ margin-top: 0.15em; // vertical alignment tweak
+ margin-right: var(--space-2);
+
+ @include viewport.until(sm) {
+ margin-top: -0.25em; // centering tweak
+
+ .voting-wrapper__count {
+ font-size: var(--font-down-3);
+ }
}
- .rtl & {
- left: unset;
- right: 80px;
+ .voting-wrapper__button {
+ width: 100%;
+ padding: var(--space-1);
}
}
-.topic-voting-menu__title {
+// voter popup
+.voting-voters__list {
display: flex;
- align-items: center;
- gap: 0.75em;
- background: var(--tertiary-low);
- color: var(--tertiary);
- padding: 0.65rem 1rem;
- font-weight: bold;
+ flex-wrap: wrap;
+ gap: var(--space-1);
+ padding: var(--space-2);
+
+ @include viewport.from(sm) {
+ max-width: 22.5em;
+ }
}
-.topic-voting-menu__title.--locked {
- background: var(--secondary);
- color: var(--primary-medium);
- font-weight: normal;
+.voting-voters__avatar {
+ display: inline-flex;
+}
+
+.voting-voters__empty,
+.voting-voters__loading {
+ padding: 0.5em;
+ color: var(--primary);
+ font-size: var(--font-down-1);
+}
+
+.voting-voters__overflow {
+ width: 100%;
+ padding: var(--space-2) var(--space-2) 0;
+ text-align: center;
+ color: var(--primary);
+ font-size: var(--font-down-1);
+ border-top: 1px solid var(--primary-low);
+}
+
+// hide like buttons on voting posts
+.topic-post.voting-post {
+ button.like-count,
+ button.toggle-like {
+ display: none;
+ }
}
diff --git a/plugins/discourse-topic-voting/assets/stylesheets/desktop/topic-voting.scss b/plugins/discourse-topic-voting/assets/stylesheets/desktop/topic-voting.scss
deleted file mode 100644
index 9fcd816d856..00000000000
--- a/plugins/discourse-topic-voting/assets/stylesheets/desktop/topic-voting.scss
+++ /dev/null
@@ -1,4 +0,0 @@
-.vote-count-wrapper {
- font-size: $font-up-2;
- height: 40px;
-}
diff --git a/plugins/discourse-topic-voting/assets/stylesheets/mobile/topic-voting.scss b/plugins/discourse-topic-voting/assets/stylesheets/mobile/topic-voting.scss
deleted file mode 100644
index ddecb582432..00000000000
--- a/plugins/discourse-topic-voting/assets/stylesheets/mobile/topic-voting.scss
+++ /dev/null
@@ -1,40 +0,0 @@
-.title-voting {
- width: 100%;
- float: none;
- max-width: none;
- text-align: inherit;
-
- .rtl & {
- float: none;
- }
-}
-
-.voting-wrapper {
- display: flex;
- align-items: center;
-}
-
-.vote-count-wrapper {
- min-width: 70px;
- box-sizing: border-box;
-
- // match button height
- min-height: 30px;
-}
-
-.vote-button {
- max-width: 10em;
- margin-left: 0.5em;
- margin-top: 0;
- white-space: nowrap;
-
- .rtl & {
- margin-left: 0;
- margin-right: 0.5em;
- }
-}
-
-.voting-popup-menu {
- // 80px left positioning + 20px .wrap padding
- width: calc(100vw - 100px);
-}
diff --git a/plugins/discourse-topic-voting/config/locales/client.en.yml b/plugins/discourse-topic-voting/config/locales/client.en.yml
index f2a59ce07f0..4fffb1781c3 100755
--- a/plugins/discourse-topic-voting/config/locales/client.en.yml
+++ b/plugins/discourse-topic-voting/config/locales/client.en.yml
@@ -20,11 +20,8 @@ en:
allow_topic_voting: "Allow users to vote on topics in this category"
vote_title: "Vote"
vote_title_plural: "Votes"
- voted_title: "Voted"
- voting_closed_title: "Closed"
- voting_limit: "Limit"
+ voting_closed_description: "Voting is closed for this topic"
see_votes: "You have %{count}/%{max} votes left"
- see_all_votes: "See your votes"
locked: "Locked"
locked_description: "Increase your trust level to vote"
votes:
@@ -34,6 +31,10 @@ en:
one: "Vote"
other: "Votes"
remove_vote: "Remove vote"
+ no_votes_yet: "No one has voted for this yet"
+ and_more_voters: "and %{count} more..."
+ show_voters: "Show who voted"
+ vote_count_thousands: "%{count}k"
no_votes_title_self: "You haven't voted for any topic yet"
no_votes_title_others: "%{username} hasn't voted for any topic yet"
filters:
diff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rb
index aa0cd00e6bc..b5babec5988 100755
--- a/plugins/discourse-topic-voting/plugin.rb
+++ b/plugins/discourse-topic-voting/plugin.rb
@@ -8,10 +8,10 @@
# url: https://github.com/discourse/discourse/tree/main/plugins/discourse-topic-voting
register_asset "stylesheets/common/topic-voting.scss"
-register_asset "stylesheets/desktop/topic-voting.scss", :desktop
-register_asset "stylesheets/mobile/topic-voting.scss", :mobile
register_svg_icon "check-to-slot"
+register_svg_icon "vote-up"
+register_svg_icon "vote-up-filled"
enabled_site_setting :topic_voting_enabled
diff --git a/plugins/discourse-topic-voting/spec/system/page_objects/pages/category.rb b/plugins/discourse-topic-voting/spec/system/page_objects/pages/category.rb
index 7872cec2794..9ecb727c190 100644
--- a/plugins/discourse-topic-voting/spec/system/page_objects/pages/category.rb
+++ b/plugins/discourse-topic-voting/spec/system/page_objects/pages/category.rb
@@ -7,12 +7,8 @@ module TopicVotingCategory
".nav-item_votes.votes"
end
- def topic_with_vote_count(vote_count)
- "tr.topic-list-item a.list-vote-count.vote-count-#{vote_count}"
- end
-
def select_topic(topic)
- find("tr[data-topic-id=\"#{topic.id}\"] a.list-vote-count.vote-count-0").click
+ find("tr[data-topic-id=\"#{topic.id}\"] a.raw-link").click
end
end
diff --git a/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb b/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb
index afde0725aff..cf906882e68 100644
--- a/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb
+++ b/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb
@@ -4,50 +4,43 @@ module TopicVotingTopic
include ::RSpec::Matchers
def vote_count
- find(".voting .vote-count")
- end
-
- def has_locked_popup?
- expected_html = I18n.t("js.topic_voting.locked_description")
- selector = ".topic-voting-menu__title.--locked"
- has_css?(selector)
- actual_html = find(selector)[:innerHTML].strip
- actual_html.include?(expected_html)
+ find(".title-voting .voting-wrapper__count-text")
end
def vote_popup
- find(".topic-voting-menu__votes-left")
+ find(".see-votes")
end
def vote
- find("button.vote-button").click
+ find(".title-voting button.voting-wrapper__button").click
self
end
def remove_vote
- vote
- find("button.remove-vote").click
+ if SiteSetting.topic_voting_enable_vote_limits
+ vote
+ find("button.remove-vote").click
+ else
+ find(".title-voting button.voting-wrapper__button").click
+ end
self
end
def click_my_votes
- find(".topic-voting-menu__votes-left").click
- end
-
- def has_vote_button_label?(text)
- has_css?("button.vote-button", text: text)
+ find(".see-votes").click
end
def has_no_remove_vote_button?
has_no_css?("button.remove-vote")
end
- def has_see_all_votes_link?
- has_css?(".see-votes", text: I18n.t("js.topic_voting.see_all_votes"))
+ def has_voted?
+ has_css?(".title-voting button.voting-wrapper__button.btn-success")
end
- def has_no_votes_left_text?
- has_no_css?(".see-votes", text: %r{\d+/\d+})
+ def has_not_voted?
+ has_css?(".title-voting button.voting-wrapper__button.btn-default") &&
+ has_no_css?(".title-voting button.voting-wrapper__button.btn-success")
end
end
diff --git a/plugins/discourse-topic-voting/spec/system/voting_spec.rb b/plugins/discourse-topic-voting/spec/system/voting_spec.rb
index 7b4ba70aa5d..1e33e916360 100644
--- a/plugins/discourse-topic-voting/spec/system/voting_spec.rb
+++ b/plugins/discourse-topic-voting/spec/system/voting_spec.rb
@@ -41,7 +41,6 @@ RSpec.describe "Topic voting" do
# make a vote
category_page.visit(category1)
expect(category_page).to have_css(category_page.votes)
- expect(category_page).to have_css(category_page.topic_with_vote_count(0), count: 2)
category_page.select_topic(topic1)
expect(topic_page.vote_count).to have_text("0")
@@ -55,7 +54,7 @@ RSpec.describe "Topic voting" do
topic_page.click_my_votes
expect(user_page.active_user_primary_navigation).to have_text("Activity")
expect(user_page.active_user_secondary_navigation).to have_text("Votes")
- expect(page).to have_css(".topic-list-body tr[data-topic-id=\"#{topic1.id}\"]", text: "1 vote")
+ expect(page).to have_css(".topic-list-body tr[data-topic-id=\"#{topic1.id}\"]")
find(".topic-list-body tr[data-topic-id=\"#{topic1.id}\"] a.raw-link").click
# unvoting
@@ -76,10 +75,10 @@ RSpec.describe "Topic voting" do
Fabricate(:post, topic: voting_topic1, raw: "Check out #{voting_topic2.url}")
visit("/t/#{voting_topic3.slug}/#{voting_topic3.id}")
- expect(topic_page).to have_vote_button_label(I18n.t("js.topic_voting.voting_closed_title"))
+ expect(page).to have_css("button.voting-wrapper__button[disabled]")
find("a[href='#{voting_topic1.url}']").click
- expect(topic_page).to have_vote_button_label(I18n.t("js.topic_voting.vote_title"))
+ expect(page).to have_no_css("button.voting-wrapper__button[disabled]")
topic_page.vote
expect(topic_page.vote_popup).to have_text(
@@ -87,7 +86,7 @@ RSpec.describe "Topic voting" do
)
find("a[href='#{voting_topic2.url}']").click
- expect(topic_page).to have_vote_button_label(I18n.t("js.topic_voting.vote_title"))
+ expect(page).to have_no_css("button.voting-wrapper__button[disabled]")
topic_page.vote
expect(topic_page.vote_popup).to have_text(
@@ -104,7 +103,7 @@ RSpec.describe "Topic voting" do
Fabricate(:post, topic: voting_topic1)
visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
- topic_page.vote
+ expect(page).to have_css("button.voting-wrapper__button[disabled]")
expect(topic_page).to have_no_remove_vote_button
end
end
@@ -125,6 +124,70 @@ RSpec.describe "Topic voting" do
end
end
+ context "when viewing as anonymous user" do
+ fab!(:voting_post) { Fabricate(:post, topic: voting_topic1) }
+
+ before do
+ DiscourseTopicVoting::CategorySetting.create!(category: voting_category)
+ Capybara.reset_session!
+ end
+
+ it "redirects to login when clicking vote" do
+ visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
+ find(".title-voting button.voting-wrapper__button").click
+ expect(page).to have_current_path("/login")
+ end
+ end
+
+ context "when scrolling down on a voting topic" do
+ fab!(:voting_posts) { Fabricate.times(21, :post, topic: voting_topic1) }
+
+ before { DiscourseTopicVoting::CategorySetting.create!(category: voting_category) }
+
+ it "shows voting in the docked header" do
+ sign_in(admin)
+ visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
+
+ expect(page).to have_css(".title-voting")
+ expect(page).to have_no_css(".header-title-voting")
+
+ page.execute_script("document.querySelector('#post_4').scrollIntoView()")
+
+ expect(page).to have_css(".header-title-voting .voting-wrapper")
+ end
+ end
+
+ context "when viewing who voted" do
+ fab!(:voting_post) { Fabricate(:post, topic: voting_topic1) }
+
+ before do
+ DiscourseTopicVoting::CategorySetting.create!(category: voting_category)
+ SiteSetting.topic_voting_show_who_voted = true
+ DiscourseTopicVoting::Vote.create!(user: admin, topic: voting_topic1)
+ voting_topic1.update_vote_count
+ end
+
+ it "shows voter avatars in the popup" do
+ sign_in(admin)
+ visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
+
+ find(".title-voting .voting-wrapper__count").click
+ expect(page).to have_css(".voting-voters__list")
+ expect(page).to have_css(".voting-voters__avatar", count: 1)
+ end
+
+ it "shows empty state when no votes" do
+ DiscourseTopicVoting::Vote.where(topic: voting_topic1).destroy_all
+ voting_topic1.update_vote_count
+
+ sign_in(admin)
+ visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
+
+ find(".title-voting .voting-wrapper__count").click
+ expect(page).to have_css(".voting-voters__empty")
+ end
+ end
+
context "when viewing navigation tooltips" do
before { DiscourseTopicVoting::CategorySetting.create!(category: voting_category) }
@@ -160,15 +223,12 @@ RSpec.describe "Topic voting" do
visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
topic_page.vote
- expect(topic_page).to have_see_all_votes_link
- expect(topic_page).to have_no_votes_left_text
expect(topic_page.vote_count).to have_text("1")
+ expect(topic_page).to have_voted
visit("/t/#{voting_topic2.slug}/#{voting_topic2.id}")
topic_page.vote
- expect(topic_page).to have_see_all_votes_link
- expect(topic_page).to have_no_votes_left_text
expect(topic_page.vote_count).to have_text("1")
end
@@ -177,6 +237,7 @@ RSpec.describe "Topic voting" do
voting_topic1.update_vote_count
visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
+ expect(topic_page.vote_count).to have_text("1")
topic_page.remove_vote
expect(topic_page.vote_count).to have_text("0")
diff --git a/plugins/discourse-topic-voting/svg-icons/plugin-icons.svg b/plugins/discourse-topic-voting/svg-icons/plugin-icons.svg
new file mode 100644
index 00000000000..0365cef0315
--- /dev/null
+++ b/plugins/discourse-topic-voting/svg-icons/plugin-icons.svg
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
+ <symbol id="vote-up" viewBox="0 0 24 24"><path d="M2 15h7v5h6v-5h7l-10-11z" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></symbol>
+ <symbol id="vote-up-filled" viewBox="0 0 24 24"><path d="M2 15h7v5h6v-5h7l-10-11z" fill="currentColor" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></symbol>
+</svg>
diff --git a/themes/horizon/common/common.scss b/themes/horizon/common/common.scss
index ec15384ae4e..04655206370 100644
--- a/themes/horizon/common/common.scss
+++ b/themes/horizon/common/common.scss
@@ -18,6 +18,7 @@
@import "sidebar-new-topic-button";
@import "topic";
@import "topic-cards";
+@import "topic-voting";
@import "high-context-cards";
@import "variables";
diff --git a/themes/horizon/scss/topic-voting.scss b/themes/horizon/scss/topic-voting.scss
new file mode 100644
index 00000000000..d3258dbfcb4
--- /dev/null
+++ b/themes/horizon/scss/topic-voting.scss
@@ -0,0 +1,35 @@
+@use "lib/viewport";
+
+.voting .voting-wrapper {
+ --d-button-border-radius: var(--d-border-radius-small);
+ --primary-100: var(--d-selected);
+ --primary-low: var(--d-selected);
+
+ &__button {
+ background: var(--secondary);
+ }
+
+ .discourse-no-touch & {
+ button {
+ &:focus-visible,
+ &:hover {
+ box-shadow: none;
+ }
+
+ &:not(.btn-success) {
+ .d-icon {
+ color: var(--primary-high);
+ }
+ }
+ }
+ }
+}
+
+.voting.header-title-voting .voting-wrapper {
+ --primary-high: var(--header_primary-medium);
+
+ @include viewport.from(lg) {
+ --secondary: var(--background-color);
+ --content-border-color: var(--d-sidebar-border-color);
+ }
+}