import { IQuestion , QuestionFileModel , surveyLocalization } from 'survey-core' ; import { SurveyPDF } from '../survey' ; import { IPoint , ISize , DocController } from '../doc_controller' ; import { FlatQuestion } from './flat_question' ; import { FlatRepository } from './flat_repository' ; import { IPdfBrick } from '../pdf_render/pdf_brick' ; import { TextBrick } from '../pdf_render/pdf_text' ; import { CompositeBrick } from '../pdf_render/pdf_composite' ; import { SurveyHelper } from '../helper_survey' ; export class FlatFile extends FlatQuestion { public static readonly IMAGE_GAP_SCALE : number = 0.195 ; public static readonly TEXT_MIN_SCALE : number = 5.0 ; protected question : QuestionFileModel ; public constructor ( protected survey , question , controller ) { super ( survey , question , controller ) ; this . question = < QuestionFileModel > question ; } private async generateFlatItem ( point , item ) { const compositeFlat = new CompositeBrick ( await SurveyHelper . createLinkFlat ( point , this . question , this . controller , item . name === undefined ? 'image' : item . name , item . content ) ) ; if ( SurveyHelper . canPreviewImage ( this . question , item , item . content ) ) { const imageSize = await SurveyHelper . getImageSize ( item . content ) ; if ( this . question . imageWidth ) { imageSize . width = SurveyHelper . parseWidth ( this . question . imageWidth , SurveyHelper . getPageAvailableWidth ( this . controller ) ) ; } if ( this . question . imageHeight ) { imageSize . height = SurveyHelper . parseWidth ( this . question . imageHeight , SurveyHelper . getPageAvailableWidth ( this . controller ) ) ; } const imagePoint = SurveyHelper . createPoint ( compositeFlat ) ; imagePoint . yTop += this . controller . unitHeight * FlatFile . IMAGE_GAP_SCALE ; compositeFlat . addBrick ( SurveyHelper . createImageFlat ( imagePoint , this . question , this . controller , item . content , imageSize . width , imageSize . height ) ) ; } return compositeFlat ; } private addLine ( rowsFlats , currPoint , index ) { if ( index !== this . question . previewValue . length - 1 ) { rowsFlats [ rowsFlats . length - 1 ] . addBrick ( SurveyHelper . createRowlineFlat ( currPoint , this . controller ) ) ; currPoint . yTop += SurveyHelper . EPSILON ; rowsFlats . push ( new CompositeBrick ( ) ) ; } } public async generateFlatsContent ( point ) { if ( this . question . previewValue . length === 0 ) { return [ await SurveyHelper . createTextFlat ( point , this . question , this . controller , surveyLocalization . getString ( 'noFileChosen' ) , TextBrick ) ] ; } const rowsFlats = [ new CompositeBrick ( ) ] ; const currPoint = SurveyHelper . clone ( point ) ; let yBot = currPoint . yTop ; for ( let i = 0 ; i < this . question . previewValue . length ; i ++ ) { const item = this . question . previewValue [ i ] ; const availableWidth = this . controller . paperWidth - this . controller . margins . right - currPoint . xLeft ; if ( SurveyHelper . canPreviewImage ( this . question , item , item . content ) ) { let contentWidth = ( await SurveyHelper . getImageSize ( item . content ) ) . width ; if ( contentWidth === undefined ) { if ( ! ! this . question . imageWidth ) { contentWidth = SurveyHelper . parseWidth ( this . question . imageWidth , SurveyHelper . getPageAvailableWidth ( this . controller ) ) ; } else { contentWidth = 0 ; } } const compositeWidth = Math . max ( contentWidth , FlatFile . TEXT_MIN_SCALE * this . controller . unitWidth ) ; if ( availableWidth < compositeWidth ) { currPoint . xLeft = point . xLeft ; currPoint . yTop = yBot ; this . addLine ( rowsFlats , currPoint , i ) ; } this . controller . pushMargins ( currPoint . xLeft , this . controller . paperWidth - currPoint . xLeft - compositeWidth ) ; const itemFlat = await this . generateFlatItem ( currPoint , item ) ; rowsFlats [ rowsFlats . length - 1 ] . addBrick ( itemFlat ) ; currPoint . xLeft += itemFlat . width ; yBot = Math . max ( yBot , itemFlat . yBot ) ; this . controller . popMargins ( ) ; } else { if ( availableWidth < this . controller . unitWidth ) { currPoint . xLeft = point . xLeft ; currPoint . yTop = yBot ; this . addLine ( rowsFlats , currPoint , i ) ; } const itemFlat = await this . generateFlatItem ( currPoint , item ) ; rowsFlats [ rowsFlats . length - 1 ] . addBrick ( itemFlat ) ; currPoint . xLeft += itemFlat . xRight - itemFlat . xLeft ; yBot = Math . max ( yBot , itemFlat . yBot ) ; } currPoint . xLeft += this . controller . unitWidth ; } return rowsFlats ; } } FlatRepository . getInstance ( ) . register ( 'file' , FlatFile ) ;