import { UUID } from 'angular2-uuid'; import { DimensionsInterface, LayoutBoxInterface, PictureInterface, ProductViewImageInterface, ProductViewImageModel, ProductViewItemModel, ProductViewTextInterface, } from '../index'; import { TextLogicFunctions, } from './../../helpers/functions/index'; class LayoutBoxModel { public static needsPicture(layoutBox: LayoutBoxInterface) { const productViewImage = ( layoutBox.containedItem ) as ProductViewImageInterface; return Boolean( productViewImage == null || ( ProductViewImageModel.isProductViewImage( productViewImage, ) && productViewImage.picture == null && !productViewImage.isLoading ), ); } public static needsText(layoutBox: LayoutBoxInterface) { const productViewText = ( layoutBox.containedItem ) as ProductViewTextInterface; return Boolean( productViewText == null || ( ProductViewItemModel.isText( productViewText, ) && productViewText.text.length === 0 ), ); } public static isTextBox(layoutBox: LayoutBoxInterface) { if (layoutBox) { if (layoutBox.contentType) { return layoutBox.contentType === 'text'; } return ProductViewItemModel.isText(layoutBox.containedItem); } return false; } public static getCountOfTypes(layoutBoxes: LayoutBoxInterface[]) { let textBoxCount = 0; let imageBoxCount = 0; layoutBoxes.forEach((layoutBox) => { if (LayoutBoxModel.isTextBox(layoutBox)) { textBoxCount++; } else { imageBoxCount++; } }); return { imageBoxCount, textBoxCount, }; } public static isTooLowQualityToPrint(layoutBox: LayoutBoxInterface) { return Boolean( !LayoutBoxModel.isTextBox(layoutBox) && layoutBox.containedItem && ( layoutBox .containedItem as ProductViewImageInterface ).isTooLowQualityToPrint, ); } public static containsPicture( layoutBox: LayoutBoxInterface, picture: PictureInterface, ) { return !LayoutBoxModel.isTextBox(layoutBox) && layoutBox.containedItem != null && (layoutBox.containedItem as ProductViewImageInterface) .picture.url === picture.url; } public static getTextWhichFitsinLayoutBox( layoutBox: LayoutBoxInterface, parentHeight: number, ) { const newLineChar = '\n'; const productViewText = layoutBox .containedItem as ProductViewTextInterface; let { text } = productViewText; if ( text && productViewText.font ) { while ( TextLogicFunctions.getTextVerticalHeight( text, productViewText.font.fontSizePx, productViewText.font.lineHeight, ) > parentHeight * layoutBox.dimensionsPercentages.height ) { const splitText = text.split(newLineChar); splitText.pop(); text = splitText.join(newLineChar); } /** * If there is another line of text we include this in the PDF. * This is to catch places where a user saw there text half cut off * yet decided to continue anyway. Because the text areas are never * that close to the edge it will not cause problems. */ const unusedText = productViewText.text.replace(text, ''); const splitRemainingText = unusedText.split(newLineChar); if (unusedText.length && splitRemainingText.length > 1) { text = text + newLineChar + splitRemainingText[1]; } } return text; } public static initialise( layoutBox: LayoutBoxInterface, ): LayoutBoxInterface { return { ...layoutBox, id: UUID.UUID(), }; } } export default LayoutBoxModel;