| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 cr.define('offlineInternals', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * @typedef {{ |
| 10 * onlineUrl: string, |
| 11 * creationTime: number, |
| 12 * status: number, |
| 13 * id: string, |
| 14 * namespace: string, |
| 15 * size: string, |
| 16 * filePath: string, |
| 17 * lastAccessTime: number, |
| 18 * accessCount: number |
| 19 * }} |
| 20 */ |
| 21 var OfflinePageItem; |
| 22 |
| 23 /** |
| 24 * @typedef {{ |
| 25 * status: string, |
| 26 * onlineUrl: string, |
| 27 * creationTime: number, |
| 28 * id: string, |
| 29 * namespace: string, |
| 30 * attemptCount: number |
| 31 * }} |
| 32 */ |
| 33 var SavePageRequest; |
| 34 |
| 35 /** |
| 36 * Clear the specified table. |
| 37 * @param {string} tableId id of the table to clear. |
| 38 */ |
| 39 function clearTable(tableId) { |
| 40 $(tableId).textContent = ''; |
| 41 } |
| 42 |
| 43 /** |
| 44 * Fill stored pages table. |
| 45 * @param {HTMLElement} element A HTML element. |
| 46 * @param {!Array<OfflinePageItem>} pages An array object representing |
| 47 * stored offline pages. |
| 48 */ |
| 49 function fillStoredPages(element, pages) { |
| 50 for (var i = 0; i < pages.length; i++) { |
| 51 var row = document.createElement('tr'); |
| 52 |
| 53 var checkboxCell = document.createElement('td'); |
| 54 var checkbox = document.createElement('input'); |
| 55 checkbox.setAttribute('type', 'checkbox'); |
| 56 checkbox.setAttribute('name', 'stored'); |
| 57 checkbox.setAttribute('value', pages[i].id); |
| 58 |
| 59 checkboxCell.appendChild(checkbox); |
| 60 row.appendChild(checkboxCell); |
| 61 |
| 62 var cell = document.createElement('td'); |
| 63 cell.textContent = pages[i].onlineUrl; |
| 64 row.appendChild(cell); |
| 65 |
| 66 cell = document.createElement('td'); |
| 67 cell.textContent = pages[i].namespace; |
| 68 row.appendChild(cell); |
| 69 |
| 70 cell = document.createElement('td'); |
| 71 cell.textContent = pages[i].size; |
| 72 row.appendChild(cell); |
| 73 |
| 74 element.appendChild(row); |
| 75 } |
| 76 } |
| 77 |
| 78 /** |
| 79 * Fill requests table. |
| 80 * @param {HTMLElement} element A HTML element. |
| 81 * @param {!Array<SavePageRequest>} requests An array object representing |
| 82 * the request queue. |
| 83 */ |
| 84 function fillRequestQueue(element, requests) { |
| 85 for (var i = 0; i < requests.length; i++) { |
| 86 var row = document.createElement('tr'); |
| 87 |
| 88 var cell = document.createElement('td'); |
| 89 cell.textContent = requests[i].onlineUrl; |
| 90 row.appendChild(cell); |
| 91 |
| 92 cell = document.createElement('td'); |
| 93 cell.textContent = new Date(requests[i].creationTime); |
| 94 row.appendChild(cell); |
| 95 |
| 96 cell = document.createElement('td'); |
| 97 cell.textContent = requests[i].status; |
| 98 row.appendChild(cell); |
| 99 |
| 100 element.appendChild(row); |
| 101 } |
| 102 } |
| 103 |
| 104 /** |
| 105 * Refresh all displayed information. |
| 106 */ |
| 107 function refreshAll() { |
| 108 cr.sendWithPromise('getOfflineInternalsInfo').then(setOfflineInternalsInfo); |
| 109 } |
| 110 |
| 111 /** |
| 112 * Delete all pages in the offline store. |
| 113 */ |
| 114 function deleteAllPages() { |
| 115 cr.sendWithPromise('deleteAllPages').then(pagesDeleted); |
| 116 } |
| 117 |
| 118 /** |
| 119 * Callback when pages are deleted. |
| 120 * @param {string} deletePageStatus The status of delete page call. |
| 121 */ |
| 122 function pagesDeleted(deletePageStatus) { |
| 123 // TODO(chili): decide what to do here. Perhaps a refresh of just |
| 124 // the stored pages table? |
| 125 } |
| 126 |
| 127 /** |
| 128 * Callback when information is loaded. |
| 129 * @param {{AllPages: !Array<OfflinePageItem>, |
| 130 * Queue: !Array<SavePageRequest>}} info An object containing both |
| 131 * stored pages and request queue status. |
| 132 */ |
| 133 function setOfflineInternalsInfo(info) { |
| 134 clearTable('stored-pages'); |
| 135 clearTable('request-queue'); |
| 136 |
| 137 fillStoredPages($('stored-pages'), info.AllPages); |
| 138 fillRequestQueue($('request-queue'), info.Queue); |
| 139 } |
| 140 |
| 141 /** |
| 142 * Delete selected pages from the offline store. |
| 143 */ |
| 144 function deleteSelectedPages() { |
| 145 var checkboxes = document.getElementsByName('stored'); |
| 146 var selectedIds = []; |
| 147 for (var checkbox of checkboxes) { |
| 148 if (checkbox.checked) |
| 149 selectedIds.push(checkbox.value); |
| 150 } |
| 151 |
| 152 cr.sendWithPromise('deleteSelectedPages', selectedIds).then(pagesDeleted); |
| 153 } |
| 154 |
| 155 function initialize() { |
| 156 $('clear-all').onclick = deleteAllPages; |
| 157 $('clear-selected').onclick = deleteSelectedPages; |
| 158 $('refresh').onclick = refreshAll; |
| 159 refreshAll(); |
| 160 } |
| 161 |
| 162 // Return an object with all of the exports. |
| 163 return { |
| 164 initialize: initialize, |
| 165 }; |
| 166 }); |
| 167 |
| 168 document.addEventListener('DOMContentLoaded', offlineInternals.initialize); |
| OLD | NEW |