All files / src/ui PrimitiveList.js

55% Statements 22/40
18.75% Branches 3/16
40% Functions 4/10
55% Lines 22/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 1671x 1x 1x 1x   1x 1x 1x 1x   1x       5800x                                                       11600x   11600x     20300x         11600x 11600x                                                                                                                                       300x 300x 300x 8700x                           8700x                       300x   300x                            
import React, {Component} from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types/prop-types';
import {PrimitiveGroup as PrimitiveGroupModel} from '../parsers/primitives';
import {Primitive as LanguagePrimitive} from '../parsers/primitives';
import {DragPrimitiveSource} from '../dnd';
import {say} from '../utils';
import SHARED from '../shared';
import {copyNodes} from '../actions';
 
require('./PrimitiveList.less');
 
 
@DragPrimitiveSource
class Primitive extends Component {
  static propTypes = {
    primitive: PropTypes.instanceOf(LanguagePrimitive).isRequired,
    className: PropTypes.string.isRequired,
    onFocus: PropTypes.func.isRequired,
    onBlur: PropTypes.func.isRequired,
    onKeyDown: PropTypes.func.isRequired,
    searchString: PropTypes.string,
    connectDragPreview: PropTypes.func.isRequired,
    connectDragSource: PropTypes.func.isRequired,
  }
 
  handleKeyDown = e => {
    switch (SHARED.keyMap[SHARED.keyName(e)]) {
    case 'copy':
      e.preventDefault();
      copyNodes([this.props.primitive]);
      say("copied " + this.props.primitive.toString());
      this.props.primitive.element?.focus(); // restore focus
      return;
    default:
      this.props.onKeyDown(e);
      return;
    }
  }
  
  render() {
    let {primitive, className, onFocus,
      connectDragPreview, connectDragSource} = this.props;
    let elem = (
      <span tabIndex={-1}
            onKeyDown={this.handleKeyDown}
            onFocus={() => onFocus(primitive)}
            ref = {elem => primitive.element = elem}
            className={classNames(className, "Primitive list-group-item")}>
        {primitive.name}
      </span>
    );
    elem = connectDragPreview(connectDragSource(elem), {offsetX: 1, offsetY: 1});
    return (<li>{elem}</li>);
  }
}
 
export class PrimitiveGroup extends Component {
  static defaultProps = {
    group: {
      name: '',
      primitives: []
    }
  }
 
  static propTypes = {
    onFocus: PropTypes.func.isRequired,
    onBlur: PropTypes.func.isRequired,
    onKeyDown: PropTypes.func.isRequired,
    selected: PropTypes.string, // to start, no primitive is selected
    group: PropTypes.object,
  }
 
  state = {
    expanded: false
  }
 
  toggleExpanded = () => {
    this.setState({expanded: !this.state.expanded});
  }
 
  render() {
    let {group, onFocus, onBlur, onKeyDown, selected} = this.props;
    let expanded = this.state.expanded;
    let expandoClass = classNames(
      'glyphicon',
      expanded ? 'glyphicon-minus' : 'glyphicon-plus'
    );
    return (
      <li className="PrimitiveGroup list-group-item" role="list">
        <div onFocus={this.toggleExpanded} className="group-header">
          <span className={expandoClass} aria-hidden="true"/>
        </div>
        {expanded ?
          <PrimitiveList
            primitives={group.primitives}
            onFocus={onFocus}
            onBlur={onBlur}
            onKeyDown={onKeyDown}
            selected={selected}
          />
          : null}
      </li>
    );
  }
}
 
export default class PrimitiveList extends Component {
  static defaultProps = {
    selected: null,
  }
 
  static propTypes = {
    onFocus: PropTypes.func.isRequired,
    onBlur: PropTypes.func.isRequired,
    onKeyDown: PropTypes.func.isRequired,
    selected: PropTypes.string,
    primitives: PropTypes.array,
    searchString: PropTypes.string
  }
  render() {
    const {primitives, selected, onFocus, onBlur, onKeyDown, searchString} = this.props;
    let nodes = [];
    for (let primitive of primitives) {
      Iif (primitive instanceof PrimitiveGroupModel) {
        // this is a group.
        nodes.push(
          <PrimitiveGroup
            key={primitive.name}
            group={primitive}
            onFocus={onFocus}
            onBlur={onBlur}
            onKeyDown={onKeyDown}
            selected={selected}
          />
        );
        continue;
      }
      nodes.push(
        <Primitive
          key={primitive.name}
          primitive={primitive}
          onFocus={onFocus}
          onBlur={onBlur}
          onKeyDown={onKeyDown}
          className={selected == primitive ? 'selected' : ''}
        />
      );
 
    }
    const text = searchString? (primitives.length == 0? "No" : primitives.length) + " blocks found" : "blocks";
 
    return (
      <div>
        <h3 
          id="toolbar_heading" 
          className="screenreader-only" 
          aria-live="assertive" 
          atomic="true">
            {text}
          </h3>
        <ul className="PrimitiveList list-group" aria-labelledby="toolbar_heading">{nodes}</ul>
      </div>
    );
  }
}