Make some of the options dynamic

This change modifies the logic of several features so they aren't
enabled/disabled depending on the options state when the page is loaded
but dynamically.

So, for instance, when the thread list avatars feature is switched from
enabled to disabled, when browsing the Community Console, newly loaded
thread lists won't have the avatars, without having to reload the whole
Community Console.

This will make "kill switches" more effective, since they will be able
to take effect without having to reload the Community Console page.

The options which still haven't been made dynamic are features which add
CSS tweaks to the Community Console. For those features (like the dark
mode) a future CL will make them dynamic.

Bug: twpowertools:61
Change-Id: I72b511dd3b2622a2e9c633850e29806953e4b284
diff --git a/src/contentScripts/communityConsole/avatars.js b/src/contentScripts/communityConsole/avatars.js
index 437e68c..b125a17 100644
--- a/src/contentScripts/communityConsole/avatars.js
+++ b/src/contentScripts/communityConsole/avatars.js
@@ -2,6 +2,7 @@
 
 import {CCApi} from '../../common/api.js';
 import {parseUrl} from '../../common/commonUtils.js';
+import {isOptionEnabled} from '../../common/optionsUtils.js';
 
 import AvatarsDB from './utils/AvatarsDB.js'
 
@@ -10,6 +11,14 @@
     this.isFilterSetUp = false;
     this.privateForums = [];
     this.db = new AvatarsDB();
+
+    // Preload whether the option is enabled or not. This is because in the case
+    // avatars should be injected, if we don't preload this the layout will
+    // shift when injecting the first avatar.
+    isOptionEnabled('threadlistavatars').then(isEnabled => {
+      if (isEnabled)
+        document.body.classList.add('TWPT-threadlistavatars-enabled');
+    });
   }
 
   // Gets a list of private forums. If it is already cached, the cached list is
@@ -338,4 +347,17 @@
               thread, err);
         });
   }
+
+  // Inject avatars for thread summary (thread item) |node| in a thread list if
+  // the threadlistavatars option is enabled.
+  injectIfEnabled(node) {
+    isOptionEnabled('threadlistavatars').then(isEnabled => {
+      if (isEnabled) {
+        document.body.classList.add('TWPT-threadlistavatars-enabled');
+        this.inject(node);
+      } else {
+        document.body.classList.remove('TWPT-threadlistavatars-enabled');
+      }
+    });
+  }
 };