import SETTINGS from "../../../common/Settings" ; import CONFIG from "../CONFIG" ; const ICONS_FOR_KNOWN_ROLL_TYPES = { publicroll : 'fas fa-dice-d20' , gmroll : 'fas fa-user-secret' , blindroll : 'fas fa-user-ninja' , selfroll : 'fas fa-ghost' } ; function calcColour ( current , count ) { return `rgb(${ ( current / count ) * 255 },${ ( 1 - ( current / count ) ) * 255 },0)` ; } interface ChatLogData { user : User ; rollMode : string ; rollModes : any ; isStream : boolean ; } async function handleChatLogRendering ( chat , html , data ) { const modes = Object . keys ( data . rollModes ) ; const buttons = [ ] ; const iconKeys = Object . keys ( ICONS_FOR_KNOWN_ROLL_TYPES ) ; for ( let c = 0 ; c < modes . length ; c ++ ) { const rt = modes [ c ] ; if ( ! ( rt in ICONS_FOR_KNOWN_ROLL_TYPES ) ) { console . warn ( Error ( `Unknown roll type '${ rt }'` ) ) ; continue ; } buttons . push ( { rt : rt , name : data . rollModes [ rt ] , active : data . rollMode === rt , icon : ICONS_FOR_KNOWN_ROLL_TYPES [ rt ] , colour : calcColour ( iconKeys . findIndex ( x => x == rt ) , iconKeys . length ) } ) ; } const buttonHtml = $ ( await renderTemplate ( 'modules/df-chat-enhance/templates/privacy-button.hbs' , { buttons } ) ) ; buttonHtml . find ( 'button' ) . on ( 'click' , function ( ) { const rollType = $ ( this ) . attr ( 'data-id' ) ; game . settings . set ( "core" , "rollMode" , < any > rollType ) ; buttonHtml . find ( 'button.active' ) . removeClass ( 'active' ) ; $ ( this ) . addClass ( 'active' ) ; } ) ; html . find ( 'select[name=rollMode]' ) . after ( buttonHtml ) ; html . find ( 'select[name=rollMode]' ) . remove ( ) ; if ( ! SETTINGS . get ( 'replace-buttons' ) ) return ; buttonHtml . attr ( 'style' , 'margin:0 0 0 0.5em' ) ; let first = true ; html . find ( '#chat-controls div.control-buttons a' ) . each ( function ( ) { const html = $ ( this ) . html ( ) ; const classes = $ ( this ) . attr ( 'class' ) ; const title = $ ( this ) . attr ( 'title' ) ; const style = $ ( this ) . attr ( 'style' ) ; const click = ( $ as any ) . _data ( this , 'events' ) [ 'click' ] [ 0 ] . handler ; const button = $ ( `<button class="${ classes }" title="${ title }" style="${ style }">${ html }</button>` ) ; button . on ( 'click' , click ) ; if ( first ) { button . attr ( 'style' , 'margin-left:0.5em' ) ; first = false ; } buttonHtml . append ( button ) ; } ) ; html . find ( '#chat-controls div.control-buttons' ) . remove ( ) ; } export default function initDFChatPrivacy ( ) { SETTINGS . register ( 'enabled' , { name : 'DF_CHAT_PRIVACY.Settings_EnableTitle' , hint : 'DF_CHAT_PRIVACY.Settings_EnableHint' , scope : 'client' , type : Boolean , default : true , config : true , onChange : CONFIG . requestReload } ) ; SETTINGS . register ( 'replace-buttons' , { name : 'DF_CHAT_PRIVACY.Settings_ReplaceButtonsTitle' , hint : 'DF_CHAT_PRIVACY.Settings_ReplaceButtonsHint' , scope : 'client' , type : Boolean , default : true , config : true , onChange : CONFIG . requestReload } ) ; if ( SETTINGS . get ( 'enabled' ) === false ) return ; Hooks . on ( 'renderChatLog' , handleChatLogRendering ) ; }