Source: components/details/series.js

define('application/components/details/series', [
    'application/components/details/details',
    'application/widgets/detail/tablist',
    'application/widgets/detail/series/header',
    'rofl/widgets/verticallist',
    'application/widgets/detail/tabs/seasons',
    'rofl/lib/utils',
    'antie/runtimecontext',
    'antie/events/keyevent',
    'rofl/analytics/web/google'
], function (
    BaseDetail,
    TabList,
    Header,
    VerticalList,
    SeasonsTab,
    Utils,
    RuntimeContext,
    KeyEvent,
    GoogleAnalytics
) {
    'use strict';

    var application = RuntimeContext.getCurrentApplication(),
        layout = application.getLayout(),
        layoutModifier = layout.modifier,
        SEASONS_ORDER = {
            ASC: 'asc',
            DESC: 'desc'
        },
        GA = GoogleAnalytics.getInstance();

    return BaseDetail.extend({

        /**
         * Initialises the component.
         */
        init: function init () {
            this._onBingeWatchEventBound = Utils.bind(this._onBingeWatchEvent, this);
            this._onNextEpisodeEventBound = Utils.bind(this._onNextEpisodeEvent, this);

            init.base.call(this, 'series');
        },

        /**
         * Sets the header data.
         *
         * @param {Object} data - Contains detail data.
         * @private
         */
        _setHeaderData: function (data) {
            var item = data.contentDetail;

            this._header.setDataItem({
                title: item.getTitle(),
                subtitle: item.getDescription(),
                seasons: item.getSeasons().length,
                image: item.getImage('manual', {
                    width: 900 * layoutModifier,
                    height: 500 * layoutModifier
                }),
                ratings: item.getVideoRatings(),
                imdbRating: item.getImdbRating()
            });
        },

        /**
         * Sets the tabs data.
         *
         * @param {Object} season - The item.
         * @private
         */
        _setTabsData: function (season) {
            var tabList = this._tabList;

            tabList.setDataItem([
                {
                    widget: SeasonsTab,
                    data: season,
                    text: '',
                    params: {
                        isDropDown: this._contentDetail.getSeasons().length > 1
                    }
                }
            ]);
        },

        /**
         * Sets the content's details.
         *
         * @param {Object} data - Contains detail data.
         * @private
         */
        _setDetailData: function (data) {
            var contentDetail = data.contentDetail,
                firstAvailableSeason;

            // Order seasons in ascending order for the seasons' dropdown.
            firstAvailableSeason = contentDetail.orderSeasons(SEASONS_ORDER.ASC)[0];

            if (firstAvailableSeason) {
                this._tabList.removeClass('no-content');

                firstAvailableSeason.prepare()
                    .then(Utils.bind(function (season) {

                        this._season = season;
                        this._setTabsData(season);
                        this._tabList.focusTab();
                        this._tabList.getTab(0).alignCarousel();
                    }, this));
            } else {
                this._season = null;
                this._tabList.addClass('no-content');
                this._tabList.setDataItem([]);
                this._backButton.focus();
            }
        },

        /**
         * Select Event.
         *
         * @param {Object} e - The event data.
         * @private
         */
        _onSelect: function (e) {
            var target = e.target,
                targetId = target.id,
                targetData = target.getDataItem();

            if (targetId === 'back-button') {
                this._onBack();
            } else if (target.hasClass('carouselItem')) {
                this._onPlay(targetData.item);
            } else if (target.hasClass('filter') && this._contentDetail.getSeasons().length > 1) {
                this._routeToDropdown(target.getDataItem());
            } else if (target.hasClass('left-pointer')) {
                this._tabList.getTab(0).alignPrevious();
            } else if (target.hasClass('right-pointer')) {
                this._tabList.getTab(0).alignNext();
            }
        },

        /**
         * Attempts to play the video.
         *
         * @param {Object} dataItem - The data item.
         * @private
         */
        _onPlay: function (dataItem) {
            var data = dataItem || this._data[0],
                seasonItems = this._season.getItems().slice(),
                index = seasonItems.indexOf(data);

            if (index >= 0) {
                seasonItems = seasonItems.slice(index + 1);
            }

            if (data.canPlay()) {
                application.route('vodplayer', {
                    data: data,
                    type: 'VOD_MOVIE',
                    callingPage: 'detail',
                    analyticsType: 'SERIES',
                    bingewatch: seasonItems
                });
                GA.onEvent('player', 'started', {
                    eventLabel: 'series'
                });
            }
        }
    });
});