]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blob - javascript/options.js
use non-persistent background page
[chrome-ext/clearhistory-advance-fork.git] / javascript / options.js
1 // Copyright 2011 Google Inc. All Rights Reserved.
2
3 /**
4 * @fileoverview Script for the options page.
5 * @author arunjit@google.com (Arunjit Singh)
6 */
7
8 window.addEventListener('load', init, false);
9
10 /**
11 * Initializes the i18n strings.
12 * Loads values from localStorage and applies them to the elements
13 */
14 function init() {
15 // Localize strings:
16 CONSTANTS.TIMES.forEach(function(time) {
17 var selector = 'input[name=time][value="{t}"], .opt-label#opt-{t}'
18 .supplant({t: time});
19 var elements = $(selector);
20 time = getUnitsForTime(time);
21 var message = (time.time === -1) ?
22 chrome.i18n.getMessage('optionsTimeAll') :
23 chrome.i18n.getMessage('optionsTime',
24 [time.time, time.units]);
25 elements[0].title = message;
26 elements[1].innerText = message;
27 });
28
29 $('#optionsTitle')[0].innerText = chrome.i18n.getMessage('optionsTitle');
30 $('#optionsHeader')[0].innerText = chrome.i18n.getMessage('optionsHeader');
31 $('#optionsPrompt')[0].innerText = chrome.i18n.getMessage('optionsPrompt');
32 $('#optionsTimeFor')[0].innerText = chrome.i18n.getMessage('optionsTimeFor');
33 $('#optionsSaved > b')[0].innerText = chrome.i18n.getMessage('optionsSaved');
34
35 // Bind all the callbacks
36 $('#opt-time input.opt-chk[type=radio]').forEach(function(e) {
37 e.onclick = toggle;
38 });
39 $('#opt-prompt input.opt-chk[type=checkbox]').forEach(function(e) {
40 e.onclick = setPrompt;
41 });
42
43 // Load or set localStorage data
44 var time = ~~(localStorage['time']) ||
45 (localStorage['time'] = CONSTANTS.DEFAULT_TIME);
46 var showPrompt = localStorage['prompt'] ||
47 (localStorage['prompt'] = CONSTANTS.YES);
48
49 $('input[name=time][value="' + time + '"]')[0].checked = true;
50 $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES);
51 }
52
53 /**
54 * Toggles the value in localStorage for the element selected
55 * @this {HTMLInputElement} The element (radio button) that was clicked.
56 */
57 function toggle() {
58 localStorage['time'] = this.value;
59 optionSaved();
60 }
61
62 /**
63 * Sets the {@code localStorage.prompt} property when selected
64 * @this {HTMLInputElement} The element (checkbox) that was clicked.
65 */
66 function setPrompt() {
67 localStorage['prompt'] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
68 optionSaved();
69 }
70
71 // For rapid changes/saves
72 var isSaving = null;
73 /**
74 * Updates the UI to indicate save completed
75 */
76 function optionSaved() {
77 var element = $('#optionsSaved')[0];
78 element.classList.add('show');
79 clearTimeout(isSaving);
80 isSaving = setTimeout(function() {
81 element.classList.remove('show');
82 }, 1000);
83 }