<commit_before>$(function () {
  var Fulfil = {
    csrfToken: "{{ csrf_token() }}",
    urls: {},
    form: {},
    cart: {},
    user: {},
    product: {}
  };

  // All the URLs this file needs to know
  Fulfil.urls.view_cart = "{{ url_for('cart.view_cart') }}";
  Fulfil.urls.remove_from_cart = "{{ url_for('cart.remove_from_cart') }}";
  Fulfil.urls.getVariations = "{{ url_for('products.get_variations') }}";

  /**
   * Helper method to change formSubmission to ajax
   *
   * @param formElement HTMLElement
   * @param options Object with successCallback, errorCallback,
   *                alwaysCallback, onSubmit as keys
   */
  Fulfil.form.ajaxSubmit = function (formElement, options) {
    var form = $(formElement);

    options = options || {}


    form.on('submit', function (event) {
      event.preventDefault();

      // Call onSubmit if function
      if ($.isFunction(options.onSubmit)) {
        options.onSubmit(event);
      }

      $.ajax({
        'url': form.attr("action"),
        'type': form.attr('method') || 'GET',
        'data': form.serialize(),
        'dataType': "json"
      })
        .done(options.successCallback)
        .fail(options.errorCallback)
        .always(options.alwaysCallback);
    });
  };

  /**
   * Method to show errors in form
   */
  Fulfil.form.showErrors = function (formElement, errorsObj) {
    var form = $(formElement);

    $.each(errorsObj, function (key, value) {
      // TODO: show errors
    });
  };

  /**
   * Helper method to initialize login form
   */
  Fulfil.form.initLoginForm =  function (formElement) {
    var errorContainer = formElement.find('.error-message');

    // by default hide error container
    errorContainer.html('').hide();

    var options = {
      onSubmit: function () {
        errorContainer.html('').hide();
      },
      successCallback: function (result) {
        location.reload();
      },
      errorCallback: function (error) {
        if (error.status == 400) {
          if (!error.responseJSON.errors) {
            console.error("No 'errors' in response on bad request.");
          }

          var errors = [];
          $.each(error.responseJSON.errors, function (key, value) {
            errors = errors.concat(value);
          });

          // Show errors in error container
          errorContainer.html(errors.join(',')).show();
          return;
        }
        console.log(error.statusText);
      }
    };

    Fulfil.form.ajaxSubmit(formElement, options);
  };

  /*
   *
   * Cart related logic
   * ==================
   *
   */


   /*
    * Refreshes the cart and updates Fulfil.cart.current_cart
    * Also returns the request promise.
    *
    */
   Fulfil.cart.refresh = function() {
    console.debug("Refreshing Cart");
    return $.ajax({
      cache: false,
      url: Fulfil.urls.view_cart,
      dataType: "json",
      success: function(data){
        Fulfil.cart.current_cart = data.cart;
      }
    })
   };

   /*
    * Delete a item from the cart.
    *
    */
   Fulfil.cart.deleteItem = function(lineId) {
    console.debug("Removing Line from Cart");
    return $.ajax({
      url: Fulfil.urls.remove_from_cart,
      method: "POST",
      data: {
        "line_id": lineId,
        "csrf_token": Fulfil.csrfToken
       },
       dataType: "json",
       success: function() {
         Fulfil.cart.refresh();
       }
    });
   }

  /*
   *
   * Product related logic
   * =====================
   *
   */

  /*
   *
   * Fetch variation data for a product template
   *
   */
  Fulfil.product.getVariations = function(templateId) {
    return $.ajax({
      url: Fulfil.urls.getVariations,
      method: "GET",
      data: {
        "template": templateId
       },
       dataType: "json",
       success: function(data) {
         Fulfil.product.loadVariations(data);
       }
    });
  };

  /*
   * Load the variation data locally to serve more
   * requests.
   */
  Fulfil.product.loadVariations = function(variationData) {
    Fulfil.product.variationData = variationData;
  };

  /*
   * Given a set of attributes, return a variant
   * that fits the attributes
   *
   */
  Fulfil.product.findMatchingVariant = function(attributeSelection) {
    return _.find(Fulfil.product.variationData['variants'], function(variant) {
      return _.isEqual(variant.attributes, attributeSelection)
    });
  };

  /*
   * Return the data of the variant with id
   *
   */
  Fulfil.product.getVariant = function(variantId) {
    return _.find(Fulfil.product.variationData['variants'], function(variant) {
      return (variant.id == variantId) ;
    });
  };

  /*
   *
   * User related logic
   * ==================
   *
   */

  window.Fulfil = Fulfil;
});
<commit_msg>Add helper method to fill countries in select field<commit_after>$(function () {
  var Fulfil = {
    csrfToken: "{{ csrf_token() }}",
    urls: {},
    form: {},
    cart: {},
    user: {},
    product: {},
    address: {}
  };

  // All the URLs this file needs to know
  Fulfil.urls.view_cart = "{{ url_for('cart.view_cart') }}";
  Fulfil.urls.remove_from_cart = "{{ url_for('cart.remove_from_cart') }}";
  Fulfil.urls.getVariations = "{{ url_for('products.get_variations') }}";

  /**
   * Helper method to change formSubmission to ajax
   *
   * @param formElement HTMLElement
   * @param options Object with successCallback, errorCallback,
   *                alwaysCallback, onSubmit as keys
   */
  Fulfil.form.ajaxSubmit = function (formElement, options) {
    var form = $(formElement);

    options = options || {}


    form.on('submit', function (event) {
      event.preventDefault();

      // Call onSubmit if function
      if ($.isFunction(options.onSubmit)) {
        options.onSubmit(event);
      }

      $.ajax({
        'url': form.attr("action"),
        'type': form.attr('method') || 'GET',
        'data': form.serialize(),
        'dataType': "json"
      })
        .done(options.successCallback)
        .fail(options.errorCallback)
        .always(options.alwaysCallback);
    });
  };

  /**
   * Method to show errors in form
   */
  Fulfil.form.showErrors = function (formElement, errorsObj) {
    var form = $(formElement);

    $.each(errorsObj, function (key, value) {
      // TODO: show errors
    });
  };

  /**
   * Helper method to initialize login form
   */
  Fulfil.form.initLoginForm =  function (formElement) {
    var errorContainer = formElement.find('.error-message');

    // by default hide error container
    errorContainer.html('').hide();

    var options = {
      onSubmit: function () {
        errorContainer.html('').hide();
      },
      successCallback: function (result) {
        location.reload();
      },
      errorCallback: function (error) {
        if (error.status == 400) {
          if (!error.responseJSON.errors) {
            console.error("No 'errors' in response on bad request.");
          }

          var errors = [];
          $.each(error.responseJSON.errors, function (key, value) {
            errors = errors.concat(value);
          });

          // Show errors in error container
          errorContainer.html(errors.join(',')).show();
          return;
        }
        console.log(error.statusText);
      }
    };

    Fulfil.form.ajaxSubmit(formElement, options);
  };

  /*
   *
   * Cart related logic
   * ==================
   *
   */


   /*
    * Refreshes the cart and updates Fulfil.cart.current_cart
    * Also returns the request promise.
    *
    */
   Fulfil.cart.refresh = function() {
    console.debug("Refreshing Cart");
    return $.ajax({
      cache: false,
      url: Fulfil.urls.view_cart,
      dataType: "json",
      success: function(data){
        Fulfil.cart.current_cart = data.cart;
      }
    })
   };

   /*
    * Delete a item from the cart.
    *
    */
   Fulfil.cart.deleteItem = function(lineId) {
    console.debug("Removing Line from Cart");
    return $.ajax({
      url: Fulfil.urls.remove_from_cart,
      method: "POST",
      data: {
        "line_id": lineId,
        "csrf_token": Fulfil.csrfToken
       },
       dataType: "json",
       success: function() {
         Fulfil.cart.refresh();
       }
    });
   }

  /*
   *
   * Product related logic
   * =====================
   *
   */

  /*
   *
   * Fetch variation data for a product template
   *
   */
  Fulfil.product.getVariations = function(templateId) {
    return $.ajax({
      url: Fulfil.urls.getVariations,
      method: "GET",
      data: {
        "template": templateId
       },
       dataType: "json",
       success: function(data) {
         Fulfil.product.loadVariations(data);
       }
    });
  };

  /*
   * Load the variation data locally to serve more
   * requests.
   */
  Fulfil.product.loadVariations = function(variationData) {
    Fulfil.product.variationData = variationData;
  };

  /*
   * Given a set of attributes, return a variant
   * that fits the attributes
   *
   */
  Fulfil.product.findMatchingVariant = function(attributeSelection) {
    return _.find(Fulfil.product.variationData['variants'], function(variant) {
      return _.isEqual(variant.attributes, attributeSelection)
    });
  };

  /*
   * Return the data of the variant with id
   *
   */
  Fulfil.product.getVariant = function(variantId) {
    return _.find(Fulfil.product.variationData['variants'], function(variant) {
      return (variant.id == variantId) ;
    });
  };

  /*
   *
   * Address related logic
   * =====================
   *
   */

  /*
   * Return list of all available countries
   */
  Fulfil.address.getCountries = function() {
    return $.getJSON('{{ url_for("public.get_countries") }}');
  };

  /*
   * Helper method to fill country options in select field
   */
  Fulfil.address.fillCountriesOpts = function(selectField, currentValue) {
    selectField.html('');
    return Fulfil.address.getCountries()
      .success(function (data) {
        var optionsHtml = '';
        $.each(data.result, function(i, country){
          optionsHtml +=
            '<option value="'+ country.id +'">' + country.name + '</option>';
        });
        selectField.html(optionsHtml);

        if (currentValue) {
          $(selectField).val(currentValue).change();
        }
      });
  };

  /*
   *
   * User related logic
   * ==================
   *
   */

  window.Fulfil = Fulfil;
});

{'fraction_non_alphanumeric': 0.15325929502655722, 'fraction_numerical': 0.0005794302269435056, 'mean_word_length': 2.2372616442638322, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '3912285', 'n_tokens_mistral': 3212, 'n_tokens_neox': 3142, 'n_words': 645}