OLD | NEW |
(Empty) | |
| 1 /* 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 */ |
| 17 var TKR_STAR_ON = '\u2605'; |
| 18 |
| 19 |
| 20 /** |
| 21 * The character to display when the user has not starred an issue. |
| 22 */ |
| 23 var 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 * @param {string} token The security token. |
| 36 */ |
| 37 function TKR_toggleStar(el, projectName, localId, userId, token) { |
| 38 var starred = (el.textContent.trim() == TKR_STAR_OFF) ? 1 : 0; |
| 39 TKR_toggleStarLocal(el); |
| 40 |
| 41 var type; |
| 42 if (userId) type = 'users'; |
| 43 if (projectName) type = 'projects'; |
| 44 if (projectName && localId) type = 'issues'; |
| 45 |
| 46 args = {'starred': starred}; |
| 47 if (type == 'users' || type == 'projects') { |
| 48 url = '/hosting/stars.do'; |
| 49 args['scope'] = type; |
| 50 args['item'] = type == 'projects' ? projectName : userId; |
| 51 } else { |
| 52 url = '/p/' + projectName + '/issues/setstar.do'; |
| 53 args['id'] = localId; |
| 54 }; |
| 55 |
| 56 TKR_setStar(el, url, args, token, url); |
| 57 } |
| 58 |
| 59 |
| 60 /** |
| 61 * Just update the display state of a star, without contacting the server. |
| 62 * Optionally update the value of a form element as well. Useful for when |
| 63 * a user is entering a new issue and wants to set its initial starred state. |
| 64 * @param {Element} el Star <img> element. |
| 65 * @param {string} opt_formElementId HTML ID of the hidden form element for |
| 66 * stars. |
| 67 */ |
| 68 function TKR_toggleStarLocal(el, opt_formElementId) { |
| 69 var starred = (el.textContent.trim() == TKR_STAR_OFF) ? 1 : 0; |
| 70 |
| 71 el.textContent = starred ? TKR_STAR_ON : TKR_STAR_OFF; |
| 72 el.style.color = starred ? 'cornflowerblue' : 'grey'; |
| 73 el.title = starred ? 'You have starred this item' : 'Click to star this item'; |
| 74 |
| 75 if (opt_formElementId) { |
| 76 $(opt_formElementId).value = '' + starred; // convert to string |
| 77 } |
| 78 } |
| 79 |
| 80 |
| 81 /** |
| 82 * Send the new star state to the server and create a callback for its response. |
| 83 * @param {Element} el The star <a> element. |
| 84 * @param {String} url The server URL to post to. |
| 85 * @param {Dict} args The arguments to send in the POST request. |
| 86 * @param {String} opt_token The security token to send in the request. |
| 87 */ |
| 88 function TKR_setStar(el, url, args, opt_token) { |
| 89 if (opt_token) { |
| 90 CS_doPost(url, function(event) { TKR_gotSetStar(el, event); }, args, |
| 91 opt_token, url); |
| 92 } else { |
| 93 CS_doPost(url, function(event) { TKR_gotSetStar(el, event); }, args); |
| 94 } |
| 95 } |
| 96 |
| 97 |
| 98 /** |
| 99 * Evaluates the server response after a starring operation completed. |
| 100 * @param {Element} el <a> element containing the star which was clicked. |
| 101 * @param {event} event with xhr JSON response from the server. |
| 102 */ |
| 103 function TKR_gotSetStar(el, event) { |
| 104 var xhr = event.target; |
| 105 if (xhr.readyState != 4 || xhr.status != 200) |
| 106 return; |
| 107 var args = CS_parseJSON(xhr); |
| 108 var localStarred = (el.textContent.trim() == TKR_STAR_ON) ? 1 : 0; |
| 109 var serverStarred = args['starred']; |
| 110 if (localStarred != serverStarred) { |
| 111 TKR_toggleStarLocal(el); |
| 112 } |
| 113 } |
| 114 |
| 115 |
| 116 /** |
| 117 * When we show two star icons on the same details page, keep them |
| 118 * in sync with each other. And, update a message about starring |
| 119 * that is displayed near the issue update form. |
| 120 * @param {Element} clickedStar The star that the user clicked on. |
| 121 * @param {string} otherStarId ID of the other star icon. |
| 122 */ |
| 123 function TKR_syncStarIcons(clickedStar, otherStarId) { |
| 124 var otherStar = document.getElementById(otherStarId); |
| 125 if (!otherStar) { |
| 126 return; |
| 127 } |
| 128 TKR_toggleStarLocal(otherStar); |
| 129 |
| 130 var vote_feedback = document.getElementById('vote_feedback'); |
| 131 if (!vote_feedback) { |
| 132 return; |
| 133 } |
| 134 |
| 135 if (clickedStar.textContent == TKR_STAR_OFF) { |
| 136 vote_feedback.textContent = |
| 137 'Vote for this issue and get email change notifications.'; |
| 138 } else { |
| 139 vote_feedback.textContent = 'Your vote has been recorded.'; |
| 140 } |
| 141 } |
| 142 |
| 143 |
| 144 // Exports |
| 145 _TKR_toggleStar = TKR_toggleStar; |
| 146 _TKR_toggleStarLocal = TKR_toggleStarLocal; |
| 147 _TKR_syncStarIcons = TKR_syncStarIcons; |
OLD | NEW |