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
Browse files

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

parent 11f3d871
......@@ -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) {
......
......@@ -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();
}
}
......@@ -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) => {
......
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