2021-02-10 17:16:34 +01:00
|
|
|
import {
|
2021-02-12 23:28:31 +01:00
|
|
|
ChangeDetectorRef,
|
2021-02-10 17:16:34 +01:00
|
|
|
Component,
|
|
|
|
NgZone,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Router,
|
|
|
|
} from '@angular/router';
|
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { SendView } from 'jslib-common/models/view/sendView';
|
2021-02-10 17:16:34 +01:00
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { SendComponent as BaseSendComponent } from 'jslib-angular/components/send/send.component';
|
2021-02-10 17:16:34 +01:00
|
|
|
|
2021-12-06 12:21:07 +01:00
|
|
|
import { BroadcasterService } from 'jslib-common/abstractions/broadcaster.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-21 11:10:46 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
|
|
|
import { PolicyService } from 'jslib-common/abstractions/policy.service';
|
|
|
|
import { SearchService } from 'jslib-common/abstractions/search.service';
|
|
|
|
import { SendService } from 'jslib-common/abstractions/send.service';
|
|
|
|
import { StateService } from 'jslib-common/abstractions/state.service';
|
|
|
|
import { SyncService } from 'jslib-common/abstractions/sync.service';
|
|
|
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
2021-02-10 17:16:34 +01:00
|
|
|
|
|
|
|
import { PopupUtilsService } from '../services/popup-utils.service';
|
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { SendType } from 'jslib-common/enums/sendType';
|
2021-02-10 17:16:34 +01:00
|
|
|
|
|
|
|
const ComponentId = 'SendComponent';
|
|
|
|
const ScopeStateId = ComponentId + 'Scope';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-send-groupings',
|
|
|
|
templateUrl: 'send-groupings.component.html',
|
|
|
|
})
|
|
|
|
export class SendGroupingsComponent extends BaseSendComponent {
|
|
|
|
// Header
|
|
|
|
showLeftHeader = true;
|
|
|
|
// Send Type Calculations
|
|
|
|
typeCounts = new Map<SendType, number>();
|
|
|
|
// State Handling
|
|
|
|
state: any;
|
|
|
|
scopeState: any;
|
|
|
|
private loadedTimeout: number;
|
|
|
|
|
|
|
|
constructor(sendService: SendService, i18nService: I18nService,
|
2021-02-12 23:28:31 +01:00
|
|
|
platformUtilsService: PlatformUtilsService, environmentService: EnvironmentService, ngZone: NgZone,
|
|
|
|
policyService: PolicyService, userService: UserService, searchService: SearchService,
|
2021-02-10 17:16:34 +01:00
|
|
|
private popupUtils: PopupUtilsService, private stateService: StateService,
|
2021-02-23 22:37:55 +01:00
|
|
|
private router: Router, private syncService: SyncService,
|
2021-10-21 11:10:46 +02:00
|
|
|
private changeDetectorRef: ChangeDetectorRef, private broadcasterService: BroadcasterService,
|
|
|
|
logService: LogService) {
|
2021-02-12 23:28:31 +01:00
|
|
|
super(sendService, i18nService, platformUtilsService, environmentService, ngZone, searchService,
|
2021-10-21 11:10:46 +02:00
|
|
|
policyService, userService, logService);
|
2021-02-10 17:16:34 +01:00
|
|
|
super.onSuccessfulLoad = async () => {
|
|
|
|
this.calculateTypeCounts();
|
2021-02-12 23:28:31 +01:00
|
|
|
this.selectAll();
|
2021-02-10 17:16:34 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
// Determine Header details
|
|
|
|
this.showLeftHeader = !(this.popupUtils.inSidebar(window) && this.platformUtilsService.isFirefox());
|
2021-02-12 23:28:31 +01:00
|
|
|
// Clear state of Send Type Component
|
|
|
|
this.stateService.remove('SendTypeComponent');
|
2021-02-10 17:16:34 +01:00
|
|
|
// Let super class finish
|
|
|
|
await super.ngOnInit();
|
|
|
|
// Handle State Restore if necessary
|
|
|
|
const restoredScopeState = await this.restoreState();
|
|
|
|
this.state = (await this.stateService.get<any>(ComponentId)) || {};
|
|
|
|
if (this.state.searchText != null) {
|
|
|
|
this.searchText = this.state.searchText;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.syncService.syncInProgress) {
|
|
|
|
this.load();
|
|
|
|
} else {
|
|
|
|
this.loadedTimeout = window.setTimeout(() => {
|
|
|
|
if (!this.loaded) {
|
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.syncService.syncInProgress || restoredScopeState) {
|
|
|
|
window.setTimeout(() => this.popupUtils.setContentScrollY(window, this.state.scrollY), 0);
|
|
|
|
}
|
2021-02-12 23:28:31 +01:00
|
|
|
|
|
|
|
// Load all sends if sync completed in background
|
|
|
|
this.broadcasterService.subscribe(ComponentId, (message: any) => {
|
|
|
|
this.ngZone.run(async () => {
|
|
|
|
switch (message.command) {
|
|
|
|
case 'syncCompleted':
|
|
|
|
window.setTimeout(() => {
|
|
|
|
this.load();
|
|
|
|
}, 500);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.changeDetectorRef.detectChanges();
|
|
|
|
});
|
|
|
|
});
|
2021-02-10 17:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
// Remove timeout
|
|
|
|
if (this.loadedTimeout != null) {
|
|
|
|
window.clearTimeout(this.loadedTimeout);
|
|
|
|
}
|
|
|
|
// Save state
|
|
|
|
this.saveState();
|
2021-02-12 23:28:31 +01:00
|
|
|
// Unsubscribe
|
|
|
|
this.broadcasterService.unsubscribe(ComponentId);
|
2021-02-10 17:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async selectType(type: SendType) {
|
2021-02-12 23:28:31 +01:00
|
|
|
this.router.navigate(['/send-type'], { queryParams: { type: type } });
|
2021-02-10 17:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async selectSend(s: SendView) {
|
2021-02-23 22:37:55 +01:00
|
|
|
this.router.navigate(['/edit-send'], { queryParams: { sendId: s.id } });
|
2021-02-10 17:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async addSend() {
|
2021-02-24 23:03:44 +01:00
|
|
|
if (this.disableSend) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-23 22:37:55 +01:00
|
|
|
this.router.navigate(['/add-send']);
|
2021-02-10 17:16:34 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 23:03:44 +01:00
|
|
|
async removePassword(s: SendView): Promise<boolean> {
|
|
|
|
if (this.disableSend) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.removePassword(s);
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:16:34 +01:00
|
|
|
showSearching() {
|
|
|
|
return this.hasSearched || (!this.searchPending && this.searchService.isSearchable(this.searchText));
|
|
|
|
}
|
|
|
|
|
|
|
|
private calculateTypeCounts() {
|
|
|
|
// Create type counts
|
|
|
|
const typeCounts = new Map<SendType, number>();
|
2021-02-12 23:28:31 +01:00
|
|
|
this.sends.forEach(s => {
|
2021-02-10 17:16:34 +01:00
|
|
|
if (typeCounts.has(s.type)) {
|
|
|
|
typeCounts.set(s.type, typeCounts.get(s.type) + 1);
|
|
|
|
} else {
|
|
|
|
typeCounts.set(s.type, 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.typeCounts = typeCounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async saveState() {
|
|
|
|
this.state = {
|
|
|
|
scrollY: this.popupUtils.getContentScrollY(window),
|
|
|
|
searchText: this.searchText,
|
|
|
|
};
|
|
|
|
await this.stateService.save(ComponentId, this.state);
|
|
|
|
|
|
|
|
this.scopeState = {
|
|
|
|
sends: this.sends,
|
|
|
|
typeCounts: this.typeCounts,
|
|
|
|
};
|
|
|
|
await this.stateService.save(ScopeStateId, this.scopeState);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async restoreState(): Promise<boolean> {
|
|
|
|
this.scopeState = await this.stateService.get<any>(ScopeStateId);
|
|
|
|
if (this.scopeState == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.scopeState.sends != null) {
|
|
|
|
this.sends = this.scopeState.sends;
|
|
|
|
}
|
|
|
|
if (this.scopeState.typeCounts != null) {
|
|
|
|
this.typeCounts = this.scopeState.typeCounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|