| 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 function submitDownload(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-download').addEventListener('click', submitDownload); | 14 $('submit-clear').addEventListener('click', function(event) { |
| 15 | |
| 16 function submitClear(event) { | |
| 17 chrome.send('clear'); | 15 chrome.send('clear'); |
| 18 event.preventDefault(); | 16 event.preventDefault(); |
| 19 } | 17 }); |
| 20 | 18 |
| 21 $('submit-clear').addEventListener('click', submitClear); | 19 $('submit-dump').addEventListener('click', function(event) { |
| 20 chrome.send('dump'); |
| 21 event.preventDefault(); |
| 22 }); |
| 22 | 23 |
| 23 function submitClearDiscarded(event) { | 24 $('discarded-snippets-clear').addEventListener('click', function(event) { |
| 24 chrome.send('clearDiscarded'); | 25 chrome.send('clearDiscarded'); |
| 25 event.preventDefault(); | 26 event.preventDefault(); |
| 26 } | 27 }); |
| 27 | |
| 28 $('discarded-snippets-clear').addEventListener('click', | |
| 29 submitClearDiscarded); | |
| 30 | 28 |
| 31 chrome.send('loaded'); | 29 chrome.send('loaded'); |
| 32 } | 30 } |
| 33 | 31 |
| 34 function receiveProperty(propertyId, value) { | 32 function receiveProperty(propertyId, value) { |
| 35 $(propertyId).textContent = value; | 33 $(propertyId).textContent = value; |
| 36 } | 34 } |
| 37 | 35 |
| 38 function receiveHosts(hosts) { | 36 function receiveHosts(hosts) { |
| 39 displayList(hosts, 'hosts'); | 37 displayList(hosts, 'hosts'); |
| 40 | 38 |
| 41 $('hosts-input').value = hosts.list.map(host => host.url).join(' '); | 39 $('hosts-input').value = hosts.list.map( |
| 40 function(host) { return host.url;}).join(' '); |
| 42 } | 41 } |
| 43 | 42 |
| 44 function receiveSnippets(snippets) { | 43 function receiveSnippets(snippets) { |
| 45 displayList(snippets, 'snippets', 'snippet-title'); | 44 displayList(snippets, 'snippets', 'snippet-title'); |
| 46 } | 45 } |
| 47 | 46 |
| 48 function receiveDiscardedSnippets(discardedSnippets) { | 47 function receiveDiscardedSnippets(discardedSnippets) { |
| 49 displayList(discardedSnippets, 'discarded-snippets', | 48 displayList(discardedSnippets, 'discarded-snippets', |
| 50 'discarded-snippet-title'); | 49 'discarded-snippet-title'); |
| 51 } | 50 } |
| 52 | 51 |
| 52 function receiveJson(json) { |
| 53 // 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; |
| 55 // create a link with download attribute and simulate a click, instead.) |
| 56 var link = document.createElement('a'); |
| 57 link.download = 'snippets.json'; |
| 58 link.href = 'data:,' + json; |
| 59 link.click(); |
| 60 } |
| 61 |
| 53 function displayList(object, domId, titleClass) { | 62 function displayList(object, domId, titleClass) { |
| 54 jstProcess(new JsEvalContext(object), $(domId)); | 63 jstProcess(new JsEvalContext(object), $(domId)); |
| 55 | 64 |
| 56 var text; | 65 var text; |
| 57 var display; | 66 var display; |
| 58 | 67 |
| 59 if (object.list.length > 0) { | 68 if (object.list.length > 0) { |
| 60 text = ''; | 69 text = ''; |
| 61 display = 'inline'; | 70 display = 'inline'; |
| 62 } else { | 71 } else { |
| 63 text = 'The list is empty.'; | 72 text = 'The list is empty.'; |
| 64 display = 'none'; | 73 display = 'none'; |
| 65 } | 74 } |
| 66 | 75 |
| 67 if ($(domId + '-empty')) $(domId + '-empty').textContent = text; | 76 if ($(domId + '-empty')) $(domId + '-empty').textContent = text; |
| 68 if ($(domId + '-clear')) $(domId + '-clear').style.display = display; | 77 if ($(domId + '-clear')) $(domId + '-clear').style.display = display; |
| 69 | 78 |
| 70 function trigger(event) { | |
| 71 // The id of the snippet is stored to 'snippet-id' attribute of the link. | |
| 72 var id = event.currentTarget.getAttribute('snippet-id'); | |
| 73 $(id).classList.toggle('snippet-hidden'); | |
| 74 event.preventDefault(); | |
| 75 } | |
| 76 | |
| 77 var links = document.getElementsByClassName(titleClass); | 79 var links = document.getElementsByClassName(titleClass); |
| 78 for (var link of links) { | 80 for (var link of links) { |
| 79 link.addEventListener('click', trigger); | 81 link.addEventListener('click', function(event) { |
| 82 var id = event.currentTarget.getAttribute('snippet-id'); |
| 83 $(id).classList.toggle('snippet-hidden'); |
| 84 event.preventDefault(); |
| 85 }); |
| 80 } | 86 } |
| 81 } | 87 } |
| 82 | 88 |
| 83 // Return an object with all of the exports. | 89 // Return an object with all of the exports. |
| 84 return { | 90 return { |
| 85 initialize: initialize, | 91 initialize: initialize, |
| 86 receiveProperty: receiveProperty, | 92 receiveProperty: receiveProperty, |
| 87 receiveHosts: receiveHosts, | 93 receiveHosts: receiveHosts, |
| 88 receiveSnippets: receiveSnippets, | 94 receiveSnippets: receiveSnippets, |
| 89 receiveDiscardedSnippets: receiveDiscardedSnippets, | 95 receiveDiscardedSnippets: receiveDiscardedSnippets, |
| 96 receiveJson: receiveJson, |
| 90 }; | 97 }; |
| 91 }); | 98 }); |
| 92 | 99 |
| 93 document.addEventListener('DOMContentLoaded', | 100 document.addEventListener('DOMContentLoaded', |
| 94 chrome.SnippetsInternals.initialize); | 101 chrome.SnippetsInternals.initialize); |
| OLD | NEW |