Source: components/players/promo.js

define('application/components/players/promo', [
    'application/components/player',
    'application/views/player/promo',
    'application/helpers/playerproperties',
    'application/managers/api',
    'antie/runtimecontext',
    'rofl/lib/utils',
    'application/constants',
    'application/decorators/player/promomanipulation',
    'application/decorators/player/promointerface',
    'rofl/events/keyevent'
], function (
    BaseComponent,
    PromoView,
    PlayerProperties,
    ApiManager,
    RuntimeContext,
    Utils,
    Constants,
    PlayerManipulation,
    AppCorePlaybackInterface,
    KeyEvent
) {
    'use strict';

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

    return BaseComponent.extend({

        /**
         * Visibility changed event.
         *
         * @private
         */
        _onVisibilityChanged: function () {
            if (!document.hidden) {
                application.showLoader(true);
                this._preparePlayback();
            }
        },

        /**
         * Prepares playback: request stream start playback.
         *
         * @private
         */
        _preparePlayback: function () {
            this._buildSeeker();
            this._requestStream()
                .then(Utils.bind(this._startPlayback, this))
                ['catch'](Utils.bind(this._onPlayerError, this));
        },

        /**
         * Requests the content's stream data.
         *
         * @returns {Object} - Stream data.
         */
        _requestStream: function () {
            return api.read('streams/promo', {
                type: 'promo'
            });
        },

        /**
         * Prepares the player properties and attempts to start playback.
         *
         * @param {Object} data - Contains the stream data.
         * @private
         */
        _startPlayback: function _startPlayback (data) {
            var properties = new PlayerProperties({
                source: {
                    src: data.url,
                    mimeType: PlayerProperties.MIME_TYPES.mp4
                },
                autoplay: true
            });

            this._view.showUI(true);
            this.playerInterface.initPlayer(properties, {
                onError: Utils.bind(this._onPlayerError, this)
            });
        },

        /**
         * Sets the view for the player.
         *
         * @private
         */
        _setView: function () {
            this._view = this.appendChildWidget(new PromoView({
                mouseOverHandler: Utils.bind(function () {
                    if (this._view.supportsPointer()) {
                        this._showUI();
                    }
                }, this)
            }));
        },

        /**
         * PlayerEvent.
         *
         * @param {Object} e - The player event data.
         * @private
         */
        onPlayerEvent: function (e) {

            switch (e.type) {
                case this.MEDIA_PLAYER_EVENTS.PLAYING:
                    this._currentProgram = this._program;
                    application.hideLoader();

                    break;
                case this.MEDIA_PLAYER_EVENTS.COMPLETE:
                    this._onBack();
                    break;
                case this.MEDIA_PLAYER_EVENTS.STATUS:
                    this._setProgress(e);

                    if (!this._playbackStatus.active) {
                        this._view.showUI();
                        this._playbackStatus.active = true;
                    }
                    break;
                case this.MEDIA_PLAYER_EVENTS.BITRATE_CHANGED:
                    break;

                    // No Default.
            }
        },

        /**
         *
         * @param {KeyEvent} e - Key Event.
         * @private
         */
        _onKeyDown: function _onKeyDown (e) {
            switch (e.keyCode) {
                case KeyEvent.VK_LEFT:
                    this._view.showUI();
                    e.stopPropagation();
                    break;

                default:
                    _onKeyDown.base.call(this, e);
            }
        },

        /**
         * Returns the player manipulation decorator.
         *
         * @returns {Object} - Player manipulation decorator.
         */
        getPlayerManipulation: function () {
            return PlayerManipulation;
        },

        /**
         * Gets the playback interface.
         *
         * @returns {Object} - The playback interface.
         */
        getPlaybackInterface: function () {
            return new AppCorePlaybackInterface();
        },

        /**
         * Updates the view's progress.
         *
         * @param {Object} progress - The player's progress data.
         * @private
         */
        _setProgress: function (progress) {
            this._view.setProgress(progress);
        },

        /**
         * Gets executed when the current time changes.
         *
         * @param {number|Object} data - The seek data.
         * @private
         */
        _onCurrentTimeChanged: function (data) {

            if (!this._seeker.isActive()) {
                return;
            }

            this._onCurrentTimeUpdated(data);
        },

        /**
         * On back action.
         *
         * @private
         */
        _onBack: function () {
            this._errorState = false;

            this._view.resetControls();
            this._removeEventListeners();
            this.playerInterface.destroy();
            this._playbackStatus.active = false;
            application.route('landing');
        }
    });
});