blob: 64590905c560738a63ca4b20792e9cfc3031b883 [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 * @fileoverview This file initializes the "My Hotlists" drop down menu in the
10 * user bar. It utilizes the menu widget defined in framework-menu.js.
11 */
12
13/** @type {Menu} */
14var myhotlists;
15
16(function() {
17 var target = document.getElementById('hotlists-dropdown');
18
19 if (!target) {
20 return;
21 }
22
23 myhotlists = new Menu(target, function() {});
24
25 myhotlists.addEvent(window, 'load', CS_updateHotlists);
26 myhotlists.addOnOpen(CS_updateHotlists);
27 myhotlists.addEvent(window, 'load', function() {
28 document.body.appendChild(myhotlists.menu);
29 });
30})();
31
32
33/**
34 * Grabs the list of logged in user's hotlists to populate the "My Hotlists"
35 * drop down menu.
36 */
37async function CS_updateHotlists() {
38 if (!myhotlists) return;
39
40 if (!window.CS_env.loggedInUserEmail) {
41 myhotlists.clear();
42 myhotlists.addItem('sign in to see your hotlists',
43 window.CS_env.login_url,
44 'controls');
45 return;
46 }
47
48 const ownedHotlistsMessage = {
49 user: {
50 display_name: window.CS_env.loggedInUserEmail,
51 }};
52
53 const responses = await Promise.all([
54 window.prpcClient.call(
55 'monorail.Features', 'ListHotlistsByUser', ownedHotlistsMessage),
56 window.prpcClient.call(
57 'monorail.Features', 'ListStarredHotlists', {}),
58 window.prpcClient.call(
59 'monorail.Features', 'ListRecentlyVisitedHotlists', {}),
60 ]);
61 const ownedHotlists = responses[0];
62 const starredHotlists = responses[1];
63 const visitedHotlists = responses[2];
64
65 myhotlists.clear();
66
67 const sortByName = (hotlist1, hotlist2) => {
68 hotlist1.name.localeCompare(hotlist2.name);
69 };
70
71 if (ownedHotlists.hotlists) {
72 ownedHotlists.hotlists.sort(sortByName);
73 ownedHotlists.hotlists.forEach(hotlist => {
74 const name = hotlist.name;
75 const userId = hotlist.ownerRef.userId;
76 const url = `/u/${userId}/hotlists/${name}`;
77 myhotlists.addItem(name, url, 'hotlists', 'Hotlists');
78 });
79 }
80
81 if (starredHotlists.hotlists) {
82 myhotlists.addSeparator();
83 starredHotlists.hotlists.sort(sortByName);
84 starredHotlists.hotlists.forEach(hotlist => {
85 const name = hotlist.name;
86 const userId = hotlist.ownerRef.userId;
87 const url = `/u/${userId}/hotlists/${name}`;
88 myhotlists.addItem(name, url, 'starred_hotlists', 'Starred Hotlists');
89 });
90 }
91
92 if (visitedHotlists.hotlists) {
93 myhotlists.addSeparator();
94 visitedHotlists.hotlists.sort(sortByName);
95 visitedHotlists.hotlists.forEach(hotlist => {
96 const name = hotlist.name;
97 const userId = hotlist.ownerRef.userId;
98 const url = `/u/${userId}/hotlists/${name}`;
99 myhotlists.addItem(
100 name, url, 'visited_hotlists', 'Recently Visited Hotlists');
101 });
102 }
103
104 myhotlists.addSeparator();
105 myhotlists.addItem(
106 'All hotlists', `/u/${window.CS_env.loggedInUserEmail}/hotlists`,
107 'controls');
108 myhotlists.addItem('Create hotlist', '/hosting/createHotlist', 'controls');
109}