comparison options/options.js @ 29:688d75e554e0

Add option to expand feed entries by default
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Mon, 21 Jan 2019 15:15:59 +0100
parents ff5e5e3eba32
children 3c97046c2348
comparison
equal deleted inserted replaced
28:104a1faebd43 29:688d75e554e0
16 constructor() { 16 constructor() {
17 this.selectedFeedReader = -1; 17 this.selectedFeedReader = -1;
18 18
19 document.querySelector('#feed-readers-title').textContent = 19 document.querySelector('#feed-readers-title').textContent =
20 browser.i18n.getMessage('feedReadersTitle'); 20 browser.i18n.getMessage('feedReadersTitle');
21 document.querySelector('#feed-preview-title').textContent =
22 browser.i18n.getMessage('feedPreviewTitle');
21 23
22 let feedReadersForm = document.forms['feed-readers']; 24 let feedReadersForm = document.forms['feed-readers'];
23 feedReadersForm.elements['move-up'].textContent = 25 feedReadersForm.elements['move-up'].textContent =
24 browser.i18n.getMessage('feedReaderMoveUpButton'); 26 browser.i18n.getMessage('feedReaderMoveUpButton');
25 feedReadersForm.elements['move-down'].textContent = 27 feedReadersForm.elements['move-down'].textContent =
44 browser.i18n.getMessage('feedReaderUrlTemplatePlaceholder'); 46 browser.i18n.getMessage('feedReaderUrlTemplatePlaceholder');
45 document.querySelector('#feed-reader-url-caption').textContent = 47 document.querySelector('#feed-reader-url-caption').textContent =
46 browser.i18n.getMessage('feedReaderUrlTemplateCaption'); 48 browser.i18n.getMessage('feedReaderUrlTemplateCaption');
47 addFeedReaderForm.addEventListener('focusout', this); 49 addFeedReaderForm.addEventListener('focusout', this);
48 50
51 let feedPreviewForm = document.forms['feed-preview'];
52 feedPreviewForm.elements['expand-entries'].labels[0].textContent =
53 browser.i18n.getMessage('feedPreviewExpandItemLabel');
54 feedPreviewForm.addEventListener('change', this);
55
49 document.addEventListener('submit', this); 56 document.addEventListener('submit', this);
50 57
51 this.initOptions(); 58 this.initOptions();
52 } 59 }
53 60
54 async initOptions() { 61 async initOptions() {
55 let {feedReaders} = await browser.storage.sync.get('feedReaders'); 62 let {feedReaders} = await browser.storage.sync.get('feedReaders');
56 if (Array.isArray(feedReaders)) { 63 if (Array.isArray(feedReaders)) {
57 console.log('initialized feedReaders from storage', feedReaders); 64 console.log('initialized feedReaders from storage', feedReaders);
58 this.updateFeedReaders(feedReaders); 65 this.updateFeedReaders(feedReaders);
66 }
67
68 let {feedPreview: feedPreviewOptions} =
69 await browser.storage.sync.get('feedPreview');
70 if (typeof feedPreviewOptions !== 'undefined' &&
71 feedPreviewOptions === Object(feedPreviewOptions)) {
72 console.log('initialized feedPreviewOptions from storage',
73 feedPreviewOptions);
74 this.updateFeedPreviewOptions({
75 expandEntries: !!feedPreviewOptions.expandEntries
76 });
59 } 77 }
60 78
61 browser.storage.onChanged.addListener(this.onStorageChanged.bind(this)); 79 browser.storage.onChanged.addListener(this.onStorageChanged.bind(this));
62 } 80 }
63 81
111 } 129 }
112 130
113 feedReadersForm.elements['buttons'].disabled = true; 131 feedReadersForm.elements['buttons'].disabled = true;
114 } 132 }
115 133
134 updateFeedPreviewOptions(feedPreviewOptions) {
135 document.forms['feed-preview'].elements['expand-entries'].checked =
136 feedPreviewOptions.expandEntries;
137 }
138
116 getFeedReaders() { 139 getFeedReaders() {
117 let feedReaderInput = 140 let feedReaderInput =
118 document.forms['feed-readers'].elements['feed-reader']; 141 document.forms['feed-readers'].elements['feed-reader'];
119 if (feedReaderInput instanceof RadioNodeList) { 142 if (feedReaderInput instanceof RadioNodeList) {
120 return Array.from(feedReaderInput); 143 return Array.from(feedReaderInput);
148 urlTemplate: element.value 171 urlTemplate: element.value
149 })); 172 }));
150 } 173 }
151 174
152 onStorageChanged(changes, areaName) { 175 onStorageChanged(changes, areaName) {
153 if (areaName !== 'sync' || typeof changes.feedReaders === 'undefined') { 176 if (areaName !== 'sync') {
154 return; 177 return;
155 } 178 }
156 179
157 let feedReaders; 180 if (typeof changes.feedReaders !== 'undefined') {
158 if (typeof changes.feedReaders.newValue !== 'undefined' && 181 let feedReaders;
159 Array.isArray(changes.feedReaders.newValue)) { 182 if (typeof changes.feedReaders.newValue !== 'undefined' &&
160 feedReaders = changes.feedReaders.newValue; 183 Array.isArray(changes.feedReaders.newValue)) {
161 console.log('feedReaders changed to', feedReaders); 184 feedReaders = changes.feedReaders.newValue;
162 } else { 185 console.log('feedReaders changed to', feedReaders);
163 // list of feed readers was removed or set to nonsensical value 186 } else {
164 feedReaders = []; 187 // list of feed readers was removed or set to nonsensical value
165 console.log('feedReaders was removed'); 188 feedReaders = [];
166 } 189 console.log('feedReaders was removed');
167 if (this.selectedFeedReader >= feedReaders.length) { 190 }
168 // save selected feed reader is no longer valid 191 if (this.selectedFeedReader >= feedReaders.length) {
169 this.selectedFeedReader = -1; 192 // save selected feed reader is no longer valid
170 } 193 this.selectedFeedReader = -1;
171 this.updateFeedReaders(feedReaders); 194 }
172 this.selectFeedReader(); 195 this.updateFeedReaders(feedReaders);
196 this.selectFeedReader();
197 }
198 if (typeof changes.feedPreview !== 'undefined') {
199 let feedPreviewOptions;
200 let newValue = changes.feedPreview.newValue;
201 if (typeof newValue !== 'undefined' &&
202 newValue === Object(newValue)) {
203 feedPreviewOptions = {expandEntries: !!newValue.expandEntries};
204 } else {
205 // feed preview preferences were removed or set to nonsensical
206 // value
207 feedPreviewOptions = {expandEntries: false};
208 console.log('feedPreview was removed');
209 }
210 this.updateFeedPreviewOptions(feedPreviewOptions);
211 }
173 } 212 }
174 213
175 handleEvent(ev) { 214 handleEvent(ev) {
176 console.log('previously selected:', this.selectedFeedReader); 215 console.log('previously selected:', this.selectedFeedReader);
177 if (ev.type === 'change' && ev.target.name === 'feed-reader') { 216 if (ev.type === 'change' && ev.target.name === 'feed-reader') {
236 }); 275 });
237 browser.storage.sync.set({feedReaders}); 276 browser.storage.sync.set({feedReaders});
238 console.log('set feedReaders to', feedReaders); 277 console.log('set feedReaders to', feedReaders);
239 278
240 document.forms['add-feed-reader'].reset(); 279 document.forms['add-feed-reader'].reset();
280 } else if (ev.type === 'change' && ev.target.id === 'expand-entries') {
281 console.log('expand entries by default:', ev.target.checked);
282 browser.storage.sync.set({
283 feedPreview: {expandEntries: ev.target.checked}
284 });
241 } 285 }
242 } 286 }
243 } 287 }
244 288
245 var page = new OptionsPage(); 289 var page = new OptionsPage();