<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() {

  //-----------------------------------------
  // browse part
  //-----------------------------------------

  BrowseViewModel=function(browseResultEntries,parentBrowseViewModel,groupId){
    var self=this;
    this.browseResultEntries=browseResultEntries;
    this.parentBrowseViewModel=parentBrowseViewModel;
    this.groupId=groupId;
    displayGroupId=function(groupId){
      displayGroupDetail(groupId,self);
    }
    displayParentGroupId=function(){
      $.log("called displayParentGroupId groupId:"+self.parentBrowseViewModel.groupId);
      // if null parent is root level
      if (self.parentBrowseViewModel.groupId){
        displayGroupDetail(self.parentBrowseViewModel.groupId,self.parentBrowseViewModel);
      } else {
        browseRoot();
      }
    }

    displayProjectEntry=function(id){
      // value org.apache.maven/maven-archiver
      // split this org.apache.maven and maven-archiver
      var values = id.split(".");
      var groupId="";
      for (var i = 0;i<values.length-1;i++){
        groupId+=values[i];
        if (i<values.length-2)groupId+=".";
      }
      var artifactId=values[values.length-1];
      displayArtifactDetail(groupId,artifactId,self);

    }

    breadCrumbEntries=function(){
      // root level ?
      if (!self.parentBrowseViewModel) return [];
      return calculateBreadCrumbEntries(self.groupId);
    }

    displayEntry=function(value){
      if (self.groupId){
        return value.substr(self.groupId.length+1,value.length-self.groupId.length);
      }
      return value;
    }
  }

  calculateBreadCrumbEntries=function(groupId){
    var splitted = groupId.split(".");
    var breadCrumbEntries=[];
    var curGroupId="";
    for (var i=0;i<splitted.length;i++){
      curGroupId+=splitted[i];
      breadCrumbEntries.push(new BreadCrumbEntry(curGroupId,splitted[i]));
      curGroupId+="."
    }
    return breadCrumbEntries;
  }

  displayGroupDetail=function(groupId,parentBrowseViewModel,restUrl){
    var mainContent = $("#main-content");
    var browseResult=mainContent.find("#browse_result");
    browseResult.show();
    mainContent.find("#browse_artifact" ).hide();
    var browseBreadCrumb=mainContent.find("#browse_breadcrumb");
    mainContent.find("#main_browse_result_content").hide( "slide", {}, 300,
        function(){
          browseResult.html(mediumSpinnerImg());
          browseBreadCrumb.html(smallSpinnerImg());
          mainContent.find("#main_browse_result_content" ).show();
          var url = restUrl ? restUrl : "restServices/archivaServices/browseService/browseGroupId/"+encodeURIComponent(groupId);
          $.ajax(url, {
            type: "GET",
            dataType: 'json',
            success: function(data) {
              var browseResultEntries = mapbrowseResultEntries(data);
              var browseViewModel = new BrowseViewModel(browseResultEntries,parentBrowseViewModel,groupId);
              ko.applyBindings(browseViewModel,browseBreadCrumb.get(0));
              ko.applyBindings(browseViewModel,browseResult.get(0));
              enableAutocompleBrowse(groupId);
            }
         });
        }
    );
  }

  ArtifactDetailViewModel=function(groupId,artifactId){
    var self=this;
    this.versions=[];
    this.projectVersionMetadata=null;
    this.groupId=groupId;
    this.artifactId=artifactId;
    breadCrumbEntries=function(){
      var entries = calculateBreadCrumbEntries(self.groupId);
      entries.push(new BreadCrumbEntry("foo",self.artifactId));
      return entries;
    }
    displayArtifactInfo=function(){
      if ($("#main-content #artifact-info:visible" ).length>0) {
        $("#main-content #artifact-info" ).hide();
      } else {
        $("#main-content #artifact-info" ).show();
      }


    }
  }

  displayArtifactDetail=function(groupId,artifactId,parentBrowseViewModel,restUrl){
    var artifactDetailViewModel=new ArtifactDetailViewModel(groupId,artifactId);
    var mainContent = $("#main-content");
    mainContent.find("#browse_result").hide();
    mainContent.find("#main_browse_result_content").hide("slide", {}, 300,function(){
      mainContent.find("#browse_breadcrumb").html(smallSpinnerImg());
      mainContent.find("#browse_artifact").show();
      mainContent.find("#browse_artifact").html(mediumSpinnerImg());
      mainContent.find("#main_browse_result_content").show();
      $.ajax("restServices/archivaServices/browseService/projectVersionMetadata/"+encodeURIComponent(groupId)+"/"+encodeURIComponent(artifactId), {
        type: "GET",
        dataType: 'json',
        success: function(data) {
          artifactDetailViewModel.projectVersionMetadata=mapProjectVersionMetadata(data);
          $.ajax("restServices/archivaServices/browseService/versionsList/"+encodeURIComponent(groupId)+"/"+encodeURIComponent(artifactId), {
            type: "GET",
            dataType: 'json',
            success: function(data) {
              artifactDetailViewModel.versions=mapVersionsList(data);
              ko.applyBindings(artifactDetailViewModel,mainContent.find("#browse_artifact").get(0));
              ko.applyBindings(artifactDetailViewModel,mainContent.find("#browse_breadcrumb").get(0));

             }
          });
        }
      });
    });
  }

  browseRoot=function(){
    displayGroupDetail(null,null,"restServices/archivaServices/browseService/rootGroups");
  }

  /**
   * call from menu entry to display root level
   */
  displayBrowse=function(freshView){
    screenChange()
    var mainContent = $("#main-content");
    if(freshView){
      mainContent.html($("#browse-tmpl" ).tmpl());
    }
    mainContent.find("#browse_result").html(mediumSpinnerImg());

    $.ajax("restServices/archivaServices/browseService/userRepositories", {
        type: "GET",
        dataType: 'json',
        success: function(data) {
          mainContent.find("#selected_repository" ).html($("#selected_repository_tmpl" ).tmpl({repositories:data}));
          var url="restServices/archivaServices/browseService/rootGroups"
          $.ajax(url, {
              type: "GET",
              dataType: 'json',
              success: function(data) {
                var browseResultEntries = mapbrowseResultEntries(data);
                $.log("size:"+browseResultEntries.length);
                var browseViewModel = new BrowseViewModel(browseResultEntries,null,null);
                ko.applyBindings(browseViewModel,mainContent.find("#browse_breadcrumb").get(0));
                ko.applyBindings(browseViewModel,mainContent.find("#browse_result").get(0));
                enableAutocompleBrowse();
              }
          });
        }
    });

  }

  changeBrowseRepository=function(){
    var selectedRepository=getSelectedBrowsingRepository();
    //displayBrowse(false,selectedRepository);
    displayGroupDetail(null,null,"restServices/archivaServices/browseService/rootGroups?repositoryId="+encodeURIComponent(selectedRepository));
  }

  getSelectedBrowsingRepository=function(){
    var selectedOption=$("#main-content #select_browse_repository option:selected" );
    if (selectedOption.length>0){
      var repoId=selectedOption.val();
      $.log("getSelectedBrowsingRepository:"+repoId);
      return repoId;
    }
    $.log("getSelectedBrowsingRepository:none");
  }

  enableAutocompleBrowse=function(groupId){
    // browse-autocomplete
    var url="restServices/archivaServices/browseService/rootGroups";
    if (groupId){
      url="restServices/archivaServices/browseService/browseGroupId/"+encodeURIComponent(groupId);
    }
    $( "#main-content #browse-autocomplete" ).autocomplete({
      minLength: 2,
			source: function(request, response){
        var query = "";
        if (request.term.indexOf('.')<0&&!groupId){
          // try with rootGroups then filtered
          $.get(url,
             function(data) {
               var browseResultEntries = mapbrowseResultEntries(data);

               var filetered = [];
               for(var i=0;i<browseResultEntries.length;i++){
                 if (browseResultEntries[i].name.startsWith(request.term)){
                   if (groupId){
                     $.log("groupId:"+groupId+",browseResultEntry.name:"+browseResultEntries[i].name);
                     if (browseResultEntries[i].name.startsWith(groupId)) {
                       filetered.push(browseResultEntries[i]);
                     }

                   } else {
                     filetered.push(browseResultEntries[i]);
                   }
                 }
               }
               response(filetered);

             }
          );
          return;
        }
        var dotEnd=request.term.endsWith(".");
        // org.apache. requets with org.apache
        // org.apa request with org before last dot and filter response with startsWith
          if (request.term.indexOf(".")>=0){
            if (dotEnd){
              query= groupId?groupId+'.'+request.term.substring(0, request.term.length-1):request.term.substring(0, request.term.length-1);
            } else {
              // substring before last
              query=groupId?groupId+'.'+request.term.substringBeforeLast("."):request.term.substringBeforeLast(".");
            }
          } else {
            query=groupId?groupId:request.term;
          }
        $.get("restServices/archivaServices/browseService/browseGroupId/"+encodeURIComponent(query),
           function(data) {
             var browseResultEntries = mapbrowseResultEntries(data);
             if (dotEnd){
              response(browseResultEntries);
             } else {
               var filetered = [];
               for(var i=0;i<browseResultEntries.length;i++){
                 if (groupId){<commit_msg>use the selected repository to browse content<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() {

  //-----------------------------------------
  // browse part
  //-----------------------------------------

  BrowseViewModel=function(browseResultEntries,parentBrowseViewModel,groupId){
    var self=this;
    this.browseResultEntries=browseResultEntries;
    this.parentBrowseViewModel=parentBrowseViewModel;
    this.groupId=groupId;
    displayGroupId=function(groupId){
      displayGroupDetail(groupId,self);
    }
    displayParentGroupId=function(){
      $.log("called displayParentGroupId groupId:"+self.parentBrowseViewModel.groupId);
      // if null parent is root level
      if (self.parentBrowseViewModel.groupId){
        displayGroupDetail(self.parentBrowseViewModel.groupId,self.parentBrowseViewModel);
      } else {
        browseRoot();
      }
    }

    displayProjectEntry=function(id){
      // value org.apache.maven/maven-archiver
      // split this org.apache.maven and maven-archiver
      var values = id.split(".");
      var groupId="";
      for (var i = 0;i<values.length-1;i++){
        groupId+=values[i];
        if (i<values.length-2)groupId+=".";
      }
      var artifactId=values[values.length-1];
      displayArtifactDetail(groupId,artifactId,self);

    }

    breadCrumbEntries=function(){
      // root level ?
      if (!self.parentBrowseViewModel) return [];
      return calculateBreadCrumbEntries(self.groupId);
    }

    displayEntry=function(value){
      if (self.groupId){
        return value.substr(self.groupId.length+1,value.length-self.groupId.length);
      }
      return value;
    }
  }

  calculateBreadCrumbEntries=function(groupId){
    var splitted = groupId.split(".");
    var breadCrumbEntries=[];
    var curGroupId="";
    for (var i=0;i<splitted.length;i++){
      curGroupId+=splitted[i];
      breadCrumbEntries.push(new BreadCrumbEntry(curGroupId,splitted[i]));
      curGroupId+="."
    }
    return breadCrumbEntries;
  }

  displayGroupDetail=function(groupId,parentBrowseViewModel,restUrl){
    var mainContent = $("#main-content");
    var browseResult=mainContent.find("#browse_result");
    browseResult.show();
    mainContent.find("#browse_artifact" ).hide();
    var browseBreadCrumb=mainContent.find("#browse_breadcrumb");
    mainContent.find("#main_browse_result_content").hide( "slide", {}, 300,
        function(){
          browseResult.html(mediumSpinnerImg());
          browseBreadCrumb.html(smallSpinnerImg());
          mainContent.find("#main_browse_result_content" ).show();
          var url = "";
          if (!restUrl) {
            url="restServices/archivaServices/browseService/browseGroupId/"+encodeURIComponent(groupId);
            var selectedRepo=getSelectedBrowsingRepository();
            if (selectedRepo){
              url+="?repositoryId="+selectedRepo;
            }
          }else {
            url=restUrl;
          }

          $.ajax(url, {
            type: "GET",
            dataType: 'json',
            success: function(data) {
              var browseResultEntries = mapbrowseResultEntries(data);
              var browseViewModel = new BrowseViewModel(browseResultEntries,parentBrowseViewModel,groupId);
              ko.applyBindings(browseViewModel,browseBreadCrumb.get(0));
              ko.applyBindings(browseViewModel,browseResult.get(0));
              enableAutocompleBrowse(groupId);
            }
         });
        }
    );
  }

  ArtifactDetailViewModel=function(groupId,artifactId){
    var self=this;
    this.versions=[];
    this.projectVersionMetadata=null;
    this.groupId=groupId;
    this.artifactId=artifactId;
    breadCrumbEntries=function(){
      var entries = calculateBreadCrumbEntries(self.groupId);
      entries.push(new BreadCrumbEntry("foo",self.artifactId));
      return entries;
    }
    displayArtifactInfo=function(){
      if ($("#main-content #artifact-info:visible" ).length>0) {
        $("#main-content #artifact-info" ).hide();
      } else {
        $("#main-content #artifact-info" ).show();
      }


    }
  }

  displayArtifactDetail=function(groupId,artifactId,parentBrowseViewModel,restUrl){
    var artifactDetailViewModel=new ArtifactDetailViewModel(groupId,artifactId);
    var mainContent = $("#main-content");
    mainContent.find("#browse_result").hide();
    mainContent.find("#main_browse_result_content").hide("slide", {}, 300,function(){
      mainContent.find("#browse_breadcrumb").html(smallSpinnerImg());
      mainContent.find("#browse_artifact").show();
      mainContent.find("#browse_artifact").html(mediumSpinnerImg());
      mainContent.find("#main_browse_result_content").show();
      $.ajax("restServices/archivaServices/browseService/projectVersionMetadata/"+encodeURIComponent(groupId)+"/"+encodeURIComponent(artifactId), {
        type: "GET",
        dataType: 'json',
        success: function(data) {
          artifactDetailViewModel.projectVersionMetadata=mapProjectVersionMetadata(data);
          $.ajax("restServices/archivaServices/browseService/versionsList/"+encodeURIComponent(groupId)+"/"+encodeURIComponent(artifactId), {
            type: "GET",
            dataType: 'json',
            success: function(data) {
              artifactDetailViewModel.versions=mapVersionsList(data);
              ko.applyBindings(artifactDetailViewModel,mainContent.find("#browse_artifact").get(0));
              ko.applyBindings(artifactDetailViewModel,mainContent.find("#browse_breadcrumb").get(0));

             }
          });
        }
      });
    });
  }

  browseRoot=function(){
    displayGroupDetail(null,null,"restServices/archivaServices/browseService/rootGroups");
  }

  /**
   * call from menu entry to display root level
   */
  displayBrowse=function(freshView){
    screenChange()
    var mainContent = $("#main-content");
    if(freshView){
      mainContent.html($("#browse-tmpl" ).tmpl());
    }
    mainContent.find("#browse_result").html(mediumSpinnerImg());

    $.ajax("restServices/archivaServices/browseService/userRepositories", {
        type: "GET",
        dataType: 'json',
        success: function(data) {
          mainContent.find("#selected_repository" ).html($("#selected_repository_tmpl" ).tmpl({repositories:data}));
          var url="restServices/archivaServices/browseService/rootGroups"
          $.ajax(url, {
              type: "GET",
              dataType: 'json',
              success: function(data) {
                var browseResultEntries = mapbrowseResultEntries(data);
                $.log("size:"+browseResultEntries.length);
                var browseViewModel = new BrowseViewModel(browseResultEntries,null,null);
                ko.applyBindings(browseViewModel,mainContent.find("#browse_breadcrumb").get(0));
                ko.applyBindings(browseViewModel,mainContent.find("#browse_result").get(0));
                enableAutocompleBrowse();
              }
          });
        }
    });

  }

  changeBrowseRepository=function(){
    var selectedRepository=getSelectedBrowsingRepository();
    //displayBrowse(false,selectedRepository);
    displayGroupDetail(null,null,"restServices/archivaServices/browseService/rootGroups?repositoryId="+encodeURIComponent(selectedRepository));
  }

  getSelectedBrowsingRepository=function(){
    var selectedOption=$("#main-content #select_browse_repository option:selected" );
    if (selectedOption.length>0){
      var repoId=selectedOption.val();
      $.log("getSelectedBrowsingRepository:"+repoId);
      return repoId;
    }
    $.log("getSelectedBrowsingRepository:none");
  }

  enableAutocompleBrowse=function(groupId){
    // browse-autocomplete
    var url="restServices/archivaServices/browseService/rootGroups";
    if (groupId){
      url="restServices/archivaServices/browseService/browseGroupId/"+encodeURIComponent(groupId);
    }
    var selectedRepo=getSelectedBrowsingRepository();
    if (selectedRepo){
      url+="?repositoryId="+selectedRepo;
    }
    $( "#main-content #browse-autocomplete" ).autocomplete({
      minLength: 2,
			source: function(request, response){
        var query = "";
        if (request.term.indexOf('.')<0&&!groupId){
          // try with rootGroups then filtered
          $.get(url,
             function(data) {
               var browseResultEntries = mapbrowseResultEntries(data);

               var filetered = [];
               for(var i=0;i<browseResultEntries.length;i++){
                 if (browseResultEntries[i].name.startsWith(request.term)){
                   if (groupId){
                     $.log("groupId:"+groupId+",browseResultEntry.name:"+browseResultEntries[i].name);
                     if (browseResultEntries[i].name.startsWith(groupId)) {
                       filetered.push(browseResultEntries[i]);
                     }

                   } else {
                     filetered.push(browseResultEntries[i]);
                   }
                 }
               }
               response(filetered);

             }
          );
          return;
        }
        var dotEnd=request.term.endsWith(".");
        // org.apache. requets with org.apache
        // org.apa request with org before last dot and filter response with startsWith
          if (request.term.indexOf(".")>=0){
            if (dotEnd){
              query= groupId?groupId+'.'+request.term.substring(0, request.term.length-1):request.term.substring(0, request.term.length-1);
            } else {
              // substring before last
              query=groupId?groupId+'.'+request.term.substringBeforeLast("."):request.term.substringBeforeLast(".");
            }
          } else {
            query=groupId?groupId:request.term;
          }
        var browseUrl="restServices/archivaServices/browseService/browseGroupId/"+encodeURIComponent(query);
        var selectedRepo=getSelectedBrowsingRepository();
        if (selectedRepo){
          browseUrl+="?repositoryId="+selectedRepo;
        }
        $.get(browseUrl,
           function(data) {
             var browseResultEntries = mapbrowseResultEntries(data);
             if (dotEnd){
              response(browseResultEntries);
             } else {
               var filetered = [];
               for(var i=0;i<browseResultEntries.length;i++){
                 if (groupId){
{'fraction_non_alphanumeric': 0.1308381022706295, 'fraction_numerical': 0.0030459664020675652, 'mean_word_length': 2.7856394129979036, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, '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': '6081589', 'n_tokens_mistral': 6247, 'n_tokens_neox': 5854, 'n_words': 932}