Source: components/modals/logout.js

define('application/components/modals/logout', [
    'application/components/modals/abstract',
    'rofl/lib/l10n',
    'rofl/lib/utils',
    'antie/runtimecontext',
    'application/managers/session',
    'rofl/analytics/web/google'
], function (
    ModalComponent,
    L10N,
    Utils,
    RuntimeContext,
    SessionManager,
    GoogleAnalytics
) {
    'use strict';

    var l10n = L10N.getInstance(),
        application = RuntimeContext.getCurrentApplication(),
        sessionManager = SessionManager.getInstance(),
        GA = GoogleAnalytics.getInstance(),
        modalconfig = {
            showGoBackButton: false,
            title: l10n.get('logout.modalconfig.title'),
            text: l10n.get('logout.modalconfig.text'),
            buttons: [
                {
                    label: l10n.get('logout.modalconfig.labelone'),
                    class: 'confirm_button',
                    button: 'confirmbutton',
                    labelname: 'confirmButtonLabel'
                },
                {
                    label: l10n.get('logout.modalconfig.labeltwo'),
                    class: 'action_button',
                    button: 'actionbutton',
                    labelname: 'actionButtonLabel'
                }
            ]
        };

    return ModalComponent.extend({

        /**
         * Initialises the component.
         */
        init: function init () {
            init.base.call(this);
        },

        /**
         * Before show event.
         */
        onBeforeShow: function onBeforeShow () {
            onBeforeShow.base.call(this, {
                args: modalconfig
            });
            GA.onPageView('logout');
            this._startLogoutPage = application.getDate();
        },

        /**
         * OnBeforeHide event.
         */
        onBeforeHide: function () {
            var duration = ((application.getDate() - this._startLogoutPage) / 60000).toFixed(2);

            GA.onEvent('page', 'time', {
                eventLabel: 'logout',
                eventValue: duration
            });
        },

        /**
         * On select function describes buttons behaviour.
         *
         * @param {event} event - Event passed on select.
         */
        _onSelect: function (event) {
            var logoutmodal = application.getComponent('modal'),
                player = application.getComponent('player'),
                menu = application.getComponent('menu'),
                main = application.getComponent('main');

            switch (event.target.id) {
                case 'actionbutton':
                    application.showLoader();
                    sessionManager.logout()
                        .then(function () {

                            // Reset the player history.
                            player._historyStack = [];

                            menu.back();
                            application.hideLoader();
                            application.route('landing', {
                                keepHistory: false
                            });
                            main.focus();
                        });
                    logoutmodal.hide();
                    break;
                case 'confirmbutton':
                    this.parentWidget.back();
                    if (Utils.isFunction(this._callback)) {
                        this._callback();
                    }
                    break;
            }
        }
    });
});