<commit_before>/**
 * Copyright (C) 2015 Laverna project Authors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
/* global define */
define([
    'q',
    'underscore',
    'marionette',
    'backbone.radio',
    'apps/settings/show/views/showView'
], function(Q, _, Marionette, Radio, View) {
    'use strict';

    /**
     * The main controller.
     *
     * Replies:
     * 1. channel: `AppSettings`, replies: `has:changes`
     *    if there are some changes, show a confirm message
     *
     * requests:
     * 1. channel: `AppSettings`, request: `activate`
     * 2. channel: `global`, request: `region:empty`
     * 3. channel: `global`, request: `region:show`
     * 4. channel: `configs`, request: `create:profile`
     * 5. channel: `configs`, request: `remove:profile`
     * 6. channel: `configs`, request: `save:objects`
     * 7. channel: `Confirm`, request: `start`
     * 8. channel: `uri`, request: `navigate`
     * 9. channel: `navbar`, request: `stop`
     */
    var Controller = Marionette.Object.extend({

        initialize: function(options) {
            _.bindAll(this, 'show', 'onFetch', 'requireView', 'redirect');

            options.tab  = options.tab || 'modules';
            this.options = options;
            this.changes = {};
            this.saves   = {};

            // Fetch configs
            Q.all([
                Radio.request('configs', 'get:all', options),
                Radio.request('configs', 'get:model', {
                    name    : 'useDefaultConfigs',
                    profile : options.profile
                }),
                Radio.request('configs', 'get:model', 'appProfiles')
            ])
            .spread(this.onFetch)
            .then(this.requireView)
            .fail(function(e) {
                console.error('Error:', e);
            });

            // Events, replies
            Radio.reply('AppSettings', 'has:changes', this.hasChanges, this);
        },

        onDestroy: function() {
            this.stopListening();
            Radio.stopReplying('AppSettings', 'has:changes');
            Radio.request('global', 'region:empty', 'content');
        },

        /**
         * Show the layout.
         */
        onFetch: function(configs, useDefault, profiles) {
            this.configs    = configs;
            this.profiles   = profiles;
            this.useDefault = useDefault;

            // Instantiate layout view and show it
            this.layout = new View(this.options);
            Radio.request('global', 'region:show', 'content', this.layout);
        },

        /**
         * Load a content view.
         */
        requireView: function() {
        },

        /**
         * Render the content view.
         */
        show: function(ContentView) {
            this.view = new ContentView({
                collection : this.configs,
                profiles   : this.profiles,
                useDefault : this.useDefault
            });

            this.layout.content.show(this.view);

            // Collection events
            this.listenTo(this.configs, 'new:value', this.onChange);

            // Layout events
            this.listenTo(this.layout, 'cancel', this.confirmRedirect);
            this.listenTo(this.layout, 'save', this.save);
        },

        onChange: function(data) {
            this.changes[data.name] = data;
        },

        /**
         * Reload the page when config 'useDefaultConfigs' is changed
         */
        onChangeConfigs: function(changes) {
            if (changes.useDefaultConfigs) {
                window.location.reload();
            }
        },

        save: function() {

            // Do nothing if there are not any changes
            if (_.isEmpty(this.changes)) {
                return;
            }

            Radio.channel('configs')
            .request('save:objects', this.changes, this.useDefault);

            this.saves = _.union(this.saves, this.changes);
            this.changes = {};
        },

        /**
         * Before closing the page, show a confirm message
         */
        confirmRedirect: function() {
            this.showConfirm(this.redirect);
        },

        /**
         * If there are any changes, show a confirm message.
         */
        hasChanges: function() {
            var defer = Q.defer();
            this.showConfirm(defer.resolve, defer.reject);
            return defer.promise;
        },

        showConfirm: function(onconfirm, onreject) {
            if (_.isEmpty(this.changes)) {
                return onconfirm();
            }

            Radio.request('Confirm', 'start', {
                content   : $.t('You have unsaved changes'),
                onconfirm : onconfirm,
                onreject  : onreject
            });
        },

    });

    return Controller;
});
<commit_msg>Close the settings when save is pressed<commit_after>/**
 * Copyright (C) 2015 Laverna project Authors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
/* global define */
define([
    'q',
    'underscore',
    'marionette',
    'backbone.radio',
    'apps/settings/show/views/showView'
], function(Q, _, Marionette, Radio, View) {
    'use strict';

    /**
     * The main controller.
     *
     * Replies:
     * 1. channel: `AppSettings`, replies: `has:changes`
     *    if there are some changes, show a confirm message
     *
     * requests:
     * 1. channel: `AppSettings`, request: `activate`
     * 2. channel: `global`, request: `region:empty`
     * 3. channel: `global`, request: `region:show`
     * 4. channel: `configs`, request: `create:profile`
     * 5. channel: `configs`, request: `remove:profile`
     * 6. channel: `configs`, request: `save:objects`
     * 7. channel: `Confirm`, request: `start`
     * 8. channel: `uri`, request: `navigate`
     * 9. channel: `navbar`, request: `stop`
     */
    var Controller = Marionette.Object.extend({

        initialize: function(options) {
            _.bindAll(this, 'show', 'onFetch', 'requireView', 'redirect');

            options.tab  = options.tab || 'modules';
            this.options = options;
            this.changes = {};
            this.saves   = {};

            // Fetch configs
            Q.all([
                Radio.request('configs', 'get:all', options),
                Radio.request('configs', 'get:model', {
                    name    : 'useDefaultConfigs',
                    profile : options.profile
                }),
                Radio.request('configs', 'get:model', 'appProfiles')
            ])
            .spread(this.onFetch)
            .then(this.requireView)
            .fail(function(e) {
                console.error('Error:', e);
            });

            // Events, replies
            Radio.reply('AppSettings', 'has:changes', this.hasChanges, this);
        },

        onDestroy: function() {
            this.stopListening();
            Radio.stopReplying('AppSettings', 'has:changes');
            Radio.request('global', 'region:empty', 'content');
        },

        /**
         * Show the layout.
         */
        onFetch: function(configs, useDefault, profiles) {
            this.configs    = configs;
            this.profiles   = profiles;
            this.useDefault = useDefault;

            // Instantiate layout view and show it
            this.layout = new View(this.options);
            Radio.request('global', 'region:show', 'content', this.layout);
        },

        /**
         * Load a content view.
         */
        requireView: function() {
        },

        /**
         * Render the content view.
         */
        show: function(ContentView) {
            this.view = new ContentView({
                collection : this.configs,
                profiles   : this.profiles,
                useDefault : this.useDefault
            });

            this.layout.content.show(this.view);

            // Collection events
            this.listenTo(this.configs, 'new:value', this.onChange);

            // Layout events
            this.listenTo(this.layout, 'cancel', this.confirmRedirect);
            this.listenTo(this.layout, 'save', this.save);
        },

        onChange: function(data) {
            this.changes[data.name] = data;
        },

        /**
         * Reload the page when config 'useDefaultConfigs' is changed
         */
        onChangeConfigs: function(changes) {
            if (changes.useDefaultConfigs) {
                window.location.reload();
            }
        },

        save: function() {

            // Do nothing if there are not any changes
            if (_.isEmpty(this.changes)) {
				this.confirmRedirect();
                return;
            }

            Radio.channel('configs')
            .request('save:objects', this.changes, this.useDefault);

            this.saves = _.union(this.saves, this.changes);
            this.changes = {};
			this.confirmRedirect();
        },

        /**
         * Before closing the page, show a confirm message
         */
        confirmRedirect: function() {
            this.showConfirm(this.redirect);
        },

        /**
         * If there are any changes, show a confirm message.
         */
        hasChanges: function() {
            var defer = Q.defer();
            this.showConfirm(defer.resolve, defer.reject);
            return defer.promise;
        },

        showConfirm: function(onconfirm, onreject) {
            if (_.isEmpty(this.changes)) {
                return onconfirm();
            }

            Radio.request('Confirm', 'start', {
                content   : $.t('You have unsaved changes'),
                onconfirm : onconfirm,
                onreject  : onreject
            });
        },

    });

    return Controller;
});

{'fraction_non_alphanumeric': 0.1302500247059986, 'fraction_numerical': 0.0035576638007708272, 'mean_word_length': 1.6082474226804124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3615925', 'n_tokens_mistral': 2796, 'n_tokens_neox': 2660, 'n_words': 644}