bitwarden-estensione-browser/src/app/settings/domain-rules.component.ts

86 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-06-26 05:16:59 +02:00
import {
Component,
OnInit,
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { ApiService } from 'jslib/abstractions/api.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-06-26 17:50:23 +02:00
import { UpdateDomainsRequest } from 'jslib/models/request/updateDomainsRequest';
2018-06-26 05:16:59 +02:00
@Component({
selector: 'app-domain-rules',
templateUrl: 'domain-rules.component.html',
})
export class DomainRulesComponent implements OnInit {
loading = true;
custom: string[] = [];
2018-06-26 17:50:23 +02:00
global: any[] = [];
2018-06-26 05:16:59 +02:00
formPromise: Promise<any>;
constructor(private apiService: ApiService, private i18nService: I18nService,
2018-06-26 17:50:23 +02:00
private analytics: Angulartics2, private toasterService: ToasterService) { }
2018-06-26 05:16:59 +02:00
async ngOnInit() {
const response = await this.apiService.getSettingsDomains();
this.loading = false;
2018-06-26 17:50:23 +02:00
if (response.equivalentDomains != null) {
this.custom = response.equivalentDomains.map(d => d.join(', '));
2018-06-26 17:50:23 +02:00
}
if (response.globalEquivalentDomains != null) {
this.global = response.globalEquivalentDomains.map(d => {
2018-06-26 17:50:23 +02:00
return {
domains: d.domains.join(', '),
excluded: d.excluded,
key: d.type,
};
});
}
}
toggleExcluded(globalDomain: any) {
globalDomain.excluded = !globalDomain.excluded;
}
customize(globalDomain: any) {
globalDomain.excluded = true;
this.custom.push(globalDomain.domains);
}
remove(index: number) {
this.custom.splice(index, 1);
}
add() {
this.custom.push('');
2018-06-26 05:16:59 +02:00
}
async submit() {
2018-06-26 17:50:23 +02:00
const request = new UpdateDomainsRequest();
request.excludedGlobalEquivalentDomains = this.global.filter(d => d.excluded)
.map(d => d.key);
2018-06-26 17:50:23 +02:00
if (request.excludedGlobalEquivalentDomains.length === 0) {
request.excludedGlobalEquivalentDomains = null;
}
request.equivalentDomains = this.custom.filter(d => d != null && d.trim() !== '')
.map(d => d.split(',').map(d2 => d2.trim()));
2018-06-26 17:50:23 +02:00
if (request.equivalentDomains.length === 0) {
request.equivalentDomains = null;
}
try {
this.formPromise = this.apiService.putSettingsDomains(request);
await this.formPromise;
this.analytics.eventTrack.next({ action: 'Saved Equivalent Domains' });
this.toasterService.popAsync('success', null, this.i18nService.t('domainsUpdated'));
} catch { }
}
2018-06-26 05:16:59 +02:00
2018-06-26 17:50:23 +02:00
indexTrackBy(index: number, obj: any): any {
return index;
2018-06-26 05:16:59 +02:00
}
}