import * as React from 'react'; import { hcl } from 'd3-color'; import { ElementModel } from '../data/model'; import { DiagramView } from '../diagram/view'; export interface ListElementViewProps { className?: string; view: DiagramView; model: ElementModel; highlightText?: string; disabled?: boolean; selected?: boolean; onClick?: (event: React.MouseEvent, model: ElementModel) => void; onDragStart?: React.HTMLProps['onDragStart']; } const CLASS_NAME = 'ontodia-list-element-view'; export class ListElementView extends React.Component { render() { const {className, view, model, highlightText, disabled, selected, onDragStart} = this.props; const {h, c, l} = view.getTypeStyle(model.types).color; const frontColor = (selected && !disabled) ? hcl(h, c, l * 1.2) : hcl('white'); let classNames = `${CLASS_NAME}`; classNames += disabled ? ` ${CLASS_NAME}--disabled` : ''; classNames += className ? ` ${className}` : ''; const localizedText = view.formatLabel(model.label.values, model.id); const classesString = model.types.length > 0 ? `\nClasses: ${view.getElementTypeString(model)}` : ''; return
  • {highlightSubstring(localizedText, highlightText)}
  • ; } private onClick = (event: React.MouseEvent) => { const {disabled, model, onClick} = this.props; if (!disabled && onClick) { event.persist(); onClick(event, model); } } } export function startDragElements(e: React.DragEvent<{}>, iris: ReadonlyArray) { try { e.dataTransfer.setData('application/x-ontodia-elements', JSON.stringify(iris)); } catch (ex) { // IE fix e.dataTransfer.setData('text', JSON.stringify(iris)); } return false; } const DEFAULT_HIGHLIGHT_PROPS: React.HTMLProps = { className: `ontodia-text-highlight` }; export function highlightSubstring( text: string, substring: string | undefined, highlightProps = DEFAULT_HIGHLIGHT_PROPS, ) { if (!substring) { return {text}; } const start = text.toLowerCase().indexOf(substring.toLowerCase()); if (start < 0) { return {text}; } const end = start + substring.length; const before = text.substring(0, start); const highlighted = text.substring(start, end); const after = text.substring(end); return {before}{highlighted}{after}; }