import { elt , on , off } from '../utils/dom' ; import { execKey } from './key-events' ; import Core , { CoreOptions } from './core' ; import ViewController from './view-controller' ; import ViewProxy from './view-proxy' ; import { ViewOptions } from './view' ; import { CoreMethod } from './types/core' ; /**
 * Our unique id of the our views, we send this through to xi-core when
 * requesting a new view.
 */ let viewInstanceId = 0 ; /**
 * Configuration options for each Workspace.
 */ export type WorkspaceOptions = { filePath ? : string ; coreOptions : CoreOptions ; viewOptions : ViewOptions ; } ; /**
 * Top parent class that controls everything.
 *   TODO: listen for commands
 *   TODO: listen for user input
 *   TODO: manage clipboard, etc
 *   TODO: manage views
 *   TODO: manage settings
 */ export default class Workspace { public wrapper : HTMLElement ; private controllers : ViewController [ ] ; private core : Core ; param { HTMLElement } place param { WorkspaceOptions } opts constructor ( place , opts ) { this . controllers = [ ] ; this . wrapper = place . appendChild ( elt ( 'div' , null , 'xi-workspace' , 'height: 100%; width: 100%' ) ) ; on ( ( < any > window ) , 'keydown' , this . keyedInput . bind ( this ) , false ) ; on ( ( < any > window ) , 'keypress' , this . keyedInput . bind ( this ) , false ) ; on ( ( < any > window ) , 'mousedown' , this . mousedown . bind ( this ) , false ) ; this . core = new Core ( opts . coreOptions ) ; this . core . on ( CoreMethod . NEW_VIEW , ( proxy ) => { this . controllers . push ( new ViewController ( this , proxy , opts . viewOptions ) ) ; } ) ; this . core . send ( CoreMethod . NEW_VIEW , { file_path : opts . filePath } , { id : viewInstanceId ++ } ) ; window . onbeforeunload = this . beforeUnload . bind ( this ) ; } param event public beforeUnload ( _event ) { this . core . close ( ) ; } return { ViewController } public activeViewController ( ) { return this . controllers . find ( ( controller ) => controller . isFocused ( ) ) ; } param { KeyboardEvent } event private keyedInput ( event ) { const controller = this . activeViewController ( ) ; if ( controller && execKey ( controller , event ) ) { event . preventDefault ( ) ; } } param { MouseEvent } event private mousedown ( event ) { const controller = this . controllers . find ( ( c ) => c . getWrapperElement ( ) . contains ( ( < Node > event . target ) ) ) ; if ( ! controller ) { return ; } controller . doClick ( event ) ; const onMove = ( event ) => controller . doDrag ( event ) ; const onUp = ( _event ) => { off ( ( < any > window ) , 'mousemove' , onMove , false ) ; off ( ( < any > window ) , 'mouseup' , onUp , false ) ; } ; on ( ( < any > window ) , 'mousemove' , onMove , false ) ; on ( ( < any > window ) , 'mouseup' , onUp , false ) ; } } /**
 * Current thoughts...
 *  - should cut/copy/paste be implemented in the frontend or backend?
 */