import { IQuestion , QuestionMatrixModel , MatrixRowModel , QuestionRadiogroupModel , ItemValue , Serializer } from 'survey-core' ; import { SurveyPDF } from '../survey' ; import { DocController , IPoint , IRect } from '../doc_controller' ; import { FlatQuestion } from './flat_question' ; import { FlatRadiogroup } from './flat_radiogroup' ; 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 FlatMatrix extends FlatQuestion { public static readonly GAP_BETWEEN_ROWS : number = 0.5 ; protected question : QuestionMatrixModel ; constructor ( protected survey , question , protected controller ) { super ( survey , < QuestionRadiogroupModel > question , controller ) ; this . question = < QuestionMatrixModel > question ; } protected async generateFlatsHeader ( point ) { const headers = [ ] ; const currPoint = SurveyHelper . clone ( point ) ; const colCount = this . question . visibleColumns . length + ( this . question . hasRows ? 1 : 0 ) ; for ( let i = 0 ; i < this . question . visibleColumns . length ; i ++ ) { const column = this . question . hasRows ? i + 1 : i ; this . controller . pushMargins ( ) ; SurveyHelper . setColumnMargins ( this . controller , colCount , column ) ; currPoint . xLeft = this . controller . margins . left ; headers . push ( await SurveyHelper . createBoldTextFlat ( currPoint , this . question , this . controller , this . question . visibleColumns [ i ] . locText ) ) ; this . controller . popMargins ( ) ; } const compositeBrick = new CompositeBrick ( ... headers ) ; return [ compositeBrick , SurveyHelper . createRowlineFlat ( SurveyHelper . createPoint ( compositeBrick ) , this . controller ) ] ; } protected async generateFlatsRows ( point , isVertical ) { const cells = [ ] ; let currPoint = SurveyHelper . clone ( point ) ; for ( let i = 0 ; i < this . question . visibleRows . length ; i ++ ) { const key = 'row' + i ; const flatsRow = await new FlatMatrixRow ( this . survey , this . question , this . controller , this . question . visibleRows [ i ] , key , i == 0 , isVertical ) . generateFlatsContent ( currPoint ) ; currPoint = SurveyHelper . createPoint ( SurveyHelper . mergeRects ( ... flatsRow ) ) ; currPoint . yTop += this . controller . unitHeight * FlatMatrix . GAP_BETWEEN_ROWS ; cells . push ( ... flatsRow ) ; } return cells ; } public async generateFlatsContent ( point ) { const cellWidth = SurveyHelper . getColumnWidth ( this . controller , this . question . visibleColumns . length + ( this . question . hasRows ? 1 : 0 ) ) ; const isVertical = this . question . renderAs === 'list' || this . controller . matrixRenderAs === 'list' || cellWidth < this . controller . measureText ( SurveyHelper . MATRIX_COLUMN_WIDTH ) . width ; let currPoint = SurveyHelper . clone ( point ) ; const cells = [ ] ; if ( ! isVertical && this . question . showHeader && this . question . visibleColumns . length != 0 ) { let headers = await this . generateFlatsHeader ( currPoint ) ; currPoint = SurveyHelper . createPoint ( SurveyHelper . mergeRects ( ... headers ) ) ; currPoint . yTop += FlatMatrix . GAP_BETWEEN_ROWS * this . controller . unitHeight ; cells . push ( ... headers ) ; } cells . push ( ... await this . generateFlatsRows ( currPoint , isVertical ) ) ; return cells ; } } export class FlatMatrixRow extends FlatRadiogroup { protected questionMatrix : QuestionMatrixModel ; public constructor ( protected survey , question , protected controller , private row , private key , protected isFirst = false , protected isVertical = false ) { super ( survey , question , controller ) ; this . questionMatrix = < QuestionMatrixModel > question ; } public async generateFlatsContent ( point ) { return this . isVertical ? await this . generateFlatsVerticallyCells ( point ) : await this . generateFlatsHorizontallyCells ( point ) ; } protected async generateTextComposite ( point , column , index ) { const currPoint = SurveyHelper . clone ( point ) ; const checked = this . row . value == column . value ; const itemRect = SurveyHelper . createRect ( currPoint , SurveyHelper . getPageAvailableWidth ( this . controller ) , this . controller . unitHeight ) ; const radioFlat = this . generateFlatItem ( itemRect , column , index , this . key , checked ) ; currPoint . yTop = radioFlat . yBot + this . controller . unitHeight * SurveyHelper . GAP_BETWEEN_ITEM_TEXT ; const cellTextFlat = await SurveyHelper . createTextFlat ( currPoint , this . questionMatrix , this . controller , this . questionMatrix . getCellDisplayLocText ( this . row . name , column ) , TextBrick ) ; return new CompositeBrick ( radioFlat , cellTextFlat ) ; } protected async generateItemCompoiste ( point , column , index ) { const currPoint = SurveyHelper . clone ( point ) ; const checked = this . row . value == column . value ; const itemRect = SurveyHelper . createRect ( currPoint , this . controller . unitHeight , this . controller . unitHeight ) ; const radioFlat = this . generateFlatItem ( SurveyHelper . moveRect ( SurveyHelper . scaleRect ( itemRect , SurveyHelper . SELECT_ITEM_FLAT_SCALE ) , itemRect . xLeft ) , column , index , this . key , checked ) ; currPoint . xLeft = radioFlat . xRight + this . controller . unitWidth * SurveyHelper . GAP_BETWEEN_ITEM_TEXT ; const radioText = await SurveyHelper . createTextFlat ( currPoint , this . questionMatrix , this . controller , column . locText , TextBrick ) ; return new CompositeBrick ( radioFlat , radioText ) ; } protected async generateFlatsHorizontallyCells ( point ) { const cells = [ ] ; const currPoint = SurveyHelper . clone ( point ) ; const colCount = this . questionMatrix . visibleColumns . length + ( this . questionMatrix . hasRows ? 1 : 0 ) ; if ( this . questionMatrix . hasRows ) { this . controller . pushMargins ( ) ; SurveyHelper . setColumnMargins ( this . controller , colCount , 0 ) ; currPoint . xLeft = this . controller . margins . left ; cells . push ( await SurveyHelper . createTextFlat ( currPoint , this . questionMatrix , this . controller , this . row . locText , TextBrick ) ) ; this . controller . popMargins ( ) ; } for ( let i = 0 ; i < this . questionMatrix . visibleColumns . length ; i ++ ) { const column = this . questionMatrix . visibleColumns [ i ] ; const checked = this . row . value == column . value ; const columnNumber = this . questionMatrix . hasRows ? i + 1 : i ; this . controller . pushMargins ( ) ; SurveyHelper . setColumnMargins ( this . controller , colCount , columnNumber ) ; currPoint . xLeft = this . controller . margins . left ; if ( this . questionMatrix . hasCellText ) { cells . push ( await this . generateTextComposite ( currPoint , column , i ) ) ; } else { const itemRect = SurveyHelper . createRect ( currPoint , this . controller . unitHeight , this . controller . unitHeight ) ; cells . push ( this . generateFlatItem ( SurveyHelper . moveRect ( SurveyHelper . scaleRect ( itemRect , SurveyHelper . SELECT_ITEM_FLAT_SCALE ) , currPoint . xLeft ) , column , i , this . key , checked ) ) ; } this . controller . popMargins ( ) ; } const compositeBrick = new CompositeBrick ( ... cells ) ; return [ compositeBrick , SurveyHelper . createRowlineFlat ( SurveyHelper . createPoint ( compositeBrick ) , this . controller ) ] ; } protected async generateFlatsVerticallyCells ( point ) { const cells = [ ] ; const currPoint = SurveyHelper . clone ( point ) ; if ( this . questionMatrix . hasRows ) { const rowTextFlat = await SurveyHelper . createTextFlat ( currPoint , this . questionMatrix , this . controller , this . row . locText , TextBrick ) ; currPoint . yTop = rowTextFlat . yBot + FlatQuestion . CONTENT_GAP_VERT_SCALE * this . controller . unitHeight ; cells . push ( rowTextFlat ) ; } this . generateFlatComposite = ( this . questionMatrix . hasCellText ) ? this . generateTextComposite : this . generateItemCompoiste ; cells . push ( ... await this . generateVerticallyItems ( currPoint , this . questionMatrix . visibleColumns ) ) ; const compositeBrick = new CompositeBrick ( ... cells ) ; return [ compositeBrick , SurveyHelper . createRowlineFlat ( SurveyHelper . createPoint ( compositeBrick ) , this . controller ) ] ; } } Serializer . removeProperty ( 'matrix' , 'renderAs' ) ; Serializer . addProperty ( 'matrix' , { name : 'renderAs' , default : 'auto' , choices : [ 'auto' , 'list' ] } ) ; FlatRepository . getInstance ( ) . register ( 'matrix' , FlatMatrix ) ;