keeweb/app/scripts/views/open-chal-resp-view.js

83 lines
2.2 KiB
JavaScript
Raw Normal View History

import { Events } from 'framework/events';
2020-05-24 20:02:49 +02:00
import { View } from 'framework/views/view';
import { YubiKey } from 'comp/app/yubikey';
import { Features } from 'util/features';
2020-05-24 20:02:49 +02:00
import { Locale } from 'util/locale';
import { Timeouts } from 'const/timeouts';
2020-05-24 20:02:49 +02:00
import template from 'templates/open-chal-resp.hbs';
class OpenChalRespView extends View {
template = template;
events = {
'click .open-chal-resp__item': 'itemClick'
};
constructor() {
super();
this.listenTo(Events, 'usb-devices-changed', this.usbDevicesChanged);
this.checkDevices();
}
render() {
let error = this.error;
const isEmpty = this.yubiKeys && !this.yubiKeys.length;
if (isEmpty) {
error = Locale.openChalRespErrorEmpty;
}
super.render({
error,
showEmptyMacWarning: isEmpty && Features.isMac,
yubiKeys: this.yubiKeys,
loading: !this.yubiKeys && !this.error
});
}
usbDevicesChanged() {
setTimeout(() => {
if (!this.removed) {
this.checkDevices();
}
}, Timeouts.ExternalDeviceAfterReconnect);
}
checkDevices() {
2020-05-29 23:15:10 +02:00
YubiKey.list((err, yubiKeys) => {
if (this.removed) {
return;
}
2020-05-29 23:15:10 +02:00
this.error = err;
this.yubiKeys = [];
if (yubiKeys) {
2020-05-30 08:10:19 +02:00
for (const { fullName, vid, pid, serial, slot1, slot2 } of yubiKeys) {
2020-06-01 16:53:51 +02:00
for (const slot of [slot1 ? 1 : 0, slot2 ? 2 : 0].filter((s) => s)) {
2020-05-29 23:15:10 +02:00
this.yubiKeys.push({
fullName,
2020-05-30 08:10:19 +02:00
vid,
pid,
2020-05-29 23:15:10 +02:00
serial,
slot
});
2020-05-24 20:02:49 +02:00
}
2020-05-29 23:15:10 +02:00
}
2020-05-24 20:02:49 +02:00
}
2020-05-29 23:15:10 +02:00
this.render();
2020-05-24 20:02:49 +02:00
});
}
itemClick(e) {
const el = e.target.closest('[data-serial]');
2020-05-30 08:10:19 +02:00
const vid = +el.dataset.vid;
const pid = +el.dataset.pid;
const serial = +el.dataset.serial;
const slot = +el.dataset.slot;
this.emit('select', { vid, pid, serial, slot });
2020-05-24 20:02:49 +02:00
}
}
export { OpenChalRespView };