# Source: themes/ess-dive/views/EditorView.js

    /* global define */
    define(['underscore',
            'jquery',
            'views/BaseEditorView',
            'themes/ess-dive/views/SubmitDataButtonView'],
            function(_, $, BaseEditorView, SubmitDataButtonView) {

                var EditorView = BaseEditorView.extend({

                    /**
                     * Extend the function to check for files that are being uploaded
                     *
                     * If no files are being uploaded call the Base class save
                     * @param e
                     */
                    save: function(e){
                        // Search for unsaved data files
                        var dataModels = _.filter(MetacatUI.rootDataPackage.models, function(item){
                            return item.get("type") == "Data" && item.isNew();
                        });

                        // Are there any unsaved data models?
                        if (dataModels.length > 0)
                        {
                             MetacatUI.appView.showAlert("Wait for the data files to finish uploading before saving.",
        						"alert-error",
        						this.$el,
        						null,
        						{
        	        				remove: true
        						});

                        }
                        else {
                            // All datafiles have been uploaded.  It is OK
                            // to call the base class save function
                            BaseEditorView.prototype.save.apply(this,[e]);
                        }
                    },

                    /**
                     * Extend Base prototype to check for user's permissions to submit data
                     */
                    fetchModel: function() {

                        if (MetacatUI.appModel.get("readOnlyMode") != 'undefined' && MetacatUI.appModel.get("readOnlyMode")){
                            var container = $(document.createElement("div")).addClass("container center");
                            this.$el.html(container);
                            $(container).append("<h1>ESS-DIVE is in READ-ONLY mode</h1>" +
                                "For assistance contact " +
                                "<a href=\"mailto:ess-dive-support@lbl.gov\">ess-dive-support@lbl.gov</a>.")
                        }
                        else if (!MetacatUI.appUserModel.get("loggedIn")){
                            // Prompt the user to login
                            this.showSignIn();
                        }
                        else if (!MetacatUI.isAllowedtoSubmitData(MetacatUI.appUserModel) ||
                           !MetacatUI.appUserModel.get("email") )
                        {
                            var container = $(document.createElement("div")).addClass("container center");
                            this.$el.html(container);
                            if (!MetacatUI.isAllowedtoSubmitData(MetacatUI.appUserModel)) {
                                // User is not in a submitters group
                                $(container).append("<h1>You don't have permission to submit data.</h1>").append(
                                    new SubmitDataButtonView().render().el)

                            } else if (!MetacatUI.appUserModel.get("email") ) {
                                // User must provide an email address in order to submit data
                                $(container).append("<h1>Please provide an email address to submit data</h1>" +
                                    "<a class='btn btn-primary' href='/profile/"+MetacatUI.appUserModel.get("username")
                                    +"/s=settings' >Update Email Address</a>");
                            }
                        }
                        else {

                            // User is allowed to submit data
                            BaseEditorView.prototype.fetchModel.apply(this);
                        }

                    },
                    /**
                     * Overrides the Base prototype
                     *
                     * Close the view and its sub views if the user is
                     * allowed to submit data. Otherwise skip
                     * the close event
                     * */
                    onClose: function () {

                         if (MetacatUI.isAllowedtoSubmitData(MetacatUI.appUserModel) &&
                             MetacatUI.appUserModel.get("email"))
                         {
                             BaseEditorView.prototype.onClose.apply(this);
                         }

                    },

                });
                return EditorView;
            }
    );