var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/**
 * @module annieUI
 */
var annieUI;
(function (annieUI) {
    /**
     * 有些时候需要大量的有规则的滚动内容。这个时候就应该用到这个类了
     * @class annieUI.ScrollList
     * @public
     * @extends annieUI.ScrollPage
     * @since 1.0.9
     */
    var ScrollList = (function (_super) {
        __extends(ScrollList, _super);
        /**
         * 构造函数
         * @method ScrollList
         * @param {Class} itemClassName 可以做为Item的类
         * @param {number} itemWidth item宽
         * @param {number} itemHeight item高
         * @param {number} viewWidth 列表的宽
         * @param {number} viewHeight 列表的高
         * @param {boolean} isVertical 是横向滚动还是纵向滚动 默认是纵向
         * @param {number} step 纵向就是分几列,横向就是分几行,默认是1列或者1行
         * @since 1.0.9
         */
        function ScrollList(itemClassName, itemWidth, itemHeight, viewWidth, viewHeight, isVertical, step) {
            if (isVertical === void 0) { isVertical = true; }
            if (step === void 0) { step = 1; }
            _super.call(this, null, viewWidth, viewHeight, viewWidth, viewHeight);
            this._items = null;
            this._isInit = 0;
            this.data = [];
            this.downL = null;
            this._lastFirstId = -1;
            this._distance = 0;
            this._paramXY = "y";
            this._isVertical = true;
            this._maxDistance = 0;
            var s = this;
            s._instanceType = "annieUI.ScrollList";
            s._itemW = itemWidth;
            s._itemH = itemHeight;
            s._items = [];
            s._itemClass = itemClassName;
            s._itemCount = 0;
            s._cols = step;
            s.isVertical = isVertical;
            s.addEventListener(annie.Event.ENTER_FRAME, s.flushData.bind(s));
        }
        Object.defineProperty(ScrollList.prototype, "isVertical", {
            get: function () {
                return this._isVertical;
            },
            set: function (value) {
                var s = this;
                s._isVertical = value;
                s._updateItems();
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ScrollList.prototype, "loadingView", {
            /**
             * 获取下拉滚动的loadingView对象
             * @property loadingView
             * @since 1.0.9
             * @return {DisplayObject}
             */
            get: function () {
                return this.downL;
            },
            enumerable: true,
            configurable: true
        });
        /**
         * 更新列表数据
         * @method updateData
         * @param {Array} data
         * @param {boolean} isReset 是否重置数据列表。
         * @since 1.0.9
         */
        ScrollList.prototype.updateData = function (data, isReset) {
            if (isReset === void 0) { isReset = false; }
            var s = this;
            if (data) {
                if (!s._isInit || isReset) {
                    s.data = data;
                }
                else {
                    s.data = s.data.concat(data);
                }
                s._isInit = 1;
            }
            s._lastFirstId = -1;
            s._maxDistance = Math.ceil(s.data.length / s._cols) * s._itemRow;
            if (s.downL) {
                s.downL[s._paramXY] = Math.max(s._distance, s._maxDistance);
                var wh = s.downL.getWH();
                s._maxDistance += (s._paramXY == "x" ? wh.width : wh.height);
            }
            s.resetMaxDistance();
        };
        ScrollList.prototype.resetMaxDistance = function () {
            var s = this;
            if (s._isVertical) {
                s.scroller._scrollHeight = s._maxDistance;
            }
            else {
                s.scroller._scrollWidth = s._maxDistance;
            }
            s.scroller._updateViewAndScroll();
        };
        ScrollList.prototype.flushData = function () {
            var s = this;
            if (s._isInit > 0) {
                var id = (Math.abs(Math.floor(s._view[s._paramXY] / s._itemRow)) - 1) * s._cols;
                id = id < 0 ? 0 : id;
                if (id != s._lastFirstId) {
                    s._lastFirstId = id;
                    if (id != s._items[0].id) {
                        for (var r = 0; r < s._cols; r++) {
                            if (s.speed > 0) {
                                s._items.unshift(s._items.pop());
                            }
                            else {
                                s._items.push(s._items.shift());
                            }
                        }
                    }
                }
                for (var i = 0; i < s._itemCount; i++) {
                    var item = s._items[i];
                    if (s._isInit == 1) {
                        item._a2x_sl_id = -1;
                    }
                    if (item._a2x_sl_id != id) {
                        item.initData(s.data[id] ? id : -1, s.data[id]);
                        item[s._paramXY] = Math.floor(id / s._cols) * s._itemRow;
                        item[s._disParam] = (id % s._cols) * s._itemCol;
                        //如果没有数据则隐藏
                        if (s.data[id]) {
                            item._a2x_sl_id = id;
                            item.visible = true;
                        }
                        else {
                            item._a2x_sl_id = -1;
                            item.visible = false;
                        }
                    }
                    id++;
                }
                s._isInit = 2;
            }
        };
        ScrollList.prototype._updateItems = function () {
            var s = this;
            if (s._isVertical) {
                s._disParam = "x";
                s._paramXY = "y";
                s._itemRow = s._itemH;
                s._itemCol = s._itemW;
                s._distance = s._scroller.viewHeight;
            }
            else {
                s._disParam = "y";
                s._paramXY = "x";
                s._itemRow = s._itemW;
                s._itemCol = s._itemH;
                s._distance = s._scroller.viewWidth;
            }
            var newCount = (Math.ceil(s._distance / s._itemRow) + 1) * s._cols;
            if (newCount != s._itemCount) {
                if (newCount > s._itemCount) {
                    for (var i = s._itemCount; i < newCount; i++) {
                        var item = new s._itemClass();
                        item.id = -1;
                        item.data = null;
                        s._items.push(item);
                        s._view.addChild(item);
                    }
                }
                else {
                    for (var i = 0; i < s._itemCount - newCount; i++) {
                        s._view.removeChild(s._items.pop());
                    }
                }
                s._itemCount = newCount;
                s._lastFirstId = -1;
            }
        };
        /**
         * 设置加载数据时显示的loading对象
         * @since 1.0.9
         * @method setLoading
         * @param {annie.DisplayObject} downLoading
         */
        ScrollList.prototype.setLoading = function (downLoading) {
            var s = this;
            if (s.downL) {
                s._view.removeChild(s.downL);
                var wh = s.downL.getWH();
                s._maxDistance -= (s._paramXY == "x" ? wh.width : wh.height);
                s.downL = null;
            }
            if (downLoading) {
                s.downL = downLoading;
                s._view.addChild(downLoading);
                s.downL[s._paramXY] = Math.max(s._distance, s._maxDistance);
                var wh = s.downL.getWH();
                s._maxDistance += (s._paramXY == "x" ? wh.width : wh.height);
            }
            else {
                s.isStop = false;
            }
            s.resetMaxDistance();
        };
        ScrollList.prototype.destroy = function () {
            var s = this;
            s._items = null;
            s._itemClass = null;
            s.data = null;
            s.downL = null;
            _super.prototype.destroy.call(this);
        };
        return ScrollList;
    }(annieUI.ScrollPage));
    annieUI.ScrollList = ScrollList;
})(annieUI || (annieUI = {}));

    
Top