2022-06-19 22:32:52 +08:00

62 lines
1.7 KiB
TypeScript

import { WALLET_TOKEN_TYPE_CHANGE } from "../common/WalletEvent";
import { ZError } from "../common/ZError";
import { DEFAULT_NFT_TYPES } from "../config/chain_config";
import WalletBase from "./WallerBase";
const {ccclass, property} = cc._decorator;
@ccclass
export default class ListNode extends WalletBase {
@property(cc.Prefab)
nftListPreb: cc.Prefab = null
@property(cc.Node)
tokenNode: cc.Node = null
otherNode: Map<string, cc.Node> = new Map()
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start () {
super.start()
this.wallet.mainHandlers.on(WALLET_TOKEN_TYPE_CHANGE, this.onTokenTypeChange.bind(this))
}
// update (dt) {}
initOneNftList(type: string) {
if (this.otherNode.has(type)) {
return this.otherNode.get(type)
}
const chain = this.wallet.currentChain.id
let nftData: any = DEFAULT_NFT_TYPES[chain]
if (!nftData) {
throw new ZError(10, 'no nft data found')
}
let node = cc.instantiate(this.nftListPreb)
node.active = false
node.getComponent('NftList').address = nftData[type]
node.getComponent('NftList').type = type
this.node.addChild(node)
this.otherNode.set(type, node)
return node
}
onTokenTypeChange(type: string) {
console.log('on token type change: ', type)
for (let key of this.otherNode.keys()) {
this.otherNode.get(key).active = key === type
}
if (type === 'tokens') {
this.tokenNode.active = true
this.tokenNode.getComponent('TokenList').updateList()
} else {
this.tokenNode.active = false
const nftList = this.initOneNftList(type)
nftList.active = true
nftList.getComponent('NftList').updateList()
}
}
}