Source: models/uibuilder/carousel.js

define('application/models/uibuilder/carousel', [
    'rofl/models/api/abstract',
    'application/managers/api',
    'application/models/broadcast',
    'rofl/lib/utils',
    'application/components/uibuilder',
    'application/models/uibuilder/watchall',
    'application/managers/bookmark',
    'antie/runtimecontext'
], function (
    Class,
    ApiManager,
    Broadcast,
    Utils,
    UIBuilder,
    WatchAll,
    BookmarkManager,
    RuntimeContext
) {
    'use strict';

    var api = ApiManager.getKPNAPI(),
        application = RuntimeContext.getCurrentApplication(),
        MAX_RESULTS = application.getConfiguration().watchallMaxResults;

    return Class.extend({

        /**
         * Initialises the model.
         *
         * @param {Object} data - The API data.
         */
        init: function (data) {
            this._title = data.metadata.label;
            this._apiEndpoint = '101/1.2.0/A/nld/smarttv/kpn' + data.retrieveItems.uri;
            this._watchAllEndpoint = data.actions[0].uri;
            this._layout = data.layout;

            this._watchAll = new WatchAll({
                title: this._title,
                url: this._watchAllEndpoint,
                layout: this._layout
            });
        },

        /**
         * Returns the type of widget this model to be used with.
         *
         * @returns {string} - The type of widget.
         */
        getWidgetType: function () {
            return UIBuilder.WIDGET_TYPES.CAROUSEL;
        },

        /**
         * Returns the title.
         *
         * @returns {string} - The title.
         */
        getTitle: function () {
            return this._title;
        },

        /**
         * Returns the api endpoint.
         *
         * @returns {string} - The api endpoint.
         */
        resolveEndpoint: function () {
            return this._apiEndpoint;
        },

        /**
         * Prepares the widget and loads all required data.
         *
         * @returns {Promise} - Promise resolving with the carousel model.
         */
        prepare: function () {
            return api.read(this, {
                withCredentials: true
            })
                .then(Utils.bind(function (response) {
                    var bookmarks = response.resultObj.bookmarks || [],
                        contentType;

                    if (this._layout === 'BOOKMARK_LARGE_HORIZONTAL' && bookmarks.length) {
                        contentType = 'vod';

                        BookmarkManager.setBookmarkList(bookmarks);
                        return api.read('searchall', {
                            params: {
                                query: '',
                                filter_contentIds: this._getContentIdsFromBookmarks(bookmarks),
                                _searchContentType: contentType
                            }
                        }).then(Utils.bind(function (searchResponse) {
                            this._setDataItems(searchResponse, true);

                            return this;
                        }, this));
                    }

                    this._setDataItems(response);

                    return this;
                }, this));
        },

        /**
         * Retrieves content ids from the bookmarks response.
         *
         * @param {Object} bookmarks - The bookmarks response.
         * @returns {Array} - Bookmarks' content ids list.
         * @private
         */
        _getContentIdsFromBookmarks: function (bookmarks) {
            var contentIds = [],
                bound = Utils.bind(function (bookmark) {
                    contentIds.push(bookmark.bookmarkSet.contentId);
            }, this);

            Utils.each(bookmarks, bound);

            return contentIds;
        },

        /**
         * Retrieve and/or set the received data items.
         *
         * @param {Object} response - Response from the requested endpoint.
         * @param {Object} transformedData - True if data is already transformed, no need to create Broadcast.
         * @private
         */
        _setDataItems: function (response, transformedData) {
            var items = [],
                containers;

            if (transformedData) {
                items = response;
            } else {
                containers = response.resultObj.containers;

                Utils.each(containers, function (container) {
                    items.push(new Broadcast(container));
                });
            }

            this._items = items;
            this._total = items && items.length;
        },

        /**
         * Returns the items.
         *
         * @returns {Array} - The carousel items to be appended.
         */
        getItems: function () {
            return this._items.slice(0, MAX_RESULTS);
        },

        /**
         * Returns the watch-all endpoint.
         *
         * @returns {Promise} - The watchall endpoint.
         */
        getWatchAll: function () {
            return this._watchAll;
        },

        /**
         * Returns the items count.
         *
         * @returns {number} - Total number of items available.
         */
        getTotal: function () {
            return this._total;
        },

        /**
         * The carousel layout type. See page model for available layouts.
         *
         * @returns {string} - The layout type. e.g. 'EPG_LARGE', 'VOD_SMALL'.
         */
        getLayout: function () {
            return this._layout;
        },

        /**
         * Returns true if the carousel's layout is EPG styled.
         *
         * @returns {boolean} - True if matches the layout.
         */
        isEpgLayout: function () {
            return this._layout === 'EPG_LARGE' || this._layout === 'EPG_SPORT';
        },

        /**
         * Returns true if the carousel's layout is Bookmark styled.
         *
         * @returns {boolean} - True if matches the layout.
         */
        isBookmarkLayout: function () {
            return this._layout === 'BOOKMARK_LARGE_HORIZONTAL';
        }
    });
});