summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pages/projects/graphs/show/stat_graph_contributors.js
blob: 76613394af6dc5b36ab26a9d57265f026eb6ab68 (plain)
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
/* eslint-disable func-names, no-var, one-var, camelcase, no-param-reassign, prefer-template, no-return-assign */

import $ from 'jquery';
import _ from 'underscore';
import { n__, s__, createDateTimeFormat, sprintf } from '~/locale';
import {
  ContributorsGraph,
  ContributorsAuthorGraph,
  ContributorsMasterGraph,
} from './stat_graph_contributors_graph';
import ContributorsStatGraphUtil from './stat_graph_contributors_util';

export default (function() {
  function ContributorsStatGraph() {
    this.dateFormat = createDateTimeFormat({ year: 'numeric', month: 'long', day: 'numeric' });
  }

  ContributorsStatGraph.prototype.init = function(log) {
    var author_commits, total_commits;
    this.parsed_log = ContributorsStatGraphUtil.parse_log(log);
    this.set_current_field('commits');
    total_commits = ContributorsStatGraphUtil.get_total_data(this.parsed_log, this.field);
    author_commits = ContributorsStatGraphUtil.get_author_data(this.parsed_log, this.field);
    this.add_master_graph(total_commits);
    this.add_authors_graph(author_commits);
    return this.change_date_header();
  };

  ContributorsStatGraph.prototype.add_master_graph = function(total_data) {
    this.master_graph = new ContributorsMasterGraph(total_data);
    return this.master_graph.draw();
  };

  ContributorsStatGraph.prototype.add_authors_graph = function(author_data) {
    var limited_author_data;
    this.authors = [];
    limited_author_data = author_data.slice(0, 100);
    return _.each(
      limited_author_data,
      (function(_this) {
        return function(d) {
          var author_graph, author_header;
          author_header = _this.create_author_header(d);
          $('.contributors-list').append(author_header);

          author_graph = new ContributorsAuthorGraph(d.dates);
          _this.authors[d.author_name] = author_graph;
          return author_graph.draw();
        };
      })(this),
    );
  };

  ContributorsStatGraph.prototype.format_author_commit_info = function(author) {
    var commits;
    commits = $('<span/>', {
      class: 'graph-author-commits-count',
    });
    commits.text(n__('%d commit', '%d commits', author.commits));
    return $('<span/>').append(commits);
  };

  ContributorsStatGraph.prototype.create_author_header = function(author) {
    var author_commit_info, author_commit_info_span, author_email, author_name, list_item;
    list_item = $('<li/>', {
      class: 'person',
      style: 'display: block;',
    });
    author_name = $('<h4>' + author.author_name + '</h4>');
    author_email = $('<p class="graph-author-email">' + author.author_email + '</p>');
    author_commit_info_span = $('<span/>', {
      class: 'commits',
    });
    author_commit_info = this.format_author_commit_info(author);
    author_commit_info_span.html(author_commit_info);
    list_item.append(author_name);
    list_item.append(author_email);
    list_item.append(author_commit_info_span);
    return list_item;
  };

  ContributorsStatGraph.prototype.redraw_master = function() {
    var total_data;
    total_data = ContributorsStatGraphUtil.get_total_data(this.parsed_log, this.field);
    this.master_graph.set_data(total_data);
    return this.master_graph.redraw();
  };

  ContributorsStatGraph.prototype.redraw_authors = function() {
    $('ol').html('');

    const { x_domain } = ContributorsGraph.prototype;
    const author_commits = ContributorsStatGraphUtil.get_author_data(
      this.parsed_log,
      this.field,
      x_domain,
    );

    return _.each(
      author_commits,
      (function(_this) {
        return function(d) {
          _this.redraw_author_commit_info(d);
          if (_this.authors[d.author_name] != null) {
            $(_this.authors[d.author_name].list_item).appendTo('ol');
            _this.authors[d.author_name].set_data(d.dates);
            return _this.authors[d.author_name].redraw();
          }
          return '';
        };
      })(this),
    );
  };

  ContributorsStatGraph.prototype.set_current_field = function(field) {
    return (this.field = field);
  };

  ContributorsStatGraph.prototype.change_date_header = function() {
    const { x_domain } = ContributorsGraph.prototype;
    const formattedDateRange = sprintf(s__('ContributorsPage|%{startDate} – %{endDate}'), {
      startDate: this.dateFormat.format(new Date(x_domain[0])),
      endDate: this.dateFormat.format(new Date(x_domain[1])),
    });
    return $('#date_header').text(formattedDateRange);
  };

  ContributorsStatGraph.prototype.redraw_author_commit_info = function(author) {
    var author_commit_info, author_list_item, $author;
    $author = this.authors[author.author_name];
    if ($author != null) {
      author_list_item = $(this.authors[author.author_name].list_item);
      author_commit_info = this.format_author_commit_info(author);
      return author_list_item.find('span').html(author_commit_info);
    }
    return '';
  };

  return ContributorsStatGraph;
})();