]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blame - javascript/options.js
rewrite options page to be more chrome-like
[chrome-ext/clearhistory-advance-fork.git] / javascript / options.js
CommitLineData
a5409a6b
MF
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
8window.addEventListener('load', init, false);
9
a5409a6b
MF
10/**
11 * Initializes the i18n strings.
12 * Loads values from localStorage and applies them to the elements
13 */
14function 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
999b23e3
MF
29 $('[i18n-content]').forEach(function(ele) {
30 ele.innerText = chrome.i18n.getMessage(ele.getAttribute('i18n-content'));
31 });
a5409a6b
MF
32
33 // Bind all the callbacks
999b23e3 34 $('input[type=radio]').forEach(function(e) {
a5409a6b
MF
35 e.onclick = toggle;
36 });
999b23e3 37 $('input[type=checkbox]').forEach(function(e) {
77dffdcd
MF
38 e.onclick = setCheck;
39 });
a5409a6b
MF
40
41 // Load or set localStorage data
03eef92c 42 var settings = [
c8fabbc8 43 'timeStart', 'time', 'prompt', 'cookies', 'downloads', 'autoclear',
03eef92c
MF
44 ];
45 chrome.storage.sync.get(settings, function(s) {
46 var timeStart = s.timeStart || CONSTANTS.YES;
47 var time = ~~(s.time) || (s.time = CONSTANTS.DEFAULT_TIME);
48 var showPrompt = s.prompt || (s.prompt = CONSTANTS.YES);
49 var clearCookies = s.cookies || CONSTANTS.NO;
c8fabbc8 50 var clearDownloads = s.downloads || CONSTANTS.NO;
03eef92c 51 var autoClear = s.autoclear || CONSTANTS.NO;
a5409a6b 52
999b23e3 53 $('input[name=timeStart][value="' + timeStart + '"]')[0].checked = true;
03eef92c
MF
54 $('input[name=time][value="' + time + '"]')[0].checked = true;
55 $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES);
56 $('input[name=cookies]')[0].checked = (clearCookies === CONSTANTS.YES);
c8fabbc8 57 $('input[name=downloads]')[0].checked = (clearDownloads === CONSTANTS.YES);
03eef92c
MF
58 $('input[name=autoclear]')[0].checked = (autoClear === CONSTANTS.YES);
59 });
a5409a6b
MF
60}
61
62/**
63 * Toggles the value in localStorage for the element selected
64 * @this {HTMLInputElement} The element (radio button) that was clicked.
65 */
66function toggle() {
999b23e3
MF
67 var setting = {};
68 setting[this.name] = this.value;
69 chrome.storage.sync.set(setting);
a5409a6b
MF
70 optionSaved();
71}
72
73/**
74 * Sets the {@code localStorage.prompt} property when selected
75 * @this {HTMLInputElement} The element (checkbox) that was clicked.
76 */
77dffdcd 77function setCheck() {
03eef92c
MF
78 var setting = {};
79 setting[this.name] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
80 chrome.storage.sync.set(setting);
77dffdcd
MF
81 optionSaved();
82}
83
a5409a6b
MF
84// For rapid changes/saves
85var isSaving = null;
86/**
87 * Updates the UI to indicate save completed
88 */
89function optionSaved() {
90 var element = $('#optionsSaved')[0];
91 element.classList.add('show');
92 clearTimeout(isSaving);
93 isSaving = setTimeout(function() {
94 element.classList.remove('show');
95 }, 1000);
96}