mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 19:23:52 +00:00
sweet alert dialog implementation
This commit is contained in:
@@ -1,18 +1,12 @@
|
||||
import { BrowserApi } from '../browser/browserApi';
|
||||
|
||||
import {
|
||||
MessagingService,
|
||||
PlatformUtilsService,
|
||||
} from 'jslib/abstractions';
|
||||
import { MessagingService } from 'jslib/abstractions';
|
||||
|
||||
export default class BrowserMessagingService implements MessagingService {
|
||||
constructor(private platformUtilsService: PlatformUtilsService) {
|
||||
}
|
||||
|
||||
send(subscriber: string, arg: any = {}) {
|
||||
const message = Object.assign({}, { command: subscriber }, arg);
|
||||
|
||||
if (this.platformUtilsService.isSafari()) {
|
||||
if (BrowserApi.isSafariApi) {
|
||||
const bgPage = BrowserApi.getBackgroundPage();
|
||||
bgPage.bitwardenMain.sendInternalRuntimeMessage(message);
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ describe('Browser Utils Service', () => {
|
||||
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
|
||||
});
|
||||
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
|
||||
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Chrome);
|
||||
});
|
||||
|
||||
@@ -58,7 +58,7 @@ describe('Browser Utils Service', () => {
|
||||
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
|
||||
});
|
||||
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
|
||||
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Firefox);
|
||||
});
|
||||
|
||||
@@ -68,7 +68,7 @@ describe('Browser Utils Service', () => {
|
||||
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3175.3 Safari/537.36 OPR/49.0.2695.0 (Edition developer)'
|
||||
});
|
||||
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
|
||||
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Opera);
|
||||
});
|
||||
|
||||
@@ -78,7 +78,7 @@ describe('Browser Utils Service', () => {
|
||||
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'
|
||||
});
|
||||
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
|
||||
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Edge);
|
||||
});
|
||||
|
||||
@@ -88,7 +88,7 @@ describe('Browser Utils Service', () => {
|
||||
value: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8'
|
||||
});
|
||||
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
|
||||
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Safari);
|
||||
});
|
||||
|
||||
@@ -98,7 +98,7 @@ describe('Browser Utils Service', () => {
|
||||
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.97 Safari/537.36 Vivaldi/1.94.1008.40'
|
||||
});
|
||||
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
|
||||
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
|
||||
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Vivaldi);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,11 +2,12 @@ import * as tldjs from 'tldjs';
|
||||
|
||||
import { BrowserApi } from '../browser/browserApi';
|
||||
|
||||
import { DeviceType } from 'jslib/enums';
|
||||
import { DeviceType } from 'jslib/enums/deviceType';
|
||||
|
||||
import { PlatformUtilsService } from 'jslib/abstractions';
|
||||
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
||||
import { MessagingService } from 'jslib/abstractions/messaging.service';
|
||||
|
||||
import { UtilsService } from 'jslib/services';
|
||||
import { UtilsService } from 'jslib/services/utils.service';
|
||||
|
||||
const AnalyticsIds = {
|
||||
[DeviceType.Chrome]: 'UA-81915606-6',
|
||||
@@ -17,6 +18,8 @@ const AnalyticsIds = {
|
||||
[DeviceType.Safari]: 'UA-81915606-16',
|
||||
};
|
||||
|
||||
const DialogPromiseExpiration = 3600000; // 1 hour
|
||||
|
||||
export default class BrowserPlatformUtilsService implements PlatformUtilsService {
|
||||
static getDomain(uriString: string): string {
|
||||
if (uriString == null) {
|
||||
@@ -57,9 +60,12 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
|
||||
|
||||
identityClientId: string = 'browser';
|
||||
|
||||
private showDialogResolves = new Map<number, { resolve: (value: boolean) => void, date: Date }>();
|
||||
private deviceCache: DeviceType = null;
|
||||
private analyticsIdCache: string = null;
|
||||
|
||||
constructor(private messagingService: MessagingService) { }
|
||||
|
||||
getDevice(): DeviceType {
|
||||
if (this.deviceCache) {
|
||||
return this.deviceCache;
|
||||
@@ -169,8 +175,18 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
|
||||
}
|
||||
|
||||
showDialog(text: string, title?: string, confirmText?: string, cancelText?: string, type?: string) {
|
||||
// TODO
|
||||
return Promise.resolve(true);
|
||||
const dialogId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
||||
this.messagingService.send('showDialog', {
|
||||
text: text,
|
||||
title: title,
|
||||
confirmText: confirmText,
|
||||
cancelText: cancelText,
|
||||
type: type,
|
||||
dialogId: dialogId,
|
||||
});
|
||||
return new Promise<boolean>((resolve) => {
|
||||
this.showDialogResolves.set(dialogId, { resolve: resolve, date: new Date() });
|
||||
});
|
||||
}
|
||||
|
||||
isDev(): boolean {
|
||||
@@ -183,6 +199,26 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
|
||||
UtilsService.copyToClipboard(text, doc);
|
||||
}
|
||||
|
||||
resolveDialogPromise(dialogId: number, confirmed: boolean) {
|
||||
if (this.showDialogResolves.has(dialogId)) {
|
||||
const resolveObj = this.showDialogResolves.get(dialogId);
|
||||
resolveObj.resolve(confirmed);
|
||||
this.showDialogResolves.delete(dialogId);
|
||||
}
|
||||
|
||||
// Clean up old promises
|
||||
const deleteIds: number[] = [];
|
||||
this.showDialogResolves.forEach((val, key) => {
|
||||
const age = new Date().getTime() - val.date.getTime();
|
||||
if (age > DialogPromiseExpiration) {
|
||||
deleteIds.push(key);
|
||||
}
|
||||
});
|
||||
deleteIds.forEach((id) => {
|
||||
this.showDialogResolves.delete(id);
|
||||
});
|
||||
}
|
||||
|
||||
private sidebarViewName(): string {
|
||||
if ((window as any).chrome.sidebarAction && this.isFirefox()) {
|
||||
return 'sidebar';
|
||||
|
||||
Reference in New Issue
Block a user