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

    /*global define */
    define(['jquery', 'underscore', 'backbone', 'clipboard', 'views/BaseUserView','collections/UserGroup', 'models/UserModel', 'views/SignInView', 'views/StatsView', 'views/DataCatalogView', 'views/GroupListView', 'text!templates/userProfile.html', 'text!templates/alert.html', 'text!templates/loading.html', 'text!templates/userProfileMenu.html', 'text!templates/userSettings.html', 'text!templates/noResults.html'],
        function ($, _, Backbone, Clipboard, BaseUserView, UserGroup, UserModel, SignInView, StatsView, DataCatalogView, GroupListView, userProfileTemplate, AlertTemplate, LoadingTemplate, ProfileMenuTemplate, SettingsTemplate, NoResultsTemplate) {
            'use strict';

            /*
             * UserView extends the MetacatUI UserView and names is BaseUserView
             *
             * A major view that displays a public profile for the user and a settings page for the logged-in user
             * to manage their account info, groups, identities, and API tokens.
             */
            var UserView = BaseUserView.extend({

                /**
                 * Override insertMembership
                 *
                 * If the user is *not* considered an ESS-DIVE admin,
                 * remove the create group list item.
                 */
                insertMembership: function() {

                    // Call the base class function
                    // This will create the menu on the profile page
                    // that lists groups the user is a member of in addition
                    // to adding a button to create a group.
                    BaseUserView.prototype.insertMembership.apply(this);

                    // Test whether a user is an admin, if not remove the "create-group"
                    // list item
                    var isAdmin = MetacatUI.checkIfMemberOf(
                                        MetacatUI.appUserModel.get("isMemberOf"),
                                        MetacatUI.officialGroups.adminGroups);
                    if (!isAdmin) {
                        // NOTE VCH 2018/01/09: Not ideal to remove create-group menu item from the
                        // DOM.  Need to explore other ways of managing access to this functionality.
                        $(".list-group.member-list").remove();
                        $("h5.list-group-item.list-group-header").remove();

                    }

                },

              showToken: function(){
                BaseUserView.prototype.showToken.apply(this);
                var isEssDiveAdmin = MetacatUI.checkIfMemberOf(MetacatUI.appUserModel.get("isMemberOf")
                                                              ,MetacatUI.officialGroups.adminGroups)
                var isAllowedtoSubmitData = MetacatUI.isAllowedtoSubmitData(MetacatUI.appUserModel)

                // Removes DataOne extra tabs for non-admin users
                if(isAllowedtoSubmitData && !isEssDiveAdmin){
                  $('a[href="#r-token-code-panel"]').closest("li").remove();
                  $('a[href="#matlab-token-code-panel"]').closest("li").remove();
                }
              },
                /*
                 * Override saveUser
                 *
                 * Gets the user account settings, updates the UserModel and saves this new info to the server
                 *
                 * Checks if email format is correctly provided
                 */
                saveUser: function(e) {
                    e.preventDefault()

                    var view = this,
                    container = this.$('[data-subsection="edit-account"] .content') || $(e.target).parent();

                    var email = this.$("#mod-email").val();

                    // Check if email has the right format using regex
                    if(email){
                        var email_pattern = MetacatUI.regexPatterns.emails;
                        // If email is not correct, then provide an error message
                        if(!email_pattern.test(email)) {
                            $(container).find(".loading").detach();
                            $(container).children().show();
                            var msg = "Please provide a correct email format.";
                            view.showAlert(msg, 'alert-error', container);
                            return
                        }
                    }

                    BaseUserView.prototype.saveUser.apply(this, [e]);
                }
            });

            return UserView;
        });