Created
June 22, 2024 01:10
-
-
Save JenHsuan/3a3d31904f98563531fb0635d871ffa6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ChangeDetectorRef, Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'; | |
import { Observable, Subject, debounceTime, distinctUntilChanged, filter, map, switchMap, takeUntil, tap } from 'rxjs'; | |
import * as _ from 'lodash'; | |
type ValueType = TopologyController; | |
@Component({ | |
selector: 'app-topology-controlbar-assistant', | |
templateUrl: './topology-controlbar-assistant.component.html', | |
styleUrls: ['./topology-controlbar-assistant.component.scss'] | |
}) | |
export class TopologyControlbarAssistantComponent implements OnInit, OnDestroy, TopologyCommandInvoker { | |
/** | |
* Options displayed in ng-select | |
*/ | |
options: TopologyController[]; | |
selection: ValueType; | |
/** | |
* Subject for search input changes | |
*/ | |
searchInput = new Subject<string>(); | |
selectionFinalizedSubject = new Subject<void>(); | |
protected destroyed = new Subject<void>(); | |
configurationType = TopologyContollerType.ASSISTANT | |
makeCommand: (statusType: TopologyStatusType) => TopologyCommand; | |
commandStack: TopologyCommand[]; | |
constructor( | |
protected service: TopologyControlbarAssistantService, | |
private changeDetector: ChangeDetectorRef, | |
) { } | |
ngOnInit(): void { | |
this.initSelectOptions(); | |
this.registerSelectionFinalized(); | |
} | |
ngOnDestroy(): void { | |
this.destroyed.next(); | |
} | |
protected initSelectOptions() { | |
this.loadOptions(); | |
} | |
private loadOptions() { | |
this.searchInput.pipe( | |
filter(query => !_.isNil(query) || !_.isEmpty(query) || this.emptySearchReturnsData), | |
distinctUntilChanged(), | |
debounceTime(TOPOLOGY_ASSISTANT_DELAY), | |
switchMap(query => this.service.list() | |
), | |
tap(() => { | |
this.loading = false; | |
this.changeDetector.detectChanges(); | |
}), | |
takeUntil(this.destroyed), | |
).subscribe( | |
(options: TopologyController[]) => { | |
this.options = options; | |
this.changeDetector.detectChanges(); | |
} | |
); | |
} | |
private registerSelectionFinalized() { | |
this.selectionFinalizedSubject.pipe( | |
takeUntil(this.destroyed), | |
map(() => this.selection), | |
filter(selection => !!selection) | |
).subscribe( | |
(selection: ValueType) => { | |
if (!_.isNil(this.makeCommand)) { | |
let statusType = !_.isNil(selection) ? selection.statusType as TopologyAssistantType : TopologyAssistantType.NONE; | |
const command = this.makeCommand(statusType) | |
command.execute(); | |
this.registerCommand(command); | |
} | |
} | |
); | |
} | |
finalizeSelection() { | |
this.selectionFinalizedSubject.next(); | |
} | |
setCommandStack(commandStack: TopologyCommand[]) { | |
this.commandStack = commandStack; | |
} | |
setMakeCommand(makeCommand: (statusType: TopologyStatusType) => TopologyCommand) { | |
this.makeCommand = makeCommand; | |
} | |
registerCommand(command: TopologyCommand) { | |
if (!_.isNil(this.commandStack)) { | |
this.commandStack.unshift(command); | |
} | |
} | |
removeLatestCommand() { | |
if (!_.isNil(this.commandStack)) { | |
this.commandStack.shift(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment