SUPPORT-9133: перенос доработок из ervu-dashboard

This commit is contained in:
Рауф Латыпов 2025-04-24 23:45:17 +03:00
parent 8aa05afa48
commit fed8b9c27f
2 changed files with 140 additions and 14 deletions

View file

@ -0,0 +1,5 @@
export enum TreeValuesCacheStrategy {
BY_PAGE_OBJECT_ID = "BY_PAGE_OBJECT_ID",
BY_OBJECT_NAME = "BY_OBJECT_NAME",
BY_CUSTOM_NAME = "BY_CUSTOM_NAME",
}

View file

@ -2,19 +2,29 @@ import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef
ElementRef,
Input
} from "@angular/core";
import {
AdvancedProperty,
Event,
InputControl,
UserService,
Visible
LocalStorageService,
NotNull,
PageContextHolder,
PageObjectUtils,
TaskParamsProvider,
Visible,
WebbpmStorage
} from "@webbpm/base-package";
import {TreeItemDto} from "../../generated/component/model/TreeItemDto";
import {TreeItemRpcService} from "../../generated/component/rpc/TreeItemRpcService";
import {DropdownTreeviewSelectI18n} from "../external/ngx-treeview/dropdown-treeview-select/dropdown-treeview-select-i18n";
import {
DropdownTreeviewSelectI18n
} from "../external/ngx-treeview/dropdown-treeview-select/dropdown-treeview-select-i18n";
import {TreeItem, TreeviewItem} from "ngx-treeview";
import {TreeValuesCacheStrategy} from "../enum/TreeValuesCacheStrategy";
@Component({
moduleId: module.id,
@ -26,44 +36,118 @@ import {TreeItem, TreeviewItem} from "ngx-treeview";
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DropdownTreeViewComponent extends InputControl {
@Input()
@AdvancedProperty()
public treeValuesCacheStrategy: TreeValuesCacheStrategy;
@Visible("treeValuesCacheStrategy == TreeValuesCacheStrategy.BY_CUSTOM_NAME")
@NotNull("treeValuesCacheStrategy == TreeValuesCacheStrategy.BY_CUSTOM_NAME")
@AdvancedProperty()
public treeValuesCustomName: string;
@Visible("false")
public cachedValue: TreeItemDto;
public collapseLevel: number;
public maxHeight: number;
@Visible("false")
public items: TreeviewItem[];
@Visible("false")
public value: any;
public value: TreeItemDto;
@Visible("false")
public valueChangeEvent: Event<TreeItemDto> = new Event<TreeItemDto>();
private rpcService: TreeItemRpcService;
constructor(el: ElementRef, cd: ChangeDetectorRef,
private i18n: DropdownTreeviewSelectI18n) {
private localStorageService: LocalStorageService;
private taskParamsProvider: TaskParamsProvider;
private pageContextHolder: PageContextHolder;
private webbpmStorage: WebbpmStorage;
private storageKey: string;
private rootValues: TreeItemDto[];
constructor(el: ElementRef, cd: ChangeDetectorRef, private i18n: DropdownTreeviewSelectI18n) {
super(el, cd);
}
public initialize() {
super.initialize();
this.rpcService = this.getScript(TreeItemRpcService);
this.taskParamsProvider = this.injector.get(TaskParamsProvider);
this.localStorageService = this.injector.get(LocalStorageService);
this.pageContextHolder = this.injector.get(PageContextHolder);
this.webbpmStorage =
this.getTreeValuesStorage(this.treeValuesCacheStrategy, this.treeValuesCustomName);
this.cachedValue = this.getCachedValue();
this.loadTreeItems();
}
private getTreeValuesStorage(treeValuesCacheStrategy: TreeValuesCacheStrategy,
customKeyName: string) {
if (!treeValuesCacheStrategy) {
return null;
}
switch (treeValuesCacheStrategy) {
case TreeValuesCacheStrategy.BY_PAGE_OBJECT_ID:
this.storageKey = PageObjectUtils.getPageObjectKey(this);
return this.readWebbpmStorage(this.storageKey);
case TreeValuesCacheStrategy.BY_OBJECT_NAME:
this.storageKey = this.getObjectName();
return this.readWebbpmStorage(this.storageKey);
case TreeValuesCacheStrategy.BY_CUSTOM_NAME:
this.storageKey = customKeyName;
return this.readWebbpmStorage(this.storageKey);
default:
throw new Error("Unknown tree values storage type = " + treeValuesCacheStrategy)
}
}
readWebbpmStorage(treeStorageKey: string) {
if (this.pageContextHolder.isPageInBpmnContext()) {
treeStorageKey = treeStorageKey + "$" + this.taskParamsProvider.processInstanceId
}
return this.localStorageService.readTemporalWebbpmStorage(treeStorageKey);
}
@Visible()
public loadTreeItems(): void {
this.rpcService.loadTreeData()
.then((res: TreeItemDto[]) => {
this.items = res.map(value => new TreeviewItem(this.createTreeItem(value)));
this.rootValues = res;
const rootItem = this.items[0];
this.i18n.selectedItem = rootItem;
this.value = rootItem ? rootItem.value : rootItem;
if (this.cachedValue) {
const matchedItem = this.findTreeItemByValue(this.items, this.cachedValue);
if (matchedItem) {
this.i18n.selectedItem = matchedItem;
this.value = matchedItem.value;
}
}
else {
this.i18n.selectedItem = rootItem;
this.value = rootItem.value;
}
this.doCollapseLevel();
this.onValueChange(this.value);
this.cd.markForCheck();
});
}
private findTreeItemByValue(rootItems: TreeviewItem[], valueToFind: any): TreeviewItem | null {
for (const item of rootItems) {
if (JSON.stringify(item.value) === JSON.stringify(valueToFind)) {
return item;
}
if (item.children) {
const found = this.findTreeItemByValue(item.children, valueToFind);
if (found) {
return found;
}
}
}
return null;
}
private createTreeItem(treeItemDto: TreeItemDto): TreeItem {
let treeItem: TreeItem;
if (treeItemDto) {
@ -84,6 +168,7 @@ export class DropdownTreeViewComponent extends InputControl {
}
public onValueChange($event: any) {
this.setCachedValue(this.value);
this.valueChangeEvent.trigger($event);
this.applyListener(this.changeListeners);
}
@ -113,20 +198,51 @@ export class DropdownTreeViewComponent extends InputControl {
}
}
protected setCachedValue(newValue: TreeItemDto): void {
if (this.webbpmStorage) {
this.webbpmStorage.put(this.storageKey, newValue);
}
}
protected getCachedValue(): TreeItemDto {
if (this.webbpmStorage) {
return this.webbpmStorage.get(this.storageKey);
}
return null;
}
getPresentationValue(): string | number | boolean {
return this.value;
return this.value ? this.value.label : '';
}
getValue(): any {
return this.value;
return this.value ? this.value.id : this.value;
}
getValueAsModel(): any {
getValueAsModel(): TreeItemDto {
return this.value ? this.value : null;
}
setValue(value: any): any {
this.value = value;
setValue(value: any): void {
const foundValue: TreeItemDto = this.findValueInRootsById(value);
if (foundValue) {
this.value = foundValue;
this.onValueChange(this.value);
}
}
private findValueInRootsById(id: any): TreeItemDto {
let searchArray: TreeItemDto[] = this.rootValues.slice();
while (searchArray.length > 0) {
const current = searchArray.shift();
if (current.id == id) {
return current;
}
if (current.children && current.children.length > 0) {
searchArray.push(...current.children);
}
}
return undefined;
}
onChange() {
@ -134,6 +250,11 @@ export class DropdownTreeViewComponent extends InputControl {
this.valueChangeEvent.trigger(this.value);
}
addChangeListener(onChangeFunction: Function): void {
super.addChangeListener(onChangeFunction);
onChangeFunction();
}
subscribeToModelChange() {
//empty because there is no ngModel here
}