<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
$(function() {

  /**
   * view model used for users grid
   */
  usersViewModel=function() {
    this.users = ko.observableArray([]);
    var self = this;

    this.loadUsers = function() {
      $.ajax("restServices/redbackServices/userService/getUsers", {
          type: "GET",
          async: false,
          dataType: 'json',
          success: function(data) {
            var mappedUsers = $.map(data.user, function(item) {
              return mapUser(item);
            });
            self.users(mappedUsers);
          }
        }
      );
    };
    this.gridViewModel = new ko.simpleGrid.viewModel({
      data: this.users,
      viewModel: this,
      columns: [
        {
          headerText: "User Name",
          rowText: "username"},
        {
          headerText: "<NAME>",
          rowText: "fullName"},
        {
          headerText: "Email",
          rowText: "email"}
      ],
      pageSize: 5
    });

    this.addUser=function() {
      clearUserMessages();
      window.redbackModel.createUser=true;
      $("#createUserForm").html("");
      $("#main-content #user-edit").remove();
      $('#main-content #user-create').show();
      ko.renderTemplate("redback/user-edit-tmpl", new User(), null, $("#createUserForm").get(0),"replaceChildren");
      $("#main-content #createUserForm #user-create").delegate("#user-create-form-cancel-button", "click keydown", function(e) {
        e.preventDefault();
        activateUsersGridTab();
      });
      $("#user-create").validate({
        rules: {
          confirmPassword: {
            equalTo: <PASSWORD>"
          }
        },
        showErrors: function(validator, errorMap, errorList) {
          customShowError(validator,errorMap,errorMap);
        }
      });
      $("#main-content #createUserForm #user-create").delegate("#user-create-form-register-button", "click keydown", function(e) {
        e.preventDefault();
      });

      // desactivate roles pill when adding user
      $("#edit_user_details_pills_headers").hide();

    };

    this.lock = function(user){
      clearUserMessages();
      user.locked(true);
      window.redbackModel.createUser=false;
      user.save();
    }

    this.unlock = function(user){
      clearUserMessages();
      user.locked(false);
      window.redbackModel.createUser=false;
      user.save();
    }

    this.passwordChangeRequire = function(user,forceChangedPassword){
      clearUserMessages();
      user.passwordChangeRequired(forceChangedPassword);
      window.redbackModel.createUser=false;
      user.save();
    }

    this.sortByName = function() {
      this.users.sort(function(a, b) {
          return a.username < b.username ? -1 : 1;
      });
    };


    this.editUserBox=function(user) {
      window.redbackModel.createUser=false;
      clearUserMessages();
      activateUsersEditTab();
      // if already created before
      //$("#main-content #createUserForm").accordion("destroy");
      $("#main-content #createUserForm").html("");
      $("#main-content #createUserForm").attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}');

      var viewModel = new UserViewModel(user);

      ko.applyBindings(viewModel,$("#main-content #createUserForm").get(0));

      $("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("user.edit"));

      $("#main-content #user-create").delegate("#user-create-form-cancel-button", "click keydown", function(e) {
        e.preventDefault();
        activateUsersGridTab();
      });

      $("#main-content #user-create").validate({
        rules: {
          confirmPassword: {
            equalTo: <PASSWORD>"
          }
        },
        showErrors: function(validator, errorMap, errorList) {
          customShowError(validator,errorMap,errorMap);
        }
      });
      $("#main-content #user-create").delegate("#user-create-form-save-button", "click keydown", function(e) {
        e.preventDefault();
        var valid = $("#user-create").valid();
        if (!valid) {
            return;
        }
        user.update();
      });

      $( "#main-content #user-edit-roles-view" ).append(smallSpinnerImg());
      $.ajax("restServices/redbackServices/roleManagementService/getEffectivelyAssignedRoles/"+encodeURIComponent(user.username()), {
          type: "GET",
          async: false,
          dataType: 'json',
          success: function(data) {
            //$("#user-edit-roles-view").show();
            var mappedRoles = $.map(data.role, function(item) {
              return mapRole(item);
            });
            viewModel.roles=mappedRoles;
            $("#main-content #user-edit-roles-view").attr("data-bind",'template: {name:"user_edit_roles_tmpl"}');
            ko.applyBindings(viewModel,$("#user-edit-roles-view").get(0));
            $("#main-content #edit-user-details-pills-headers").pills();
          }
        }
      );

    }
  }

  UserViewModel=function(user) {
    this.user=user;
    this.roles = ko.observableArray(new Array());
  }

  /**
   * called from the menu to display tabs with users grid
    */
  displayUsersGrid=function() {<commit_msg>fix changed id<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
$(function() {

  /**
   * view model used for users grid
   */
  usersViewModel=function() {
    this.users = ko.observableArray([]);
    var self = this;

    this.loadUsers = function() {
      $.ajax("restServices/redbackServices/userService/getUsers", {
          type: "GET",
          async: false,
          dataType: 'json',
          success: function(data) {
            var mappedUsers = $.map(data.user, function(item) {
              return mapUser(item);
            });
            self.users(mappedUsers);
          }
        }
      );
    };
    this.gridViewModel = new ko.simpleGrid.viewModel({
      data: this.users,
      viewModel: this,
      columns: [
        {
          headerText: "User Name",
          rowText: "username"},
        {
          headerText: "Full Name",
          rowText: "fullName"},
        {
          headerText: "Email",
          rowText: "email"}
      ],
      pageSize: 5
    });

    this.addUser=function() {
      clearUserMessages();
      window.redbackModel.createUser=true;
      $("#createUserForm").html("");
      $("#main-content #user-edit").remove();
      $('#main-content #user-create').show();
      ko.renderTemplate("redback/user-edit-tmpl", new User(), null, $("#createUserForm").get(0),"replaceChildren");
      $("#main-content #createUserForm #user-create").delegate("#user-create-form-cancel-button", "click keydown", function(e) {
        e.preventDefault();
        activateUsersGridTab();
      });
      $("#user-create").validate({
        rules: {
          confirmPassword: {
            equalTo: <PASSWORD>"
          }
        },
        showErrors: function(validator, errorMap, errorList) {
          customShowError(validator,errorMap,errorMap);
        }
      });
      $("#main-content #createUserForm #user-create").delegate("#user-create-form-register-button", "click keydown", function(e) {
        e.preventDefault();
      });

      // desactivate roles pill when adding user
      $("#edit_user_details_pills_headers").hide();

    };

    this.lock = function(user){
      clearUserMessages();
      user.locked(true);
      window.redbackModel.createUser=false;
      user.save();
    }

    this.unlock = function(user){
      clearUserMessages();
      user.locked(false);
      window.redbackModel.createUser=false;
      user.save();
    }

    this.passwordChangeRequire = function(user,forceChangedPassword){
      clearUserMessages();
      user.passwordChangeRequired(forceChangedPassword);
      window.redbackModel.createUser=false;
      user.save();
    }

    this.sortByName = function() {
      this.users.sort(function(a, b) {
          return a.username < b.username ? -1 : 1;
      });
    };


    this.editUserBox=function(user) {
      window.redbackModel.createUser=false;
      clearUserMessages();
      activateUsersEditTab();
      // if already created before
      //$("#main-content #createUserForm").accordion("destroy");
      $("#main-content #createUserForm").html("");
      $("#main-content #createUserForm").attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}');

      var viewModel = new UserViewModel(user);

      ko.applyBindings(viewModel,$("#main-content #createUserForm").get(0));

      $("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("user.edit"));

      $("#main-content #user-create").delegate("#user-create-form-cancel-button", "click keydown", function(e) {
        e.preventDefault();
        activateUsersGridTab();
      });

      $("#main-content #user-create").validate({
        rules: {
          confirmPassword: {
            equalTo: <PASSWORD>"
          }
        },
        showErrors: function(validator, errorMap, errorList) {
          customShowError(validator,errorMap,errorMap);
        }
      });
      $("#main-content #user-create").delegate("#user-create-form-save-button", "click keydown", function(e) {
        e.preventDefault();
        var valid = $("#user-create").valid();
        if (!valid) {
            return;
        }
        user.update();
      });

      $( "#main-content #user-edit-roles-view" ).append(smallSpinnerImg());
      $.ajax("restServices/redbackServices/roleManagementService/getEffectivelyAssignedRoles/"+encodeURIComponent(user.username()), {
          type: "GET",
          async: false,
          dataType: 'json',
          success: function(data) {
            //$("#user-edit-roles-view").show();
            var mappedRoles = $.map(data.role, function(item) {
              return mapRole(item);
            });
            viewModel.roles=mappedRoles;
            $("#main-content #user-edit-roles-view").attr("data-bind",'template: {name:"user_edit_roles_tmpl"}');
            ko.applyBindings(viewModel,$("#user-edit-roles-view").get(0));
            $("#main-content #edit_user_details_pills_headers").pills();

          }
        }
      );

    }
  }

  UserViewModel=function(user) {
    this.user=user;
    this.roles = ko.observableArray(new Array());
  }

  /**
   * called from the menu to display tabs with users grid
    */
  displayUsersGrid=function() {
{'fraction_non_alphanumeric': 0.14775681341719077, 'fraction_numerical': 0.002012578616352201, 'mean_word_length': 2.5148835838491013, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5904975', 'n_tokens_mistral': 3355, 'n_tokens_neox': 3353, 'n_words': 677}