baseview.js 3.15 KB
Newer Older
1 2 3
define(['jquery', 'underscore', 'backbone', 'gettext', 'js/utils/handle_iframe_binding', 'js/utils/templates',
        'common/js/components/utils/view_utils'],
    function($, _, Backbone, gettext, IframeUtils, TemplateUtils, ViewUtils) {
4
        /*
5 6 7 8
         This view is extended from backbone to provide useful functionality for all Studio views.
         This functionality includes:
         - automatic expand and collapse of elements with the 'ui-toggle-expansion' class specified
         - additional control of rendering by overriding 'beforeRender' or 'afterRender'
9

10 11
         Note: the default 'afterRender' function calls a utility function 'iframeBinding' which modifies
         iframe src urls on a page so that they are rendered as part of the DOM.
12 13 14 15
         */

        var BaseView = Backbone.View.extend({
            events: {
16
                'click .ui-toggle-expansion': 'toggleExpandCollapse'
17 18
            },

19
            options: {
20 21 22
                // UX is moving towards using 'is-collapsed' in preference over 'collapsed',
                // but use the old scheme as the default so that existing code doesn't need
                // to be rewritten.
23 24 25
                collapsedClass: 'collapsed'
            },

26
            // override the constructor function
27 28
            constructor: function(options) {
                _.bindAll(this, 'beforeRender', 'render', 'afterRender');
29 30 31 32 33 34 35 36

                // Merge passed options and view's options property and
                // attach to the view's options property
                if (this.options) {
                    options = _.extend({}, _.result(this, 'options'), options);
                }
                this.options = options;

37
                var _this = this;
38
                this.render = _.wrap(this.render, function(render, options) {
39
                    _this.beforeRender();
40
                    render(options);
41 42 43 44
                    _this.afterRender();
                    return _this;
                });

45
                // call Backbone's own constructor
46 47
                Backbone.View.prototype.constructor.apply(this, arguments);
            },
48

49 50 51 52 53 54 55 56 57 58 59 60 61
            beforeRender: function() {
            },

            render: function() {
                return this;
            },

            afterRender: function() {
                IframeUtils.iframeBinding(this);
            },

            toggleExpandCollapse: function(event) {
                var target = $(event.target);
62 63
                // Don't propagate the event as it is possible that two views will both contain
                // this element, e.g. clicking on the element of a child view container in a parent.
64
                event.stopPropagation();
65
                event.preventDefault();
66
                ViewUtils.toggleExpandCollapse(target, this.options.collapsedClass);
67 68 69
            },

            /**
70 71 72 73 74
             * Loads the named template from the page, or logs an error if it fails.
             * @param name The name of the template.
             * @returns The loaded template.
             */
            loadTemplate: function(name) {
75
                return TemplateUtils.loadTemplate(name);
76 77 78 79 80
            }
        });

        return BaseView;
    });