<commit_before>window.Models = {};
window.Collections = {};
window.Views = {};

_.templateSettings = {
  interpolate: /\{\{(.+?)\}\}/g
};

Models.NLCDocumentGroup = Backbone.Model.extend({
  initialize: function() {}
});

Models.NLCDocument = Backbone.Model.extend({
  initialize: function() {}
});

Collections.NLCDocuments = Backbone.Collection.extend({
  model: Models.NLCDocument,
  initialize: function() {}
});

Collections.NLCDocumentGroups = Backbone.Collection.extend({
  model: Models.NLCDocumentGroup,
  initialize: function() {}
});

Views.NLCDocument = Backbone.View.extend({
  model: Models.NLCDocument,
  template: null,
  initialize: function() {
    this.template = _.template($('#link').html());
  },
  render: function() {
    this.$el.html(this.template(this.model.toJSON()));
    return this;
  },
  events: 
  {
    "click .list-group-item" : "toggleSelected"
  },
  toggleSelected: function() {
    if(this.model.get("isSelected")){
      this.model.set("isSelected", false);
      this.$el.find("input").prop('checked', false);
    } else {
      this.model.set("isSelected", true);
      this.$el.find("input").prop('checked', true);
    }
  }
});

Views.NLCDocumentGroup = Backbone.View.extend({
  model: Models.NLCDocumentGroup,
  template: null,
  initialize: function() {
    this.template = _.template($('#docGroup').html());
    this.collection = new Collections.NLCDocuments(this.model.get("links"));
  },
  render: function() {
    this.$el.html(this.template(this.model.toJSON()));
    var $links = this.$el.find(".list-group"); 
    $links.empty();
    this.collection.each(function(model) {
      var nlcDocument = new Views.NLCDocument({
        model: model
      });
      $links.append(nlcDocument.render().el);
    });
    return this;
  },
  events:
  {
    "click .panel-heading" : "toggleList"
  },
  toggleList: function(){
    this.$el.find(".list-group").slideToggle();
  }
});

Views.NLC = Backbone.View.extend({
  collection: null,
  initialize: function() {
    this.collection = new Collections.NLCDocumentGroups(this.getLinksFromCSV());
    this.mode = "email"; 
    this.render();
  },
  render: function() {
    this.$el.empty();
    var that = this;
    this.collection.each(function(group) {
      var nlcDocumentGroup = new Views.NLCDocumentGroup({
        model: group
      });
      that.$el.append(nlcDocumentGroup.render().el);
    });
  },
  getLinksFromCSV : function() {
    //Where we parse the csv and then return the Array of JSON

    // $.ajax({ //my ajax request
    //         url: "http://www.kcba.org/pbs/pdf/NLCMap.csv",
    //         type: "GET",
    //         dataType: "text",
    //         success : function(response){
    //          // csvToArray(response);
    //          arr1 = csvToArray(str1);
    //          console.log(arr1);
    //         }
    // });

    var str1 = "Document Title,Document Category,Link,Available to receive by mail\r\n" +
          "Client Intake Form - English,ADMINISTRATION,http://www.kcba.org/pbs/pdf/NLClinks/intakesheet.pdf,FALSE\r\n"+
          "Client Intake Form—Spanish,ADMINISTRATION,http://www.kcba.org/pbs/pdf/NLClinks/IntakeSheet-Spanish.pdf,FALSE";

    var arr1 = csvToArray(str1);
    // console.log(arr1);
    var links = arrToJson(arr1);
    console.log(links);

    //convert array to json
    function arrToJson(arr){
      var arr2 = [];
      for(var i=1; i<arr.length; i++){
        var obj = {};
        obj["links"] = {};
        obj["links"]["linkName"] = arr[i][0];
        obj["groupName"] = arr[i][1];
        obj["links"]["linkURL"] = arr[i][2];
        obj["links"]["mail"] = Boolean.valueOf(arr[i][3])();
        arr2.push(obj);
      }
      return arr2;
    }

    //Convert CSV to Array
    function csvToArray (strData, strDelimiter){
        // Check to see if the delimiter is defined. If not,
        // then default to comma.
        strDelimiter = (strDelimiter || ",");

        // Create a regular expression to parse the CSV values.
        var objPattern = new RegExp(
            (
                // Delimiters.
                "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

                // Quoted fields.
                "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

                // Standard fields.
                "([^\"\\" + strDelimiter + "\\r\\n]*))"
            ),
            "gi"
            );
        // Create an array to hold our data. Give the array
        // a default empty first row.
        var arrData = [[]];

        // Create an array to hold our individual pattern
        // matching groups.
        var arrMatches = null;


        // Keep looping over the regular expression matches
        // until we can no longer find a match.
        while (arrMatches = objPattern.exec(strData)) {

            // Get the delimiter that was found.
            var strMatchedDelimiter = arrMatches[1];

            // Check to see if the given delimiter has a length
            // (is not the start of string) and if it matches
            // field delimiter. If id does not, then we know
            // that this delimiter is a row delimiter.
            if (
                strMatchedDelimiter.length &&
                strMatchedDelimiter !== strDelimiter
                ) {

                // Since we have reached a new row of data,
                // add an empty row to our data array.
                arrData.push([]);

            }

            var strMatchedValue;

            // Now that we have our delimiter out of the way,
            // let's check to see which kind of value we
            // captured (quoted or unquoted).
            if (arrMatches[2]) {

                // We found a quoted value. When we capture
                // this value, unescape any double quotes.
                strMatchedValue = arrMatches[2].replace(
                    new RegExp("\"\"", "g"),
                    "\""
                    );

            } else {

                // We found a non-quoted value.
                strMatchedValue = arrMatches[3];

            }


            // Now that we have our value string, let's add
            // it to the data array.
            arrData[arrData.length - 1].push(strMatchedValue);
        }

        // Return the parsed data.
        return (arrData);
    };

    return links;
  }
});


