310 lines
7.7 KiB
TypeScript
310 lines
7.7 KiB
TypeScript
import { RedPointInfo } from "./RedPointInfo";
|
|
import { IRedPointData } from "./IRedPointData";
|
|
import { RedCondEvent } from "./RedPointCoust";
|
|
|
|
export class RedPointTree {
|
|
|
|
/**
|
|
* 数据展示
|
|
*/
|
|
public info: RedPointInfo = null;
|
|
/**
|
|
* 子节点
|
|
*/
|
|
public children: Map<string, RedPointTree> = new Map<string, RedPointTree>();
|
|
// /**
|
|
// * 是否绑定节点
|
|
// */
|
|
// public isBind: boolean = false;
|
|
/**
|
|
* 展示类型
|
|
*/
|
|
private showType: number = 0;
|
|
|
|
// public showCount: number = 0;
|
|
/**
|
|
* id唯一值
|
|
*/
|
|
public id: string;
|
|
|
|
|
|
// public isBind: boolean = false;
|
|
public data: IRedPointData = null;
|
|
|
|
//自身是否有逻辑控制红点
|
|
public isSelfControl: boolean = false;
|
|
|
|
//是否要显示自身红点(不依赖子红点)
|
|
public isSelfShowRed: boolean = false;
|
|
|
|
public mCondition: Array<IRedTreeCond> = [];
|
|
|
|
public callFunc: Function = null;
|
|
|
|
/**
|
|
*红点更新目标
|
|
*/
|
|
public stepTag: number = 0;
|
|
|
|
/**
|
|
* 红点树初始化
|
|
* @param info
|
|
* @param showType
|
|
* @param key
|
|
* @param isBind
|
|
*/
|
|
initData(key: string, data: IRedPointData) {
|
|
this.id = key;
|
|
this.data = data;
|
|
}
|
|
|
|
public getChildren(key: string): RedPointTree {
|
|
let children = this.children.get(key);
|
|
if (children != null) {
|
|
return children;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 绑定脚本
|
|
* @param info
|
|
*/
|
|
public bindInfo(info: RedPointInfo, showType, isSelfControl: boolean = false) {
|
|
this.info = info;
|
|
this.showType = showType;
|
|
this.isSelfControl = isSelfControl;
|
|
console.log("updateRedPoint 绑定脚本" + this.data.mId);
|
|
this.updateRedPoint(this.data.mShowType >= 0 ? true : false, this.data.mShowCount);
|
|
}
|
|
|
|
/**
|
|
* 查找Key值对应的对象
|
|
* @param _key
|
|
* @returns
|
|
*/
|
|
public findChild(_key: string) {
|
|
for (let [key, item] of this.children.entries()) {
|
|
if (item.id == _key) {
|
|
return item;
|
|
}
|
|
if (item) {
|
|
let childItem = item.findChild(_key);
|
|
if (childItem) {
|
|
return childItem;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取子节点,如果不存在就添加
|
|
* @param key
|
|
* @returns
|
|
*/
|
|
public getOrAddChild(key: string,callFunc:Function): RedPointTree {
|
|
let children = this.children.get(key);
|
|
if (children != null) {
|
|
return children;
|
|
}
|
|
children = this.addChild(key,callFunc);
|
|
return children;
|
|
}
|
|
|
|
|
|
/**
|
|
* 根据Id创建节点
|
|
* @param key 唯一Id
|
|
* @returns
|
|
*/
|
|
public addChild(key: string,callFunc:Function) {
|
|
let children = this.children.get(key);
|
|
if (children != null) {
|
|
return children;
|
|
}
|
|
children = new RedPointTree();
|
|
let data: IRedPointData = {
|
|
mId: key,
|
|
mName: key,
|
|
mFunctionId: 0,
|
|
mParent: null,
|
|
mRedNode: null,
|
|
mParentNode: null,
|
|
mRefreshType: 0,
|
|
mShowCount: 0,
|
|
mShowType: -1
|
|
};
|
|
children.callFunc = callFunc;
|
|
children.initData(key, data);
|
|
this.children.set(key, children);
|
|
return children;
|
|
}
|
|
|
|
setRedSelf(value: boolean) {
|
|
if (this.isSelfControl) {
|
|
this.isSelfShowRed = value;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 更新节点红点
|
|
* @param isShow
|
|
* @param showCount
|
|
* @returns
|
|
*/
|
|
updateRedPoint(isShow: boolean, showCount: number) {
|
|
// this.stepTag+= isShow?1:-1;
|
|
let _showType: number = isShow ? this.showType : -1;
|
|
if (this.isSelfControl) {
|
|
if (this.isSelfShowRed) {
|
|
_showType = this.showType
|
|
} else {
|
|
// 更新红点
|
|
if (this.children.size > 0) {
|
|
for (let [key, value] of this.children) {
|
|
if (value.data.mShowType != -1) {
|
|
_showType = this.showType
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
// 更新红点
|
|
if (this.children.size > 0) {
|
|
for (let [key, value] of this.children) {
|
|
if (value.data.mShowType != -1) {
|
|
_showType = this.showType
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
this.data.mShowType = _showType;
|
|
this.data.mShowCount = showCount;
|
|
|
|
this.updateInfo();
|
|
|
|
if (this.data.mParent) {
|
|
if (isShow) {
|
|
if (this.data.mParent.data.mShowType >= 0) return;
|
|
} else {
|
|
if (this.data.mParent.data.mShowType < 0) return;
|
|
}
|
|
this.data.mParent.updateRedPoint(isShow, showCount);
|
|
}
|
|
}
|
|
private updateInfo() {
|
|
if (this.data.mRedNode && this.data.mRedNode.active && this.data.mRedNode.isValid) {
|
|
// console.log("updateRedPoint" + this.data.mId, this.data.mRedNode);
|
|
// this.info.updateRedPoint(this.data.mShowType, showCount);
|
|
// this.data.mRedNode.getChildByName('RedPoint').getComponent(RedPointInfo).updateRedPoint(this.data.mShowType, this.data.mShowCount);
|
|
this.data.mRedNode.getComponentInChildren(RedPointInfo).updateRedPoint(this.data.mShowType, this.data.mShowCount);
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 添加条件
|
|
* @param cond
|
|
*/
|
|
public addCond(cond: IRedTreeCond) {
|
|
this.mCondition.push(cond);
|
|
this.updateCondCount(cond)
|
|
}
|
|
|
|
|
|
/**
|
|
* 更新条件
|
|
* @param event
|
|
* @param count
|
|
*/
|
|
public updateCondCount(event: IRedTreeCond) {
|
|
let condIndex = this.mCondition.find(p => p.condEvent == event.condEvent && p.condId == event.condId && event.condType == p.condType);
|
|
if (condIndex != null) {
|
|
condIndex.currValue = event.currValue;
|
|
if (event.condValue != 0) {
|
|
condIndex.condValue = event.condValue;
|
|
}
|
|
this.updateCond();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除条件
|
|
* @param cond
|
|
*/
|
|
public removeCond(redEvent: string) {
|
|
let condIndex = this.mCondition.find(p => p.condEvent == redEvent);
|
|
if (condIndex != null) {
|
|
let index = this.mCondition.indexOf(condIndex);
|
|
this.mCondition.splice(index, 1);
|
|
}
|
|
this.updateCond();
|
|
}
|
|
|
|
/**
|
|
* 刷新红点
|
|
*/
|
|
refresh() {
|
|
let isShow = true;
|
|
for (let i = 0; i < this.mCondition.length; i++) {
|
|
let item = this.mCondition[i];
|
|
if (item.condValue > item.currValue) {
|
|
isShow = false;
|
|
break;
|
|
}
|
|
}
|
|
this.data.mShowType = isShow ? this.showType : -1;
|
|
this.updateInfo();
|
|
}
|
|
|
|
|
|
/**
|
|
* 更新条件
|
|
*/
|
|
private updateCond() {
|
|
let cond = this.callFunc==null?false: this.callFunc();
|
|
if (this.data.mShowType < 0) {
|
|
if (cond) {
|
|
this.updateRedPoint(true, this.data.mShowCount);
|
|
}
|
|
} else {
|
|
if (!cond) {
|
|
this.updateRedPoint(false, this.data.mShowCount);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export interface IRedTreeCond {
|
|
/**
|
|
* 条件Id
|
|
*/
|
|
condId: number
|
|
/**
|
|
* 条件类型
|
|
*/
|
|
condType: number
|
|
/**
|
|
* 条件值
|
|
*/
|
|
condValue: number
|
|
/**
|
|
* 事件名字
|
|
*/
|
|
condEvent: RedCondEvent;
|
|
|
|
/**
|
|
* 当前值
|
|
*/
|
|
currValue: number;
|
|
}
|