summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/change/gr-account-entry/gr-account-entry.js
blob: 715ddc05ea034f02a22c2ed85ed4b593b9a088f6 (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
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
/**
 * @license
 * 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';

  Polymer({
    is: 'gr-account-entry',

    /**
     * Fired when an account is entered.
     *
     * @event add
     */

    /**
     * When allowAnyInput is true, account-text-changed is fired when input text
     * changed. This is needed so that the reply dialog's save button can be
     * enabled for arbitrary cc's, which don't need a 'commit'.
     *
     * @event account-text-changed
     */
    properties: {
      allowAnyInput: Boolean,
      borderless: Boolean,
      change: Object,
      filter: Function,
      placeholder: String,
      /**
       * When true, account-entry uses the account suggest API endpoint, which
       * suggests any account in that Gerrit instance (and does not suggest
       * groups).
       *
       * When false/undefined, account-entry uses the suggest_reviewers API
       * endpoint, which suggests any account or group in that Gerrit instance
       * that is not already a reviewer (or is not CCed) on that change.
       */
      allowAnyUser: Boolean,

      // suggestFrom = 0 to enable default suggestions.
      suggestFrom: {
        type: Number,
        value: 0,
      },

      query: {
        type: Function,
        value() {
          return this._getReviewerSuggestions.bind(this);
        },
      },

      _config: Object,
      /** The value of the autocomplete entry. */
      _inputText: {
        type: String,
        observer: '_inputTextChanged',
      },

      _loggedIn: Boolean,
    },

    behaviors: [
      Gerrit.AnonymousNameBehavior,
    ],

    attached() {
      this.$.restAPI.getConfig().then(cfg => {
        this._config = cfg;
      });
      this.$.restAPI.getLoggedIn().then(loggedIn => {
        this._loggedIn = loggedIn;
      });
    },

    get focusStart() {
      return this.$.input.focusStart;
    },

    focus() {
      this.$.input.focus();
    },

    clear() {
      this.$.input.clear();
    },

    setText(text) {
      this.$.input.setText(text);
    },

    getText() {
      return this.$.input.text;
    },

    _handleInputCommit(e) {
      this.fire('add', {value: e.detail.value});
      this.$.input.focus();
    },

    _accountOrAnon(reviewer) {
      return this.getUserName(this._config, reviewer, false);
    },

    _inputTextChanged(text) {
      if (text.length && this.allowAnyInput) {
        this.dispatchEvent(new CustomEvent('account-text-changed',
            {bubbles: true}));
      }
    },

    _makeSuggestion(reviewer) {
      let name;
      let value;
      const generateStatusStr = function(account) {
        return account.status ? '(' + account.status + ')' : '';
      };
      if (reviewer.account) {
        // Reviewer is an account suggestion from getChangeSuggestedReviewers.
        const reviewerName = this._accountOrAnon(reviewer.account);
        const reviewerEmail = this._reviewerEmail(reviewer.account.email);
        const reviewerStatus = generateStatusStr(reviewer.account);
        name = [reviewerName, reviewerEmail, reviewerStatus]
            .filter(p => p.length > 0).join(' ');
        value = reviewer;
      } else if (reviewer.group) {
        // Reviewer is a group suggestion from getChangeSuggestedReviewers.
        name = reviewer.group.name + ' (group)';
        value = reviewer;
      } else if (reviewer._account_id) {
        // Reviewer is an account suggestion from getSuggestedAccounts.
        const reviewerName = this._accountOrAnon(reviewer);
        const reviewerEmail = this._reviewerEmail(reviewer.email);
        const reviewerStatus = generateStatusStr(reviewer);
        name = [reviewerName, reviewerEmail, reviewerStatus]
            .filter(p => p.length > 0).join(' ');
        value = {account: reviewer, count: 1};
      }
      return {name, value};
    },

    _getReviewerSuggestions(input) {
      if (!this.change || !this.change._number || !this._loggedIn) {
        return Promise.resolve([]);
      }

      const api = this.$.restAPI;
      const xhr = this.allowAnyUser ?
          api.getSuggestedAccounts(`cansee:${this.change._number} ${input}`) :
          api.getChangeSuggestedReviewers(this.change._number, input);

      return xhr.then(reviewers => {
        if (!reviewers) { return []; }
        if (!this.filter) {
          return reviewers.map(this._makeSuggestion.bind(this));
        }
        return reviewers
            .filter(this.filter)
            .map(this._makeSuggestion.bind(this));
      });
    },

    _reviewerEmail(email) {
      if (typeof email !== 'undefined') {
        return '<' + email + '>';
      }

      return '';
    },
  });
})();