Source: components/modals/error.js

define('application/components/modals/error', [
    'application/components/modals/abstract',
    'rofl/lib/l10n',
    'rofl/lib/utils',
    'rofl/widgets/image',
    'application/widgets/pointerfocusablebutton',
    'rofl/widgets/label',
    'rofl/widgets/horizontallist',
    'antie/runtimecontext',
    'rofl/analytics/web/google',
    'antie/events/keyevent',
    'application/widgets/detail/iconbutton'
], function (
    ModalComponent,
    L10N,
    Utils,
    Image,
    Button,
    Label,
    HorizontalList,
    RuntimeContext,
    GoogleAnalytics,
    KeyEvent,
    IconButton
) {
    'use strict';

    var application;

    return ModalComponent.extend({

        /**
         * Initialises the component.
         *
         * @param {Object} initParams - Params to initialize error modal.
         * @param {Array} initParams.classNames - Error class names.
         */
        init: function init (initParams) {
            var classNames;

            initParams = initParams || {};
            classNames = (initParams.classNames || []).concat('error');
            application = RuntimeContext.getCurrentApplication();

            init.base.call(this, null, classNames);
        },

        /**
         * Before show event.
         *
         * @param {event} event - Config.
         */
        onBeforeShow: function onBeforeShow (event) {
            var args = event.args;

            // Reset modal
            this._resetModal();

            onBeforeShow.base.call(this, event);

            if (args.imgUrl) {
                this._buildImage(args);
            }

            if (application) {
                application.hideLoader();
            }

            this._backDisabled = args.canNotExit;
            this._closeApp = args.closeApp;

            this.addClass(args.type);
        },

        /**
         * Reset modal window.
         */
        _resetModal: function () {
            this.removeChildWidget();
            this._view.removeChildWidget(this._image);
            this._buttonsList.removeChildWidgets();

            if (this.hasClass('fullscreen')) {
                this.removeClass('fullscreen');
            }
        },

        /**
         * Builds confiramtion button.
         *
         * @param {Object} config - Config.
         */
        _createButtons: function (config) {
            var buttonsContainer = new HorizontalList(),
                args = config.args,
                type = args.type,
                buttons = args.button,
                buttonLabel,
                confirmRejectButton,
                singleButton;

            if (args.button.length > 1 || args.okButton) {
                Utils.each(buttons, Utils.bind(function (button) {
                    buttonLabel = new Label({ text: button.label });
                    confirmRejectButton = new Button(button.id);

                    confirmRejectButton.appendChildWidget(buttonLabel);
                    confirmRejectButton.addClass('kpn-button');
                    buttonsContainer.appendChildWidget(confirmRejectButton);
                    this._buttonsList.appendChildWidget(buttonsContainer);
                    this._buttonsList.focus();
                }, this));
            } else {
                singleButton = new IconButton(args.button.id, args.button.label, 'icon-back-v2');
                this._buttonsList.appendChildWidget(singleButton);
                this._buttonsList.focus();
            }

            if (type === 'exit-popup') {
                buttonsContainer.getChildWidgetByIndex(1).focus();
            }
        },

        /**
         * Builds confiramtion button.
         *
         * @param {Object} config - Config.
         */
        _buildImage: function (config) {
            var image = this._image = new Image();

            image.setOnErrorCallback(function () {
                image.hide({
                    skipAnim: true
                });
            });

            image.setSrc(config.imgUrl);
            this._view.appendChildWidget(image);
        },

        /**
         * KeyDown Event.
         *
         * @param {Object} e - The event.
         * @private
         */
        _onKeyDown: function (e) {

            switch (e.keyCode) {
                case KeyEvent.VK_BACK:

                    if (this._closeApp) {
                        this._onCloseApp();
                    } else if (!this._backDisabled) {
                        this._onClose();
                    }
                    break;

                case KeyEvent.VK_LEFT:
                case KeyEvent.VK_RIGHT:
                    e.stopPropagation();
                    e.preventDefault();
                    break;

                case KeyEvent.VK_CHANNEL_UP:
                case KeyEvent.VK_CHANNEL_DOWN:

                    // Prevent channel up/down for samsung keyhold behaviour when error is showing.
                    e.stopPropagation();
                    e.preventDefault();
                    break;
            }
        },

        /**
         * Select event handler.
         *,
         * @param {Event} e - Select event.
         */
        _onSelect: function (e) {
            switch (e.target.id) {
                case 'error-close-app-button':
                case 'exit-confirm-button':
                    this._onCloseApp();
                    break;
                case 'error-close-button':
                case 'exit-reject-button':
                    this._onClose();
                    break;
            }
        },

        /**
         * Closes the app.
         */
        _onCloseApp: function () {
            this.parentWidget.back();
            application.appDurationEvent();
            application.exit();
        }
    });
});