import { ImpressionCode } from '../../../protocol/impression/impressioncode' ; import { Repertoire } from '../../../protocol/storage' ; import { assert } from '../../../util/assert' ; import { ChessgroundBoardFactory } from '../board/chessgroundboardfactory' ; import { DelegatingBoard } from '../board/delegatingboard' ; import { ListRefreshableView } from '../common/listrefreshableview' ; import { ImpressionSender } from '../impressions/impressionsender' ; import { Mode } from '../mode/mode' ; import { ModeManager } from '../mode/modemanager' ; import { ModeType } from '../mode/modetype' ; import { PickerController } from '../picker/pickercontroller' ; import { ServerWrapper } from '../server/serverwrapper' ; import { SoundToggler } from '../sound/soundtoggler' ; import { EmptyMessage } from '../tree/emptymessage' ; import { TreeButtons } from '../tree/treebuttons' ; import { TreeModel } from '../tree/treemodel' ; import { TreeNavigator } from '../tree/treenavigator' ; import { TreeNodeHandler } from '../tree/treenodehandler' ; import { TreeView } from '../tree/treeview' ; import { DefaultAnnotationRenderer } from './annotation/defaultannotationrenderer' ; import { DefaultAnnotator } from './annotation/defaultannotator' ; import { BuildBoardHandler } from './buildboardhandler' ; import { ColorChooser } from './colorchooser' ; import { CurrentRepertoireExporter } from './currentrepertoireexporter' ; import { CurrentRepertoireUpdater } from './currentrepertoireupdater' ; import { ExampleRepertoireHandler } from './examplerepertoirehandler' ; import { CurrentRepertoireImporter } from './import/currentrepertoireimporter' ; import { ImportDialog } from './import/importdialog' ; import { RenameInput } from './renameinput' ; import { TreeController } from './treecontroller' ; export class BuildMode implements Mode { private impressionSender_ : ImpressionSender ; private server_ : ServerWrapper ; private pickerController_ : PickerController ; private modeManager_ : ModeManager ; private soundToggler_ : SoundToggler ; private board_ : DelegatingBoard ; private treeModel_ : TreeModel ; private renameInput_ : RenameInput ; private buildModeView_ : ListRefreshableView ; private treeController_ : TreeController ; private treeNavigator_ : TreeNavigator ; private buildModeElement_ : HTMLElement ; private buildButton_ : HTMLElement ; private importDialog_ : ImportDialog ; constructor ( impressionSender , server , pickerController , modeManager , soundToggler , chessgroundBoardFactory ) { this . impressionSender_ = impressionSender ; this . server_ = server ; this . pickerController_ = pickerController ; this . modeManager_ = modeManager ; this . soundToggler_ = soundToggler ; this . board_ = new DelegatingBoard ( ) ; this . treeModel_ = new TreeModel ( ) ; this . buildModeView_ = new ListRefreshableView ( ) ; const currentRepertoireUpdater = new CurrentRepertoireUpdater ( server , pickerController , this . treeModel_ ) ; const currentRepertoireExporter = new CurrentRepertoireExporter ( this . treeModel_ ) ; this . renameInput_ = new RenameInput ( assert ( document . getElementById ( 'renameInput' ) ) as HTMLInputElement , this . treeModel_ , pickerController , currentRepertoireUpdater ) ; this . buildModeView_ . addView ( this . renameInput_ ) ; const treeNodeHandler = new TreeNodeHandler ( impressionSender , this . treeModel_ , this . buildModeView_ ) ; const treeView = new TreeView ( assert ( document . getElementById ( 'buildTreeViewInner' ) ) , assert ( document . getElementById ( 'buildTreeViewOuter' ) ) , this . treeModel_ , treeNodeHandler , this . board_ , new DefaultAnnotator ( ) , new DefaultAnnotationRenderer ( ) ) ; this . buildModeView_ . addView ( treeView ) ; const colorChooser = new ColorChooser ( assert ( document . getElementById ( 'colorChooserWhite' ) ) , assert ( document . getElementById ( 'colorChooserBlack' ) ) , impressionSender , this . treeModel_ , this . buildModeView_ , currentRepertoireUpdater ) ; this . buildModeView_ . addView ( colorChooser ) ; this . treeController_ = new TreeController ( impressionSender , this . treeModel_ , this . buildModeView_ , currentRepertoireUpdater , currentRepertoireExporter ) ; this . treeNavigator_ = new TreeNavigator ( impressionSender , this . treeModel_ , this . buildModeView_ ) ; const treeButtons = new TreeButtons ( assert ( document . getElementById ( 'buildTreeButtons' ) ) , this . treeModel_ ) ; treeButtons . addNavigationButtons ( assert ( document . getElementById ( 'buildTreeLeft' ) ) , assert ( document . getElementById ( 'buildTreeRight' ) ) , this . treeNavigator_ ) . addButton ( { buttonEl : assert ( document . getElementById ( 'buildTreeTrash' ) ) , handleClick : ( ) => this . treeController_ . trash ( ) , isEnabled : ( ) => this . treeModel_ . canRemoveSelectedPgn ( ) } ) . addButton ( { buttonEl : assert ( document . getElementById ( 'buildTreeExport' ) ) , handleClick : ( ) => this . treeController_ . export ( ) , isEnabled : ( ) => true } ) ; this . buildModeView_ . addView ( treeButtons ) ; const emptyMessage = new EmptyMessage ( this . treeModel_ , assert ( document . getElementById ( 'emptyBuild' ) ) ) ; this . buildModeView_ . addView ( emptyMessage ) ; const exampleRepertoireHandler = new ExampleRepertoireHandler ( impressionSender , this . treeModel_ , this . buildModeView_ , pickerController , currentRepertoireUpdater ) ; exampleRepertoireHandler . handleButtonClicks ( assert ( document . getElementById ( 'exampleRepertoire' ) ) ) ; this . importDialog_ = new ImportDialog ( impressionSender , assert ( document . getElementById ( 'importPgnDialog' ) ) , document . getElementById ( 'importPgnTextArea' ) as HTMLTextAreaElement , document . getElementById ( 'importPgnUpload' ) as HTMLInputElement , assert ( document . getElementById ( 'importPgnOk' ) ) , assert ( document . getElementById ( 'importPgnCancel' ) ) , assert ( document . getElementById ( 'importPgnProgress' ) ) ) ; const currentRepertoireImporter = new CurrentRepertoireImporter ( this . importDialog_ , this . treeModel_ , this . buildModeView_ , pickerController , currentRepertoireUpdater ) ; this . importDialog_ . setImporter ( currentRepertoireImporter ) ; const importPgnEl = assert ( document . getElementById ( 'importPgn' ) ) ; importPgnEl . onclick = ( ) => this . importDialog_ . show ( ) ; const handler = new BuildBoardHandler ( this . treeModel_ , this . treeNavigator_ , this . buildModeView_ , currentRepertoireUpdater ) ; chessgroundBoardFactory . createBoardAndSetDelegate ( this . board_ , 'buildBoard' , handler , false ) ; this . buildModeElement_ = assert ( document . getElementById ( 'buildMode' ) ) ; this . buildButton_ = assert ( document . getElementById ( 'buildButton' ) ) ; this . buildButton_ . onclick = ( ) => this . modeManager_ . selectModeType ( ModeType . BUILD ) ; } preEnter ( ) { this . board_ . setInitialPositionImmediately ( ) ; return this . pickerController_ . updatePicker ( ) . then ( ( ) => this . notifySelectedMetadata ( ) ) ; } exit ( ) { this . buildModeElement_ . classList . add ( 'hidden' ) ; this . buildButton_ . classList . remove ( 'selectedMode' ) ; return Promise . resolve ( ) ; } postEnter ( ) { this . impressionSender_ . sendImpression ( ImpressionCode . ENTER_BUILD_MODE ) ; this . buildModeElement_ . classList . remove ( 'hidden' ) ; this . buildButton_ . classList . add ( 'selectedMode' ) ; this . board_ . redraw ( ) ; return Promise . resolve ( ) ; } onKeyDown ( e ) { if ( this . renameInput_ . isFocused ( ) ) { return ; } if ( this . importDialog_ . isVisible ( ) ) { this . importDialog_ . onKeyDown ( e ) ; return ; } if ( e . keyCode == 83 ) { this . modeManager_ . selectModeType ( ModeType . STUDY ) ; } else if ( e . keyCode == 82 ) { this . modeManager_ . selectModeType ( ModeType . EVALUATE ) ; } else if ( e . keyCode == 77 ) { this . soundToggler_ . toggle ( ) ; } else if ( e . keyCode == 70 ) { this . treeController_ . flipRepertoireColor ( ) ; } else if ( e . keyCode == 37 ) { this . treeNavigator_ . selectLeft ( ) ; e . preventDefault ( ) ; } else if ( e . keyCode == 38 ) { this . treeNavigator_ . selectUp ( ) ; e . preventDefault ( ) ; } else if ( e . keyCode == 39 ) { this . treeNavigator_ . selectRight ( ) ; e . preventDefault ( ) ; } else if ( e . keyCode == 40 ) { this . treeNavigator_ . selectDown ( ) ; e . preventDefault ( ) ; } else if ( e . keyCode == 8 ) { this . treeController_ . trash ( ) ; } } notifySelectedMetadata ( ) { if ( this . pickerController_ . isModelEmpty ( ) ) { return Promise . resolve ( ) ; } const selectedMetadataId = this . pickerController_ . getSelectedMetadataId ( ) ; return this . server_ . loadRepertoire ( selectedMetadataId ) . then ( repertoire => this . onLoadRepertoire_ ( repertoire ) ) ; } private onLoadRepertoire_ ( repertoire ) { this . treeModel_ . loadRepertoire ( repertoire ) ; this . buildModeView_ . refresh ( ) ; } }