| 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 // Stores the list of suggestions we received in receiveContentSuggestions. | 8 // Stores the list of snippets we received in receiveSnippets. |
| 9 var lastSuggestions = []; | 9 var lastSnippets = []; |
| 10 | 10 |
| 11 function initialize() { | 11 function initialize() { |
| 12 $('submit-download').addEventListener('click', function(event) { | 12 $('submit-download').addEventListener('click', function(event) { |
| 13 chrome.send('download', [$('hosts-input').value]); | 13 chrome.send('download', [$('hosts-input').value]); |
| 14 event.preventDefault(); | 14 event.preventDefault(); |
| 15 }); | 15 }); |
| 16 | 16 |
| 17 $('submit-clear').addEventListener('click', function(event) { |
| 18 chrome.send('clear'); |
| 19 event.preventDefault(); |
| 20 }); |
| 21 |
| 17 $('submit-dump').addEventListener('click', function(event) { | 22 $('submit-dump').addEventListener('click', function(event) { |
| 18 downloadJson(JSON.stringify(lastSuggestions)); | 23 downloadJson(JSON.stringify(lastSnippets)); |
| 19 event.preventDefault(); | 24 event.preventDefault(); |
| 20 }); | 25 }); |
| 21 | 26 |
| 22 $('last-json-button').addEventListener('click', function(event) { | 27 $('last-json-button').addEventListener('click', function(event) { |
| 23 $('last-json-container').classList.toggle('hidden'); | 28 $('last-json-container').classList.toggle('hidden'); |
| 24 }); | 29 }); |
| 25 | 30 |
| 26 $('last-json-dump').addEventListener('click', function(event) { | 31 $('last-json-dump').addEventListener('click', function(event) { |
| 27 downloadJson($('last-json-text').innerText); | 32 downloadJson($('last-json-text').innerText); |
| 28 event.preventDefault(); | 33 event.preventDefault(); |
| 29 }); | 34 }); |
| 30 | 35 |
| 36 $('dismissed-snippets-clear').addEventListener('click', function(event) { |
| 37 chrome.send('clearDismissed'); |
| 38 event.preventDefault(); |
| 39 }); |
| 40 |
| 41 $('submit-clear-cached-suggestions') |
| 42 .addEventListener('click', function(event) { |
| 43 chrome.send('clearCachedSuggestions'); |
| 44 event.preventDefault(); |
| 45 }); |
| 46 |
| 47 $('submit-clear-dismissed-suggestions') |
| 48 .addEventListener('click', function(event) { |
| 49 chrome.send('clearDismissedSuggestions'); |
| 50 event.preventDefault(); |
| 51 }); |
| 52 |
| 31 window.addEventListener('focus', refreshContent); | 53 window.addEventListener('focus', refreshContent); |
| 32 window.setInterval(refreshContent, 1000); | 54 window.setInterval(refreshContent, 1000); |
| 33 | 55 |
| 34 refreshContent(); | 56 refreshContent(); |
| 35 } | 57 } |
| 36 | 58 |
| 37 function setHostRestricted(restricted) { | 59 function setHostRestricted(restricted) { |
| 38 receiveProperty('switch-restrict-to-hosts', restricted ? 'True' : 'False'); | 60 receiveProperty('switch-restrict-to-hosts', restricted ? 'True' : 'False'); |
| 39 if (!restricted) { | 61 if (!restricted) { |
| 40 $('hosts-restrict').classList.add('hidden'); | 62 $('hosts-restrict').classList.add('hidden'); |
| 41 } | 63 } |
| 42 } | 64 } |
| 43 | 65 |
| 44 function receiveProperty(propertyId, value) { | 66 function receiveProperty(propertyId, value) { |
| 45 $(propertyId).textContent = value; | 67 $(propertyId).textContent = value; |
| 46 } | 68 } |
| 47 | 69 |
| 48 function receiveHosts(hosts) { | 70 function receiveHosts(hosts) { |
| 49 displayList(hosts, 'hosts'); | 71 displayList(hosts, 'hosts'); |
| 50 | 72 |
| 51 $('hosts-input').value = hosts.list.map( | 73 $('hosts-input').value = hosts.list.map( |
| 52 function(host) { return host.url;}).join(' '); | 74 function(host) { return host.url;}).join(' '); |
| 53 } | 75 } |
| 54 | 76 |
| 55 function receiveContentSuggestions(categoriesList) { | 77 function receiveSnippets(snippets) { |
| 56 lastSuggestions = categoriesList; | 78 lastSnippets = snippets; |
| 57 displayList(categoriesList, 'content-suggestions', | 79 displayList(snippets, 'snippets', 'snippet-title'); |
| 58 'hidden-toggler'); | |
| 59 | |
| 60 var clearCachedButtons = | |
| 61 document.getElementsByClassName('submit-clear-cached-suggestions'); | |
| 62 for (var button of clearCachedButtons) { | |
| 63 button.addEventListener('click', onClearCachedButtonClicked); | |
| 64 } | |
| 65 | |
| 66 var clearDismissedButtons = | |
| 67 document.getElementsByClassName('submit-clear-dismissed-suggestions'); | |
| 68 for (var button of clearDismissedButtons) { | |
| 69 button.addEventListener('click', onClearDismissedButtonClicked); | |
| 70 } | |
| 71 } | 80 } |
| 72 | 81 |
| 73 function onClearCachedButtonClicked(event) { | 82 function receiveDismissedSnippets(dismissedSnippets) { |
| 74 event.preventDefault(); | 83 displayList(dismissedSnippets, 'dismissed-snippets', |
| 75 var id = parseInt(event.currentTarget.getAttribute('category-id'), 10); | 84 'dismissed-snippet-title'); |
| 76 chrome.send('clearCachedSuggestions', [id]); | |
| 77 } | 85 } |
| 78 | 86 |
| 79 function onClearDismissedButtonClicked(event) { | 87 function receiveContentSuggestions(categoriesList) { |
| 80 event.preventDefault(); | 88 displayList(categoriesList, 'content-suggestions', |
| 81 var id = parseInt(event.currentTarget.getAttribute('category-id'), 10); | 89 'content-suggestion-title'); |
| 82 chrome.send('clearDismissedSuggestions', [id]); | |
| 83 } | 90 } |
| 84 | 91 |
| 85 function receiveJson(json) { | 92 function receiveJson(json) { |
| 86 var trimmed = json.trim(); | 93 var trimmed = json.trim(); |
| 87 var hasContent = (trimmed && trimmed != '{}'); | 94 var hasContent = (trimmed && trimmed != '{}'); |
| 88 | 95 |
| 89 if (hasContent) { | 96 if (hasContent) { |
| 90 receiveProperty('last-json-text', trimmed); | 97 receiveProperty('last-json-text', trimmed); |
| 91 $('last-json').classList.remove('hidden'); | 98 $('last-json').classList.remove('hidden'); |
| 92 } else { | 99 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 106 | 113 |
| 107 function refreshContent() { | 114 function refreshContent() { |
| 108 chrome.send('refreshContent'); | 115 chrome.send('refreshContent'); |
| 109 } | 116 } |
| 110 | 117 |
| 111 function toggleHidden(event) { | 118 function toggleHidden(event) { |
| 112 var id = event.currentTarget.getAttribute('hidden-id'); | 119 var id = event.currentTarget.getAttribute('hidden-id'); |
| 113 $(id).classList.toggle('hidden'); | 120 $(id).classList.toggle('hidden'); |
| 114 } | 121 } |
| 115 | 122 |
| 116 function displayList(object, domId, toggleClass) { | 123 function displayList(object, domId, titleClass) { |
| 117 jstProcess(new JsEvalContext(object), $(domId)); | 124 jstProcess(new JsEvalContext(object), $(domId)); |
| 118 | 125 |
| 119 var text; | 126 var text; |
| 120 var display; | 127 var display; |
| 121 | 128 |
| 122 if (object.list.length > 0) { | 129 if (object.list.length > 0) { |
| 123 text = ''; | 130 text = ''; |
| 124 display = 'inline'; | 131 display = 'inline'; |
| 125 } else { | 132 } else { |
| 126 text = 'The list is empty.'; | 133 text = 'The list is empty.'; |
| 127 display = 'none'; | 134 display = 'none'; |
| 128 } | 135 } |
| 129 | 136 |
| 130 if ($(domId + '-empty')) $(domId + '-empty').textContent = text; | 137 if ($(domId + '-empty')) $(domId + '-empty').textContent = text; |
| 131 if ($(domId + '-clear')) $(domId + '-clear').style.display = display; | 138 if ($(domId + '-clear')) $(domId + '-clear').style.display = display; |
| 132 | 139 |
| 133 var links = document.getElementsByClassName(toggleClass); | 140 var links = document.getElementsByClassName(titleClass); |
| 134 for (var link of links) { | 141 for (var link of links) { |
| 135 link.addEventListener('click', toggleHidden); | 142 link.addEventListener('click', toggleHidden); |
| 136 } | 143 } |
| 137 } | 144 } |
| 138 | 145 |
| 139 // Return an object with all of the exports. | 146 // Return an object with all of the exports. |
| 140 return { | 147 return { |
| 141 initialize: initialize, | 148 initialize: initialize, |
| 142 setHostRestricted: setHostRestricted, | 149 setHostRestricted: setHostRestricted, |
| 143 receiveProperty: receiveProperty, | 150 receiveProperty: receiveProperty, |
| 144 receiveHosts: receiveHosts, | 151 receiveHosts: receiveHosts, |
| 152 receiveSnippets: receiveSnippets, |
| 153 receiveDismissedSnippets: receiveDismissedSnippets, |
| 145 receiveContentSuggestions: receiveContentSuggestions, | 154 receiveContentSuggestions: receiveContentSuggestions, |
| 146 receiveJson: receiveJson, | 155 receiveJson: receiveJson, |
| 147 }; | 156 }; |
| 148 }); | 157 }); |
| 149 | 158 |
| 150 document.addEventListener('DOMContentLoaded', | 159 document.addEventListener('DOMContentLoaded', |
| 151 chrome.SnippetsInternals.initialize); | 160 chrome.SnippetsInternals.initialize); |
| OLD | NEW |