generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Add highlight to dropdown options (#18)
* FEATURE: Add highlight to dropdown options * FEATURE: Add highlight to dropdown when it matches highlight url property * DEV: refactor new-topic-dropdown to highlight the correct button * DEV: move functions to service * DEV: lint schema.rb and remove unsed import in js * DEV: Add tests to ensure that the dropdown button is highlighted
- Loading branch information
Showing
6 changed files
with
121 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters