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.
6 * Handles DumpDatabase tab for syncfs-internals.
8 var DumpDatabase = (function() {
11 var DumpDatabase = {};
14 * Get the database dump.
16 function getDatabaseDump() {
17 chrome.send('getDatabaseDump');
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.
26 function createElementFromText(elementName, text) {
27 var element = document.createElement(elementName);
28 element.appendChild(document.createTextNode(text));
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
37 * @param {HTMLElement} body The table body element to be filled by this
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
45 function createDatabaseDumpTable(div, header, body, databaseDump) {
46 var metadata = databaseDump.shift();
47 div.appendChild(createElementFromText('h3', metadata['title']));
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);
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]]));
64 * Handles callback from onGetDatabaseDump.
65 * @param {Array} databaseDump List of lists for the database dump.
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);
85 $('refresh-database-dump').addEventListener('click', getDatabaseDump);
88 document.addEventListener('DOMContentLoaded', main);