2023-03-28 16:08:09 -04:00
import Component from "@glimmer/component" ;
import { tracked } from "@glimmer/tracking" ;
2024-03-27 12:16:16 -04:00
import { getOwner } from "@ember/application" ;
2023-03-28 16:08:09 -04:00
import { action } from "@ember/object" ;
2024-03-27 12:16:16 -04:00
import { service } from "@ember/service" ;
2023-03-28 16:08:09 -04:00
import { htmlSafe } from "@ember/template" ;
2024-03-27 12:16:16 -04:00
import Category from "discourse/models/category" ;
2023-03-28 16:08:09 -04:00
export default class DiscourseCategoryBanners extends Component {
@ service router ;
@ service site ;
2023-05-03 12:51:41 -04:00
@ service categoryBannerPresence ;
2023-03-28 16:08:09 -04:00
@ tracked category = null ;
@ tracked keepDuringLoadingRoute = false ;
2023-03-30 13:10:47 -04:00
get hasIconComponent ( ) {
return getOwner ( this ) . hasRegistration ( "component:category-icon" ) ;
}
2023-03-28 16:08:09 -04:00
get categorySlugPathWithID ( ) {
return this . router ? . currentRoute ? . params ? . category _slug _path _with _id ;
}
get shouldRender ( ) {
2023-05-03 12:51:41 -04:00
return (
this . categorySlugPathWithID ||
( this . keepDuringLoadingRoute &&
this . router . currentRoute . name . includes ( "loading" ) )
) ;
}
get isVisible ( ) {
if ( this . categorySlugPathWithID ) {
2023-03-31 10:41:01 -04:00
return true ;
2023-05-03 12:51:41 -04:00
} else if ( this . router . currentRoute . name . includes ( "loading" ) ) {
return this . keepDuringLoadingRoute ;
2023-03-31 10:41:01 -04:00
}
2023-05-03 12:51:41 -04:00
return false ;
2023-03-28 16:08:09 -04:00
}
get safeStyle ( ) {
return htmlSafe (
` background-color: # ${ this . category . color } ; color: # ${ this . category . text _color } ; `
) ;
}
2023-05-03 12:51:41 -04:00
get consoleWarn ( ) {
// eslint-disable-next-line no-console
return console . warn (
"The category banners component is trying to use the category icons component, but it is not available. https://meta.discourse.org/t/category-icons/104683"
) ;
}
2023-10-23 00:17:00 -04:00
get displayCategoryDescription ( ) {
return settings . show _description && this . category . description ? . length > 0 ;
}
2023-03-28 16:08:09 -04:00
# parseExceptions ( exceptionsStr ) {
return exceptionsStr
. split ( "|" )
. filter ( Boolean )
. map ( ( value ) => value . toLowerCase ( ) ) ;
}
2024-05-06 01:08:39 -04:00
# checkTargetCategory ( ) {
if ( settings . categories . length === 0 ) {
return true ;
}
2023-03-28 16:08:09 -04:00
2024-05-06 01:08:39 -04:00
const currentCategoryId = this . category ? . id ;
const activeCategory = settings . categories . find ( ( category ) => {
return category . category _id [ 0 ] === currentCategoryId ;
} ) ;
if (
activeCategory &&
( activeCategory . target === "all" || activeCategory . target === "no_sub" )
) {
return true ;
}
const parentCategoryId = this . category ? . parentCategory ? . id ;
if ( parentCategoryId ) {
const activeParentCategory = settings . categories . find ( ( category ) => {
return category . category _id [ 0 ] === parentCategoryId ;
} ) ;
if (
activeParentCategory &&
( activeParentCategory . target === "all" ||
activeParentCategory . target === "only_sub" )
) {
return true ;
}
}
return false ;
2023-03-28 16:08:09 -04:00
}
2023-05-03 12:51:41 -04:00
@ action
teardownComponent ( ) {
document . body . classList . remove ( "category-header" ) ;
this . category = null ;
this . categoryBannerPresence . setTo ( false ) ;
}
2023-03-28 16:08:09 -04:00
@ action
getCategory ( ) {
if ( ! this . isVisible ) {
return ;
}
if ( this . categorySlugPathWithID ) {
this . category = Category . findBySlugPathWithID (
this . categorySlugPathWithID
) ;
2023-05-03 12:51:41 -04:00
this . categoryBannerPresence . setTo ( true ) ;
this . keepDuringLoadingRoute = true ;
} else {
if ( ! this . router . currentRoute . name . includes ( "loading" ) ) {
return ( this . keepDuringLoadingRoute = false ) ;
}
2023-03-28 16:08:09 -04:00
}
const exceptions = this . # parseExceptions ( settings . exceptions ) ;
2023-05-03 12:51:41 -04:00
const isException = exceptions . includes ( this . category ? . name . toLowerCase ( ) ) ;
2024-05-06 01:08:39 -04:00
const isTarget = this . # checkTargetCategory ( ) ;
2023-10-23 00:17:00 -04:00
const hideMobile = this . site . mobileView && ! settings . show _mobile ;
const hideSubCategory =
this . category ? . parentCategory && ! settings . show _subcategory ;
2023-03-28 16:08:09 -04:00
const hasNoCategoryDescription =
2023-05-03 12:51:41 -04:00
settings . hide _if _no _description && ! this . category ? . description _text ;
2023-03-28 16:08:09 -04:00
if (
isTarget &&
! isException &&
! hasNoCategoryDescription &&
2023-10-23 00:17:00 -04:00
! hideSubCategory &&
2023-03-28 16:08:09 -04:00
! hideMobile
) {
document . body . classList . add ( "category-header" ) ;
} else {
document . body . classList . remove ( "category-header" ) ;
2023-05-03 12:51:41 -04:00
this . categoryBannerPresence . setTo ( false ) ;
2023-03-28 16:08:09 -04:00
}
}
}