| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('chrome.SnippetsInternals', function() { | 5 cr.define('chrome.SnippetsInternals', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 function initialize() { | 8 function initialize() { |
| 9 $('submit-download').addEventListener('click', function(event) { | 9 $('submit-download').addEventListener('click', function(event) { |
| 10 chrome.send('download', [$('hosts-input').value]); | 10 chrome.send('download', [$('hosts-input').value]); |
| 11 event.preventDefault(); | 11 event.preventDefault(); |
| 12 }); | 12 }); |
| 13 | 13 |
| 14 $('submit-clear').addEventListener('click', function(event) { | 14 $('submit-clear').addEventListener('click', function(event) { |
| 15 chrome.send('clear'); | 15 chrome.send('clear'); |
| 16 event.preventDefault(); | 16 event.preventDefault(); |
| 17 }); | 17 }); |
| 18 | 18 |
| 19 $('submit-dump').addEventListener('click', function(event) { | 19 $('submit-dump').addEventListener('click', function(event) { |
| 20 chrome.send('dump'); | 20 chrome.send('dump'); |
| 21 event.preventDefault(); | 21 event.preventDefault(); |
| 22 }); | 22 }); |
| 23 | 23 |
| 24 $('last-json-button').addEventListener('click', function(event) { |
| 25 $('last-json-container').classList.toggle('hidden'); |
| 26 }); |
| 27 |
| 28 $('last-json-dump').addEventListener('click', function(event) { |
| 29 receiveJsonToDownload($('last-json-text').innerText); |
| 30 event.preventDefault(); |
| 31 }); |
| 32 |
| 24 $('discarded-snippets-clear').addEventListener('click', function(event) { | 33 $('discarded-snippets-clear').addEventListener('click', function(event) { |
| 25 chrome.send('clearDiscarded'); | 34 chrome.send('clearDiscarded'); |
| 26 event.preventDefault(); | 35 event.preventDefault(); |
| 27 }); | 36 }); |
| 28 | 37 |
| 29 chrome.send('loaded'); | 38 chrome.send('loaded'); |
| 30 } | 39 } |
| 31 | 40 |
| 32 function receiveProperty(propertyId, value) { | 41 function receiveProperty(propertyId, value) { |
| 33 $(propertyId).textContent = value; | 42 $(propertyId).textContent = value; |
| 34 } | 43 } |
| 35 | 44 |
| 36 function receiveHosts(hosts) { | 45 function receiveHosts(hosts) { |
| 37 displayList(hosts, 'hosts'); | 46 displayList(hosts, 'hosts'); |
| 38 | 47 |
| 39 $('hosts-input').value = hosts.list.map( | 48 $('hosts-input').value = hosts.list.map( |
| 40 function(host) { return host.url;}).join(' '); | 49 function(host) { return host.url;}).join(' '); |
| 41 } | 50 } |
| 42 | 51 |
| 43 function receiveSnippets(snippets) { | 52 function receiveSnippets(snippets) { |
| 44 displayList(snippets, 'snippets', 'snippet-title'); | 53 displayList(snippets, 'snippets', 'snippet-title'); |
| 45 } | 54 } |
| 46 | 55 |
| 47 function receiveDiscardedSnippets(discardedSnippets) { | 56 function receiveDiscardedSnippets(discardedSnippets) { |
| 48 displayList(discardedSnippets, 'discarded-snippets', | 57 displayList(discardedSnippets, 'discarded-snippets', |
| 49 'discarded-snippet-title'); | 58 'discarded-snippet-title'); |
| 50 } | 59 } |
| 51 | 60 |
| 52 function receiveJson(json) { | 61 function receiveJson(json) { |
| 62 receiveProperty('last-json-text', json); |
| 63 if (json) { |
| 64 $('last-json').classList.remove('hidden'); |
| 65 } else { |
| 66 $('last-json').classList.add('hidden'); |
| 67 } |
| 68 } |
| 69 |
| 70 function receiveJsonToDownload(json) { |
| 53 // Redirect the browser to download data in |json| as a file "snippets.json" | 71 // Redirect the browser to download data in |json| as a file "snippets.json" |
| 54 // (Setting Content-Disposition: attachment via a data: URL is not possible; | 72 // (Setting Content-Disposition: attachment via a data: URL is not possible; |
| 55 // create a link with download attribute and simulate a click, instead.) | 73 // create a link with download attribute and simulate a click, instead.) |
| 56 var link = document.createElement('a'); | 74 var link = document.createElement('a'); |
| 57 link.download = 'snippets.json'; | 75 link.download = 'snippets.json'; |
| 58 link.href = 'data:,' + json; | 76 link.href = 'data:,' + json; |
| 59 link.click(); | 77 link.click(); |
| 60 } | 78 } |
| 61 | 79 |
| 62 function displayList(object, domId, titleClass) { | 80 function displayList(object, domId, titleClass) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 73 display = 'none'; | 91 display = 'none'; |
| 74 } | 92 } |
| 75 | 93 |
| 76 if ($(domId + '-empty')) $(domId + '-empty').textContent = text; | 94 if ($(domId + '-empty')) $(domId + '-empty').textContent = text; |
| 77 if ($(domId + '-clear')) $(domId + '-clear').style.display = display; | 95 if ($(domId + '-clear')) $(domId + '-clear').style.display = display; |
| 78 | 96 |
| 79 var links = document.getElementsByClassName(titleClass); | 97 var links = document.getElementsByClassName(titleClass); |
| 80 for (var link of links) { | 98 for (var link of links) { |
| 81 link.addEventListener('click', function(event) { | 99 link.addEventListener('click', function(event) { |
| 82 var id = event.currentTarget.getAttribute('snippet-id'); | 100 var id = event.currentTarget.getAttribute('snippet-id'); |
| 83 $(id).classList.toggle('snippet-hidden'); | 101 $(id).classList.toggle('hidden'); |
| 84 event.preventDefault(); | |
| 85 }); | 102 }); |
| 86 } | 103 } |
| 87 } | 104 } |
| 88 | 105 |
| 89 // Return an object with all of the exports. | 106 // Return an object with all of the exports. |
| 90 return { | 107 return { |
| 91 initialize: initialize, | 108 initialize: initialize, |
| 92 receiveProperty: receiveProperty, | 109 receiveProperty: receiveProperty, |
| 93 receiveHosts: receiveHosts, | 110 receiveHosts: receiveHosts, |
| 94 receiveSnippets: receiveSnippets, | 111 receiveSnippets: receiveSnippets, |
| 95 receiveDiscardedSnippets: receiveDiscardedSnippets, | 112 receiveDiscardedSnippets: receiveDiscardedSnippets, |
| 96 receiveJson: receiveJson, | 113 receiveJson: receiveJson, |
| 114 receiveJsonToDownload: receiveJsonToDownload, |
| 97 }; | 115 }; |
| 98 }); | 116 }); |
| 99 | 117 |
| 100 document.addEventListener('DOMContentLoaded', | 118 document.addEventListener('DOMContentLoaded', |
| 101 chrome.SnippetsInternals.initialize); | 119 chrome.SnippetsInternals.initialize); |
| OLD | NEW |