OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 localStrings = new LocalStrings(); |
| 6 |
| 7 // UTF8 sequence for an arrow triangle pointing down. |
| 8 kExpandedArrow = "\u25BE"; |
| 9 // UTF8 sequence for an arrow triangle pointing right. |
| 10 kCollapsedArrow = "\u25B8"; |
| 11 |
| 12 /** |
| 13 * Requests the list of sessions from the backend. |
| 14 */ |
| 15 function requestSessions() { |
| 16 chrome.send('requestSessionList', []) |
| 17 } |
| 18 |
| 19 /** |
| 20 * Expands or collapses the specified list. |
| 21 */ |
| 22 function toggleExpandedState(div) { |
| 23 if (div.textContent.indexOf(kExpandedArrow) != -1) { |
| 24 div.textContent = div.textContent.replace(kExpandedArrow, kCollapsedArrow); |
| 25 div.parentNode.querySelector(".indent").hidden = true; |
| 26 } else { |
| 27 div.textContent = div.textContent.replace(kCollapsedArrow, kExpandedArrow); |
| 28 div.parentNode.querySelector(".indent").hidden = false; |
| 29 } |
| 30 } |
| 31 |
| 32 /** |
| 33 * Creates a div element to serve as an expandable/collapsable |
| 34 * title for a list of windows or tabs. |
| 35 */ |
| 36 function createTitleDiv(className, textContent) { |
| 37 var div = document.createElement('div'); |
| 38 div.className = className + ' expandable'; |
| 39 div.textContent = kExpandedArrow + ' ' + textContent; |
| 40 div.onclick = Function('toggleExpandedState(this)'); |
| 41 return div; |
| 42 } |
| 43 |
| 44 /** |
| 45 * Utility function to call |transformFn| on each element of |from| |
| 46 * and add them to a div which is then added to |container|. |
| 47 */ |
| 48 function addItems(container, from, transformFn) { |
| 49 var divBlock = document.createElement('div'); |
| 50 divBlock.className = 'indent'; |
| 51 for (var i = 0; i < from.length; i++) { |
| 52 divBlock.appendChild(transformFn(from[i])); |
| 53 } |
| 54 container.appendChild(divBlock); |
| 55 } |
| 56 |
| 57 /** |
| 58 * Transforms a tab into an HTML element. |
| 59 */ |
| 60 function transformTab(tab) { |
| 61 var tabItem = document.createElement('a'); |
| 62 tabItem.setAttribute('href', tab.url); |
| 63 tabItem.textContent = tab.title; |
| 64 tabItem.className = 'tab-link'; |
| 65 tabItem.style.backgroundImage = url('chrome://favicon/' + tab.url); |
| 66 return tabItem; |
| 67 } |
| 68 |
| 69 /** |
| 70 * Transforms a window into an HTML element. |
| 71 */ |
| 72 function transformWindow(window) { |
| 73 var windowDiv = document.createElement('div'); |
| 74 windowDiv.className = "window"; |
| 75 windowDiv.appendChild(createTitleDiv('window-title', 'Window')); |
| 76 addItems(windowDiv, window.tabs, transformTab); |
| 77 return windowDiv; |
| 78 } |
| 79 |
| 80 /** |
| 81 * Transforms a session into an HTML element. |
| 82 */ |
| 83 function transformSession(session) { |
| 84 var sessionDiv = document.createElement('div'); |
| 85 sessionDiv.className = "session"; |
| 86 sessionDiv.appendChild(createTitleDiv('session-title', 'Session')); |
| 87 addItems(sessionDiv, session.windows, transformWindow); |
| 88 return sessionDiv; |
| 89 } |
| 90 |
| 91 /** |
| 92 * Creates the UI for the sessions tree. |
| 93 */ |
| 94 function createSessionTreeUI(sessionList) { |
| 95 $('sessions-summary-text').textContent = |
| 96 localStrings.getStringF('sessionsCountFormat', sessionList.length); |
| 97 |
| 98 var sessionSection = $('session-list'); |
| 99 |
| 100 // Clear any previous list. |
| 101 sessionSection.textContent = ''; |
| 102 |
| 103 var sectionDiv = document.createElement('div'); |
| 104 addItems(sectionDiv, sessionList, transformSession); |
| 105 sessionSection.appendChild(sectionDiv); |
| 106 |
| 107 $('no-sessions').hidden = sessionList.length != 0; |
| 108 } |
| 109 |
| 110 /** |
| 111 * Creates the UI for the "magic" list. |
| 112 */ |
| 113 function createMagicListUI(magicList) { |
| 114 $('magic-summary-text').textContent = |
| 115 localStrings.getStringF('magicCountFormat', magicList.length); |
| 116 |
| 117 var magicSection = $('magic-list'); |
| 118 |
| 119 // Clear any previous list. |
| 120 magicSection.textContent = ''; |
| 121 |
| 122 var sectionDiv = document.createElement('div'); |
| 123 addItems(sectionDiv, magicList, transformTab); |
| 124 magicSection.appendChild(sectionDiv); |
| 125 |
| 126 $('no-magic').hidden = magicList.length != 0; |
| 127 } |
| 128 |
| 129 /** |
| 130 * Callback from backend with the list of sessions. Builds the UI. |
| 131 * @param {array} sessionList The list of sessions. |
| 132 * @param {array} magicList List of "interesting" tabs. |
| 133 */ |
| 134 function updateSessionList(sessionList, magicList) { |
| 135 createSessionTreeUI(sessionList); |
| 136 createMagicListUI(magicList); |
| 137 } |
| 138 |
| 139 document.addEventListener('DOMContentLoaded', requestSessions); |
OLD | NEW |