| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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 /** |
| 6 * Handles DumpDatabase tab for syncfs-internals. |
| 7 */ |
| 8 var DumpDatabase = (function() { |
| 9 'use strict'; |
| 10 |
| 11 var DumpDatabase = {}; |
| 12 |
| 13 /** |
| 14 * Get the database dump. |
| 15 */ |
| 16 function getDatabaseDump() { |
| 17 chrome.send('getDatabaseDump'); |
| 18 } |
| 19 |
| 20 /** |
| 21 * Creates an element named |elementName| containing the content |text|. |
| 22 * @param {string} elementName Name of the new element to be created. |
| 23 * @param {string} text Text to be contained in the new element. |
| 24 * @return {HTMLElement} The newly created HTML element. |
| 25 */ |
| 26 function createElementFromText(elementName, text) { |
| 27 var element = document.createElement(elementName); |
| 28 element.appendChild(document.createTextNode(text)); |
| 29 return element; |
| 30 } |
| 31 |
| 32 /** |
| 33 * Creates a table by filling |header| and |body|. |
| 34 * @param {HTMLElement} div The outer container of the table to be renderered. |
| 35 * @param {HTMLElement} header The table header element to be fillied by |
| 36 * this function. |
| 37 * @param {HTMLElement} body The table body element to be filled by this |
| 38 * function. |
| 39 * @param {Array} databaseDump List of dictionaries for the database dump. |
| 40 * The first element must have metadata of the entry. |
| 41 * The remaining elements must be dictionaries for the database dump, |
| 42 * which can be iterated using the 'keys' fields given by the first |
| 43 * element. |
| 44 */ |
| 45 function createDatabaseDumpTable(div, header, body, databaseDump) { |
| 46 var metadata = databaseDump.shift(); |
| 47 div.appendChild(createElementFromText('h3', metadata['title'])); |
| 48 |
| 49 var tr = document.createElement('tr'); |
| 50 for (var i = 0; i < metadata.keys.length; ++i) |
| 51 tr.appendChild(createElementFromText('td', metadata.keys[i])); |
| 52 header.appendChild(tr); |
| 53 |
| 54 for (var i = 0; i < databaseDump.length; i++) { |
| 55 var entry = databaseDump[i]; |
| 56 var tr = document.createElement('tr'); |
| 57 for (var k = 0; k < metadata.keys.length; ++k) |
| 58 tr.appendChild(createElementFromText('td', entry[metadata.keys[k]])); |
| 59 body.appendChild(tr); |
| 60 } |
| 61 } |
| 62 |
| 63 /** |
| 64 * Handles callback from onGetDatabaseDump. |
| 65 * @param {Array} databaseDump List of lists for the database dump. |
| 66 */ |
| 67 DumpDatabase.onGetDatabaseDump = function(databaseDump) { |
| 68 var placeholder = $('dump-database-placeholder'); |
| 69 placeholder.innerHTML = ''; |
| 70 for (var i = 0; i < databaseDump.length; ++i) { |
| 71 var div = document.createElement('div'); |
| 72 var table = document.createElement('table'); |
| 73 var header = document.createElement('thead'); |
| 74 var body = document.createElement('tbody'); |
| 75 createDatabaseDumpTable(div, header, body, databaseDump[i]); |
| 76 table.appendChild(header); |
| 77 table.appendChild(body); |
| 78 div.appendChild(table); |
| 79 placeholder.appendChild(div); |
| 80 } |
| 81 } |
| 82 |
| 83 function main() { |
| 84 getDatabaseDump(); |
| 85 $('refresh-database-dump').addEventListener('click', getDatabaseDump); |
| 86 } |
| 87 |
| 88 document.addEventListener('DOMContentLoaded', main); |
| 89 return DumpDatabase; |
| 90 })(); |
| OLD | NEW |