6e88bf8c-bde8-4ae4-9361-55bb1a983d10.js 4.5 KB
"use strict";
cc._RF.push(module, '6e88b+MvehK5JNhVbsamD0Q', 'ListSubviewImpl');
// script/game/ui/baseview/impl/ListSubviewImpl.ts

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListSubviewImpl = void 0;
const CCViewBase_1 = require("../../../../common/classbase/CCViewBase");
const CCDummyObjects_1 = require("../../../../common/CCDummyObjects");
const simba_utils_1 = require("simba-utils");
const { ccclass, property } = cc._decorator;
let ListSubviewImpl = /** @class */ (() => {
    let ListSubviewImpl = class ListSubviewImpl extends CCViewBase_1.CCPureSubview {
        constructor() {
            super(...arguments);
            //#region editor bindings
            this.contentLayout = CCDummyObjects_1.DummyLayout;
            this.scrollView = CCDummyObjects_1.DummyScrollView;
            //#endregion
            this._refreshing = false;
            this._itemViews = {};
            this._viewCache = [];
            //#endregion
            this.onItemClick = (key) => {
                return () => {
                    if (this._props.onItemClick) {
                        this._props.onItemClick(key);
                    }
                };
            };
            this.refreshList = async (list) => {
                while (this._refreshing) {
                    await simba_utils_1.delay(0.5);
                }
                this._refreshing = true;
                let allKeys = new Set(Object.keys(this._itemViews));
                for (let i = 0; i < list.length; i++) {
                    let view = this._itemViews[list[i].key];
                    allKeys.delete(list[i].key);
                    if (!view) {
                        view = (this._viewCache.pop() || await this.createListItemView(i));
                        view.index = i;
                        view.node.parent = this.contentLayout.node;
                        view.node.setSiblingIndex(i);
                        this._itemViews[list[i].key] = view;
                    }
                    else if (view.index !== i) {
                        view.node.setSiblingIndex(i);
                        view.index = i;
                    }
                    view.setProps(Object.assign(Object.assign({}, list[i]), { onClick: this.onItemClick(list[i].key) }));
                }
                for (let key of allKeys) {
                    let view = this._itemViews[key];
                    view.node.removeFromParent(false);
                    delete this._itemViews[key];
                    this._viewCache.push(view);
                }
                this._refreshing = false;
                this.onRefreshListEnd();
            };
        }
        onLoad() {
            console.log("ListViewImpl onLoad", this.scrollView);
        }
        //#region base class overrides
        onPropsReceive() {
            this._props.items = [...this._props.items];
            if (this._prevProps) {
                if (!simba_utils_1.shallowEqual(this._props.items, this._prevProps.items)) {
                    this.refreshList(this._props.items);
                }
            }
        }
        onPropsLoad(props) {
            super.onPropsLoad(props);
            this.refreshList(props.items);
        }
        //#endregion
        //#region interface impl
        scrollToTop() {
            this.scrollView.scrollToTop();
        }
        scrollToBottom(animate = false) {
            if (this.contentLayout.node.height > this.scrollView.node.height) {
                this.scrollView.scrollToBottom(animate ? 0.3 : undefined);
            }
        }
        onRefreshListEnd() { }
    };
    __decorate([
        property(cc.Layout)
    ], ListSubviewImpl.prototype, "contentLayout", void 0);
    __decorate([
        property(cc.ScrollView)
    ], ListSubviewImpl.prototype, "scrollView", void 0);
    ListSubviewImpl = __decorate([
        ccclass
    ], ListSubviewImpl);
    return ListSubviewImpl;
})();
exports.ListSubviewImpl = ListSubviewImpl;

cc._RF.pop();