]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blame - javascript/options.js
allow people to delete *old* history rather than *new* history
[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
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');
cc8be426 33 $('#optionsTimeStart')[0].innerText = chrome.i18n.getMessage('optionsTimeStart');
77dffdcd 34 $('#optionsCookies')[0].innerText = chrome.i18n.getMessage('optionsCookies');
a5409a6b
MF
35 $('#optionsSaved > b')[0].innerText = chrome.i18n.getMessage('optionsSaved');
36
37 // Bind all the callbacks
38 $('#opt-time input.opt-chk[type=radio]').forEach(function(e) {
39 e.onclick = toggle;
40 });
77dffdcd
MF
41 $('input.opt-chk[type=checkbox]').forEach(function(e) {
42 e.onclick = setCheck;
43 });
a5409a6b
MF
44 $('#opt-prompt input.opt-chk[type=checkbox]').forEach(function(e) {
45 e.onclick = setPrompt;
46 });
47
48 // Load or set localStorage data
cc8be426 49 var timeStart = localStorage['timeStart'] || CONSTANTS.YES;
a5409a6b
MF
50 var time = ~~(localStorage['time']) ||
51 (localStorage['time'] = CONSTANTS.DEFAULT_TIME);
52 var showPrompt = localStorage['prompt'] ||
53 (localStorage['prompt'] = CONSTANTS.YES);
77dffdcd 54 var clearCookies = localStorage['cookies'] || CONSTANTS.NO;
a5409a6b 55
cc8be426 56 $('input[name=timeStart]')[0].checked = (timeStart === CONSTANTS.YES);
a5409a6b
MF
57 $('input[name=time][value="' + time + '"]')[0].checked = true;
58 $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES);
77dffdcd 59 $('input[name=cookies]')[0].checked = (clearCookies === CONSTANTS.YES);
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() {
67 localStorage['time'] = this.value;
68 optionSaved();
69}
70
71/**
72 * Sets the {@code localStorage.prompt} property when selected
73 * @this {HTMLInputElement} The element (checkbox) that was clicked.
74 */
75function setPrompt() {
76 localStorage['prompt'] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
77 optionSaved();
78}
79
77dffdcd
MF
80function setCheck() {
81 localStorage[this.name] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
82 optionSaved();
83}
84
a5409a6b
MF
85// For rapid changes/saves
86var isSaving = null;
87/**
88 * Updates the UI to indicate save completed
89 */
90function optionSaved() {
91 var element = $('#optionsSaved')[0];
92 element.classList.add('show');
93 clearTimeout(isSaving);
94 isSaving = setTimeout(function() {
95 element.classList.remove('show');
96 }, 1000);
97}