Chromium Code Reviews| Index: components/offline_pages/resources/offline_internals.js |
| diff --git a/components/offline_pages/resources/offline_internals.js b/components/offline_pages/resources/offline_internals.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0b51998f8eae7d98491ca3f41fbaf1f57a7af3a5 |
| --- /dev/null |
| +++ b/components/offline_pages/resources/offline_internals.js |
| @@ -0,0 +1,139 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +cr.define('offlineInternals', function() { |
| + 'use strict'; |
| + |
| + var storedPagesAttributes = ['onlineUrl', 'namespace', 'size']; |
| + var queueAttributes = ['onlineUrl', 'creation time', 'status']; |
| + |
| + /** |
| + * Remove all the child nodes of the element |
| + * @param {HTMLElement} element A HTML element |
| + */ |
| + function clearChildren(element) { |
| + element.textContent = ''; |
| + } |
| + |
| + /** |
| + * Refresh the tables |
|
jianli
2016/05/25 00:17:17
Comment is not consistent with the operation below
chili
2016/05/25 02:14:48
Done.
|
| + */ |
| + function clearTable(tableId) { |
| + var element = $(tableId); |
| + if (!element) |
| + return; |
| + |
| + clearChildren(element); |
| + } |
| + |
| + /** |
| + * Fill stored pages table. |
| + */ |
| + function fillStoredPages(element, data) { |
| + if (element == null) { |
|
jianli
2016/05/25 00:17:17
nit: be consistent with line 24
chili
2016/05/25 02:14:48
Done.
|
| + return; |
| + } |
| + for (var i = 0; i < data.length; i++) { |
| + var row = document.createElement('tr'); |
| + |
| + var checkboxCell = document.createElement('td'); |
| + var checkbox = document.createElement('input'); |
| + checkbox.setAttribute('type', 'checkbox'); |
| + checkbox.setAttribute('name', 'stored'); |
| + checkbox.setAttribute('value', data[i]['id']); |
| + |
| + checkboxCell.appendChild(checkbox); |
| + row.appendChild(checkboxCell); |
| + |
| + for (var key in storedPagesAttributes) { |
| + var cell = document.createElement('td'); |
| + cell.textContent = data[i][key]; |
| + row.appendChild(cell); |
| + } |
| + element.appendChild(row); |
| + } |
| + } |
| + |
| + /** |
| + * Fill requests table. |
| + */ |
| + function fillRequestQueue(element, data) { |
| + if (element == null) { |
|
jianli
2016/05/25 00:17:17
ditto
chili
2016/05/25 02:14:48
Done.
|
| + return; |
| + } |
| + for (var i = 0; i < data.length; i++) { |
| + var row = document.createElement('tr'); |
| + |
| + for (var key in queueAttributes) { |
| + var cell = document.createElement('td'); |
| + cell.textContent = data[i][key]; |
| + row.appendChild(cell); |
| + } |
| + element.appendChild(row); |
| + } |
| + } |
| + |
| + /** |
| + * Refresh all displayed information. |
| + */ |
| + function refreshAll() { |
| + chrome.send('getOfflineInternalsInfo', []); |
| + } |
| + |
| + /** |
| + * Delete all pages in the offline store. |
| + */ |
| + function deleteAllPages() { |
| + chrome.send('deleteAllPages', []); |
| + } |
| + |
| + /** |
| + * Callback when pages are deleted. |
| + */ |
| + function pagesDeleted() { |
| + // TODO(chili): decide what to do here. Perhaps a refresh of just |
| + // the stored pages table? |
| + } |
| + |
| + /** |
| + * Callback when information is loaded. |
| + */ |
| + function setOfflineInternalsInfo(info) { |
| + clearTable('stored-pages'); |
| + clearTable('request-queue'); |
| + |
| + fillStoredPages($('stored-pages'), info.AllPages); |
| + fillRequestQueue($('request-queue'), info.Queue); |
| + } |
| + |
| + /** |
| + * Delete selected pages from the offline store. |
| + */ |
| + function deleteSelectedPages() { |
| + var selectedIds = new Array(); |
| + $('input:checkbox[name=stored]:checked').each(function() { |
| + selectedIds.push($(this).val()); |
| + }); |
| + chrome.send('deleteSelectedPages', [selectedIds]); |
| + } |
| + |
| + /** |
| + * Initializing everything. |
| + */ |
| + function initialize() { |
| + $('clear-all').onclick = deleteAllPages; |
| + $('clear-selected').onClick = deleteSelectedPages; |
| + $('refresh').onclick = refreshAll; |
| + chrome.send('getOfflineInternalsInfo', []); |
| + } |
| + |
| + // Return an object with all of the exports. |
| + return { |
| + initialize: initialize, |
| + setOfflineInternalsInfo: setOfflineInternalsInfo, |
| + pagesDeleted: pagesDeleted |
| + }; |
| +}); |
| + |
| +document.addEventListener('DOMContentLoaded', offlineInternals.initialize); |