]>
Commit | Line | Data |
---|---|---|
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 | ||
8 | window.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 | */ | |
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'); | |
cc8be426 | 33 | $('#optionsTimeStart')[0].innerText = chrome.i18n.getMessage('optionsTimeStart'); |
77dffdcd | 34 | $('#optionsCookies')[0].innerText = chrome.i18n.getMessage('optionsCookies'); |
6565d921 | 35 | $('#optionsAutoclear')[0].innerText = chrome.i18n.getMessage('optionsAutoclear'); |
a5409a6b MF |
36 | $('#optionsSaved > b')[0].innerText = chrome.i18n.getMessage('optionsSaved'); |
37 | ||
38 | // Bind all the callbacks | |
39 | $('#opt-time input.opt-chk[type=radio]').forEach(function(e) { | |
40 | e.onclick = toggle; | |
41 | }); | |
77dffdcd MF |
42 | $('input.opt-chk[type=checkbox]').forEach(function(e) { |
43 | e.onclick = setCheck; | |
44 | }); | |
a5409a6b MF |
45 | |
46 | // Load or set localStorage data | |
03eef92c MF |
47 | var settings = [ |
48 | 'timeStart', 'time', 'prompt', 'cookies', 'autoclear', | |
49 | ]; | |
50 | chrome.storage.sync.get(settings, function(s) { | |
51 | var timeStart = s.timeStart || CONSTANTS.YES; | |
52 | var time = ~~(s.time) || (s.time = CONSTANTS.DEFAULT_TIME); | |
53 | var showPrompt = s.prompt || (s.prompt = CONSTANTS.YES); | |
54 | var clearCookies = s.cookies || CONSTANTS.NO; | |
55 | var autoClear = s.autoclear || CONSTANTS.NO; | |
a5409a6b | 56 | |
03eef92c MF |
57 | $('input[name=timeStart]')[0].checked = (timeStart === CONSTANTS.YES); |
58 | $('input[name=time][value="' + time + '"]')[0].checked = true; | |
59 | $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES); | |
60 | $('input[name=cookies]')[0].checked = (clearCookies === CONSTANTS.YES); | |
61 | $('input[name=autoclear]')[0].checked = (autoClear === CONSTANTS.YES); | |
62 | }); | |
a5409a6b MF |
63 | } |
64 | ||
65 | /** | |
66 | * Toggles the value in localStorage for the element selected | |
67 | * @this {HTMLInputElement} The element (radio button) that was clicked. | |
68 | */ | |
69 | function toggle() { | |
03eef92c | 70 | chrome.storage.sync.set({'time': this.value}); |
a5409a6b MF |
71 | optionSaved(); |
72 | } | |
73 | ||
74 | /** | |
75 | * Sets the {@code localStorage.prompt} property when selected | |
76 | * @this {HTMLInputElement} The element (checkbox) that was clicked. | |
77 | */ | |
77dffdcd | 78 | function setCheck() { |
03eef92c MF |
79 | var setting = {}; |
80 | setting[this.name] = this.checked ? CONSTANTS.YES : CONSTANTS.NO; | |
81 | chrome.storage.sync.set(setting); | |
77dffdcd MF |
82 | optionSaved(); |
83 | } | |
84 | ||
a5409a6b MF |
85 | // For rapid changes/saves |
86 | var isSaving = null; | |
87 | /** | |
88 | * Updates the UI to indicate save completed | |
89 | */ | |
90 | function 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 | } |