Source: widgets/channellist/grid.js

define('application/widgets/channellist/grid', [
    'application/widgets/grid',
    'antie/runtimecontext',
    'application/formatters/channellist'
], function (
    Grid,
    RuntimeContext,
    ChannelListFormatter
) {
    'use strict';

    var application = RuntimeContext.getCurrentApplication(),
        configuration = application.getConfiguration(),
        layout = application.getLayout().channellist,
        assetLayout = application.getLayout().asset;

    return Grid.extend({

        init: function init (opts) {
            this._loadmoreIndex = configuration.loading.channelList.loadMoreAfterRows;
            this._loadmoreCallback = opts.loadmoreCallback;
            this._reachedEnd = false;
            this._startIndex = null;
            this._endIndex = null;
            this._inRowIndex = null;
            this._rowIndex = null;

            init.base.call(this, 'channellistgrid', {
                grid: {
                    columns: 4,
                    width: layout.width,
                    height: layout.height,
                    alignPoint: 0.2,
                    navigateNext: false,
                    navigatePrevious: false,
                    culling: true,
                    animOptions: {
                        skipAnim: true
                    },
                    continuousListener: true
                },
                asset: {
                    width: assetLayout.epg.width,
                    height: assetLayout.epg.height,
                    formatter: ChannelListFormatter
                }
            });
        },

        /**
         * Check if we need to load more rows.
         *
         * @param {boolean} shouldPrepend - True if items should prepending to the grid.
         */
        checkLoadMore: function (shouldPrepend) {
            var list = this.getList(),
                activeRowIndex = list.getActiveChildWidgetIndex() + 1,
                rowsCount = list.getChildWidgetCount(),
                maxChannels = configuration.loading.channelList.maxChannels,
                addedRows = maxChannels / 4,
                startIndex = this.getStartIndex() || 0,
                endIndex = this.getEndIndex() || 0,
                nextIndex = endIndex + maxChannels,
                reset = false;

            shouldPrepend = shouldPrepend || false;

            if (shouldPrepend) {
                endIndex = startIndex - maxChannels;
                nextIndex = endIndex + maxChannels;

                if (startIndex < 0) {
                    endIndex = 0;
                    nextIndex = maxChannels + startIndex;
                }

                this.setStartIndex(endIndex);
                this._loadmoreCallback(endIndex, nextIndex, reset, shouldPrepend);
            }

            if (!this._reachedEnd && (activeRowIndex === rowsCount - 2)) {
                this._loadmoreIndex = this._loadmoreIndex + addedRows;

                this.setEndIndex(nextIndex);
                this._loadmoreCallback(endIndex, nextIndex, reset, shouldPrepend);
            }
        },

        /**
         * Reset the loadmore index.
         */
        resetLoadmoreIndex: function () {
            this._loadmoreIndex = configuration.loading.channelList.loadMoreAfterRows;
        },

        /**
         * Reset the start / end index.
         */
        resetStartEndIndex: function () {
            this._startIndex = null;
            this._endIndex = null;
            this._reachedEnd = false;
        },

        /**
         * Gets the current row index.
         *
         * @returns {number} - The row index.
         */
        getRowIndex: function () {
            return this._rowIndex;
        },

        /**
         * Gets the current item index in the row.
         *
         * @returns {number} - The item index in the row.
         */
        getInRowIndex: function () {
            return this._inRowIndex;
        },

        /**
         * Gets the current start index.
         *
         * @returns {number} - The start index.
         */
        getStartIndex: function () {
            return this._startIndex;
        },

        /**
         * Gets the current end index.
         *
         * @returns {number} - The end index.
         */
        getEndIndex: function () {
            return this._endIndex;
        },

        /**
         * Sets the the current index of row.
         *
         * @param {number} value - Current row index.
         */
        setRowIndex: function (value) {
            this._rowIndex = value;
        },

        /**
         * Sets the the current index of item in the row.
         *
         * @param {number} value - Current item index in the row.
         */
        setInRowIndex: function (value) {
            this._inRowIndex = value;
        },

        /**
         * Sets the current start index.
         *
         * @param {number} value - Sets the current start index.
         */
        setStartIndex: function (value) {
            this._startIndex = value;
        },

        /**
         * Sets the current end index.
         *
         * @param {number} value - Sets the current end index.
         */
        setEndIndex: function (value) {
            this._endIndex = value;
        },


        /**
         * Reset the loadmore index.
         *
         * @param {boolean} value - Is reached.
         */
        setReachedEnd: function (value) {
            this._reachedEnd = value;
        }
    });
});