import lineClamp from 'line-clamp'; import { isListedToText } from 'di2-types'; import { IConnectionCompanyData } from '../connection-map.component'; import { D3Selection, ConnectionCompanySVGSelection } from './subsidiary-list-group.class'; import get from 'lodash/get'; import has from 'lodash/has'; import uniq from 'lodash/uniq'; import values from 'lodash/values'; export const clamp = ( selection: D3Selection, selector: string, lines: number ): void => { const textNodes = (selection.node() as SVGElement).querySelectorAll( selector ); Array.prototype.forEach.call(textNodes, textNode => lineClamp(textNode, lines) ); }; export const listedCountryText = (data: IConnectionCompanyData) => { if (data.isScoutCompany) { return `${isListedToText(get(data, ['company', 'isListed'], ''))} in ${data.country}`; } else { return `Non-Scout company in ${data.country}`; } }; export const sectorText = data => { if (!has(data, ['company', 'icb_5_primary'])) return 'Unknown'; const icbPrimary = data.company.icb_5_primary; const sectors = []; icbPrimary.forEach(sector => { const sect = []; for (let key in sector) { sect.push(sector[key]); } sectors.push(sect.join(' > ')); }); return sectors.join(', '); }; export const limitedSectorText = ( limit: number, data: IConnectionCompanyData ): string => { const icbPrimary = get(data, ['company', 'icb_5_primary']); if (!icbPrimary) return 'Unknown'; const sectors = uniq(icbPrimary.map(sector => values(sector).join(' > '))); while (sectors.join(', ').length > limit && sectors.length > 1) { sectors.pop(); } return ellipsis(sectors.join(', '), limit); }; export const selectionHeight = ( selection: ConnectionCompanySVGSelection ): number => selection && selection.node() ? selection.node().getBoundingClientRect().height : 0; export const ellipsis = (text: string, maxLength: number): string => text.length < maxLength ? text : text .slice(0, maxLength) .trim() .concat('...'); export const maxSelectionHeight = ( selections: Array ) => Math.max(...selections.map(selectionHeight));