xblock_validation.js 3.15 KB
Newer Older
1 2
define(['jquery', 'underscore', 'js/views/baseview', 'gettext'],
    function($, _, BaseView, gettext) {
3 4 5 6 7 8 9 10 11 12 13 14
        /**
         * View for xblock validation messages as displayed in Studio.
         */
        var XBlockValidationView = BaseView.extend({

            // Takes XBlockValidationModel as a model
            initialize: function(options) {
                BaseView.prototype.initialize.call(this);
                this.template = this.loadTemplate('xblock-validation-messages');
                this.root = options.root;
            },

15
            render: function() {
16 17 18 19 20 21 22 23 24 25 26 27 28 29
                this.$el.html(this.template({
                    validation: this.model,
                    additionalClasses: this.getAdditionalClasses(),
                    getIcon: this.getIcon.bind(this),
                    getDisplayName: this.getDisplayName.bind(this)
                }));
                return this;
            },

            /**
             * Returns the icon css class based on the message type.
             * @param messageType
             * @returns string representation of css class that will render the correct icon, or null if unknown type
             */
30
            getIcon: function(messageType) {
31
                if (messageType === this.model.ERROR) {
32
                    return 'fa-exclamation-circle';
33 34
                }
                else if (messageType === this.model.WARNING || messageType === this.model.NOT_CONFIGURED) {
35
                    return 'fa-exclamation-triangle';
36 37 38 39 40 41 42 43 44
                }
                return null;
            },

            /**
             * Returns a display name for a message (useful for screen readers), based on the message type.
             * @param messageType
             * @returns string display name (translated)
             */
45
            getDisplayName: function(messageType) {
46 47 48
                if (messageType === this.model.WARNING || messageType === this.model.NOT_CONFIGURED) {
                    // Translators: This message will be added to the front of messages of type warning,
                    // e.g. "Warning: this component has not been configured yet".
49
                    return gettext('Warning');
50 51 52 53
                }
                else if (messageType === this.model.ERROR) {
                    // Translators: This message will be added to the front of messages of type error,
                    // e.g. "Error: required field is missing".
54
                    return gettext('Error');
55 56 57 58 59 60 61 62 63 64
                }
                return null;
            },

            /**
             * Returns additional css classes that can be added to HTML containing the validation messages.
             * Useful for rendering NOT_CONFIGURED in a special way.
             *
             * @returns string of additional css classes (or empty string)
             */
65 66 67 68
            getAdditionalClasses: function() {
                if (this.root && this.model.get('summary').type === this.model.NOT_CONFIGURED &&
                    this.model.get('messages').length === 0) {
                    return 'no-container-content';
69
                }
70
                return '';
71 72 73 74 75
            }
        });

        return XBlockValidationView;
    });