cohort_discussions_inline.js 7.76 KB
Newer Older
1
;(function (define) {
2
    'use strict';
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    define(['jquery', 'underscore', 'backbone', 'gettext', 'js/groups/views/cohort_discussions', 'js/vendor/jquery.qubit'],
            function ($, _, Backbone, gettext, CohortDiscussionConfigurationView) {
                var InlineDiscussionsView = CohortDiscussionConfigurationView.extend({
                    events: {
                        'change .check-discussion-category': 'setSaveButton',
                        'change .check-discussion-subcategory-inline': 'setSaveButton',
                        'click .cohort-inline-discussions-form .action-save': 'saveInlineDiscussionsForm',
                        'change .check-all-inline-discussions': 'setAllInlineDiscussions',
                        'change .check-cohort-inline-discussions': 'setSomeInlineDiscussions'
                    },

                    initialize: function (options) {
                        this.template = _.template($('#cohort-discussions-inline-tpl').text());
                        this.cohortSettings = options.cohortSettings;
                    },

                    render: function () {
                        var alwaysCohortInlineDiscussions = this.cohortSettings.get('always_cohort_inline_discussions');

                        this.$('.cohort-inline-discussions-nav').html(this.template({
                            inlineDiscussionTopics: this.getInlineDiscussionsHtml(this.model.get('inline_discussions')),
                            alwaysCohortInlineDiscussions:alwaysCohortInlineDiscussions
                        }));

                        // Provides the semantics for a nested list of tri-state checkboxes.
                        // When attached to a jQuery element it listens for change events to
                        // input[type=checkbox] elements, and updates the checked and indeterminate
                        // based on the checked values of any checkboxes in child elements of the DOM.
                        this.$('ul.inline-topics').qubit();

                        this.setElementsEnabled(alwaysCohortInlineDiscussions, true);
                    },

                    /**
                    * Generate html list for inline discussion topics.
                    * @params {object} inlineDiscussions - inline discussions object from server.
                    * @returns {Array} - HTML for inline discussion topics.
                    */
                    getInlineDiscussionsHtml: function (inlineDiscussions) {
                        var categoryTemplate = _.template($('#cohort-discussions-category-tpl').html()),
                            entryTemplate = _.template($('#cohort-discussions-subcategory-tpl').html()),
                            isCategoryCohorted = false,
                            children = inlineDiscussions.children,
                            entries = inlineDiscussions.entries,
                            subcategories = inlineDiscussions.subcategories;

                        return _.map(children, function (name) {
                            var html = '', entry;
                            if (entries && _.has(entries, name)) {
                                entry = entries[name];
                                html = entryTemplate({
                                    name: name,
                                    id: entry.id,
                                    is_cohorted: entry.is_cohorted,
                                    type: 'inline'
                                });
                            } else { // subcategory
                                html = categoryTemplate({
                                    name: name,
                                    entries: this.getInlineDiscussionsHtml(subcategories[name]),
                                    isCategoryCohorted: isCategoryCohorted
                                });
                            }
                            return html;
                        }, this).join('');
                    },

                    /**
                    * Enable/Disable the inline discussion elements.
                    *
                    * Disables the category and sub-category checkboxes.
                    * Enables the save button.
                    */
                    setAllInlineDiscussions: function(event) {
                        event.preventDefault();
                        this.setElementsEnabled(($(event.currentTarget).prop('checked')), false);
                    },

                    /**
                    * Enables the inline discussion elements.
                    *
                    * Enables the category and sub-category checkboxes.
                    * Enables the save button.
                    */
                    setSomeInlineDiscussions: function(event) {
                        event.preventDefault();
                        this.setElementsEnabled(!($(event.currentTarget).prop('checked')), false);
                    },

                    /**
                    * Enable/Disable the inline discussion elements.
                    *
                    * Enable/Disable the category and sub-category checkboxes.
                    * Enable/Disable the save button.
                    * @param {bool} enable_checkboxes - The flag to enable/disable the checkboxes.
                    * @param {bool} enable_save_button - The flag to enable/disable the save button.
                    */
                    setElementsEnabled: function(enable_checkboxes, enable_save_button) {
                        this.setDisabled(this.$('.check-discussion-category'), enable_checkboxes);
                        this.setDisabled(this.$('.check-discussion-subcategory-inline'), enable_checkboxes);
                        this.setDisabled(this.$('.cohort-inline-discussions-form .action-save'), enable_save_button);
                    },

                    /**
                    * Enables the save button for inline discussions.
                    */
                    setSaveButton: function(event) {
                        this.setDisabled(this.$('.cohort-inline-discussions-form .action-save'), false);
                    },

                    /**
                    * Sends the cohorted_inline_discussions to the server and renders the view.
                    */
                    saveInlineDiscussionsForm: function (event) {
                        event.preventDefault();

                        var self = this,
                            cohortedInlineDiscussions = self.getCohortedDiscussions(
                                '.check-discussion-subcategory-inline:checked'
                            ),
                            fieldData= {
                                cohorted_inline_discussions: cohortedInlineDiscussions,
                                always_cohort_inline_discussions: self.$('.check-all-inline-discussions').prop('checked')
                            };

                        self.saveForm(self.$('.inline-discussion-topics'), fieldData)
                            .done(function () {
                                self.model.fetch()
                                    .done(function () {
                                        self.render();
                                        self.showMessage(gettext('Your changes have been saved.'), self.$('.inline-discussion-topics'));
                                    }).fail(function() {
                                        var errorMessage = gettext("We've encountered an error. Refresh your browser and then try again.");
                                        self.showMessage(errorMessage, self.$('.inline-discussion-topics'), 'error')
                                    });
                            });
                    }

            });
            return InlineDiscussionsView;
143
    });
144
}).call(this, define || RequireJS.define);