Skip to content

Commit

Permalink
DEV: move functions to service
Browse files Browse the repository at this point in the history
  • Loading branch information
Grubba27 committed Jun 28, 2024
1 parent e377a93 commit b03a7c1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 65 deletions.
68 changes: 3 additions & 65 deletions assets/javascripts/discourse/components/new-topic-dropdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { computed } from "@ember/object";
import { getOwner } from "@ember/owner";
import { inject as service } from "@ember/service";
import Composer from "discourse/models/composer";
Expand All @@ -9,6 +8,7 @@ export default DropdownSelectBoxComponent.extend({
siteSettings: service(),
historyStore: service(),
router: service(),
dropdownButtons: service(),

selectKitOptions: {
icon: "plus",
Expand All @@ -18,72 +18,10 @@ export default DropdownSelectBoxComponent.extend({
showCaret: true,
none: "topic.create",
},
init() {
this._super(...arguments);
},

shouldHighlightByURL(url) {
// case 1 - url does not contain *, e.g. example, it should match exact url "example"
// case 2 - url starts and ends with *, e.g. *example*, it should match any url containing "example"
// case 3 - url starts with *, e.g. *example, it should match any url ending with "example"
// case 4 - url ends with *, e.g. example*, it should match any url starting with "example"

const startsWithStar = url.startsWith("*");
const endsWithStar = url.endsWith("*");
const exactMatch = !startsWithStar && !endsWithStar;

if (exactMatch) {
return url === this.router.currentURL;
}

if (startsWithStar && endsWithStar) {
return this.router.currentURL.includes(url.replace(/\*/g, ""));
}

if (startsWithStar) {
return this.router.currentURL.endsWith(url.replace(/\*/g, ""));
}

if (endsWithStar) {
return this.router.currentURL.startsWith(url.replace(/\*/g, ""));
}

return false;
get content() {
return this.dropdownButtons.buttons;
},

shouldHighlightByCategoryID(categoryId) {
const isCategoryRoute =
this.router.currentRoute.localName === "category" &&
this.router.currentURL.startsWith("/c/");
if (!isCategoryRoute) {
return false;
}

const currentCategory = Number(this.router.currentURL.split("/").at(-1));
if (isNaN(currentCategory)) {
return false;
}

return categoryId === currentCategory;
},

content: computed("new-topic", function () {
return this.currentUser.topic_preset_buttons
.map((b) => ({
...b,
highlightUrls: b.highlightUrls || [],
}))
.map((button) => {
if (
this.shouldHighlightByCategoryID(button.categoryId) ||
button.highlightUrls.some((url) => this.shouldHighlightByURL(url))
) {
button.classNames = "is-highlighted";
}
return button;
});
}),

actions: {
onChange(selectedAction) {
const composerController = getOwner(this).lookup("controller:composer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export default {
Composer.serializeOnCreate("tags_to_add");

withPluginApi("0.8.12", (api) => {

api.onPageChange(() =>
api.container.lookup("service:dropdown-buttons").refreshButtons()
);

api.modifyClass("model:composer", {
pluginId: "preset-topic-composer-initializer",
tag_groups: {},
Expand Down
78 changes: 78 additions & 0 deletions assets/javascripts/discourse/services/dropdown-buttons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { tracked } from "@glimmer/tracking";
import Service, { service } from "@ember/service";
import { TrackedArray } from "@ember-compat/tracked-built-ins";

export default class DropdownButtonsService extends Service {
@service router;
@service currentUser;
@tracked buttons = new TrackedArray();

constructor() {
super(...arguments);
this.refreshButtons();
}

refreshButtons() {
this.buttons = new TrackedArray(
this.currentUser.topic_preset_buttons
.map((b) => ({
...b,
highlightUrls: b.highlightUrls || [],
}))
.map((button) => {
if (
this.#shouldHighlightByCategoryID(button.categoryId) ||
button.highlightUrls.some((url) => this.#shouldHighlightByURL(url))
) {
button.classNames = "is-highlighted";
}
return button;
})
);
}

#shouldHighlightByURL(url) {
// case 1 - url does not contain *, e.g. example, it should match exact url "example"
// case 2 - url starts and ends with *, e.g. *example*, it should match any url containing "example"
// case 3 - url starts with *, e.g. *example, it should match any url ending with "example"
// case 4 - url ends with *, e.g. example*, it should match any url starting with "example"

const startsWithStar = url.startsWith("*");
const endsWithStar = url.endsWith("*");
const exactMatch = !startsWithStar && !endsWithStar;

if (exactMatch) {
return url === this.router.currentURL;
}

if (startsWithStar && endsWithStar) {
return this.router.currentURL.includes(url.replace(/\*/g, ""));
}

if (startsWithStar) {
return this.router.currentURL.endsWith(url.replace(/\*/g, ""));
}

if (endsWithStar) {
return this.router.currentURL.startsWith(url.replace(/\*/g, ""));
}

return false;
}

#shouldHighlightByCategoryID(categoryId) {
const isCategoryRoute =
this.router.currentRoute.localName === "category" &&
this.router.currentURL.startsWith("/c/");
if (!isCategoryRoute) {
return false;
}

const currentCategory = Number(this.router.currentURL.split("/").at(-1));
if (isNaN(currentCategory)) {
return false;
}

return categoryId === currentCategory;
}
}

0 comments on commit b03a7c1

Please sign in to comment.