<commit_before>/*
This file is part of the Juju GUI, which lets users view and manage Juju
environments within a graphical interface (https://launchpad.net/juju-gui).
Copyright (C) 2015 Canonical Ltd.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License version 3, as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
General Public License for more details.
You should have received a copy of the GNU Affero General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

'use strict';

YUI.add('inspector-relate-to-type', function() {

  juju.components.InspectorRelateToType = React.createClass({

    propTypes: {
      backState: React.PropTypes.object.isRequired,
      changeState: React.PropTypes.func.isRequired,
      createRelation: React.PropTypes.func.isRequired,
      relationTypes: React.PropTypes.array.isRequired
    },

    /**
      Generate the initial state.

      @method getInitialState
      @returns {String} The intial state.
    */
    getInitialState: function() {
      return {activeCount: 0};
    },

    /**
      Update the count of the number of active checkboxes.

      @method _updateActiveCount
    */
    _updateActiveCount: function() {
      var activeCount = 0;
      var refs = this.refs;
      Object.keys(refs).forEach((ref) => {
        if (ref.split('-')[0] === 'InspectorRelateToType') {
          if (refs[ref].state.checked) {
            activeCount += 1;
          }
        }
      });
      this.setState({'activeCount': activeCount});
    },

    /**
      Generate the relation label.

      @method _generateRelationLabel
      @param {Object} start Start of the relation.
      @param {Object} end Far end of the relation.
      @returns {String} The relation label.
    */
    _generateRelationLabel: function(start, end) {
      return <span>{end.name} &rarr; {start.name}</span>;
    },

    /**
      Generate the relation list for the two application.

      @method _generateRelations
      @returns {Object} The relation components.
    */
    _generateRelations: function() {
      var relations = this.props.relationTypes;
      var relationTypes = [];
      if (relations.length === 0) {
        return (
          <li className="inspector-relate-to-type__message">
            No relatable endpoints for these applications.
          </li>);
      }
      relations.forEach((relation, i) => {
        var start = relation[0];
        var end = relation[1];
        var ref = 'InspectorRelateToType-' + i;
        var key = i;
        relationTypes.push(
        <juju.components.CheckListItem
          index={i}
          key={key}
          ref={ref}
          label={this._generateRelationLabel(start, end)}
          relation={relation}
          changeState={this.props.changeState}
          whenChanged={this._updateActiveCount} />);
      }, this);
      return relationTypes;
    },<commit_msg>Tidy up the inspector-relate-to-type component<commit_after>/*
This file is part of the Juju GUI, which lets users view and manage Juju
environments within a graphical interface (https://launchpad.net/juju-gui).
Copyright (C) 2015 Canonical Ltd.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License version 3, as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
General Public License for more details.
You should have received a copy of the GNU Affero General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

'use strict';

YUI.add('inspector-relate-to-type', function() {

  juju.components.InspectorRelateToType = React.createClass({

    propTypes: {
      backState: React.PropTypes.object.isRequired,
      changeState: React.PropTypes.func.isRequired,
      createRelation: React.PropTypes.func.isRequired,
      relationTypes: React.PropTypes.array.isRequired
    },

    /**
      Generate the initial state.

      @method getInitialState
      @returns {String} The intial state.
    */
    getInitialState: function() {
      return {activeCount: 0};
    },

    /**
      Update the count of the number of active checkboxes.

      @method _updateActiveCount
    */
    _updateActiveCount: function() {
      var activeCount = 0;
      var refs = this.refs;
      Object.keys(refs).forEach((ref) => {
        if (ref.split('-')[0] === 'InspectorRelateToType') {
          if (refs[ref].state.checked) {
            activeCount += 1;
          }
        }
      });
      this.setState({'activeCount': activeCount});
    },

    /**
      Generate the relation label.

      @method _generateRelationLabel
      @param {Object} start Start of the relation.
      @param {Object} end Far end of the relation.
      @returns {String} The relation label.
    */
    _generateRelationLabel: function(start, end) {
      return <span>{end.name} &rarr; {start.name}</span>;
    },

    /**
      Generate the relation list for the two application.

      @method _generateRelations
      @returns {Object} The relation components.
    */
    _generateRelations: function() {
      var relations = this.props.relationTypes;
      if (relations.length === 0) {
        return (
          <li className="inspector-relate-to-type__message">
            No relatable endpoints for these applications.
          </li>);
      }
      return relations.map((relation, index) => {
        return (<juju.components.CheckListItem
          index={index}
          key={index}
          ref={`InspectorRelateToType-${index}`}
          label={this._generateRelationLabel(relation[0], relation[1])}
          relation={relation}
          changeState={this.props.changeState}
          whenChanged={this._updateActiveCount} />);
      });
    },
{'fraction_non_alphanumeric': 0.10296461587503984, 'fraction_numerical': 0.003825310806503028, 'mean_word_length': 2.6717378583967233, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3145525', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1697, 'n_words': 539}