diff 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
line wrap: on
line diff
--- a/options/options.js	Mon Jan 21 08:22:52 2019 +0100
+++ b/options/options.js	Mon Jan 21 15:15:59 2019 +0100
@@ -18,6 +18,8 @@
 
         document.querySelector('#feed-readers-title').textContent =
                 browser.i18n.getMessage('feedReadersTitle');
+        document.querySelector('#feed-preview-title').textContent =
+                browser.i18n.getMessage('feedPreviewTitle');
 
         let feedReadersForm = document.forms['feed-readers'];
         feedReadersForm.elements['move-up'].textContent =
@@ -46,6 +48,11 @@
                 browser.i18n.getMessage('feedReaderUrlTemplateCaption');
         addFeedReaderForm.addEventListener('focusout', this);
 
+        let feedPreviewForm = document.forms['feed-preview'];
+        feedPreviewForm.elements['expand-entries'].labels[0].textContent =
+                browser.i18n.getMessage('feedPreviewExpandItemLabel');
+        feedPreviewForm.addEventListener('change', this);
+
         document.addEventListener('submit', this);
 
         this.initOptions();
@@ -58,6 +65,17 @@
             this.updateFeedReaders(feedReaders);
         }
 
+        let {feedPreview: feedPreviewOptions} =
+                await browser.storage.sync.get('feedPreview');
+        if (typeof feedPreviewOptions !== 'undefined' &&
+                feedPreviewOptions === Object(feedPreviewOptions)) {
+            console.log('initialized feedPreviewOptions from storage',
+                    feedPreviewOptions);
+            this.updateFeedPreviewOptions({
+                    expandEntries: !!feedPreviewOptions.expandEntries
+            });
+        }
+
         browser.storage.onChanged.addListener(this.onStorageChanged.bind(this));
     }
 
@@ -113,6 +131,11 @@
         feedReadersForm.elements['buttons'].disabled = true;
     }
 
+    updateFeedPreviewOptions(feedPreviewOptions) {
+        document.forms['feed-preview'].elements['expand-entries'].checked =
+                feedPreviewOptions.expandEntries;
+    }
+
     getFeedReaders() {
         let feedReaderInput =
                 document.forms['feed-readers'].elements['feed-reader'];
@@ -150,26 +173,42 @@
     }
 
     onStorageChanged(changes, areaName) {
-        if (areaName !== 'sync' || typeof changes.feedReaders === 'undefined') {
+        if (areaName !== 'sync') {
             return;
         }
 
-        let feedReaders;
-        if (typeof changes.feedReaders.newValue !== 'undefined' &&
-                Array.isArray(changes.feedReaders.newValue)) {
-            feedReaders = changes.feedReaders.newValue;
-            console.log('feedReaders changed to', feedReaders);
-        } else {
-            // list of feed readers was removed or set to nonsensical value
-            feedReaders = [];
-            console.log('feedReaders was removed');
+        if (typeof changes.feedReaders !== 'undefined') {
+            let feedReaders;
+            if (typeof changes.feedReaders.newValue !== 'undefined' &&
+                    Array.isArray(changes.feedReaders.newValue)) {
+                feedReaders = changes.feedReaders.newValue;
+                console.log('feedReaders changed to', feedReaders);
+            } else {
+                // list of feed readers was removed or set to nonsensical value
+                feedReaders = [];
+                console.log('feedReaders was removed');
+            }
+            if (this.selectedFeedReader >= feedReaders.length) {
+                // save selected feed reader is no longer valid
+                this.selectedFeedReader = -1;
+            }
+            this.updateFeedReaders(feedReaders);
+            this.selectFeedReader();
         }
-        if (this.selectedFeedReader >= feedReaders.length) {
-            // save selected feed reader is no longer valid
-            this.selectedFeedReader = -1;
+        if (typeof changes.feedPreview !== 'undefined') {
+            let feedPreviewOptions;
+            let newValue = changes.feedPreview.newValue;
+            if (typeof newValue !== 'undefined' &&
+                    newValue === Object(newValue)) {
+                feedPreviewOptions = {expandEntries: !!newValue.expandEntries};
+            } else {
+                // feed preview preferences were removed or set to nonsensical
+                // value
+                feedPreviewOptions = {expandEntries: false};
+                console.log('feedPreview was removed');
+            }
+            this.updateFeedPreviewOptions(feedPreviewOptions);
         }
-        this.updateFeedReaders(feedReaders);
-        this.selectFeedReader();
     }
 
     handleEvent(ev) {
@@ -238,6 +277,11 @@
             console.log('set feedReaders to', feedReaders);
 
             document.forms['add-feed-reader'].reset();
+        } else if (ev.type === 'change' && ev.target.id === 'expand-entries') {
+            console.log('expand entries by default:', ev.target.checked);
+            browser.storage.sync.set({
+                feedPreview: {expandEntries: ev.target.checked}
+            });
         }
     }
 }