blob: 946264e8cc668661584323f0ee3a0c5d2b9d7009 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001/* Copyright 2016 The Chromium Authors. All Rights Reserved.
2 *
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file or at
5 * https://developers.google.com/open-source/licenses/bsd
6 */
7
8/**
9 * This file contains JS functions that support setting and showing
10 * stars throughout Monorail.
11 */
12
13
14/**
15 * The character to display when the user has starred an issue.
16 */
17var TKR_STAR_ON = '\u2605';
18
19
20/**
21 * The character to display when the user has not starred an issue.
22 */
23var TKR_STAR_OFF = '\u2606';
24
25
26/**
27 * Function to toggle the star on an issue. Does both an update of the
28 * DOM and hit the server to record the star.
29 *
30 * @param {Element} el The star <a> element.
31 * @param {String} projectName name of the project to be starred, or name of
32 * the project containing the issue to be starred.
33 * @param {Integer} localId number of the issue to be starred.
34 * @param {String} projectName number of the user to be starred.
35 */
36function TKR_toggleStar(el, projectName, localId, userId, hotlistId) {
37 const starred = (el.textContent.trim() == TKR_STAR_OFF);
38 TKR_toggleStarLocal(el);
39
40 const starRequestMessage = {starred: Boolean(starred)};
41 if (userId) {
42 starRequestMessage.user_ref = {user_id: userId};
43 window.prpcClient.call('monorail.Users', 'StarUser', starRequestMessage);
44 } else if (projectName && localId) {
45 starRequestMessage.issue_ref = {
46 project_name: projectName,
47 local_id: localId,
48 };
49 window.prpcClient.call('monorail.Issues', 'StarIssue', starRequestMessage);
50 } else if (projectName) {
51 starRequestMessage.project_name = projectName;
52 window.prpcClient.call(
53 'monorail.Projects', 'StarProject', starRequestMessage);
54 } else if (hotlistId) {
55 starRequestMessage.hotlist_ref = {hotlist_id: hotlistId};
56 window.prpcClient.call(
57 'monorail.Features', 'StarHotlist', starRequestMessage);
58 }
59}
60
61
62/**
63 * Just update the display state of a star, without contacting the server.
64 * Optionally update the value of a form element as well. Useful for when
65 * a user is entering a new issue and wants to set its initial starred state.
66 * @param {Element} el Star <img> element.
67 * @param {string} opt_formElementId HTML ID of the hidden form element for
68 * stars.
69 */
70function TKR_toggleStarLocal(el, opt_formElementId) {
71 let starred = (el.textContent.trim() == TKR_STAR_OFF) ? 1 : 0;
72
73 el.textContent = starred ? TKR_STAR_ON : TKR_STAR_OFF;
74 el.style.color = starred ? 'cornflowerblue' : 'grey';
75 el.title = starred ? 'You have starred this item' : 'Click to star this item';
76
77 if (opt_formElementId) {
78 $(opt_formElementId).value = '' + starred; // convert to string
79 }
80}
81
82
83/**
84 * When we show two star icons on the same details page, keep them
85 * in sync with each other. And, update a message about starring
86 * that is displayed near the issue update form.
87 * @param {Element} clickedStar The star that the user clicked on.
88 * @param {string} otherStarId ID of the other star icon.
89 */
90function TKR_syncStarIcons(clickedStar, otherStarId) {
91 let otherStar = document.getElementById(otherStarId);
92 if (!otherStar) {
93 return;
94 }
95 TKR_toggleStarLocal(otherStar);
96
97 let vote_feedback = document.getElementById('vote_feedback');
98 if (!vote_feedback) {
99 return;
100 }
101
102 if (clickedStar.textContent == TKR_STAR_OFF) {
103 vote_feedback.textContent =
104 'Vote for this issue and get email change notifications.';
105 } else {
106 vote_feedback.textContent = 'Your vote has been recorded.';
107 }
108}
109
110
111// Exports
112_TKR_toggleStar = TKR_toggleStar;
113_TKR_toggleStarLocal = TKR_toggleStarLocal;
114_TKR_syncStarIcons = TKR_syncStarIcons;