[tor-commits] [snowflake-webext/master] Refactor popup on/off to be shared between webext and badge

arlo at torproject.org arlo at torproject.org
Thu Apr 30 19:59:30 UTC 2020


commit dc9d4fab26e788f6a4b5bd4f3cf40f2a31b3a78a
Author: Arlo Breault <arlolra at gmail.com>
Date:   Wed Apr 15 14:21:48 2020 -0400

    Refactor popup on/off to be shared between webext and badge
---
 init-badge.js   | 23 +++++------------------
 static/popup.js | 25 ++++++++++++++++++++++++-
 webext/embed.js | 22 ++++------------------
 3 files changed, 33 insertions(+), 37 deletions(-)

diff --git a/init-badge.js b/init-badge.js
index e676323..4beceb1 100644
--- a/init-badge.js
+++ b/init-badge.js
@@ -22,43 +22,30 @@ class BadgeUI extends UI {
 
   constructor() {
     super();
-    this.popup = new Popup();
+    this.popup = new Popup((...args) => messages.getMessage(...args));
   }
 
   setStatus() {}
 
   missingFeature(missing) {
-    this.popup.setEnabled(false);
-    this.popup.setActive(false);
-    this.popup.setStatusText(messages.getMessage('popupStatusOff'));
     this.setIcon('off');
-    this.popup.setStatusDesc(messages.getMessage(missing), true);
-    this.popup.hideButton();
+    this.popup.missingFeature(missing);
   }
 
   turnOn() {
     const clients = this.active ? 1 : 0;
-    this.popup.setChecked(true);
     if (clients > 0) {
-      this.popup.setStatusText(messages.getMessage('popupStatusOn', String(clients)));
       this.setIcon('running');
     } else {
-      this.popup.setStatusText(messages.getMessage('popupStatusReady'));
       this.setIcon('on');
     }
-    // FIXME: Share stats from webext
-    this.popup.setStatusDesc('');
-    this.popup.setEnabled(true);
-    this.popup.setActive(this.active);
+    const total = 0;  // FIXME: Share stats from webext
+    this.popup.turnOn(clients, total);
   }
 
   turnOff() {
-    this.popup.setChecked(false);
-    this.popup.setStatusText(messages.getMessage('popupStatusOff'));
     this.setIcon('off');
-    this.popup.setStatusDesc('');
-    this.popup.setEnabled(false);
-    this.popup.setActive(false);
+    this.popup.turnOff();
   }
 
   setActive(connected) {
diff --git a/static/popup.js b/static/popup.js
index 80cbcc6..5223350 100644
--- a/static/popup.js
+++ b/static/popup.js
@@ -10,7 +10,8 @@ function setClass(elem, className, cond) {
 }
 
 class Popup {
-  constructor() {
+  constructor(getMsgFunc) {
+    this.getMsgFunc = getMsgFunc;
     this.div = document.getElementById('active');
     this.statustext = document.getElementById('statustext');
     this.statusdesc = document.getElementById('statusdesc');
@@ -47,4 +48,26 @@ class Popup {
         break;
     }
   }
+  turnOn(clients, total) {
+    this.setChecked(true);
+    if (clients > 0) {
+      this.setStatusText(this.getMsgFunc('popupStatusOn', String(clients)));
+    } else {
+      this.setStatusText(this.getMsgFunc('popupStatusReady'));
+    }
+    this.setStatusDesc((total > 0) ? this.getMsgFunc('popupDescOn', String(total)) : '');
+    this.setEnabled(true);
+    this.setActive(this.active);
+  }
+  turnOff(desc, error) {
+    this.setChecked(false);
+    this.setStatusText(this.getMsgFunc('popupStatusOff'));
+    this.setStatusDesc(desc ? this.getMsgFunc(desc) : '', error);
+    this.setEnabled(false);
+    this.setActive(false);
+  }
+  missingFeature(desc) {
+    this.turnOff(desc, true);
+    this.hideButton();
+  }
 }
diff --git a/webext/embed.js b/webext/embed.js
index eae482f..7371005 100644
--- a/webext/embed.js
+++ b/webext/embed.js
@@ -13,34 +13,20 @@ const port = chrome.runtime.connect({
 
 port.onMessage.addListener((m) => {
   const { active, enabled, total, missingFeature } = m;
-  const popup = new Popup();
+  const popup = new Popup((...args) => chrome.i18n.getMessage(...args));
 
   if (missingFeature) {
-    popup.setEnabled(false);
-    popup.setActive(false);
-    popup.setStatusText(chrome.i18n.getMessage('popupStatusOff'));
-    popup.setStatusDesc(chrome.i18n.getMessage(missingFeature), true);
-    popup.hideButton();
+    popup.missingFeature(missingFeature);
     return;
   }
 
   const clients = active ? 1 : 0;
 
   if (enabled) {
-    popup.setChecked(true);
-    if (clients > 0) {
-      popup.setStatusText(chrome.i18n.getMessage('popupStatusOn', String(clients)));
-    } else {
-      popup.setStatusText(chrome.i18n.getMessage('popupStatusReady'));
-    }
-    popup.setStatusDesc((total > 0) ? chrome.i18n.getMessage('popupDescOn', String(total)) : '');
+    popup.turnOn(clients, total);
   } else {
-    popup.setChecked(false);
-    popup.setStatusText(chrome.i18n.getMessage('popupStatusOff'));
-    popup.setStatusDesc("");
+    popup.turnOff();
   }
-  popup.setEnabled(enabled);
-  popup.setActive(active);
 });
 
 document.addEventListener('change', (event) => {





More information about the tor-commits mailing list