2023-07-14 01:57:45 +08:00
import require from "require" ;
2024-11-21 23:00:49 +08:00
import { consolePrefix } from "discourse/lib/source-identifier" ;
import deprecated from "discourse-common/lib/deprecated" ;
2023-10-11 02:38:59 +08:00
import { getResolverOption } from "discourse-common/resolver" ;
2020-05-06 00:15:03 +08:00
export const _ _DISCOURSE _RAW _TEMPLATES = { } ;
2024-11-21 23:00:49 +08:00
let _needsHbrTopicList = false ;
export function needsHbrTopicList ( value ) {
if ( value === undefined ) {
return _needsHbrTopicList ;
} else {
_needsHbrTopicList = value ;
}
}
export function resetNeedsHbrTopicList ( ) {
_needsHbrTopicList = false ;
}
const TOPIC _LIST _TEMPLATE _NAMES = [
"list/action-list" ,
"list/activity-column" ,
"list/category-column" ,
"list/new-list-header-controls" ,
"list/participant-groups" ,
"list/post-count-or-badges" ,
"list/posters-column" ,
"list/posts-count-column" ,
"list/topic-excerpt" ,
"list/topic-list-item" ,
"list/unread-indicator" ,
"list/visited-line" ,
"mobile/list/topic-list-item" ,
"topic-bulk-select-dropdown" ,
"topic-list-header-column" ,
"topic-list-header" ,
"topic-post-badges" ,
"topic-status" ,
] ;
2020-05-06 00:15:03 +08:00
2021-05-08 02:41:06 +08:00
export function addRawTemplate ( name , template , opts = { } ) {
2024-11-21 23:00:49 +08:00
const cleanName = name . replace ( /^javascripts\// , "" ) ;
if (
( TOPIC _LIST _TEMPLATE _NAMES . includes ( cleanName ) ||
name . includes ( "/connectors/" ) ) &&
! opts . core &&
! opts . hasModernReplacement
) {
const message = ` [ ${ name } ] hbr topic-list template overrides and connectors are deprecated. Use the value transformer \` topic-list-columns \` and other new topic-list plugin APIs instead. ` ;
2024-11-22 21:28:01 +08:00
// NOTE: addRawTemplate is called too early for deprecation handlers to process this:
2024-11-21 23:00:49 +08:00
deprecated ( message , {
since : "v3.4.0.beta3-dev" ,
id : "discourse.hbr-topic-list-overrides" ,
} ) ;
2024-11-22 21:28:01 +08:00
needsHbrTopicList ( true ) ;
2024-11-21 23:00:49 +08:00
let prefix ;
if ( opts . themeId ) {
prefix = consolePrefix ( null , {
type : "theme" ,
id : opts . themeId ,
name : opts . themeName ,
} ) ;
} else if ( opts . pluginName ) {
prefix = consolePrefix ( null , {
type : "plugin" ,
name : opts . pluginName ,
} ) ;
}
// eslint-disable-next-line no-console
console . debug ( prefix , message ) ;
}
2021-05-08 02:41:06 +08:00
// Core templates should never overwrite themes / plugins
if ( opts . core && _ _DISCOURSE _RAW _TEMPLATES [ name ] ) {
return ;
}
2020-05-06 00:15:03 +08:00
_ _DISCOURSE _RAW _TEMPLATES [ name ] = template ;
}
export function removeRawTemplate ( name ) {
delete _ _DISCOURSE _RAW _TEMPLATES [ name ] ;
}
export function findRawTemplate ( name ) {
if ( getResolverOption ( "mobileView" ) ) {
return (
_ _DISCOURSE _RAW _TEMPLATES [ ` javascripts/mobile/ ${ name } ` ] ||
_ _DISCOURSE _RAW _TEMPLATES [ ` javascripts/ ${ name } ` ] ||
_ _DISCOURSE _RAW _TEMPLATES [ ` mobile/ ${ name } ` ] ||
_ _DISCOURSE _RAW _TEMPLATES [ name ]
) ;
}
return (
_ _DISCOURSE _RAW _TEMPLATES [ ` javascripts/ ${ name } ` ] ||
_ _DISCOURSE _RAW _TEMPLATES [ name ]
) ;
}
2023-08-18 19:07:10 +08:00
export function buildRawConnectorCache ( ) {
2020-05-06 00:15:03 +08:00
let result = { } ;
2023-08-18 19:07:10 +08:00
Object . keys ( _ _DISCOURSE _RAW _TEMPLATES ) . forEach ( ( resource ) => {
const segments = resource . split ( "/" ) ;
const connectorIndex = segments . indexOf ( "connectors" ) ;
if ( connectorIndex >= 0 ) {
const outletName = segments [ connectorIndex + 1 ] ;
2022-11-29 18:24:35 +08:00
result [ outletName ] ? ? = [ ] ;
result [ outletName ] . push ( {
template : _ _DISCOURSE _RAW _TEMPLATES [ resource ] ,
} ) ;
}
2023-08-18 19:07:10 +08:00
} ) ;
2020-05-06 00:15:03 +08:00
return result ;
}
2022-11-29 18:24:35 +08:00
export function eagerLoadRawTemplateModules ( ) {
2023-07-14 01:57:45 +08:00
for ( const key of Object . keys ( requirejs . entries ) ) {
if ( key . includes ( "/raw-templates/" ) ) {
2022-11-29 18:24:35 +08:00
require ( key ) ;
}
}
}