<commit_before>// This file handles the behaviour for managing form building.
//
// Dependencies:
//
//   jQuery.ui.sortable: https://jqueryui.com/sortable/
//   - Allows form fields to be dragged and re-oredered
//
//   Twitter's typeahead: http://twitter.github.io/typeahead.js/
//   - Use for autocompleting for setting the field's name value

(function(){
  var initialize = function(){
    makeSortable();
    bindHandlers();
    autoComplete()
  };

  var whitelist = [
    // Permitted fields provided by ActionKit
    "address1",
    "address2",
    "city",
    "country",
    "email",
    "first_name",
    "last_name",
    "middle_name",
    "mobile_phone",
    "name",
    "phone",
    "plus4",
    "postal",
    "prefix",
    "region",
    "state",
    "suffix",
    "zip",

    // Common custom fields used by campaigners
    "customer",
    "employee",
    "shareholder",
    "investor"
  ];

  var substringMatcher = function(strs) {
    return function findMatches(q, cb) {
      var matches, substringRegex;

      // An array that will be populated with substring matches
      matches = [];

      // Regex used to determine if a string contains the substring `q`
      substrRegex = new RegExp(q, 'i');

      // Iterate through the pool of strings and for any string that
      // contains the substring `q`, add it to the `matches` array
      $.each(strs, function(i, str) {
        if (substrRegex.test(str)) {
          matches.push(str);
        }
      });

      cb(matches);
    };
  };

  var autoComplete = function(){
    $('.typeahead').typeahead({
      hint: true,
      highlight: true,
      minLength: 1
    }, {
      name: 'fields',
      source: substringMatcher(whitelist)
    });
  };

  var makeSortable = function(){
    $( ".list-group.sortable" ).sortable();
  };

  var updateSort = function( event, ui, a, b ) {
    var ids = ui.item.parent().
      children().
        map(function(i, el){
          return $(el).data('id');
        }).get().join();

    $('#form_element_ids').val(ids);
    $('form#sort-collection-elements').submit();
  };

  var bindHandlers = function(){
    $('.collection-editor #new_collection_element').on('ajax:success', function(e, resp, c){
      $('.list-group').append(resp);
      makeSortable();
    });

    $('.collection-editor').on('ajax:success', "a[data-method=delete]", function(){
      $(this).parents('.list-group-item').fadeOut();
    });

    $( ".collection-editor" ).on( "sortupdate", updateSort );

    $('#change-form-template').on('ajax:success', function(e, resp) {
      $('.forms-edit').html(resp.html);
      makeSortable();

      // Updates the inline form's action URL with the new form ID.
      $('#sort-collection-elements, #new_collection_element').each(function(i, el){
        var action = $(el).attr('action').replace(/\d+/, resp.form_id);
        $(el).attr('action', action);
      });
    });
  };

  $.subscribe("collection:edit:loaded", initialize);
}());

<commit_msg>only initialize collections once<commit_after>// This file handles the behaviour for managing form building.
//
// Dependencies:
//
//   jQuery.ui.sortable: https://jqueryui.com/sortable/
//   - Allows form fields to be dragged and re-oredered
//
//   Twitter's typeahead: http://twitter.github.io/typeahead.js/
//   - Use for autocompleting for setting the field's name value

(function(){
  var initialized = initialized || false;

  var initialize = function(){
    if (!initialized) {
      makeSortable();
      bindHandlers();
      autoComplete();
      initialized = true;
    }
  };

  var whitelist = [
    // Permitted fields provided by ActionKit
    "address1",
    "address2",
    "city",
    "country",
    "email",
    "first_name",
    "last_name",
    "middle_name",
    "mobile_phone",
    "name",
    "phone",
    "plus4",
    "postal",
    "prefix",
    "region",
    "state",
    "suffix",
    "zip",

    // Common custom fields used by campaigners
    "customer",
    "employee",
    "shareholder",
    "investor"
  ];

  var substringMatcher = function(strs) {
    return function findMatches(q, cb) {
      var matches, substringRegex;

      // An array that will be populated with substring matches
      matches = [];

      // Regex used to determine if a string contains the substring `q`
      substrRegex = new RegExp(q, 'i');

      // Iterate through the pool of strings and for any string that
      // contains the substring `q`, add it to the `matches` array
      $.each(strs, function(i, str) {
        if (substrRegex.test(str)) {
          matches.push(str);
        }
      });

      cb(matches);
    };
  };

  var autoComplete = function(){
    $('.typeahead').typeahead({
      hint: true,
      highlight: true,
      minLength: 1
    }, {
      name: 'fields',
      source: substringMatcher(whitelist)
    });
  };

  var makeSortable = function(){
    $( ".list-group.sortable" ).sortable();
  };

  var updateSort = function( event, ui, a, b ) {
    var ids = ui.item.parent().
      children().
        map(function(i, el){
          return $(el).data('id');
        }).get().join();

    $('#form_element_ids').val(ids);
    $('form#sort-collection-elements').submit();
  };

  var bindHandlers = function(){
    $('.collection-editor #new_collection_element').on('ajax:success', function(e, resp, c){
      $('.list-group').append(resp);
      makeSortable();
    });

    $('.collection-editor').on('ajax:success', "a[data-method=delete]", function(){
      $(this).parents('.list-group-item').fadeOut();
    });

    $( ".collection-editor" ).on( "sortupdate", updateSort );

    $('#change-form-template').on('ajax:success', function(e, resp) {
      $('.forms-edit').html(resp.html);
      makeSortable();

      // Updates the inline form's action URL with the new form ID.
      $('#sort-collection-elements, #new_collection_element').each(function(i, el){
        var action = $(el).attr('action').replace(/\d+/, resp.form_id);
        $(el).attr('action', action);
      });
    });
  };

  $.subscribe("collection:edit:loaded", initialize);
}());


{'fraction_non_alphanumeric': 0.17117560255779635, 'fraction_numerical': 0.0013116904410559109, 'mean_word_length': 2.6052009456264775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '1679481', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1849, 'n_words': 451}