group_configurations.js 2.95 KB
Newer Older
1
define([
2
    'jquery', 'underscore', 'gettext', 'js/views/pages/base_page',
3
    'js/views/group_configurations_list', 'js/views/content_group_list'
4
],
5
function ($, _, gettext, BasePage, GroupConfigurationsListView, ContentGroupListView) {
6
    'use strict';
7
    var GroupConfigurationsPage = BasePage.extend({
8
        initialize: function(options) {
9
            BasePage.prototype.initialize.call(this);
10 11 12 13 14 15 16 17 18 19
            this.experimentsEnabled = options.experimentsEnabled;
            if (this.experimentsEnabled) {
                this.experimentGroupConfigurations = options.experimentGroupConfigurations;
                this.experimentGroupsListView = new GroupConfigurationsListView({
                    collection: this.experimentGroupConfigurations
                });
            }
            this.contentGroupConfiguration = options.contentGroupConfiguration;
            this.cohortGroupsListView = new ContentGroupListView({
                collection: this.contentGroupConfiguration.get('groups')
20 21 22
            });
        },

23
        renderPage: function() {
24
            var hash = this.getLocationHash();
25
            if (this.experimentsEnabled) {
26
                this.$('.wrapper-groups.experiment-groups').append(this.experimentGroupsListView.render().el);
27
            }
28
            this.$('.wrapper-groups.content-groups').append(this.cohortGroupsListView.render().el);
29
            this.addWindowActions();
30 31
            if (hash) {
                // Strip leading '#' to get id string to match
32
                this.expandConfiguration(hash.replace('#', ''));
33
            }
34
            return $.Deferred().resolve().promise();
35 36
        },

37
        addWindowActions: function () {
38 39 40 41
            $(window).on('beforeunload', this.onBeforeUnload.bind(this));
        },

        onBeforeUnload: function () {
42 43 44 45
            var dirty = this.contentGroupConfiguration.isDirty() ||
                (this.experimentsEnabled && this.experimentGroupConfigurations.find(function(configuration) {
                    return configuration.isDirty();
                }));
46

47
            if (dirty) {
48
                return gettext('You have unsaved changes. Do you really want to leave this page?');
49
            }
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        },

        /**
         * Helper method that returns url hash.
         * @return {String} Returns anchor part of current url.
         */
        getLocationHash: function() {
            return window.location.hash;
        },

        /**
         * Focus on and expand group configuration with peculiar id.
         * @param {String|Number} Id of the group configuration.
         */
        expandConfiguration: function (id) {
65
            var groupConfig = this.experimentsEnabled && this.experimentGroupConfigurations.findWhere({
66 67 68 69 70 71 72
                id: parseInt(id)
            });

            if (groupConfig) {
                groupConfig.set('showGroups', true);
                this.$('#' + id).focus();
            }
73 74 75 76 77
        }
    });

    return GroupConfigurationsPage;
}); // end define();