module_edit.js 4.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
(function() {
    'use strict';

    var __hasProp = {}.hasOwnProperty,
        __extends = function(child, parent) {
            var key;
            for (key in parent) {
                if (__hasProp.call(parent, key)) {
                    child[key] = parent[key];
                }
            }
            function Ctor() {
                this.constructor = child;
            }
            Ctor.prototype = parent.prototype;
            child.prototype = new Ctor();
            child.__super__ = parent.prototype;
            return child;
        };

    define(['jquery', 'underscore', 'gettext', 'xblock/runtime.v1', 'js/views/xblock', 'js/views/modals/edit_xblock'],
        function($, _, gettext, XBlock, XBlockView, EditXBlockModal) {
            var ModuleEdit = (function(_super) {
24
                __extends(ModuleEdit, _super);
25

26 27
                function ModuleEdit() {
                    return ModuleEdit.__super__.constructor.apply(this, arguments);
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
                ModuleEdit.prototype.tagName = 'li';

                ModuleEdit.prototype.className = 'component';

                ModuleEdit.prototype.editorMode = 'editor-mode';

                ModuleEdit.prototype.events = {
                    'click .edit-button': 'clickEditButton',
                    'click .delete-button': 'onDelete'
                };

                ModuleEdit.prototype.initialize = function() {
                    this.onDelete = this.options.onDelete;
                    return this.render();
                };

                ModuleEdit.prototype.loadDisplay = function() {
                    var xblockElement;
                    xblockElement = this.$el.find('.xblock-student_view');
                    if (xblockElement.length > 0) {
                        return XBlock.initializeBlock(xblockElement);
                    }
                };

                ModuleEdit.prototype.createItem = function(parent, payload, callback) {
                    var _this = this;
                    if (_.isNull(callback)) {
                        callback = function() {};
                    }
                    payload.parent_locator = parent;
                    return $.postJSON(this.model.urlRoot + '/', payload, function(data) {
                        _this.model.set({
                            id: data.locator
                        });
                        _this.$el.data('locator', data.locator);
                        _this.$el.data('courseKey', data.courseKey);
                        return _this.render();
                    }).success(callback);
                };

                ModuleEdit.prototype.loadView = function(viewName, target, callback) {
                    var _this = this;
                    if (this.model.id) {
                        return $.ajax({
                            url: '' + (decodeURIComponent(this.model.url())) + '/' + viewName,
                            type: 'GET',
                            cache: false,
                            headers: {
                                Accept: 'application/json'
                            },
                            success: function(fragment) {
                                return _this.renderXBlockFragment(fragment, target).done(callback);
                            }
                        });
                    }
                };

                ModuleEdit.prototype.render = function() {
                    var _this = this;
                    return this.loadView('student_view', this.$el, function() {
                        _this.loadDisplay();
                        return _this.delegateEvents();
92
                    });
93 94 95 96 97 98 99 100
                };

                ModuleEdit.prototype.clickEditButton = function(event) {
                    var modal;
                    event.preventDefault();
                    modal = new EditXBlockModal();
                    return modal.edit(this.$el, this.model, {
                        refresh: _.bind(this.render, this)
101
                    });
102
                };
103

104 105
                return ModuleEdit;
            })(XBlockView);
106
            return ModuleEdit;
107
        });
108
}).call(this);