GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Commit dc9d4fab authored by Arlo Breault's avatar Arlo Breault

Refactor popup on/off to be shared between webext and badge

parent 11f3d871
...@@ -22,43 +22,30 @@ class BadgeUI extends UI { ...@@ -22,43 +22,30 @@ class BadgeUI extends UI {
constructor() { constructor() {
super(); super();
this.popup = new Popup(); this.popup = new Popup((...args) => messages.getMessage(...args));
} }
setStatus() {} setStatus() {}
missingFeature(missing) { missingFeature(missing) {
this.popup.setEnabled(false);
this.popup.setActive(false);
this.popup.setStatusText(messages.getMessage('popupStatusOff'));
this.setIcon('off'); this.setIcon('off');
this.popup.setStatusDesc(messages.getMessage(missing), true); this.popup.missingFeature(missing);
this.popup.hideButton();
} }
turnOn() { turnOn() {
const clients = this.active ? 1 : 0; const clients = this.active ? 1 : 0;
this.popup.setChecked(true);
if (clients > 0) { if (clients > 0) {
this.popup.setStatusText(messages.getMessage('popupStatusOn', String(clients)));
this.setIcon('running'); this.setIcon('running');
} else { } else {
this.popup.setStatusText(messages.getMessage('popupStatusReady'));
this.setIcon('on'); this.setIcon('on');
} }
// FIXME: Share stats from webext const total = 0; // FIXME: Share stats from webext
this.popup.setStatusDesc(''); this.popup.turnOn(clients, total);
this.popup.setEnabled(true);
this.popup.setActive(this.active);
} }
turnOff() { turnOff() {
this.popup.setChecked(false);
this.popup.setStatusText(messages.getMessage('popupStatusOff'));
this.setIcon('off'); this.setIcon('off');
this.popup.setStatusDesc(''); this.popup.turnOff();
this.popup.setEnabled(false);
this.popup.setActive(false);
} }
setActive(connected) { setActive(connected) {
......
...@@ -10,7 +10,8 @@ function setClass(elem, className, cond) { ...@@ -10,7 +10,8 @@ function setClass(elem, className, cond) {
} }
class Popup { class Popup {
constructor() { constructor(getMsgFunc) {
this.getMsgFunc = getMsgFunc;
this.div = document.getElementById('active'); this.div = document.getElementById('active');
this.statustext = document.getElementById('statustext'); this.statustext = document.getElementById('statustext');
this.statusdesc = document.getElementById('statusdesc'); this.statusdesc = document.getElementById('statusdesc');
...@@ -47,4 +48,26 @@ class Popup { ...@@ -47,4 +48,26 @@ class Popup {
break; 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();
}
} }
...@@ -13,34 +13,20 @@ const port = chrome.runtime.connect({ ...@@ -13,34 +13,20 @@ const port = chrome.runtime.connect({
port.onMessage.addListener((m) => { port.onMessage.addListener((m) => {
const { active, enabled, total, missingFeature } = m; const { active, enabled, total, missingFeature } = m;
const popup = new Popup(); const popup = new Popup((...args) => chrome.i18n.getMessage(...args));
if (missingFeature) { if (missingFeature) {
popup.setEnabled(false); popup.missingFeature(missingFeature);
popup.setActive(false);
popup.setStatusText(chrome.i18n.getMessage('popupStatusOff'));
popup.setStatusDesc(chrome.i18n.getMessage(missingFeature), true);
popup.hideButton();
return; return;
} }
const clients = active ? 1 : 0; const clients = active ? 1 : 0;
if (enabled) { if (enabled) {
popup.setChecked(true); popup.turnOn(clients, total);
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)) : '');
} else { } else {
popup.setChecked(false); popup.turnOff();
popup.setStatusText(chrome.i18n.getMessage('popupStatusOff'));
popup.setStatusDesc("");
} }
popup.setEnabled(enabled);
popup.setActive(active);
}); });
document.addEventListener('change', (event) => { document.addEventListener('change', (event) => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment