Code coverage report for components/sortable_table/row.js

Statements: 90.2% (46 / 51)      Branches: 73.08% (19 / 26)      Functions: 83.33% (15 / 18)      Lines: 90.2% (46 / 51)      Ignored: none     

All files » components/sortable_table/ » row.js
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 2561 1 1 1 1 1 1 1                   1 36       1                           1               33             1 1       36   36 36       36   36         36                       36   36 3             36 270             36                   3       3               36 36       36 36         36                 36               30               30                                               36       36               36                 36                   30             30           30                 30                                       1
var React = window.React || require('react/addons');
var _ = require('lodash');
var Estimator = require('../estimator');
var Status = require('../status');
var Tags = require('../tags');
var TagEditor = require('../tag_editor');
var Styles = require('../../styles/sortable_table');
var moment = require('moment');
 
/*
 * Renders a single table row for displaying item data.
 * Row accepts expanded prop for controlling whether the row appears
 * condensed (default) or optionally expanded (if using row with the sortable TableHeader.
 * Toggling between the two states happens via the Expander element in TableHeader.)
 * TODO(fw): reorg styles so don't have to calculate here.
 */
 
var abbreviateUsername = function (user) {
  return user.last_name ? user.first_name + ' ' + user.last_name[0] + '.' :
    user.first_name;
};
 
