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