Source: widgets/player/controls.js

define('application/widgets/player/controls', [
    'antie/runtimecontext',
    'rofl/widgets/horizontallist',
    'application/widgets/player/buttons/playpause',
    'application/widgets/player/buttons/seek',
    'application/widgets/player/buttons/replay',
    'application/widgets/player/buttons/record',
    'application/widgets/player/buttons/menu',
    'application/widgets/player/buttons/info',
    'application/widgets/player/buttons/nownext',
    'application/widgets/player/buttons/nextepisode',
    'application/widgets/player/buttons/live',
    'antie/events/keyevent',
    'application/constants',
    'application/widgets/player/buttons'
], function (
    RuntimeContext,
    HorizontalList,
    PlayPauseButton,
    SeekButton,
    ReplayButton,
    RecordButton,
    MenuButton,
    InfoButton,
    NowNextButton,
    NextEpisode,
    LiveButton,
    KeyEvent,
    Constants,
    PlayerButtons
) {
    'use strict';

    var application = RuntimeContext.getCurrentApplication(),
        CONTROLS = {
            BACK: Constants.PLAYER_CONTROLS_BACK,
            INFO: Constants.PLAYER_CONTROLS_INFO,
            RESTART: Constants.PLAYER_CONTROLS_RESTART,
            REWIND: Constants.PLAYER_CONTROLS_REWIND,
            PLAYPAUSE: Constants.PLAYER_CONTROLS_PLAYPAUSE,
            FORWARD: Constants.PLAYER_CONTROLS_FORWARD,
            RECORD: Constants.PLAYER_CONTROLS_RECORD,
            MINIEPG: Constants.PLAYER_CONTROLS_MINIEPG,
            LIVE: Constants.PLAYER_CONTROLS_LIVE,
            NEXT: Constants.PLAYER_CONTROLS_NEXT,
            CLOCK: Constants.PLAYER_CONTROLS_CLOCK,
            MENU: Constants.PLAYER_CONTROLS_MENU
        };

    return HorizontalList.extend({

        /**
         * Initialises the widget.
         */
        init: function init () {
            init.base.call(this);

            this.addClass('controls');
        },

        /**
         * Sets the player ui controls.
         *
         * @param {Array} controls - Array with the control buttons to be set.
         */
        setControls: function (controls) {
            var i = 0,
                j = controls.length;

            for (; i < j; i++) {
                this._buildControl(controls[i]);
            }
        },

        /**
         * Builds the given control button name.
         *
         * @param {string} control - Contains the control button to build.
         * @private
         */
        _buildControl: function (control) {

            switch (control) {
                case CONTROLS.INFO:
                    this._buildInfo();
                    break;
                case CONTROLS.RESTART:
                    this._buildRestart();
                    break;
                case CONTROLS.REWIND:
                    this._buildRewind();
                    break;
                case CONTROLS.PLAYPAUSE:
                    this._buildPlayPause();
                    break;
                case CONTROLS.FORWARD:
                    this._buildForward();
                    break;
                case CONTROLS.RECORD:
                    this._buildRecord();
                    break;
                case CONTROLS.MINIEPG:
                    this._buildMiniEpg();
                    break;
                case CONTROLS.LIVE:
                    this._buildLive();
                    break;
                case CONTROLS.NEXT:
                    this._buildNext();
                    break;
                case CONTROLS.CLOCK:
                    break;
                case CONTROLS.MENU:
                    this._buildMenu();
                    break;
            }
        },

        _buildMenu: function () {
            this._menuButton = this.appendChildWidget(new PlayerButtons.MENU(CONTROLS.MENU));
        },

        _buildInfo: function () {
            this._infoButton = this.appendChildWidget(new PlayerButtons.INFO(CONTROLS.INFO));
        },

        _buildRestart: function () {
            this._restartButton = this.appendChildWidget(new PlayerButtons.RESTART(CONTROLS.RESTART));
        },

        _buildRewind: function () {
            this._rewindButton = this.appendChildWidget(new PlayerButtons.SEEK(CONTROLS.REWIND, PlayerButtons.SEEK.TYPES.REWIND));
        },

        _buildPlayPause: function () {
            this._playPauseButton = this.appendChildWidget(new PlayerButtons.PLAYPAUSE(CONTROLS.PLAYPAUSE));
        },

        _buildForward: function () {
            this._forwardButton = this.appendChildWidget(new PlayerButtons.SEEK(CONTROLS.FORWARD, PlayerButtons.SEEK.TYPES.FASTFORWARD));
        },

        _buildRecord: function () {
            this._recordButton = this.appendChildWidget(new PlayerButtons.RECORD(CONTROLS.RECORD));
        },

        _buildLive: function () {
            this._liveButton = this.appendChildWidget(new PlayerButtons.LIVE(CONTROLS.LIVE));
        },

        _buildNext: function () {
            this._nextButton = this.appendChildWidget(new PlayerButtons.NEXT(CONTROLS.NEXT));
        },

        _buildMiniEpg: function () {
            this._miniEpgButton = this.appendChildWidget(new PlayerButtons.NOWNEXT(CONTROLS.MINIEPG));
        },

        /**
         * Shows/hides next episode button.
         *
         * @param {boolean} show - Boolean flag to show (true) or hide next episode button..
         */
        setNextEpisodeButton: function (show) {
            if (this._nextButton) {
                if (show) {
                    this._nextButton.show({skipAnim: true});
                    this._nextButton.setDisabled(false);
                } else {
                    this._nextButton.hide({skipAnim: true});
                    this._nextButton.setDisabled(true);
                }
            }
        },

        /**
         * Shows/hides program info button.
         *
         * @param {boolean} show - True to show button.
         */
        setProgramInfoButton: function (show) {
            if (this._infoButton) {
                if (show) {
                    this._infoButton.show({skipAnim: true});
                    this._infoButton.setDisabled(false);
                } else {
                    this._infoButton.hide({skipAnim: true});
                    this._infoButton.setDisabled(true);
                }
            }
        },

        /**
         * Sets the record state.
         */
        onRecord: function () {
            this._recordButton.setState(PlayerButtons.RECORD.STATES.RECORD);
        },

        /**
         * Sets the recording state.
         */
        onRecording: function () {
            this._recordButton.setState(PlayerButtons.RECORD.STATES.RECORDING);
        },

        /**
         * Focuses on rewind.
         */
        onRewind: function () {
            this._rewindButton.focus();
        },

        /**
         * Focuses on ff.
         */
        onFastForward: function () {
            this._forwardButton.focus();
        },

        /**
         * Toggle info buttons.
         *
         * @param {boolean} deselect - True if the button should deselect.
         */
        onProgramInfo: function (deselect) {
            if (this._infoButton.hasClass('selected') || deselect) {
                this._infoButton.removeClass('selected');
            } else {
                this._infoButton.addClass('selected');
            }
        },

        /**
         * Toggle miniEPG button.
         *
         * @param {boolean} deselect - True if the button should deselect.
         */
        onMiniEPG: function (deselect) {
            if (this._miniEpgButton.hasClass('selected') || deselect) {
                this._miniEpgButton.removeClass('selected');
            } else {
                this._miniEpgButton.addClass('selected');
            }
        },

        /**
         * Should be called when the video player plays.
         */
        onPlay: function () {
            this._playPauseButton.focus();
            this._playPauseButton.setState(PlayerButtons.PLAYPAUSE.STATES.PLAYING);
        },

        /**
         * Should be called when the video player pauses.
         *
         * @param {boolean} focusPlayPause - True if should focus on playpause button.
         */
        onPause: function (focusPlayPause) {
            if (focusPlayPause) {
                this._playPauseButton.focus();
            }
            this._playPauseButton.setState(PlayerButtons.PLAYPAUSE.STATES.PAUSED);
        },

        /**
         * Updates the seek speed.
         *
         * @param {number} speed - The seek speed.
         */
        updateSeekSpeed: function (speed) {
            this.onPause();

            if (speed < 0) {
                this._forwardButton.reset();
                this._rewindButton.setSpeed(speed);
            } else {
                this._rewindButton.reset();
                this._forwardButton.setSpeed(speed);
            }

            this.showUI();
        },

        /**
         * Checks if seeking is active.
         *
         * @returns {boolean} - Returns true if seeking is active.
         */
        isSeekActive: function () {
            return this._forwardButton.hasClass('activated') || this._rewindButton.hasClass('activated');
        },

        /**
         * Attempts to reset trickmode buttons. (set them to initial state).
         */
        resetControls: function () {
            this._rewindButton.reset();
            this._forwardButton.reset();
            this._playPauseButton.reset();
        },

        /**
         * On focus event. Sets the focus on play pause button.
         *
        focus: function () {
            this._playPauseButton.focus();
        },*/

        /**
         * Triggers on key down event for navigation keys to show player ui if hidden.
         *
         * @param {KeyEvent} e - The event data.
         **/
        _onKeyDown: function _onKeyDown (e) {

            switch (e.keyCode) {
                case KeyEvent.VK_LEFT:
                    if (this.isUIVisible()) {

                        // Show UI on every key down.
                        this.showUI();
                        _onKeyDown.base.call(this, e);
                    }

                    // if not visible, event will propagate to player component to show menu.
                    break;
                case KeyEvent.VK_UP:
                case KeyEvent.VK_RIGHT:
                case KeyEvent.VK_DOWN:

                    if (this.isUIVisible()) {
                        _onKeyDown.base.call(this, e);
                    } else {

                        // Show UI on every navigation key down.
                        this.showUI();
                        e.stopPropagation();
                        e.preventDefault();
                    }
                    break;
                default:
                    _onKeyDown.base.call(this, e);
            }
        },

        /**
         * Checks if player ui is visible.
         *
         * @returns {boolean} - True if ui is visible.
         */
        isUIVisible: function () {
            if (!this._playerView) {
                this._playerView = this._getPlayerView();
            }

            return this._playerView.isUIVisible();
        },

        /**
         * Shows player UI.
         */
        showUI: function () {
            if (!this._playerView) {
                this._playerView = this._getPlayerView();
            }

            this._playerView.showUI();
        },

        /**
         * Gets the player view.
         *
         * @returns {Object} - The player's view.
         * @private
         */
        _getPlayerView: function () {
            var playerComponent = application.getComponent('player').getChildWidgets()[0];

            return playerComponent.getView();
        },

        /**
         * Resets the controls.
         */
        reset: function () {
            this._rewindButton.reset();
            this._forwardButton.reset();
            this._playPauseButton.reset();
        },

        /**
         * Shows replay button.
         */
        showReplay: function () {
            this._restartButton.setDisabled(false);
            this._restartButton.show({skipAnim: true});
        },

        /**
         * Hides replay button.
         */
        hideReplay: function () {
            this._restartButton.setDisabled(true);
            this._restartButton.hide({skipAnim: true});
        },

        /**
         * Shows record button.
         */
        showRecord: function () {
            this._recordButton.setDisabled(false);
            this._recordButton.show({skipAnim: true});
        },

        /**
         * Hides record button.
         */
        hideRecord: function () {
            this._recordButton.setDisabled(true);
            this._recordButton.hide({skipAnim: true});
        },

        /**
         * Shows live button.
         */
        showLive: function () {
            this._liveButton.setDisabled(false);
            this._liveButton.show({skipAnim: true});
        },

        /**
         * Hides live button.
         */
        hideLive: function () {
            this._liveButton.setDisabled(true);
            this._liveButton.hide({skipAnim: true});
        },

        /**
         * Focuses the button.
         */
        focus: function focus () {
            focus.base.call(this);
            this._playPauseButton.focus();
        },

        /**
         * Focus play/pause button.
         */
        focusPlayPauseButton: function () {
            this._playPauseButton.focus();
        },

        /**
         * Focus rewind button.
         */
        focusRewindButton: function () {
            this._rewindButton.focus();
        },

        /**
         * Focus fast forward button.
         */
        focusFastForwardButton: function () {
            this._forwardButton.focus();
        },

        /**
         * Disables live button.
         *
         * @param {boolean} disable - Disable flag.
         */
        disableLiveButton: function (disable) {
            this._liveButton.setPointerSupport(!disable);
            this._liveButton.setDisabled(disable);
        },

        /**
         * Key handler for horizontal list.
         *
         * @param {Object} evt - The key event.
         *
        _onKeyDown: function onKeyDown (evt) {
            if (this.playerIsHidden && evt.keyCode === KeyEvent.VK_LEFT) {
                application.focusMenu();
            }

            if (this.isVisible() || evt.keyCode !== KeyEvent.VK_LEFT) {
                onKeyDown.base.call(this, evt);
            } else {
                application.focusMenu();
                evt.stopPropagation();
                evt.preventDefault();
            }
        },*/

        /**
         * Sets flag whether the player is in hidden state.
         *
         * @param {boolean} flag - If is hidden.
         */
        setPlayerHiddenFlag: function (flag) {
            this.playerIsHidden = flag;
        },

        /**
         * Gets the play pause button.
         *
         * @returns {Object} - The play pause button.
         */
        isPaused: function () {
            return this._playPauseButton.getState() === PlayPauseButton.STATES.PAUSED;
        }
    });
});