OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 (function() { | 5 (function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 cr.define('cr.translateInternals', function() { | 8 cr.define('cr.translateInternals', function() { |
9 | 9 |
10 /** | 10 /** |
11 * Initializes UI and sends a message to the browser for | 11 * Initializes UI and sends a message to the browser for |
12 * initialization. | 12 * initialization. |
13 */ | 13 */ |
14 function initialize() { | 14 function initialize() { |
15 cr.ui.decorate('tabbox', cr.ui.TabBox); | 15 cr.ui.decorate('tabbox', cr.ui.TabBox); |
16 chrome.send('requestInfo'); | 16 chrome.send('requestInfo'); |
17 } | 17 } |
18 | 18 |
19 /** | 19 /** |
20 * Creates a new LI element with a button to dismiss the item. | 20 * Creates a new LI element with a button to dismiss the item. |
21 * @param {string} text lable of the LI element. | 21 * |
22 * @param {Function} func callback when the button is clicked. | 22 * @param {string} text The lable of the LI element. |
23 * @param {Function} func Callback called when the button is clicked. | |
23 */ | 24 */ |
24 function createLIWithDismissingButton(text, func) { | 25 function createLIWithDismissingButton(text, func) { |
25 var span = document.createElement('span'); | 26 var span = document.createElement('span'); |
26 span.textContent = text; | 27 span.textContent = text; |
27 | 28 |
28 var li = document.createElement('li'); | 29 var li = document.createElement('li'); |
29 li.appendChild(span); | 30 li.appendChild(span); |
30 | 31 |
31 var button = document.createElement('button'); | 32 var button = document.createElement('button'); |
32 button.textContent = 'X'; | 33 button.textContent = 'X'; |
33 button.addEventListener('click', function(e) { | 34 button.addEventListener('click', function(e) { |
34 e.preventDefault(); | 35 e.preventDefault(); |
35 func(); | 36 func(); |
36 }, false); | 37 }, false); |
37 | 38 |
38 li.appendChild(button); | 39 li.appendChild(button); |
39 return li; | 40 return li; |
40 } | 41 } |
41 | 42 |
42 /** | 43 /** |
44 * Formats the language name to a human-readable text. For example, if | |
45 * |langCode| is 'en', this may return 'en (English)'. | |
46 * | |
47 * @param {string} langCode ISO 639 language code. | |
48 * @return {string} The formatted string. | |
49 */ | |
50 function formatLanguageCode(langCode) { | |
51 var key = 'language-' + langCode; | |
52 if (key in templateData) { | |
53 var langName = templateData[key]; | |
54 return langCode + ' (' + langName + ')'; | |
55 } | |
56 | |
57 return langCode; | |
58 } | |
59 | |
60 /** | |
43 * Handles the message of 'prefsUpdated' from the browser. | 61 * Handles the message of 'prefsUpdated' from the browser. |
44 * @param {Object} detail The object which represents pref values. | 62 * |
63 * @param {Object} detail the object which represents pref values. | |
45 */ | 64 */ |
46 function onPrefsUpdated(detail) { | 65 function onPrefsUpdated(detail) { |
47 var ul; | 66 var ul; |
48 ul = document.querySelector('#prefs-language-blacklist ul'); | 67 ul = document.querySelector('#prefs-language-blacklist ul'); |
49 ul.innerHTML = ''; | 68 ul.innerHTML = ''; |
50 | 69 |
51 if ('translate_language_blacklist' in detail) { | 70 if ('translate_language_blacklist' in detail) { |
52 var langs = detail['translate_language_blacklist']; | 71 var langs = detail['translate_language_blacklist']; |
53 | 72 |
54 langs.forEach(function(langCode) { | 73 langs.forEach(function(langCode) { |
55 var langName = templateData['language-' + langCode]; | 74 var text = formatLanguageCode(langCode); |
56 var text = langCode + ' (' + langName + ')'; | |
57 | 75 |
58 var li = createLIWithDismissingButton(text, function() { | 76 var li = createLIWithDismissingButton(text, function() { |
59 chrome.send('removePrefItem', | 77 chrome.send('removePrefItem', |
60 ['language_blacklist', langCode]); | 78 ['language_blacklist', langCode]); |
61 }); | 79 }); |
62 ul.appendChild(li); | 80 ul.appendChild(li); |
63 }); | 81 }); |
64 } | 82 } |
65 | 83 |
66 ul = document.querySelector('#prefs-site-blacklist ul'); | 84 ul = document.querySelector('#prefs-site-blacklist ul'); |
(...skipping 11 matching lines...) Expand all Loading... | |
78 } | 96 } |
79 | 97 |
80 ul = document.querySelector('#prefs-whitelists ul'); | 98 ul = document.querySelector('#prefs-whitelists ul'); |
81 ul.innerHTML = ''; | 99 ul.innerHTML = ''; |
82 | 100 |
83 if ('translate_whitelists' in detail) { | 101 if ('translate_whitelists' in detail) { |
84 var pairs = detail['translate_whitelists']; | 102 var pairs = detail['translate_whitelists']; |
85 | 103 |
86 Object.keys(pairs).forEach(function(fromLangCode) { | 104 Object.keys(pairs).forEach(function(fromLangCode) { |
87 var toLangCode = pairs[fromLangCode]; | 105 var toLangCode = pairs[fromLangCode]; |
88 var fromLangName = templateData['language-' + fromLangCode]; | 106 var text = formatLanguageCode(fromLangCode) + ' \u2192 ' + |
89 var toLangName = templateData['language-' + toLangCode]; | 107 formatLanguageCode(toLangCode); |
90 var text = fromLangCode + ' (' + fromLangName + ') \u2192 ' + | |
91 toLangCode + ' (' + toLangName + ')'; | |
92 | 108 |
93 var li = createLIWithDismissingButton(text, function() { | 109 var li = createLIWithDismissingButton(text, function() { |
94 chrome.send('removePrefItem', | 110 chrome.send('removePrefItem', |
95 ['whitelists', fromLangCode, toLangCode]); | 111 ['whitelists', fromLangCode, toLangCode]); |
96 }); | 112 }); |
97 ul.appendChild(li); | 113 ul.appendChild(li); |
98 }); | 114 }); |
99 } | 115 } |
100 | 116 |
101 var p = document.querySelector('#prefs-dump p'); | 117 var p = document.querySelector('#prefs-dump p'); |
102 var content = JSON.stringify(detail, null, 2); | 118 var content = JSON.stringify(detail, null, 2); |
103 p.textContent = content; | 119 p.textContent = content; |
104 } | 120 } |
105 | 121 |
106 /** | 122 /** |
123 * Addes '0's to |number| as a string. |width| is length of the | |
Evan Stade
2013/05/18 23:42:58
please keep the number of spaces after a period co
hajimehoshi
2013/05/20 01:11:34
Done.
| |
124 * string including '0's. | |
125 * | |
126 * @param {string} number The number to be converted into a string. | |
127 * @param {number} width The width of the returned string. | |
128 * @return {string} The formatted string. | |
129 */ | |
130 function fillWithZeros(number, width) { | |
Evan Stade
2013/05/18 23:42:58
nit: call the function padWithZeros
hajimehoshi
2013/05/20 01:11:34
Done.
| |
131 var numberStr = number.toString(); | |
132 var restWidth = width - numberStr.length; | |
133 if (restWidth <= 0) | |
134 return numberStr; | |
135 | |
136 Array(restWidth + 1).join('0') + numberStr; | |
Evan Stade
2013/05/18 23:42:58
you seem to be missing an assignment here? did you
hajimehoshi
2013/05/20 01:11:34
Right, I meant to return this...
On 2013/05/18 23
| |
137 return numberStr; | |
138 } | |
139 | |
140 /** | |
141 * Formats |date| as a Date object into a string. The format is like | |
142 * '2006-01-02 15:04:05'. | |
143 * | |
144 * @param {Date} date Date to be formatted. | |
145 * @return {string} The formatted string. | |
146 */ | |
147 function formatDate(date) { | |
148 var year = date.getFullYear(); | |
149 var month = date.getMonth() + 1; | |
150 var day = date.getDay(); | |
151 var hour = date.getHours(); | |
152 var minute = date.getMinutes(); | |
153 var second = date.getSeconds(); | |
154 | |
155 var yearStr = fillWithZeros(year, 4); | |
156 var monthStr = fillWithZeros(month, 2); | |
157 var dayStr = fillWithZeros(day, 2); | |
158 var hourStr = fillWithZeros(hour, 2); | |
159 var minuteStr = fillWithZeros(minute, 2); | |
160 var secondStr = fillWithZeros(second, 2); | |
161 | |
162 var str = yearStr + '-' + monthStr + '-' + dayStr + ' ' + | |
163 hourStr + ':' + minuteStr + ':' + secondStr; | |
164 | |
165 return str; | |
166 } | |
167 | |
168 /** | |
169 * Returns a new TD element. | |
170 * | |
171 * @param {string} content The text content of the element. | |
172 * @param {string} className The class name of the element. | |
173 * @return {string} The new TD element. | |
174 */ | |
175 function createTD(content, className) { | |
176 var td = document.createElement('td'); | |
177 td.textContent = content; | |
178 td.className = className; | |
179 return td; | |
180 } | |
181 | |
182 /** | |
183 * Handles the message of 'languageDetectionInfoAdded' from the | |
184 * browser. | |
185 * | |
186 * @param {Object} detail The object which represents the logs. | |
187 */ | |
188 function onLanguageDetectionInfoAdded(detail) { | |
189 var tr = document.createElement('tr'); | |
190 | |
191 var date = new Date(detail['time']); | |
192 [ | |
193 createTD(formatDate(date), 'detection-logs-time'), | |
194 createTD(detail['url'], 'detection-logs-url'), | |
195 createTD(formatLanguageCode(detail['content_language']), | |
196 'detection-logs-content-language'), | |
197 createTD(formatLanguageCode(detail['cld_language']), | |
198 'detection-logs-cld-language'), | |
199 createTD(detail['is_cld_reliable'], | |
200 'detection-logs-is-cld-reliable'), | |
201 createTD(formatLanguageCode(detail['language']), | |
202 'detection-logs-language'), | |
203 ].forEach(function(td) { | |
204 tr.appendChild(td); | |
205 }); | |
206 | |
207 var tbody = $('detection-logs').getElementsByTagName('tbody')[0]; | |
208 tbody.appendChild(tr); | |
209 } | |
210 | |
211 /** | |
107 * The callback entry point from the browser. This function will be | 212 * The callback entry point from the browser. This function will be |
108 * called by the browser. | 213 * called by the browser. |
109 * @param {string} message The name of the sent message | 214 * |
110 * @param {Object} detail The argument of the sent message | 215 * @param {string} message The name of the sent message. |
216 * @param {Object} detail The argument of the sent message. | |
111 */ | 217 */ |
112 function messageHandler(message, detail) { | 218 function messageHandler(message, detail) { |
113 switch (message) { | 219 switch (message) { |
220 case 'languageDetectionInfoAdded': | |
221 cr.translateInternals.onLanguageDetectionInfoAdded(detail); | |
222 break; | |
114 case 'prefsUpdated': | 223 case 'prefsUpdated': |
115 cr.translateInternals.onPrefsUpdated(detail); | 224 cr.translateInternals.onPrefsUpdated(detail); |
116 break; | 225 break; |
117 default: | 226 default: |
118 console.error('Unknown message:', message); | 227 console.error('Unknown message:', message); |
119 break; | 228 break; |
120 } | 229 } |
121 } | 230 } |
122 | 231 |
123 return { | 232 return { |
124 initialize: initialize, | 233 initialize: initialize, |
125 messageHandler: messageHandler, | 234 messageHandler: messageHandler, |
126 onPrefsUpdated: onPrefsUpdated | 235 onLanguageDetectionInfoAdded: onLanguageDetectionInfoAdded, |
236 onPrefsUpdated: onPrefsUpdated, | |
127 }; | 237 }; |
128 }); | 238 }); |
129 | 239 |
130 /** | 240 /** |
131 * The entry point of the UI. | 241 * The entry point of the UI. |
132 */ | 242 */ |
133 function main() { | 243 function main() { |
134 cr.doc.addEventListener('DOMContentLoaded', | 244 cr.doc.addEventListener('DOMContentLoaded', |
135 cr.translateInternals.initialize); | 245 cr.translateInternals.initialize); |
136 } | 246 } |
137 | 247 |
138 main(); | 248 main(); |
139 })(); | 249 })(); |
OLD | NEW |