Code coverage report for components/tag_editor.js

Statements: 86.49% (32 / 37)      Branches: 77.78% (14 / 18)      Functions: 88.89% (8 / 9)      Lines: 86.49% (32 / 37)      Ignored: none     

All files » components/ » tag_editor.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 1381 1 1 1                 1                       1             38                         9 9       9   9           2 2       2 2   2 2     2 2             3 3       3   3     3 1             53 53 53                             53                       8 7                       1
var React = window.React || require('react/addons');
var _ = require('lodash');
var ClickOff = require('react-onclickoutside');
var Styles = require('../styles/tag_editor');
 
/*
 * TagEditor element provides interface for adding and removing item tags.
 * Used throughout app in tandem with Tags element.
 * Model id prop is optional, though if you're editing tags on an item,
 * you'll probably want to pass those in.
 */
 
var TagEditor = React.createClass({
 
  propTypes: {
    modelId: React.PropTypes.arrayOf(React.PropTypes.number),
    readOnly: React.PropTypes.bool,
    tags: React.PropTypes.arrayOf(React.PropTypes.string).isRequired,
    tagChanger: React.PropTypes.object.isRequired
  },
 
  mixins: [ClickOff, Styles],
 
  getDefaultProps: function() {
    return {
      modelId: null,
      readOnly: false
    };
  },
 
  getInitialState: function() {
    return {
      showMenu: false
    };
  },
 
  handleClickOutside: function(ev) {
    ev.stopPropagation();
    this.setState({
      showMenu: false
    });
  },
 
  onTagEditClick: function(ev) {
    ev.stopPropagation();
    Iif (this.props.readOnly) {
      return;
    }
 
    var showOrHide = this.state.showMenu ? false : true;
 
    this.setState({
      showMenu: showOrHide
    });
  },
 
  onFormSubmit: function(ev) {
    ev.preventDefault();
    Iif (this.props.readOnly) {
      return;
    }
 
    var input = ev.target.childNodes[0];
    var tag = input.value;
 
    this.props.tagChanger.addOrRemove(this.props.modelId, this.props.tags, tag, 'add');
    input.value = '';
 
    // Close popup if we've just added our first tag
    Eif (this.props.tags.length < 1) {
      this.setState({
        showMenu: false
      });
    }
  },
 
  onTagRemoveClick: function(ev) {
    ev.stopPropagation();
    Iif (this.props.readOnly) {
      return;
    }
 
    var tag = ev.target.parentNode.nextSibling.textContent;
 
    this.props.tagChanger.addOrRemove(this.props.modelId, this.props.tags, tag, 'remove');
 
    // Close popup if we've just removed our last tag
    if (this.props.tags.length === 1) {
      this.setState({
        showMenu: false
      });
    }
  },
 
  render: function() {
    var tagsLength = this.props.tags.length;
    var addTagText = tagsLength ? null : 'Add a tag.'
    var tagEditMenu = this.state.showMenu ?
      (
        <div>
          <div className="left-arrow" style={Styles.leftArrow} />
          <div className="tag-editor-menu" style={Styles.popup}>
            <form onSubmit={this.onFormSubmit}>
              <input type="text" placeholder="Add a tag" style={Styles.popupInput} />
            </form>
            <ul style={Styles.list}>
              {this.buildTagList()}
            </ul>
          </div>
        </div>
      ) : null;
 
    return (
      <div className="tag-editor" key={this.props.modelId} style={Styles.wrapper}>
        <button style={Styles.tag} onClick={this.onTagEditClick}>
          <i style={Styles.editIcon} />
          {addTagText}
        </button>
        {tagEditMenu}
      </div>
    );
  },
 
  buildTagList: function() {
    return _.map(this.props.tags, function(tag) {
      return (
        <li key={this.props.modelId + ':' + tag} style={Styles.listItem}>
          <button style={Styles.menuTag} onClick={this.onTagRemoveClick}>
            <i style={Styles.deleteIcon} />
          </button>
          {tag}
        </li>
      );
    }, this);
  }
});
 
module.exports = TagEditor;