2 * Copyright (c) 2010 The Chromium Authors. All rights reserved. Use of this
3 * source code is governed by a BSD-style license that can be found in the
8 * @fileoverview Includes the country selection, topics selection and
9 * selection of no. of news stories to be shown. Include default settings also.
10 * @author navneetg@google.com (Navneet Goel).
14 * Stores number of selected topics on the options page.
19 * Stores maximum count of custom topics.
21 var MAX_CUS_TOPICS = 10;
24 * Stores temporary added custom topics which are not yet saved.
26 var tempCusTopics = [];
29 * Checks whether ENTER key is pressed or not.
31 function addCustomTopic() {
32 if (window.event.keyCode == 13) {
38 * Retrieves and sets last saved country from local storage(if found),
39 * else sets country retrieved from feed.
41 function setCountry() {
42 var country = window.localStorage.getItem('country');
44 // If country is not found in localstorage or default value is selected in
46 if ((!country) || country == 'noCountry') {
47 // XMLHttpRequest object that tries to load the feed for the purpose of
48 // retrieving the country value out of feed.
49 var req = new XMLHttpRequest();
50 req.onload = handleResponse;
51 req.onerror = handleError;
52 req.open('GET', DEFAULT_NEWS_URL, true);
55 // Sets country to default Country in dropdown menu.
56 function handleError() {
57 $('countryList').value = 'noCountry';
60 // Handles parsing the feed data got back from XMLHttpRequest.
61 function handleResponse() {
62 // Feed document retrieved from URL.
63 var doc = req.responseXML;
68 var imageLink = doc.querySelector('image link');
70 // Stores link to set value of country.
71 var newsUrl = imageLink.textContent;
74 // Stores country value
75 $('countryList').value = newsUrl.substring(newsUrl.indexOf('&ned=') + 5,
76 newsUrl.indexOf('&hl='));
79 $('countryList').value = country;
84 * Displays various messages to user based on user input.
85 * @param {String} id Id of status element.
86 * @param {Number} timeOut Timeout value of message shown.
87 * @param {String} message Message to be shown.
89 function showUserMessages(id, timeOut, message) {
90 $(id).style.setProperty('-webkit-transition',
91 'opacity 0s ease-in');
92 $(id).style.opacity = 1;
93 $(id).innerText = chrome.i18n.getMessage(message);
94 window.setTimeout(function() {
95 $(id).style.setProperty(
96 '-webkit-transition', 'opacity' + timeOut + 's ease-in');
97 $(id).style.opacity = 0;
103 * Sets options page CSS according to the browser language(if found), else sets
106 function setOptionPageCSS() {
107 if (chrome.i18n.getMessage('direction') == 'rtl') {
108 document.querySelector('body').className = 'rtl';
113 * Initializes the options page by retrieving country, topics and count of
114 * stories from local storage if present, else sets to default settings.
116 function initialize() {
119 setCountAndTopicList();
120 setLocalizedTopicList();
122 // Adds a custom topic on press of Enter key.
123 $('newKeyword').onkeypress = addCustomTopic;
127 * Retrieves locale values from locale file.
129 function setLocalizedTopicList() {
130 var getI18nMsg = chrome.i18n.getMessage;
132 $('top').innerText = getI18nMsg('1');
133 $('nation').innerText = getI18nMsg('n');
134 $('world').innerText = getI18nMsg('w');
135 $('business').innerText = getI18nMsg('b');
136 $('science').innerText = getI18nMsg('t');
137 $('entertainment').innerText = getI18nMsg('e');
138 $('sports').innerText = getI18nMsg('s');
139 $('health').innerText = getI18nMsg('m');
140 $('most').innerText = getI18nMsg('po');
141 $('select_country').innerText = getI18nMsg('country');
142 $('topic').innerText = getI18nMsg('topic');
143 $('save_button').innerText = getI18nMsg('save');
144 $('story_count').innerText = getI18nMsg('storyCount');
145 $('logo').innerHTML = $('logo').innerHTML + getI18nMsg('newsOption');
146 $('custom_text').innerHTML = getI18nMsg('customText') + '<br/>' +
147 getI18nMsg('maximumTopics',[MAX_CUS_TOPICS]);
148 $('submit_button').value = getI18nMsg('submitButton');
152 * Sets topic list and number of stories retrieved from localstorage(if any)
153 * otherwise sets to default.
155 function setCountAndTopicList() {
156 var topicLists = document.getElementsByClassName('checkBox');
158 // Retrieves topics list from localStorage.
159 var topics = JSON.parse(window.localStorage.getItem('topics'));
161 // Runs if retrieved topic list from local storage contains topics.
163 for (var x = 0, topicList; topicList = topicLists[x]; x++) {
165 // Saves whether checkbox is checked or not.
166 var isPresent = false;
167 for (var y = 0; y < topics.length; y++) {
168 if (topics[y] == topicList.value) {
169 topicList.checked = true;
176 topicList.checked = false;
181 // Retrieves list of custom topics from localstorage(if any) and shows it
183 var keywords = JSON.parse(window.localStorage.getItem('keywords'));
186 // Template to store custom topics in a table.
188 var title = chrome.i18n.getMessage('deleteTitle');
189 for (var i = 0; i < keywords.length; i++) {
192 template.push('<tr style = "height: 22px;">');
193 template.push('<td id = "keyword_value" class = "cusTopicsClass">');
194 template.push('<textarea class="noborder" readonly>');
195 template.push(keywords[i]);
196 template.push('</textarea>');
197 template.push('<td class = "suppr" onclick = "delCusTopic(this)" ');
198 template.push('title="');
199 template.push(title);
201 template.push('</td>');
202 template.push('</tr>');
204 $('custom_topics').innerHTML = template.join('');
205 if (keywords.length == MAX_CUS_TOPICS) {
206 $('submit_button').disabled = true;
207 $('newKeyword').readOnly = 'readonly';
210 // Check all checkboxes(default settings) if no custom topic list and
211 // checkbox topic list from local storage is found.
212 if (!keywords && !topics) {
213 for (var x = 0, topicList; topicList = topicLists[x]; x++) {
214 topicList.checked = true;
219 // Retrieves saved value of number of stories.
220 var count = window.localStorage.getItem('count');
222 // Sets number of stories in dropdown.
224 $('storyCount').value = count;
229 * Saves checked topic list(if any), Custom topics(if any), number of
230 * stories and country value in local storage.
232 function saveTopicsCountry() {
233 var country = $('countryList').value;
234 var topicLists = document.getElementsByClassName('checkBox');
236 // Contains selected number of stories.
237 var count = $('storyCount').value;
239 // Stores checked topics list.
241 for (var i = 0, topicList; topicList = topicLists[i]; i++) {
242 if (topicList.checked) {
243 topicArr.push(topicList.value);
246 var keywords = JSON.parse(window.localStorage.getItem('keywords'));
248 // Saves custom topics to local storage(if any).
249 if (tempCusTopics.length > 0) {
251 keywords = keywords.concat(tempCusTopics);
252 window.localStorage.setItem('keywords', JSON.stringify(keywords));
254 window.localStorage.setItem('keywords', JSON.stringify(tempCusTopics));
256 tempCusTopics.splice(0, tempCusTopics.length);
259 // Saves checkbox topics(if any).
260 if (topicArr.length > 0) {
261 window.localStorage.setItem('topics', JSON.stringify(topicArr));
263 window.localStorage.removeItem('topics');
266 window.localStorage.setItem('count', count);
267 window.localStorage.setItem('country', country);
269 showUserMessages('save_status', 0.5, 'saveStatus');
270 $('save_button').disabled = true;
274 * Disables the save button on options page if no topic is selected by the user.
275 * @param {String} id Id of checkbox checked or unchecked.
277 function manageCheckCount(id) {
278 checkCount = ($(id).checked) ? (checkCount + 1) : (checkCount - 1);
279 $('save_button').disabled = (checkCount == 0) ? true : false;
283 * Enables save button if at least one topic is selected.
285 function enableSaveButton() {
286 if (checkCount != 0) {
287 $('save_button').disabled = false;
292 * Adds new entered custom topic.
294 function addCusTopic() {
295 // Retrieves custom topic list from local storage(if any), else create new
297 var keywords = JSON.parse(window.localStorage.getItem('keywords') || "[]");
299 // Adds topic only if total number of added custom topics are less than 10.
300 if (keywords.length + tempCusTopics.length <= (MAX_CUS_TOPICS - 1)) {
302 // Stores new entered value in input textbox.
303 var val = $('newKeyword').value;
306 if (val.length > 0) {
309 // Runs if comma(,) is not present in topic entered.
310 if (val.match(pattern) == null) {
312 tempCusTopics.push(val);
314 // Template to store custom topics in a table.
316 var title = chrome.i18n.getMessage('deleteTitle');
318 template.push('<tr style = "height: 22px;">');
319 template.push('<td id = "keyword_value" class = "cusTopicsClass">');
320 template.push('<textarea class="noborder" readonly>');
322 template.push('</textarea>');
323 template.push('<td class = "suppr" onclick = "delCusTopic(this)" ');
324 template.push('title="');
325 template.push(title);
327 template.push('</td>');
328 template.push('</tr>');
330 $('custom_topics').innerHTML += template.join('');
333 showUserMessages('invalid_status', 2.5, 'invalidChars');
336 $('newKeyword').value = '';
340 if ((keywords.length + tempCusTopics.length) == (MAX_CUS_TOPICS)) {
341 $('submit_button').disabled = true;
342 $('newKeyword').readOnly = 'readonly';
347 * Delete custom topic whenever users click on delete icon.
348 * @param {HTMLTableColElement} obj HTML table column element to be deleted.
350 function delCusTopic(obj) {
351 // Deletes only if total number of topics are greater than 1, else shows
353 if (checkCount > 1) {
356 // Extract custom topic value.
357 value = obj.parentNode.querySelector('.cusTopicsClass textarea').value;
359 // Removes custom topic element from UI.
360 $('custom_topics').removeChild(obj.parentNode);
362 // Removes custom topic element either from temporary array(if topic is
363 // not yet saved) or from saved topic list and saves new list to
366 for (var i = 0; i < tempCusTopics.length; i++) {
367 if (tempCusTopics[i] == value) {
368 tempCusTopics.splice(i, 1);
375 var keywords = JSON.parse(window.localStorage.getItem('keywords'));
376 for (i = 0; i < keywords.length; i++) {
377 if (keywords[i] == value) {
378 keywords.splice(i, 1);
382 if (keywords.length > 0) {
383 window.localStorage.setItem('keywords', JSON.stringify(keywords));
385 window.localStorage.removeItem('keywords');
390 $('submit_button').disabled = false;
392 showUserMessages('save_status', 2.5, 'noTopic');
394 $('newKeyword').readOnly = false;