add_xblock.js 3.47 KB
Newer Older
1 2 3
/**
 * This is a simple component that renders add buttons for all available XBlock template types.
 */
4 5 6
define(['jquery', 'underscore', 'gettext', 'js/views/baseview', 'common/js/components/utils/view_utils',
        'js/views/components/add_xblock_button', 'js/views/components/add_xblock_menu'],
    function($, _, gettext, BaseView, ViewUtils, AddXBlockButton, AddXBlockMenu) {
7 8
        var AddXBlockComponent = BaseView.extend({
            events: {
9 10
                'click .new-component .new-component-type .multiple-templates': 'showComponentTemplates',
                'click .new-component .new-component-type .single-template': 'createNewComponent',
11
                'click .new-component .cancel-button': 'closeNewComponent',
12
                'click .new-component-templates .new-component-template .button-component': 'createNewComponent',
13 14 15 16 17 18 19 20
                'click .new-component-templates .cancel-button': 'closeNewComponent'
            },

            initialize: function(options) {
                BaseView.prototype.initialize.call(this, options);
                this.template = this.loadTemplate('add-xblock-component');
            },

21
            render: function() {
22 23 24 25
                if (!this.$el.html()) {
                    var that = this;
                    this.$el.html(this.template({}));
                    this.collection.each(
26
                        function(componentModel) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
                            var view, menu;

                            view = new AddXBlockButton({model: componentModel});
                            that.$el.find('.new-component-type').append(view.render().el);

                            menu = new AddXBlockMenu({model: componentModel});
                            that.$el.append(menu.render().el);
                        }
                    );
                }
            },

            showComponentTemplates: function(event) {
                var type;
                event.preventDefault();
                event.stopPropagation();
                type = $(event.currentTarget).data('type');
                this.$('.new-component').slideUp(250);
                this.$('.new-component-' + type).slideDown(250);
46
                this.$('.new-component-' + type + ' div').focus();
47 48 49 50 51
            },

            closeNewComponent: function(event) {
                event.preventDefault();
                event.stopPropagation();
52
                type = $(event.currentTarget).data('type');
53 54
                this.$('.new-component').slideDown(250);
                this.$('.new-component-templates').slideUp(250);
55
                this.$('ul.new-component-type li button[data-type=' + type + ']').focus();
56 57 58 59 60 61
            },

            createNewComponent: function(event) {
                var self = this,
                    element = $(event.currentTarget),
                    saveData = element.data(),
62
                    oldOffset = ViewUtils.getScrollOffset(this.$el);
63 64
                event.preventDefault();
                this.closeNewComponent(event);
65
                ViewUtils.runOperationShowingMessage(
Bertrand Marron committed
66
                    gettext('Adding'),
67 68 69 70
                    _.bind(this.options.createComponent, this, saveData, element)
                ).always(function() {
                    // Restore the scroll position of the buttons so that the new
                    // component appears above them.
71
                    ViewUtils.setScrollOffset(self.$el, oldOffset);
72 73 74 75 76 77
                });
            }
        });

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