Source: widgets/detail/descriptioncontainer.js

define('application/widgets/detail/descriptioncontainer', [
    'rofl/widgets/button',
    'rofl/widgets/label',
    'application/widgets/detail/iconbutton',
    'rofl/widgets/container',
    'rofl/widgets/verticallist',
    'antie/runtimecontext',
    'rofl/lib/utils',
    'application/widgets/detail/buttons/play',
    'application/widgets/detail/buttons/record',
    'application/widgets/detail/buttons/close',
    'application/widgets/detail/buttons/restart',
    'application/widgets/detail/buttons/delete',
    'rofl/widgets/horizontallist',
    'application/managers/recording',
    'application/managers/feature',
    'application/models/production/recordings/record',
    'application/events/recorddeletedevent'
], function (
    Button,
    Label,
    IconButton,
    Container,
    VerticalList,
    RuntimeContext,
    Utils,
    PlayButton,
    RecordButton,
    CloseButton,
    RestartButton,
    DeleteButton,
    HorizontalList,
    RecordingManager,
    FeatureManager,
    Record,
    RecordDeletedEvent
) {
    'use strict';

    var application = RuntimeContext.getCurrentApplication(),
        recordingManager = RecordingManager.getInstance(),
        layoutObject = application.getLayout(),
        featureManager = FeatureManager.getInstance(),
        TYPES = {
            LIVE: 'LIVE',
            VOD: 'VOD',
            RESTART: 'RESTART',
            PROGRAM: 'PROGRAM',
            RECORDING: 'RECORDING'
        };

    return Container.extend({

        /**
         * Initialises the button.
         *
         * @param {Object} data - The data item.
         */
        init: function init (data) {
            init.base.call(this);
            this.addClass('episode-description');

            this._onRecordingChangeBound = Utils.bind(this._onRecordingChange, this);
            application.addEventListener('$record', this._onRecordingChangeBound);

            this.setDataItem(data);
            this._build(data);
        },

        /**
         * Builds the widget.
         *
         * @param {Object} data - The data.
         * @private
         */
        _build: function (data) {
            this._buildDuration(data.getDuration());
            this._buildDescription(data.getDescription());
            this._buildButtonsList();
            this._buildButtons();
        },

        /**
         * Builds the duration.
         *
         * @param {number} duration - Stream duration.
         * @private
         */
        _buildDuration: function (duration) {
            var streamduration = new Label({ text: Math.floor(duration / 60) + 'm', classNames: ['duration'] });

            this.appendChildWidget(streamduration);
        },

        /**
         * Builds the description.
         *
         * @param {Object} desc - The text to set.
         * @private
         */
        _buildDescription: function (desc) {
            var descriptionText = new Label({ text: desc, classNames: ['desc-text', 'description-trunction-style'] });

            descriptionText.setTruncationMode(Label.TRUNCATION_MODE_RIGHT_ELLIPSIS);
            descriptionText.setWidth(layoutObject.details.description.width);
            descriptionText.setMaximumLines(5);

            this.appendChildWidget(descriptionText);
        },

        /**
         * Builds the description.
         *
         * @param {Object} params - The button parameters.
         * @private
         */
        _buildButton: function (params) {
            var dataItem = params.data,
                config = params.config,
                button = this._button = new IconButton(config.id, config.text, config.classes);

            button.addClass(['kpn-button', 'play-now']);
            button.setDataItem(dataItem);

            this.appendChildWidget(button);
        },

        /**
         * Builds the buttons list.
         */
        _buildButtonsList: function () {
            var list = this._list = new HorizontalList('');

            list.addClass('buttons-list');

            this._buttons = [];

            this.appendChildWidget(list);
        },

        /**
         * Builds the buttons.
         *
         * @private
         */
        _buildButtons: function () {
            var item = this.getDataItem(),
                hasRecording = recordingManager.hasRecording(item.getId(), item.getSeriesId());

            if (item.isLive()) {
                this._addPlayButton();

                if ((hasRecording.episode || item instanceof Record)
                    && item.getStartTime() <= application.getDate() / 1000) {
                    this._addDeleteButton();
                }
            } else if (item.canPlay() && featureManager.isReplayEnabled() && item.isPlayable()) {

                this._addPlayButton();

                if ((hasRecording.episode || item instanceof Record)
                    && item.getStartTime() <= application.getDate() / 1000) {
                    this._addDeleteButton();
                }
            } else {
                if ((hasRecording.episode || item instanceof Record)
                    && item.getStartTime() <= application.getDate() / 1000) {
                    this._addDeleteButton();
                }

                this._addCloseButton();
            }

            // Add record button for future assets
            if (!item.isLive() && item.getStartTime() >= application.getDate() / 1000) {
                this._addRecordButton();
            }
        },

        /**
         * Disables the buttons.
         */
        disable: function () {
            if (this.hasClass('enabled')) {
                this.removeClass('enabled');
            }

            Utils.each(this._buttons, function (button) {
                button.setDisabled(true);
            });
        },

        /**
         * Enables the buttons.
         */
        enable: function () {
            if (!this.hasClass('enabled')) {
                this.addClass('enabled');
            }

            Utils.each(this._buttons, function (button) {
                button.setDisabled(false);
            });

            this._buttons[0].focus();
        },

        /**
         * Adds a play button.
         *
         * @private
         */
        _addPlayButton: function () {
            var button = this._playButton = new PlayButton();

            button.setDataItem(this.getDataItem());
            button.setDisabled(true);
            this._buttons.push(button);

            this._list.appendChildWidget(button);
        },

        /**
         * Adds a record button.
         *
         * @private
         */
        _addRecordButton: function () {
            var dataItem = this.getDataItem(),
                button,
                hasRecording;

            if (!application.getUser().canRecord()) {
                return;
            }

            button = this._recordButton = new RecordButton();
            hasRecording = recordingManager.hasRecording(
                dataItem.getId(), dataItem.getSeriesId());

            button.setDataItem(dataItem);
            button.setDisabled(true);
            this._buttons.push(button);

            if (hasRecording.episode || hasRecording.series) {
                if (dataItem.isFutureItem() && dataItem.getContentType() === TYPES.RECORDING) {
                    button.cancelRecording();
                } else {
                    button.recording();
                }
            }

            this._list.appendChildWidget(button);
        },

        /**
         * Adds a close button.
         *
         * @private
         */
        _addCloseButton: function () {
            var button = this._closeButton = new CloseButton();

            button.setDataItem(this.getDataItem());
            button.setDisabled(true);
            this._buttons.push(button);

            this._list.appendChildWidget(button);
        },

        /**
         * Adds a restart button.
         *
         * @private
         */
        _addRestartButton: function () {
            var button = this._restartButton = new RestartButton();

            button.setDataItem(this.getDataItem());
            button.setDisabled(true);
            this._buttons.push(button);

            this._list.appendChildWidget(button);
        },

        /**
         * Adds a delete button.
         *
         * @private
         */
        _addDeleteButton: function () {
            var button = this._deleteButton = new DeleteButton();

            button.setDataItem(this.getDataItem());
            button.setDisabled(true);
            this._buttons.push(button);

            this._list.appendChildWidget(button);
        },

        /**
         * Disposes the widget.
         */
        dispose: function dispose () {
            dispose.base.call(this);
            application.removeEventListener('$record', this._onRecordingChangeBound);
        },

        /**
         * Gets executed when the recording state changes.
         *
         * @param {Object} e - The event data.
         * @private
         */
        _onRecordingChange: function (e) {
            if (e.target === 'series' && e.id === this.getDataItem().getSeriesId()) {
                this._updateRecordingStatus();
            } else if (e.target === 'episode' && e.id === this.getDataItem().getId()) {
                this._updateRecordingStatus();
            }
        },

        /**
         * Show / hide delete recording button based on given value.
         *
         * @param {boolean} enable - True if should be visible.
         * @private
         */
        _toggleDeleteRecordingButton: function (enable) {
            var buttons = this._buttons,
                deleteButton = this._deleteButton;

            if (enable) {
                deleteButton.show();
                deleteButton.setDisabled(false);
            } else {
                buttons[0].focus();
                deleteButton.hide();
                deleteButton.setDisabled(true);
            }
        },

        /**
         * Updates the recording status.
         *
         * @private
         */
        _updateRecordingStatus: function () {
            var dataItem = this.getDataItem(),
                hasRecording = recordingManager.hasRecording(
                    dataItem.getId(), dataItem.getSeriesId());

            if (this._recordButton) {
                if (hasRecording.episode || hasRecording.series) {

                    if (dataItem.isFutureItem() && dataItem.getContentType() === TYPES.RECORDING) {
                        this._recordButton.cancelRecording();
                    } else {
                        this._recordButton.recording();
                    }

                } else {
                    this._recordButton.record();
                }
            }

            if (this._deleteButton) {
                this._toggleDeleteRecordingButton(hasRecording.episode || hasRecording.series);
            }

            if (dataItem instanceof Record && !hasRecording.episode) {
                this.dispose();
                this.bubbleEvent(new RecordDeletedEvent(this.parentWidget));
            }
        }
    });
});