note_group.js 2.32 KB
Newer Older
1 2 3 4 5
;(function (define, undefined) {
'use strict';
define([
    'gettext', 'underscore', 'backbone'
], function (gettext, _, Backbone) {
6
    var GroupView, ChapterView;
7

8
    GroupView = Backbone.View.extend({
9 10 11 12
        tagName: 'section',
        id: function () {
            return 'note-section-' + _.uniqueId();
        },
13

14 15
        initialize: function (options) {
            this.options = _.extend({}, options);
16 17 18
            this.template = _.template(this.options.template);
            this.className = this.options.className;
        },
19 20 21

        render: function () {
            this.$el.prepend(this.template({
22
                displayName: this.options.displayName
23 24 25 26 27 28 29 30 31 32
            }));

            return this;
        },

        addChild: function (child) {
            this.$el.append(child);
        }
    });

33
    ChapterView = Backbone.View.extend({
34 35 36 37 38 39 40
        tagName: 'section',
        className: 'note-group',
        id: function () {
            return 'note-group-' + _.uniqueId();
        },
        template: _.template('<h3 class="course-title"><%- chapterName %></h3>'),

41
        initialize: function (options) {
42
            this.children = [];
43
            this.options = _.extend({}, options);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        },

        render: function () {
            var container = document.createDocumentFragment();
            this.$el.html(this.template({
                chapterName: this.options.chapter.display_name || ''
            }));
            _.each(this.children, function (section) {
                container.appendChild(section.render().el);
            });
            this.$el.append(container);

            return this;
        },

        addChild: function (sectionInfo) {
60 61 62 63 64 65 66
            var section = new GroupView(
                {
                    displayName: sectionInfo.display_name,
                    template: '<h4 class="course-subtitle"><%- displayName %></h4>',
                    className: "note-section"
                }
            );
67 68 69 70 71 72 73 74 75 76 77 78
            this.children.push(section);
            return section;
        },

        remove: function () {
            _.invoke(this.children, 'remove');
            this.children = null;
            Backbone.View.prototype.remove.call(this);
            return this;
        }
    });

79
    return {GroupView: GroupView, ChapterView: ChapterView};
80 81
});
}).call(this, define || RequireJS.define);