-
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.
DEV: refactor to remove widgets, modifyClass (#24)
- Loading branch information
1 parent
841455b
commit 35626b4
Showing
11 changed files
with
259 additions
and
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { apiInitializer } from "discourse/lib/api"; | ||
import CategoryColor from "../components/category-color"; | ||
import ComposerCategoryColor from "../components/composer-category-color"; | ||
|
||
export default apiInitializer("1.14.0", (api) => { | ||
api.renderInOutlet("above-site-header", <template> | ||
<CategoryColor /> | ||
<ComposerCategoryColor /> | ||
</template>); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
import Component from "@glimmer/component"; | ||
import { action } from "@ember/object"; | ||
import { service } from "@ember/service"; | ||
import Category from "discourse/models/category"; | ||
|
||
function applyCategoryColors(color, textColor) { | ||
if (color && textColor) { | ||
document.body.style.setProperty("--category-color", `#${color}`); | ||
document.body.style.setProperty("--category-text-color", `#${textColor}`); | ||
} | ||
} | ||
|
||
function clearCategoryColors() { | ||
document.body.style.removeProperty("--category-color"); | ||
document.body.style.removeProperty("--category-text-color"); | ||
} | ||
|
||
export default class CategoryColor extends Component { | ||
@service router; | ||
|
||
constructor() { | ||
super(...arguments); | ||
this.setCategoryColors(); | ||
this.router.on("routeDidChange", this.setCategoryColors); | ||
} | ||
|
||
willDestroy() { | ||
super.willDestroy(...arguments); | ||
this.router.off("routeDidChange", this.setCategoryColors); | ||
} | ||
|
||
get isCategoryRoute() { | ||
return this.router.currentRoute.params.hasOwnProperty( | ||
"category_slug_path_with_id" | ||
); | ||
} | ||
|
||
get isTopicRoute() { | ||
return this.router.currentRoute.name.includes("topic.fromParams"); | ||
} | ||
|
||
@action | ||
setCategoryColors() { | ||
let category; | ||
|
||
if (this.isCategoryRoute) { | ||
category = this.router.currentRoute.attributes.category; | ||
} else if (this.isTopicRoute) { | ||
const categoryId = | ||
this.router.currentRoute.parent?.attributes?.category_id; | ||
category = Category.findById(categoryId); | ||
} | ||
|
||
if (category) { | ||
applyCategoryColors(category.color, category.text_color); | ||
} else { | ||
clearCategoryColors(); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
javascripts/discourse/components/composer-category-color.gjs
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,79 @@ | ||
import Component from "@glimmer/component"; | ||
import { action } from "@ember/object"; | ||
import { addObserver, removeObserver } from "@ember/object/observers"; | ||
import { service } from "@ember/service"; | ||
import Category from "discourse/models/category"; | ||
|
||
export default class ComposerCategoryColor extends Component { | ||
@service composer; | ||
@service appEvents; | ||
|
||
currentCategory = null; | ||
|
||
constructor() { | ||
super(...arguments); | ||
|
||
this.appEvents.on("composer:will-open", this.handleComposerOpened); | ||
} | ||
|
||
willDestroy() { | ||
super.willDestroy(...arguments); | ||
|
||
this.appEvents.off("composer:will-open", this.handleComposerOpened); | ||
this.removeCategoryObserver(); | ||
} | ||
|
||
observeCategoryChanges() { | ||
if (this.composer.model) { | ||
addObserver(this.composer.model, "categoryId", this, this.updateCategory); | ||
} | ||
} | ||
|
||
removeCategoryObserver() { | ||
if (this.composer.model) { | ||
removeObserver( | ||
this.composer.model, | ||
"categoryId", | ||
this, | ||
this.updateCategory | ||
); | ||
} | ||
} | ||
|
||
get categoryId() { | ||
return this.composer.model?.get("categoryId"); | ||
} | ||
|
||
@action | ||
handleComposerOpened() { | ||
this.observeCategoryChanges(); | ||
this.updateCategory(); | ||
} | ||
|
||
@action | ||
updateCategory() { | ||
const categoryId = this.categoryId; | ||
const category = Category.findById(categoryId); | ||
|
||
if (category) { | ||
this.currentCategory = category; | ||
document.body.style.setProperty( | ||
"--composer-category-color", | ||
`#${category.color}` | ||
); | ||
document.body.style.setProperty( | ||
"--composer-category-text-color", | ||
`#${category.text_color}` | ||
); | ||
} else { | ||
this.clearCategoryColors(); | ||
} | ||
} | ||
|
||
@action | ||
clearCategoryColors() { | ||
this.currentCategory = null; | ||
document.body.style.removeProperty("--composer-category-color"); | ||
document.body.style.removeProperty("--composer-category-text-color"); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
javascripts/discourse/connectors/above-site-header/colorful-categories.hbs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.