Source: widgets/player/miniepg/currentprogram.js

define('application/widgets/player/miniepg/currentprogram', [
    'rofl/widgets/container',
    'rofl/widgets/label',
    'rofl/widgets/image',
    'application/utils',
    'antie/runtimecontext',
    'rofl/lib/l10n'
], function (
    Container,
    Label,
    Image,
    AppUtils,
    RuntimeContext,
    L10N
) {
    'use strict';


    var app = RuntimeContext.getCurrentApplication(),
        layout = app.getLayout().miniepg,
        l10n = L10N.getInstance();

    return Container.extend({

        /**
         * Initialises the widget.
         *
         * @param {Object} [data] - The data to be set.
         */
        init: function init (data) {

            init.base.call(this);

            this.addClass(['asset', 'currentprogram']);

            this._buildLockedBackground();
            this._buildBackgroundImg();
            this._buildProgress();

            this._logo = this.appendChildWidget(new Image('', ''));
            this._logo.addClass('logo');

            this._title = this.appendChildWidget(new Label({
                text: '',
                classNames: 'title'
            }));

            this._subtitle = this.appendChildWidget(new Label({
                text: '',
                classNames: 'subtitle'
            }));

            this._buildGradientBottom();

            this.setDataItem(data);
        },

        /**
         * Builds the background.
         *
         * @private
         */
        _buildBackgroundImg: function () {
            var background = this._background = new Container();

            background.addClass('background');

            this.appendChildWidget(background);
        },

        /**
         * Builds the locked background.
         *
         * @private
         */
        _buildLockedBackground: function () {
            var lockedContainer = new Container(),
                lockedIcon = new Container();

            lockedContainer.addClass('locked-container');
            lockedIcon.addClass(['locked-icon', 'icon-lock']);

            lockedContainer.appendChildWidget(lockedIcon);
            this._lockedContainer = lockedContainer;

            this.appendChildWidget(lockedContainer);
        },

        /**
         * Builds the gradient.
         *
         * @private
         */
        _buildGradientBottom: function () {
            var gradient = new Container();

            gradient.addClass('gradient-bottom');

            this.appendChildWidget(gradient);
        },

        /**
         * Builds the progress bar.
         *
         * @private
         */
        _buildProgress: function () {
            var bar = new Container(),
                progress = this._progress = new Container();

            bar.addClass('progressbar');
            progress.addClass('progress');
            bar.appendChildWidget(progress);

            this.appendChildWidget(bar);
        },

        /**
         * Checks whether asset is child protected.
         *
         * @param {Object} item - The data item.
         *
         * @returns {boolean} - True if is child protected.
         */
        isLocked: function (item) {
            return AppUtils.isBroadcastLocked(item || this.getDataItem());
        },

        /**
         * Sets the progress.
         */
        setProgress: function () {
            var self = this,
                startTime = new Date(this.getDataItem().getStartTime() * 1000),
                endTime = new Date(this.getDataItem().getEndTime() * 1000),
                _setProgress;

            clearInterval(this._progressInterval);

            _setProgress = function () {
                var now = app.getDate(),
                    percentage,
                    allTime,
                    passedTime;

                allTime = endTime - startTime;
                passedTime = now - startTime;
                percentage = Math.floor((passedTime * 100) / allTime);

                self._progress.setStyleTo('width', percentage + '%');

                if (percentage >= 100) {
                    clearInterval(self._progressInterval);

                    // Nasty check to avoid items that are removed from updating.
                    if (self.parentWidget && self.parentWidget.parentWidget) {
                        self.parentWidget.loadNext();
                    }
                }
            };

            _setProgress();

            this._progressInterval = setInterval(_setProgress, 5 * 1000);
        },

        /**
         * Sets the data item.
         *
         * @param {Object} item - The data item.
         */
        setData: function (item) {
            var showLockedTitle = AppUtils.showLockedTitle(),
                background = item.getImageUrl(Math.round(layout.itemWidth) + 'x' + Math.round(layout.itemHeight)),
                subtitle = new Date(item.getStartTime() * 1000).format('H:i')
                    + ' | ' + Math.floor(item.getDuration() / 60) + 'm',
                title = item.getTitleBrief();

            this.setDataItem(item);

            if (AppUtils.isBroadcastLocked(item)) {
                this._isLocked = true;

                if (!showLockedTitle) {
                    title = l10n.get('asset.locked');
                }
            } else {
                this._lockedContainer.hide({
                    skipAnim: true
                });
                this._background.setStyleTo('background-image', 'url(' + background + ')');
            }

            this._title.setText(title);
            this._subtitle.setText(subtitle);
            this._logo.setSrc(item.getChannel().getImage('128'));
            this.setProgress();
        },

        /**
         * Renders the widget.
         *
         * @param {Object} device - The device.
         * @returns {Object} - The output element.
         */
        render: function render (device) {
            var outputElement;

            if (this.rendered) {
                return this.outputElement;
            }

            this.rendered = true;
            outputElement = render.base.call(this, device);

            this.setData(this.getDataItem());

            return outputElement;
        },

        /**
         * For the rendered assets remove the locked container and set backrgound and title.
         */
        unlock: function () {
            var data = this.getDataItem(),
                background;

            if (!this._isLocked || !this.rendered) {
                return;
            }

            background = data.getImageUrl(Math.round(layout.itemWidth) + 'x' + Math.round(layout.itemHeight));

            this._lockedContainer.hide();
            this._background.setStyleTo('background-image', 'url(' + background + ')');
            this._title = data.getTitleBrief();
        },

        /**
         * Locks the asset.
         */
        lock: function () {
            var showLockedTitle = AppUtils.showLockedTitle();

            if (!this.rendered || this._isLocked) {
                return;
            }

            this._lockedContainer.show();
            this._background.setStyleTo('background-image', '""');

            if (!showLockedTitle) {
                this._title.setText(l10n.get('asset.locked'));
            }
        }
    });
});