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 * @fileoverview This file initializes the "My favorites" drop down menu in the |
| 10 * user bar. It utilizes the menu widget defined in framework-menu.js. |
| 11 */ |
| 12 |
| 13 /** @type {Menu} */ |
| 14 var myprojects; |
| 15 |
| 16 (function() { |
| 17 var target = document.getElementById('projects-dropdown'); |
| 18 |
| 19 if (!target) { |
| 20 return; |
| 21 } |
| 22 |
| 23 myprojects = new Menu(target, function() {}); |
| 24 |
| 25 myprojects.addEvent(window, 'load', CS_updateProjects); |
| 26 myprojects.addOnOpen(CS_updateProjects); |
| 27 myprojects.addEvent(window, 'load', function() { |
| 28 document.body.appendChild(myprojects.menu); |
| 29 }); |
| 30 })(); |
| 31 |
| 32 /** |
| 33 * Grabs the list of logged in user's projects to populate the "My favorites" |
| 34 * drop down menu. |
| 35 */ |
| 36 function CS_updateProjects() { |
| 37 if (!myprojects) return; |
| 38 // Set a request token to prevent XSRF leaking of user project lists. |
| 39 if (CS_env.token) { |
| 40 var postUrl = '/hosting/projects.do'; |
| 41 CS_doPost(postUrl, CS_updateProjectsCallback, {}); |
| 42 } else { |
| 43 CS_updateProjectsCallback(null); |
| 44 } |
| 45 } |
| 46 |
| 47 /** |
| 48 * Updates the drop down menu based on the json data received. |
| 49 * @param {event} event with xhr Response with JSON data of list of projects. |
| 50 */ |
| 51 function CS_updateProjectsCallback(event) { |
| 52 var xhr = event ? event.target : null; |
| 53 // Grab and show projects if user is signed in |
| 54 if (xhr) { |
| 55 if (xhr.readyState != 4 || xhr.status != 200) |
| 56 return; |
| 57 var projects = []; |
| 58 var starredProjects = []; |
| 59 |
| 60 var json = CS_parseJSON(xhr); |
| 61 for (var category in json) { |
| 62 switch (category) { |
| 63 case 'contributorto': |
| 64 case 'memberof': |
| 65 case 'ownerof': |
| 66 for (var i = 0; i < json[category].length; i++) { |
| 67 projects.push(json[category][i]); |
| 68 } |
| 69 break; |
| 70 |
| 71 case 'starred_projects': |
| 72 for (var i = 0; i < json[category].length; i++) { |
| 73 starredProjects.push(json[category][i]); |
| 74 } |
| 75 break; |
| 76 |
| 77 case 'error': |
| 78 return; |
| 79 |
| 80 default: |
| 81 break; |
| 82 } |
| 83 } |
| 84 |
| 85 myprojects.clear(); |
| 86 |
| 87 projects.sort(); |
| 88 for (var i = 0; i < projects.length; i++) { |
| 89 var url = '/p/' + projects[i] + '/'; |
| 90 myprojects.addItem(projects[i], url, 'projects', 'Projects'); |
| 91 } |
| 92 |
| 93 starredProjects.sort(); |
| 94 for (var i = 0; i < starredProjects.length; i++) { |
| 95 var url = '/p/' + starredProjects[i] + '/'; |
| 96 myprojects.addItem( |
| 97 starredProjects[i], url, 'starred_projects', 'Starred projects'); |
| 98 } |
| 99 |
| 100 if (projects.length == 0 && starredProjects.length == 0) { |
| 101 // If user has no project memberships then add default control. |
| 102 CS_addDefaultControl(); |
| 103 } else { |
| 104 // If there is atleast one project membership then add a 'All projects' |
| 105 // link that goes to hosting/ |
| 106 myprojects.addCategory('---', '---'); |
| 107 myprojects.addItem('All projects', '/hosting/', '---'); |
| 108 } |
| 109 |
| 110 // Otherwise, ask the user to sign in |
| 111 } else { |
| 112 myprojects.clear(); |
| 113 |
| 114 myprojects.addItem( |
| 115 'Sign in to see your favorites', |
| 116 CS_env['login_url'], |
| 117 'controls'); |
| 118 |
| 119 CS_addDefaultControl(); |
| 120 } |
| 121 } |
| 122 |
| 123 /** |
| 124 * Adds default control to the bottom of the "My favorites" menu. |
| 125 * It currently adds links to /more and /hosting. |
| 126 */ |
| 127 function CS_addDefaultControl() { |
| 128 myprojects.addSeparator('controls', ''); |
| 129 myprojects.addItem('Find projects...', '/hosting/', |
| 130 'controls'); |
| 131 } |
OLD | NEW |