import { ErrorRenderer } from "./error_renderer" ; import { Location } from "./location" ; import { Snapshot } from "./snapshot" ; import { RenderCallback , RenderDelegate , SnapshotRenderer } from "./snapshot_renderer" ; export type RenderOptions = { snapshot : Snapshot ; error : string ; isPreview : boolean ; } ; export class View { readonly delegate : RenderDelegate ; readonly htmlElement = document . documentElement as HTMLHtmlElement ; constructor ( delegate ) { this . delegate = delegate ; } getRootLocation ( ) { return this . getSnapshot ( ) . getRootLocation ( ) ; } getElementForAnchor ( anchor ) { return this . getSnapshot ( ) . getElementForAnchor ( anchor ) ; } getSnapshot ( ) { return Snapshot . fromHTMLElement ( this . htmlElement ) ; } render ( { snapshot , error , isPreview } , callback ) { this . markAsPreview ( isPreview ) ; if ( snapshot ) { this . renderSnapshot ( snapshot , isPreview , callback ) ; } else { this . renderError ( error , callback ) ; } } markAsPreview ( isPreview ) { if ( isPreview ) { this . htmlElement . setAttribute ( "data-turbolinks-preview" , "" ) ; } else { this . htmlElement . removeAttribute ( "data-turbolinks-preview" ) ; } } renderSnapshot ( snapshot , isPreview , callback ) { SnapshotRenderer . render ( this . delegate , callback , this . getSnapshot ( ) , snapshot , isPreview || false ) ; } renderError ( error , callback ) { ErrorRenderer . render ( this . delegate , callback , error || "" ) ; } }