var TableRow = React.createClass({
  propTypes: {
    model: React.PropTypes.object.isRequired,
    columns: React.PropTypes.arrayOf(React.PropTypes.string).isRequired,
    expanded: React.PropTypes.string,
    modelChangerUtilities: React.PropTypes.object,
    navigatorUtility: React.PropTypes.object,
    isBulkEditable: React.PropTypes.bool,
    onBulkSelect: React.PropTypes.func
  },
 
  mixins: [Styles],
 
  getDefaultProps: function() {
    return {
      expanded: 'condensed',
      modelChangerUtilities: {},
      onBulkSelect: _.noop()
    };
  },
 
  getInitialState: function() {
    return {
      hover: false
    };
  },
 
  bulkSelectClicked: function(ev) {
    // WIP: This will likely change.
    ev.stopPropagation();
    this.props.onBulkSelect(this.props.model);
  },
 
  render: function() {
    var modelId = [this.props.model.product.id, this.props.model.number];
 
    var condensed = this.props.expanded === 'condensed' ? true : false;
    var wrapperStyles = condensed ? Styles.cell.condensed : Styles.cell.expanded;
 
    // "matched": item is grouped with parent/subitems.
    // "nonMatching": item doesn't fit collection filters, but is included to match w/subitems that do.
    var rowStyles = _.extend({}, Styles.row[this.props.model.type],
      !condensed ? Styles.row.expanded : null);
    Iif (this.props.model.isMatched) {
      rowStyles = _.extend({}, rowStyles, Styles.row.matched,
        this.props.model.isNonMatching ? Styles.row.nonMatching : null);
    }
 
    var columnMap = {
      product: this.buildProductCell,
      number: this.buildNumberCell,
      size: this.buildEstimateCell,
      status: this.buildStatusCell,
      title: this.buildTitleCell,
      tags: this.buildTagsCell,
      'created by': this.buildCreatedByCell,
      'assigned to': this.buildAssigneeCell,
      created: this.buildCreatedAtCell
    };
 
    var cells = [];
 
    if (this.props.isBulkEditable) {
      cells.push(
        <td key={'control' + ':' + modelId} style={Styles.cell.base}>
          {this.buildControlCell(wrapperStyles)}
        </td>
      );
    }
 
    _.each(this.props.columns, function(column) {
      cells.push(
        <td key={column + ':' + modelId} style={Styles.cell.base}>
          {columnMap[column](wrapperStyles, modelId)}
        </td>
      );
    }, this);
 
    return (
      <tr className={'table-row' + this.props.expanded} style={rowStyles}>
        {cells}
      </tr>
    );
  },
 
  buildControlCell: function(styles) {
    // Left border on matched rows causes padding weirdness in checkboxes,
    // so we add corrective styles on matched rows.
    var controlStyles = this.props.model.isMatched ?
      _.extend({}, styles, Styles.cell.narrow, Styles.cell.borderCorrection) :
      _.extend({}, styles, Styles.cell.narrow);
 
    return (
      <div style={controlStyles}>
        <input type="checkbox" onClick={this.bulkSelectClicked} />
      </div>
    );
  },
 
  buildProductCell: function(styles) {
    var subitemArrow = null;
    var subitemArrowStyles = this.props.expanded === 'expanded' ?
      _.extend({}, Styles.cell.subitemArrow, Styles.cell.subitemArrowExpanded) :
      Styles.cell.subitemArrow;
 
    Eif (this.props.model.parent) {
      subitemArrow = (
        <i className="subitem-arrow" style={subitemArrowStyles}></i>
      );
    }
 
    return (
      <div style={_.extend({}, styles, Styles.cell.wider)}>
        {subitemArrow}
        {this.props.model.product.name}
      </div>
    );
  },
 
  buildNumberCell: function(styles) {
    return (
      <div style={styles}>
        #{this.props.model.number}
      </div>
    );
  },
 
  buildEstimateCell: function(styles, mId) {
    var props = {
      modelId: mId,
      readOnly: !!this.props.model.isNonMatching,
      itemType: this.props.model.type,
      score: this.props.model.score,
      estimateChanger: this.props.modelChangerUtilities.estimateChanger
    };
 
    return (
      <div style={_.extend({}, styles, Styles.cell.narrow)}>
        <Estimator {...props} />
      </div>
    );
  },
 
  buildStatusCell: function(styles, mId) {
    var props = {
      modelId: mId,
      readOnly: !!this.props.model.isNonMatching,
      status: this.props.model.status,
      statusChanger: this.props.modelChangerUtilities.statusChanger
    };
 
    return (
      <div style={_.extend({}, styles, Styles.cell.narrow)}>
        <Status {...props} />
      </div>
    );
  },
 
  buildAssigneeCell: function(styles, mId) {
    // TODO(fw): implement
    return (<div></div>);
  },
 
  buildCreatedByCell: function(styles) {
    return (
      <div style={styles}>
        {abbreviateUsername(this.props.model.created_by)}
      </div>
    );
  },
 
  buildTitleCell: function(styles) {
    var props = {
      href: '/product/' + this.props.model.product.id + '/item/' + this.props.model.number,
      className: 'js-item-link',
      'data-item-number': this.props.model.number,
      style: this.state.hover ? Styles.cell.linkHover : Styles.cell.link,
      onMouseOver: this.onTitleLinkHover,
      onMouseOut: this.onTitleLinkOut
    };
 
    return (
      <div style={_.extend({}, styles, Styles.cell.widest)}>
        <a {...props}>
          {this.props.model.title}
        </a>
      </div>
    );
  },
 
  buildTagsCell: function(styles, mId) {
    var editorProps = {
      modelId: mId,
      readOnly: !!this.props.model.isNonMatching,
      tags: this.props.model.tags,
      tagChanger: this.props.modelChangerUtilities.tagChanger
    };
 
    var tagsProps = {
      tags: this.props.model.tags,
      condensed: this.props.expanded === "condensed" ? true : false,
      navigatorUtility: this.props.navigatorUtility
    };
 
    return (
      <div style={_.extend({}, styles, Styles.cell.wider)}>
        <TagEditor {...editorProps} />
        <Tags {...tagsProps} />
      </div>
    );
  },
 
  buildCreatedAtCell: function(styles) {
    return (
      <div style={_.extend({}, styles, Styles.cell.wide)}>
        {moment(this.props.model.created_at).format('MM/DD/YY')}
      </div>
    );
  },
 
  onTitleLinkHover: function() {
    this.setState({
      hover: true
    });
  },
 
  onTitleLinkOut: function() {
    this.setState({
      hover: false
    });
  }
});
 
module.exports = TableRow;