group_configuration_details.js 3.74 KB
Newer Older
1
define([
2
    'js/views/baseview', 'underscore', 'gettext', 'underscore.string'
3
],
4
function(BaseView, _, gettext, str) {
5 6
    'use strict';
    var GroupConfigurationDetails = BaseView.extend({
7
        tagName: 'div',
8
        events: {
9
            'click .edit': 'editConfiguration',
10 11 12 13
            'click .show-groups': 'showGroups',
            'click .hide-groups': 'hideGroups'
        },

14 15 16 17 18 19 20 21 22
        className: function () {
            var index = this.model.collection.indexOf(this.model);

            return [
                'group-configuration-details',
                'group-configuration-details-' + index
            ].join(' ');
        },

23 24 25 26 27 28 29 30 31 32
        initialize: function() {
            this.template = _.template(
                $('#group-configuration-details-tpl').text()
            );
            this.listenTo(this.model, 'change', this.render);
        },

        render: function() {
            var attrs = $.extend({}, this.model.attributes, {
                groupsCountMessage: this.getGroupsCountTitle(),
33 34
                usageCountMessage: this.getUsageCountTitle(),
                outlineAnchorMessage: this.getOutlineAnchorMessage(),
35 36 37 38 39 40 41
                index: this.model.collection.indexOf(this.model)
            });

            this.$el.html(this.template(attrs));
            return this;
        },

42 43 44 45 46
        editConfiguration: function(event) {
            if(event && event.preventDefault) { event.preventDefault(); }
            this.model.set('editing', true);
        },

47 48
        showGroups: function(event) {
            if(event && event.preventDefault) { event.preventDefault(); }
49 50 51
            this.model.set('showGroups', true);
        },

52 53
        hideGroups: function(event) {
            if(event && event.preventDefault) { event.preventDefault(); }
54 55 56 57 58 59
            this.model.set('showGroups', false);
        },

        getGroupsCountTitle: function () {
            var count = this.model.get('groups').length,
                message = ngettext(
60 61 62 63
                    /*
                        Translators: 'count' is number of groups that the group
                        configuration contains.
                    */
64 65 66 67 68
                    'Contains %(count)s group', 'Contains %(count)s groups',
                    count
                );

            return interpolate(message, { count: count }, true);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        },

        getUsageCountTitle: function () {
            var count = this.model.get('usage').length, message;

            if (count === 0) {
                message = gettext('Not in Use');
            } else {
                message = ngettext(
                    /*
                        Translators: 'count' is number of units that the group
                        configuration is used in.
                    */
                    'Used in %(count)s unit', 'Used in %(count)s units',
                    count
                );
            }

            return interpolate(message, { count: count }, true);
        },

        getOutlineAnchorMessage: function () {
            var message = gettext(
                    /*
                        Translators: 'outlineAnchor' is an anchor pointing to
                        the course outline page.
                    */
                    'This Group Configuration is not in use. Start by adding a content experiment to any Unit via the %(outlineAnchor)s.'
                ),
                anchor = str.sprintf(
                    '<a href="%(url)s" title="%(text)s">%(text)s</a>',
                    {
                            url: this.model.collection.outlineUrl,
                            text: gettext('Course Outline')
                    }
                );

            return str.sprintf(message, {outlineAnchor: anchor});
107 108 109 110 111
        }
    });

    return GroupConfigurationDetails;
});