AnnieJS API Docs for: 1.1.3
show:

File: libs/ScrollList.js

var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/**
 * Created by anlun on 16/8/14.
 */
/**
 * @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} vW 列表的宽
         * @param {number} vH 列表的高
         * @param {boolean} isVertical 是横向滚动还是纵向滚动 默认是纵向
         * @param {number} cols 分几列,默认是1列
         * @since 1.0.9
         */
        function ScrollList(itemClassName, itemWidth, itemHeight, vW, vH, isVertical, cols) {
            if (isVertical === void 0) { isVertical = true; }
            if (cols === void 0) { cols = 1; }
            var _this = _super.call(this, vW, vH, 0, isVertical) || this;
            _this._items = null;
            _this.data = [];
            _this.downL = null;
            _this._lastFirstId = -1;
            var s = _this;
            s._isInit = false;
            s._instanceType = "annieUI.ScrollList";
            s._itemW = itemWidth;
            s._itemH = itemHeight;
            s._items = [];
            s._itemClass = itemClassName;
            s._itemCount = 0;
            s._cols = cols;
            s._updateViewRect();
            s.addEventListener(annie.Event.ENTER_FRAME, s.flushData.bind(s));
            return _this;
        }
        Object.defineProperty(ScrollList.prototype, "loadingView", {
            /**
             * 获取下拉滚动的loadingView对象
             * @property loadingView
             * @since 1.0.9
             * @returns {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 (!s._isInit || isReset) {
                s.data = data;
                s._isInit = true;
            }
            else {
                s.data = s.data.concat(data);
            }
            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);
            }
        };
        ScrollList.prototype.flushData = function () {
            var s = this;
            if (s._isInit) {
                var id = (Math.abs(Math.floor(s.view[s.paramXY] / s._itemRow)) - 1) * s._cols;
                id = id < 0 ? 0 : id;
                if (id != s._lastFirstId) {
                    var isMustUpdate = s._lastFirstId == -1;
                    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 (item.id != id || isMustUpdate) {
                            item.initData(s.data[id] ? id : -1, s.data[id]);
                            item.visible = s.data[id] ? true : false;
                            item[s.paramXY] = Math.floor(id / s._cols) * s._itemRow;
                            item[s._disParam] = (id % s._cols) * s._itemCol;
                        }
                        id++;
                    }
                }
            }
        };
        /**
         * 设置可见区域,可见区域的坐标始终在本地坐标中0,0点位置
         * @method setViewRect
         * @param {number}w 设置可见区域的宽
         * @param {number}h 设置可见区域的高
         * @param {boolean} isVertical 方向
         * @public
         * @since 1.1.1
         */
        ScrollList.prototype.setViewRect = function (w, h, isVertical) {
            _super.prototype.setViewRect.call(this, w, h, isVertical);
            var s = this;
            if (s._itemRow && s._itemCol) {
                s._updateViewRect();
            }
        };
        ScrollList.prototype._updateViewRect = function () {
            var s = this;
            if (s.isVertical) {
                s._disParam = "x";
                s._itemRow = s._itemH;
                s._itemCol = s._itemW;
            }
            else {
                s._disParam = "y";
                s._itemRow = s._itemW;
                s._itemCol = s._itemH;
            }
            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;
            }
        };
        return ScrollList;
    }(annieUI.ScrollPage));
    annieUI.ScrollList = ScrollList;
})(annieUI || (annieUI = {}));