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 snippets we received in receiveSnippets. | 8 // Stores the list of snippets we received in receiveSnippets. |
9 var lastSnippets = []; | 9 var lastSnippets = []; |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... |
31 $('last-json-dump').addEventListener('click', function(event) { | 31 $('last-json-dump').addEventListener('click', function(event) { |
32 downloadJson($('last-json-text').innerText); | 32 downloadJson($('last-json-text').innerText); |
33 event.preventDefault(); | 33 event.preventDefault(); |
34 }); | 34 }); |
35 | 35 |
36 $('discarded-snippets-clear').addEventListener('click', function(event) { | 36 $('discarded-snippets-clear').addEventListener('click', function(event) { |
37 chrome.send('clearDiscarded'); | 37 chrome.send('clearDiscarded'); |
38 event.preventDefault(); | 38 event.preventDefault(); |
39 }); | 39 }); |
40 | 40 |
| 41 $('submit-clear-cached-suggestions') |
| 42 .addEventListener('click', function(event) { |
| 43 chrome.send('clearCachedSuggestions'); |
| 44 event.preventDefault(); |
| 45 }); |
| 46 |
| 47 $('submit-clear-discarded-suggestions') |
| 48 .addEventListener('click', function(event) { |
| 49 chrome.send('clearDiscardedSuggestions'); |
| 50 event.preventDefault(); |
| 51 }); |
| 52 |
41 chrome.send('loaded'); | 53 chrome.send('loaded'); |
42 } | 54 } |
43 | 55 |
44 function setHostRestricted(restricted) { | 56 function setHostRestricted(restricted) { |
45 receiveProperty('switch-restrict-to-hosts', restricted ? 'True' : 'False'); | 57 receiveProperty('switch-restrict-to-hosts', restricted ? 'True' : 'False'); |
46 if (!restricted) { | 58 if (!restricted) { |
47 $('hosts-restrict').classList.add('hidden'); | 59 $('hosts-restrict').classList.add('hidden'); |
48 } | 60 } |
49 } | 61 } |
50 | 62 |
(...skipping 11 matching lines...) Expand all Loading... |
62 function receiveSnippets(snippets) { | 74 function receiveSnippets(snippets) { |
63 lastSnippets = snippets; | 75 lastSnippets = snippets; |
64 displayList(snippets, 'snippets', 'snippet-title'); | 76 displayList(snippets, 'snippets', 'snippet-title'); |
65 } | 77 } |
66 | 78 |
67 function receiveDiscardedSnippets(discardedSnippets) { | 79 function receiveDiscardedSnippets(discardedSnippets) { |
68 displayList(discardedSnippets, 'discarded-snippets', | 80 displayList(discardedSnippets, 'discarded-snippets', |
69 'discarded-snippet-title'); | 81 'discarded-snippet-title'); |
70 } | 82 } |
71 | 83 |
| 84 function receiveContentSuggestions(categoriesList) { |
| 85 displayList(categoriesList, 'content-suggestions', |
| 86 'content-suggestion-title'); |
| 87 } |
| 88 |
72 function receiveJson(json) { | 89 function receiveJson(json) { |
73 var trimmed = json.trim(); | 90 var trimmed = json.trim(); |
74 var hasContent = (trimmed && trimmed != '{}'); | 91 var hasContent = (trimmed && trimmed != '{}'); |
75 | 92 |
76 if (hasContent) { | 93 if (hasContent) { |
77 receiveProperty('last-json-text', trimmed); | 94 receiveProperty('last-json-text', trimmed); |
78 $('last-json').classList.remove('hidden'); | 95 $('last-json').classList.remove('hidden'); |
79 } else { | 96 } else { |
80 $('last-json').classList.add('hidden'); | 97 $('last-json').classList.add('hidden'); |
81 } | 98 } |
82 } | 99 } |
83 | 100 |
84 function downloadJson(json) { | 101 function downloadJson(json) { |
85 // Redirect the browser to download data in |json| as a file "snippets.json" | 102 // Redirect the browser to download data in |json| as a file "snippets.json" |
86 // (Setting Content-Disposition: attachment via a data: URL is not possible; | 103 // (Setting Content-Disposition: attachment via a data: URL is not possible; |
87 // create a link with download attribute and simulate a click, instead.) | 104 // create a link with download attribute and simulate a click, instead.) |
88 var link = document.createElement('a'); | 105 var link = document.createElement('a'); |
89 link.download = 'snippets.json'; | 106 link.download = 'snippets.json'; |
90 link.href = 'data:,' + json; | 107 link.href = 'data:,' + json; |
91 link.click(); | 108 link.click(); |
92 } | 109 } |
93 | 110 |
| 111 function toggleHidden(event) { |
| 112 var id = event.currentTarget.getAttribute('hidden-id'); |
| 113 $(id).classList.toggle('hidden'); |
| 114 } |
| 115 |
94 function displayList(object, domId, titleClass) { | 116 function displayList(object, domId, titleClass) { |
95 jstProcess(new JsEvalContext(object), $(domId)); | 117 jstProcess(new JsEvalContext(object), $(domId)); |
96 | 118 |
97 var text; | 119 var text; |
98 var display; | 120 var display; |
99 | 121 |
100 if (object.list.length > 0) { | 122 if (object.list.length > 0) { |
101 text = ''; | 123 text = ''; |
102 display = 'inline'; | 124 display = 'inline'; |
103 } else { | 125 } else { |
104 text = 'The list is empty.'; | 126 text = 'The list is empty.'; |
105 display = 'none'; | 127 display = 'none'; |
106 } | 128 } |
107 | 129 |
108 if ($(domId + '-empty')) $(domId + '-empty').textContent = text; | 130 if ($(domId + '-empty')) $(domId + '-empty').textContent = text; |
109 if ($(domId + '-clear')) $(domId + '-clear').style.display = display; | 131 if ($(domId + '-clear')) $(domId + '-clear').style.display = display; |
110 | 132 |
111 var links = document.getElementsByClassName(titleClass); | 133 var links = document.getElementsByClassName(titleClass); |
112 for (var link of links) { | 134 for (var link of links) { |
113 link.addEventListener('click', function(event) { | 135 link.addEventListener('click', toggleHidden); |
114 var id = event.currentTarget.getAttribute('snippet-id'); | |
115 $(id).classList.toggle('hidden'); | |
116 }); | |
117 } | 136 } |
118 } | 137 } |
119 | 138 |
120 // Return an object with all of the exports. | 139 // Return an object with all of the exports. |
121 return { | 140 return { |
122 initialize: initialize, | 141 initialize: initialize, |
123 setHostRestricted: setHostRestricted, | 142 setHostRestricted: setHostRestricted, |
124 receiveProperty: receiveProperty, | 143 receiveProperty: receiveProperty, |
125 receiveHosts: receiveHosts, | 144 receiveHosts: receiveHosts, |
126 receiveSnippets: receiveSnippets, | 145 receiveSnippets: receiveSnippets, |
127 receiveDiscardedSnippets: receiveDiscardedSnippets, | 146 receiveDiscardedSnippets: receiveDiscardedSnippets, |
| 147 receiveContentSuggestions: receiveContentSuggestions, |
128 receiveJson: receiveJson, | 148 receiveJson: receiveJson, |
129 }; | 149 }; |
130 }); | 150 }); |
131 | 151 |
132 document.addEventListener('DOMContentLoaded', | 152 document.addEventListener('DOMContentLoaded', |
133 chrome.SnippetsInternals.initialize); | 153 chrome.SnippetsInternals.initialize); |
OLD | NEW |