import { getUtcDate , getUtcTime } from '../../../util/datetime' ; import { TreeModel } from '../tree/treemodel' ; export class CurrentRepertoireExporter { private treeModel_ : TreeModel ; constructor ( treeModel ) { this . treeModel_ = treeModel ; } exportCurrentRepertoire ( ) { const now = new Date ( ) ; const linkEl = document . createElement ( 'a' ) ; linkEl . style . display = 'none' ; linkEl . download = this . getExportFilename_ ( now ) ; const contents = this . getExportContents_ ( now ) ; linkEl . href = `data:application/x-chess-pgn,${ contents }` ; document . body . appendChild ( linkEl ) ; linkEl . click ( ) ; document . body . removeChild ( linkEl ) ; } private getExportContents_ ( now ) { const tags = this . getExportTags_ ( now ) ; let tagsString = '' ; for ( const key in tags ) { tagsString += `[${ key } "${ tags [ key ] }"]\n` ; } const moves = this . treeModel_ . exportToPgn ( ) ; return encodeURIComponent ( `${ tagsString }\n${ moves }` ) ; } private getExportTags_ ( now ) { return { 'Event' : this . treeModel_ . getRepertoireName ( ) , 'Site' : 'http://studyopenings.com' , 'UTCDate' : getUtcDate ( now ) , 'UTCTime' : getUtcTime ( now ) , 'Result' : '*' } ; } private getExportFilename_ ( now ) { const name = this . treeModel_ . getRepertoireName ( ) ; const formattedName = name . toLocaleLowerCase ( ) . trim ( ) . replace ( /[^a-z0-9\s]+/g , '' ) . replace ( /\s+/g , '-' ) ; const utcDate = getUtcDate ( now ) ; return `studyopenings_${ formattedName }_${ utcDate }.pgn` ; } }