$(function() {
  var documentGroup = new Views.NLC({
    el: $("#main")
  });
});

<commit_msg>fixed json format<commit_after>window.Models = {};
window.Collections = {};
window.Views = {};

_.templateSettings = {
  interpolate: /\{\{(.+?)\}\}/g
};

Models.NLCDocumentGroup = Backbone.Model.extend({
  initialize: function() {}
});

Models.NLCDocument = Backbone.Model.extend({
  initialize: function() {}
});

Collections.NLCDocuments = Backbone.Collection.extend({
  model: Models.NLCDocument,
  initialize: function() {}
});

Collections.NLCDocumentGroups = Backbone.Collection.extend({
  model: Models.NLCDocumentGroup,
  initialize: function() {}
});

Views.NLCDocument = Backbone.View.extend({
  model: Models.NLCDocument,
  template: null,
  initialize: function() {
    this.template = _.template($('#link').html());
  },
  render: function() {
    this.$el.html(this.template(this.model.toJSON()));
    return this;
  },
  events: 
  {
    "click .list-group-item" : "toggleSelected"
  },
  toggleSelected: function() {
    if(this.model.get("isSelected")){
      this.model.set("isSelected", false);
      this.$el.find("input").prop('checked', false);
    } else {
      this.model.set("isSelected", true);
      this.$el.find("input").prop('checked', true);
    }
  }
});

Views.NLCDocumentGroup = Backbone.View.extend({
  model: Models.NLCDocumentGroup,
  template: null,
  initialize: function() {
    this.template = _.template($('#docGroup').html());
    this.collection = new Collections.NLCDocuments(this.model.get("links"));
  },
  render: function() {
    this.$el.html(this.template(this.model.toJSON()));
    var $links = this.$el.find(".list-group"); 
    $links.empty();
    this.collection.each(function(model) {
      var nlcDocument = new Views.NLCDocument({
        model: model
      });
      $links.append(nlcDocument.render().el);
    });
    return this;
  },
  events:
  {
    "click .panel-heading" : "toggleList"
  },
  toggleList: function(){
    this.$el.find(".list-group").slideToggle();
  }
});

