summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/change-list/gr-dashboard-view/gr-dashboard-view.js
blob: 6c5bad35e19f0f009f4c949afcc02fc9d5e5d4bc (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
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
(function() {
  'use strict';

  const DEFAULT_SECTIONS = [
    {
      name: 'Work in progress',
      query: 'is:open owner:${user} is:wip',
      selfOnly: true,
    },
    {
      name: 'Outgoing reviews',
      query: 'is:open owner:${user} -is:wip',
    },
    {
      name: 'Incoming reviews',
      query: 'is:open ((reviewer:${user} -owner:${user} -is:ignored) OR ' +
          'assignee:${user}) -is:wip',
    },
    {
      name: 'Recently closed',
      query: 'is:closed (owner:${user} OR reviewer:${user} OR ' +
          'assignee:${user})',
      suffixForDashboard: '-age:4w limit:10',
    },
  ];

  Polymer({
    is: 'gr-dashboard-view',

    /**
     * Fired when the title of the page should change.
     *
     * @event title-change
     */

    properties: {
      account: {
        type: Object,
        value() { return {}; },
      },
      /** @type {{ selectedChangeIndex: number }} */
      viewState: Object,
      params: {
        type: Object,
      },

      _results: Array,
      _sectionMetadata: {
        type: Array,
        value() { return DEFAULT_SECTIONS; },
      },

      /**
       * For showing a "loading..." string during ajax requests.
       */
      _loading: {
        type: Boolean,
        value: true,
      },
    },

    observers: [
      '_userChanged(params.user)',
    ],

    behaviors: [
      Gerrit.RESTClientBehavior,
    ],

    get options() {
      return this.listChangesOptionsToHex(
          this.ListChangesOption.LABELS,
          this.ListChangesOption.DETAILED_ACCOUNTS,
          this.ListChangesOption.REVIEWED
      );
    },

    _computeTitle(user) {
      if (user === 'self') {
        return 'My Reviews';
      }
      return 'Dashboard for ' + user;
    },

    /**
     * Allows a refresh if menu item is selected again.
     */
    _userChanged(user) {
      if (!user) { return; }

      // NOTE: This method may be called before attachment. Fire title-change
      // in an async so that attachment to the DOM can take place first.
      this.async(
          () => this.fire('title-change', {title: this._computeTitle(user)}));

      this._loading = true;
      const sections = this._sectionMetadata.filter(
          section => (user === 'self' || !section.selfOnly));
      const queries =
          sections.map(
              section => this._dashboardQueryForSection(section, user));
      this.$.restAPI.getChanges(null, queries, null, this.options)
          .then(results => {
            this._results = sections.map((section, i) => {
              return {
                sectionName: section.name,
                query: queries[i],
                results: results[i],
              };
            });
            this._loading = false;
          }).catch(err => {
            this._loading = false;
            console.warn(err.message);
          });
    },

    _dashboardQueryForSection(section, user) {
      const query =
          section.suffixForDashboard ?
          section.query + ' ' + section.suffixForDashboard :
          section.query;
      return query.replace(/\$\{user\}/g, user);
    },

  });
})();