certificate_editor.js 5.93 KB
Newer Older
1 2
// Backbone Application View: Certificate Editor

3
define([
4 5 6 7 8 9
    'jquery',
    'underscore',
    'backbone',
    'gettext',
    'js/views/list_item_editor',
    'js/certificates/models/signatory',
10 11
    'js/certificates/views/signatory_editor',
    'text!templates/certificate-editor.underscore'
12 13
],
function($, _, Backbone, gettext,
14
         ListItemEditorView, SignatoryModel, SignatoryEditorView, certificateEditorTemplate) {
15
    'use strict';
asadiqbal committed
16 17 18 19

    // If signatories limit is required to specific value then we can change it.
    // However, Setting this limit to 100 that will allow PMs to add as many signatories as they want.
    var MAX_SIGNATORIES_LIMIT = 100;
20 21 22 23 24 25 26 27 28 29
    var CertificateEditorView = ListItemEditorView.extend({
        tagName: 'div',
        events: {
            'change .collection-name-input': 'setName',
            'change .certificate-description-input': 'setDescription',
            'change .certificate-course-title-input': 'setCourseTitle',
            'focus .input-text': 'onFocus',
            'blur .input-text': 'onBlur',
            'submit': 'setAndClose',
            'click .action-cancel': 'cancel',
30
            'click .action-add-signatory': 'addSignatory'
31 32
        },

33
        className: function() {
34 35 36 37 38 39 40 41 42 43 44
            // Determine the CSS class names for this model instance
            var index = this.model.collection.indexOf(this.model);

            return [
                'collection-edit',
                'certificates',
                'certificate-edit',
                'certificate-edit-' + index
            ].join(' ');
        },

45
        initialize: function(options) {
46
            // Set up the initial state of the attributes set for this model instance
47
            _.bindAll(this, 'onSignatoryRemoved', 'clearErrorMessage');
48 49
            this.max_signatories_limit = options.max_signatories_limit || MAX_SIGNATORIES_LIMIT;
            this.template = _.template(certificateEditorTemplate);
50
            this.eventAgg = _.extend({}, Backbone.Events);
51 52
            this.eventAgg.bind('onSignatoryRemoved', this.onSignatoryRemoved);
            this.eventAgg.bind('onSignatoryUpdated', this.clearErrorMessage);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
            ListItemEditorView.prototype.initialize.call(this);
        },

        onSignatoryRemoved: function() {
            // Event handler for model deletions
            this.model.setOriginalAttributes();
            this.render();
        },

        clearErrorMessage: function() {
            // Hides away the error message displayed during field validations
            this.$('.certificate-edit-error').remove();
        },

        render: function() {
            // Assemble the editor view for this model
            ListItemEditorView.prototype.render.call(this);
            var self = this;
            // Ensure we have at least one signatory associated with the certificate.
72
            this.model.get('signatories').each(function(modelSignatory) {
73 74 75 76 77 78 79 80 81 82 83 84 85
                var signatory_view = new SignatoryEditorView({
                    model: modelSignatory,
                    isEditingAllCollections: true,
                    eventAgg: self.eventAgg
                });
                self.$('div.signatory-edit-list').append($(signatory_view.render()));
            });
            this.disableAddSignatoryButton();
            return this;
        },

        addSignatory: function() {
            // Append a new signatory to the certificate model's signatories collection
86
            var signatory = new SignatoryModel({certificate: this.getSaveableModel()});  // eslint-disable-line max-len, no-unused-vars
87 88 89 90 91
            this.render();
        },

        disableAddSignatoryButton: function() {
            // Disable the 'Add Signatory' link if the constraint has been met.
92 93
            if (this.$('.signatory-edit-list > div.signatory-edit').length >= this.max_signatories_limit) {
                this.$('.action-add-signatory').addClass('disableClick');
94 95 96 97 98 99 100 101
            }
        },

        getTemplateOptions: function() {
            // Retrieves the current attributes/options for the model
            return {
                id: this.model.get('id'),
                uniqueId: _.uniqueId(),
102 103 104 105 106
                name: this.model.get('name'),
                description: this.model.get('description'),
                course_title: this.model.get('course_title'),
                org_logo_path: this.model.get('org_logo_path'),
                is_active: this.model.get('is_active'),
107 108 109 110 111 112 113 114 115 116 117 118 119 120
                isNew: this.model.isNew()
            };
        },

        getSaveableModel: function() {
            // Returns the current model instance
            return this.model;
        },

        setName: function(event) {
            // Updates the indicated model field (still requires persistence on server)
            if (event && event.preventDefault) { event.preventDefault(); }
            this.model.set(
                'name', this.$('.collection-name-input').val(),
121
                {silent: true}
122 123 124 125 126 127 128 129 130
            );
        },

        setDescription: function(event) {
            // Updates the indicated model field (still requires persistence on server)
            if (event && event.preventDefault) { event.preventDefault(); }
            this.model.set(
                'description',
                this.$('.certificate-description-input').val(),
131
                {silent: true}
132 133 134 135 136 137 138 139 140
            );
        },

        setCourseTitle: function(event) {
            // Updates the indicated model field (still requires persistence on server)
            if (event && event.preventDefault) { event.preventDefault(); }
            this.model.set(
                'course_title',
                this.$('.certificate-course-title-input').val(),
141
                {silent: true}
142 143 144 145 146 147 148 149 150 151 152 153 154
            );
        },

        setValues: function() {
            // Update the specified values in the local model instance
            this.setName();
            this.setDescription();
            this.setCourseTitle();
            return this;
        }
    });
    return CertificateEditorView;
});