Views.NLC = Backbone.View.extend({
  collection: null,
  initialize: function() {
    this.collection = new Collections.NLCDocumentGroups(this.getLinksFromCSV());
    this.mode = "email"; 
    this.render();
  },
  render: function() {
    this.$el.empty();
    var that = this;
    this.collection.each(function(group) {
      var nlcDocumentGroup = new Views.NLCDocumentGroup({
        model: group
      });
      that.$el.append(nlcDocumentGroup.render().el);
    });
  },
  getLinksFromCSV : function() {
    //Where we parse the csv and then return the Array of JSON

    // $.ajax({ //my ajax request
    //         url: "http://www.kcba.org/pbs/pdf/NLCMap.csv",
    //         type: "GET",
    //         dataType: "text",
    //         success : function(response){
    //          // csvToArray(response);
    //          arr1 = csvToArray(str1);
    //          console.log(arr1);
    //         }
    // });

    var str1 = "Document Title,Document Category,Link,Available to receive by mail\r\n" +
          "Client Intake Form - English,ADMINISTRATION,http://www.kcba.org/pbs/pdf/NLClinks/intakesheet.pdf,FALSE\r\n"+
          "Client Intake Form—Spanish,ADMINISTRATION,http://www.kcba.org/pbs/pdf/NLClinks/IntakeSheet-Spanish.pdf,FALSE";

    var arr1 = csvToArray(str1);
    // console.log(arr1);
    var links = arrToJson(arr1);
    console.log(links);

    //convert array to json
    // function arrToJson(arr){
    //   var arr2 = [];
    //   for(var i=1; i<arr.length; i++){
    //     var obj = {};
    //     obj["links"] = {};
    //     obj["links"]["linkName"] = arr[i][0];
    //     obj["groupName"] = arr[i][1];
    //     obj["links"]["linkURL"] = arr[i][2];
    //     obj["links"]["mail"] = Boolean.valueOf(arr[i][3])();
    //     arr2.push(obj);
    //   }
    //   return arr2;
    // }

    //convert array to json
    function arrToJson(arr){
      console.log(arr);
      var arr2 = {};
      for(var i=1; i<arr.length; i++){
        var obj1 = {};
        if(!arr2[arr[i][1]]){
          arr2[arr[i][1]] = {};
        }
        arr2[arr[i][1]]["groupName"] = arr[i][1];
        if(!arr2[arr[i][1]]["links"]){
          arr2[arr[i][1]]["links"] = [];
        }

        obj1["linkName"] = arr[i][0];
        obj1["linkURL"] = arr[i][2];
        obj1["mail"] = Boolean.valueOf(arr[i][3])();
        console.log(obj1);
        arr2[arr[i][1]]["links"].push(obj1);
      }
      var arr3 = [];
      for(var i in arr2){
        arr3.push(arr2[i]);
      }
      return arr3;
    }

    //Convert CSV to Array
    function csvToArray (strData, strDelimiter){
        // Check to see if the delimiter is defined. If not,
        // then default to comma.
        strDelimiter = (strDelimiter || ",");

        // Create a regular expression to parse the CSV values.
        var objPattern = new RegExp(
            (
                // Delimiters.
                "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

                // Quoted fields.
                "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

                // Standard fields.
                "([^\"\\" + strDelimiter + "\\r\\n]*))"
            ),
            "gi"
            );
        // Create an array to hold our data. Give the array
        // a default empty first row.
        var arrData = [[]];

        // Create an array to hold our individual pattern
        // matching groups.
        var arrMatches = null;


        // Keep looping over the regular expression matches
        // until we can no longer find a match.
        while (arrMatches = objPattern.exec(strData)) {

            // Get the delimiter that was found.
            var strMatchedDelimiter = arrMatches[1];

            // Check to see if the given delimiter has a length
            // (is not the start of string) and if it matches
            // field delimiter. If id does not, then we know
            // that this delimiter is a row delimiter.
            if (
                strMatchedDelimiter.length &&
                strMatchedDelimiter !== strDelimiter
                ) {

                // Since we have reached a new row of data,
                // add an empty row to our data array.
                arrData.push([]);

            }

            var strMatchedValue;

            // Now that we have our delimiter out of the way,
            // let's check to see which kind of value we
            // captured (quoted or unquoted).
            if (arrMatches[2]) {

                // We found a quoted value. When we capture
                // this value, unescape any double quotes.
                strMatchedValue = arrMatches[2].replace(
                    new RegExp("\"\"", "g"),
                    "\""
                    );

            } else {

                // We found a non-quoted value.
                strMatchedValue = arrMatches[3];

            }


            // Now that we have our value string, let's add
            // it to the data array.
            arrData[arrData.length - 1].push(strMatchedValue);
        }

        // Return the parsed data.
        return (arrData);
    };

    return links;
  }
});


$(function() {
  var documentGroup = new Views.NLC({
    el: $("#main")
  });
});


{'fraction_non_alphanumeric': 0.15764671231868052, 'fraction_numerical': 0.005227891907812385, 'mean_word_length': 1.9177228786251344, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '2331864', 'n_tokens_mistral': 4205, 'n_tokens_neox': 4095, 'n_words': 934}