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 var domDistiller = { 6 /** 7 * Callback from the backend with the list of entries to display. 8 * This call will build the entries section of the DOM distiller page, or hide 9 * that section if there are none to display. 10 * @param {!Array.<string>} entries The entries. 11 */ 12 onReceivedEntries: function(entries) { 13 $('entries-list-loading').classList.add('hidden'); 14 if (!entries.length) $('entries-list').classList.add('hidden'); 15 16 var list = $('entries-list'); 17 domDistiller.removeAllChildren(list); 18 for (var i = 0; i < entries.length; i++) { 19 var listItem = document.createElement('li'); 20 var link = document.createElement('a'); 21 var entry_id = entries[i].entry_id; 22 link.setAttribute('id', 'entry-' + entry_id); 23 link.setAttribute('href', '#'); 24 link.innerText = entries[i].title; 25 link.addEventListener('click', function(event) { 26 domDistiller.onSelectArticle(event.target.id.substr("entry-".length)); 27 }, true); 28 listItem.appendChild(link); 29 list.appendChild(listItem); 30 } 31 }, 32 33 /** 34 * Callback from the backend when adding an article failed. 35 */ 36 onArticleAddFailed: function() { 37 $('add-entry-error').classList.remove('hidden'); 38 }, 39 40 /** 41 * Callback from the backend when viewing a URL failed. 42 */ 43 onViewUrlFailed: function() { 44 $('view-url-error').classList.remove('hidden'); 45 }, 46 47 removeAllChildren: function(root) { 48 while(root.firstChild) { 49 root.removeChild(root.firstChild); 50 } 51 }, 52 53 /** 54 * Sends a request to the browser process to add the URL specified to the list 55 * of articles. 56 */ 57 onAddArticle: function() { 58 $('add-entry-error').classList.add('hidden'); 59 var url = $('article_url').value; 60 chrome.send('addArticle', [url]); 61 }, 62 63 /** 64 * Sends a request to the browser process to view a distilled version of the 65 * URL specified. 66 */ 67 onViewUrl: function() { 68 $('view-url-error').classList.add('hidden'); 69 var url = $('article_url').value; 70 chrome.send('viewUrl', [url]); 71 }, 72 73 /** 74 * Sends a request to the browser process to view a distilled version of the 75 * selected article. 76 */ 77 onSelectArticle: function(articleId) { 78 chrome.send('selectArticle', [articleId]); 79 }, 80 81 /* All the work we do on load. */ 82 onLoadWork: function() { 83 $('list-section').classList.remove('hidden'); 84 $('entries-list-loading').classList.add('hidden'); 85 $('add-entry-error').classList.add('hidden'); 86 $('view-url-error').classList.add('hidden'); 87 88 $('refreshbutton').addEventListener('click', function(event) { 89 domDistiller.onRequestEntries(); 90 }, false); 91 $('addbutton').addEventListener('click', function(event) { 92 domDistiller.onAddArticle(); 93 }, false); 94 $('viewbutton').addEventListener('click', function(event) { 95 domDistiller.onViewUrl(); 96 }, false); 97 domDistiller.onRequestEntries(); 98 }, 99 100 onRequestEntries: function() { 101 $('entries-list-loading').classList.remove('hidden'); 102 chrome.send('requestEntries'); 103 }, 104 } 105 106 document.addEventListener('DOMContentLoaded', domDistiller.onLoadWork); 107