import SETTINGS from "../../common/Settings" ; export default class DFSceneNav { static ON_CLICK = 'nav-on-click' ; static ON_CLICK_PLAYER = 'nav-on-click-player' ; static patchSceneDirectoryClick ( newValue ? , isPlayer ? ) { let gmClick = SETTINGS . get ( DFSceneNav . ON_CLICK ) ; let pcClick = SETTINGS . get ( DFSceneNav . ON_CLICK_PLAYER ) ; if ( newValue !== undefined ) { if ( isPlayer ) pcClick = newValue ; else gmClick = newValue ; } const enabled = ( game . user . isGM && gmClick ) || ( ! game . user . isGM && pcClick ) ; if ( enabled ) { libWrapper . register ( SETTINGS . MOD_NAME , 'SceneDirectory.prototype._onClickDocumentName' , this . _onClickDocumentName , 'MIXED' ) ; libWrapper . register ( SETTINGS . MOD_NAME , 'SceneDirectory.prototype._getEntryContextOptions' , this . _getEntryContextOptions , 'MIXED' ) ; } else { libWrapper . unregister ( SETTINGS . MOD_NAME , 'SceneDirectory.prototype._onClickDocumentName' , false ) ; libWrapper . unregister ( SETTINGS . MOD_NAME , 'SceneDirectory.prototype._getEntryContextOptions' , false ) ; } } private static _onClickDocumentName ( this , wrapper , event ) { event . preventDefault ( ) ; const entity = SceneDirectory . collection . get ( event . currentTarget . parentElement . dataset . documentId ) ; if ( entity instanceof Scene ) entity . view ( ) ; else wrapper ( event ) ; } private static _getEntryContextOptions ( wrapper , ... args ) { if ( game . user . isGM ) return wrapper ( ... args ) ; else return [ { name : "SCENES.View" , icon : '<i class="fas fa-eye"></i>' , condition : ( li ) => ! canvas . ready || ( li . data ( "entityId" ) !== ( canvas as Canvas ) . scene . id ) , callback : ( li ) => { const scene = game . scenes . get ( li . data ( "entityId" ) ) ; scene . view ( ) ; } } ] ; } static patchSceneDirectory ( ) { const defaultOptions = duplicate ( SceneDirectory . defaultOptions ) ; Object . defineProperty ( SceneDirectory , 'defaultOptions' , { get : function ( ) { const options = mergeObject ( defaultOptions , { template : `modules/${ SETTINGS . MOD_NAME }/templates/scene-directory.hbs` , } ) ; return options ; } } ) ; } static patchSidebar ( ) { libWrapper . register ( SETTINGS . MOD_NAME , 'Sidebar.prototype._render' , async function ( this , wrapper , force , options = { } ) { if ( ! this . rendered ) await Application . prototype . _render . bind ( this ) ( force , options ) ; const pcClick = SETTINGS . get ( DFSceneNav . ON_CLICK_PLAYER ) ; const tabs = [ "chat" , "combat" , "actors" , "items" , "journal" , "tables" , "playlists" , "compendium" , "settings" ] ; if ( game . user . isGM || pcClick ) tabs . push ( "scenes" ) ; for ( const [ name , app ] of Object . entries ( this . tabs ) ) { app . _render ( true ) . catch ( err => { err . message = `Failed to render Sidebar tab ${ name }: ${ err . message }` ; console . error ( err ) ; } ) ; } } , 'MIXED' ) ; libWrapper . register ( SETTINGS . MOD_NAME , 'SceneDirectory.prototype._render' , async ( wrapper , ... args ) => { if ( ! game . user . isGM && ! SETTINGS . get ( DFSceneNav . ON_CLICK_PLAYER ) ) return ; return wrapper ( ... args ) ; } , 'MIXED' ) ; libWrapper . register ( SETTINGS . MOD_NAME , 'Sidebar.prototype.getData' , ( wrapper , options ) => { return mergeObject ( wrapper ( options ) , { scenesAllowed : game . user . isGM || SETTINGS . get ( DFSceneNav . ON_CLICK_PLAYER ) } ) ; } , 'WRAPPER' ) ; const sidebarDefaultOptions = Object . getOwnPropertyDescriptor ( Sidebar , 'defaultOptions' ) ; Object . defineProperty ( Sidebar , 'defaultOptions' , { get : function ( ) { return mergeObject ( sidebarDefaultOptions . get ( ) , { template : `modules/${ SETTINGS . MOD_NAME }/templates/sidebar.hbs` } ) ; } } ) ; } static init ( ) { SETTINGS . register ( DFSceneNav . ON_CLICK , { name : "DF-SCENE-ENHANCE.Nav_SettingOnClick" , hint : "DF-SCENE-ENHANCE.Nav_SettingOnClickHint" , scope : "world" , config : true , type : Boolean , default : true , onChange : ( value ) => DFSceneNav . patchSceneDirectoryClick ( value , false ) } ) ; SETTINGS . register ( DFSceneNav . ON_CLICK_PLAYER , { name : "DF-SCENE-ENHANCE.Nav_SettingOnClickPC" , hint : "DF-SCENE-ENHANCE.Nav_SettingOnClickPCHint" , scope : "world" , config : true , type : Boolean , default : true , onChange : ( value ) => DFSceneNav . patchSceneDirectoryClick ( value , true ) } ) ; Handlebars . registerHelper ( 'dfCheck' , function ( scene ) { return ( ( game . user && game . user . isGM ) || ! scene . data . navName ) ? scene . data . name : scene . data . navName ; } ) ; DFSceneNav . patchSceneDirectory ( ) ; DFSceneNav . patchSidebar ( ) ; } static ready ( ) { DFSceneNav . patchSceneDirectoryClick ( ) ; } }