Commit 36651df4 authored by Jonathan Sacksick's avatar Jonathan Sacksick Committed by Jonathan Sacksick
Browse files

Issue #3310335 by jsacksick, Dharti Patel: Remove the jQuery once dependency.

parent 835f2f12
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -6,9 +6,8 @@ address:
      - core/jquery
      - core/drupal
      - core/drupalSettings
      - core/jquery.once
      - core/once
      - core/drupal.dialog.ajax
      - core/jquery.form
  css:
    theme:
      css/commerce_avatax.theme.css: {}
+168 −163
Original line number Diff line number Diff line
(function($, Drupal, drupalSettings) {

  var isValidating = false;

  function submitForm($form) {
    isValidating = false;
  Drupal.commerceAvatax = {
    isValidating: false,
    submitForm: function submitForm($form) {
      this.isValidating = false;
      $form.find(":input.button--primary").click();
  }

  function getConfirmationDialog(content, title, buttons) {
    },
    getConfirmationDialog: function getConfirmationDialog(content, title, buttons) {
      return Drupal.dialog($('<div class="address-suggestions">' + content + '</div>'), {
        title: title,
        dialogClass: 'address-format-modal',
@@ -17,23 +16,27 @@
        maxWidth: "80%",
        draggable: false,
        close: function close(event) {
        isValidating = false;
          this.isValidating = false;
          Drupal.dialog(event.target).close();
          Drupal.detachBehaviors(event.target, null, 'unload');
          $(event.target).remove();
        }
    })
      });
    }
  };

  Drupal.behaviors.commerceAvatax = {
    attach: function attach(context) {
      if (!drupalSettings.commerceAvatax || !drupalSettings.commerceAvatax.address || !drupalSettings.commerceAvatax.country) {
      if (!drupalSettings.commerceAvatax ||
        !drupalSettings.commerceAvatax.address ||
        !drupalSettings.commerceAvatax.country) {
        return;
      }

      var $form = $('.avatax-form', context).closest('form');
      $form.once('.avatax-form-processed').each(function () {
        $form.on('submit.commerce_avatax', function () {
      if (!$form.attr('id')) {
        return;
      }
      $(once($form.attr('id'), 'form')).on('submit.commerce_avatax', function () {
        var allowFormSubmit = true;
        // Get data from module.
        var address = drupalSettings.commerceAvatax.address;
@@ -45,8 +48,9 @@
          return allowFormSubmit;
        }

          // Check if this submit handler is already performing validation and triggered again by another module.
          if (isValidating) {
        // Check if this submit handler is already performing validation and
        // triggered again by another module.
        if (Drupal.commerceAvatax.isValidating) {
          return false;
        }

@@ -58,12 +62,16 @@
          });
        }

          // If country code matches those which we need to validate.
          if (drupalSettings.commerceAvatax.countries.hasOwnProperty(address.country_code) && $('.address-format-modal').length === 0) {
        // Stop here if the address doesn't match countries that we're supposed
        // to validate.
        if (!drupalSettings.commerceAvatax.countries.hasOwnProperty(address.country_code) ||
          $('.address-format-modal').length > 0) {
          return allowFormSubmit;
        }
        // Do not submit form automatically, we need to attempt address validation.
        allowFormSubmit = false;
        // Start validating field.
            isValidating = true;
        Drupal.commerceAvatax.isValidating = true;

        $.ajax({
          async: true,
@@ -72,13 +80,19 @@
          data: JSON.stringify(address),
          dataType: 'json',
          success: function success(response) {
                if (response.output) {
            if (!response.output) {
              // The address had no suggestions.
              $addressSuggestionEl.val('original');
              allowFormSubmit = true;
              Drupal.commerceAvatax.submitForm($form);
              return;
            }
            var actions = [{
              text: Drupal.t('Let me change the address'),
              class: 'button button--primary',
              id: 'button-again',
              click: function click() {
                      isValidating = false;
                Drupal.commerceAvatax.isValidating = false;
                confirmationDialog.close();
              }
            }, {
@@ -88,7 +102,7 @@
              click: function click() {
                $addressSuggestionEl.val('original');
                confirmationDialog.close();
                      submitForm($form);
                Drupal.commerceAvatax.submitForm($form);
              }
            }];

@@ -111,7 +125,7 @@
                    $inlineForm.find('[name*="' + i + ']"]').val(response.suggestion[i]);
                  });
                  confirmationDialog.close();
                        submitForm($form);
                  Drupal.commerceAvatax.submitForm($form);
                }
              }, {
                text: Drupal.t('Use as entered'),
@@ -120,7 +134,7 @@
                click: function click() {
                  $addressSuggestionEl.val('original');
                  confirmationDialog.close();
                        submitForm($form);
                  Drupal.commerceAvatax.submitForm($form);
                }
              },
              {
@@ -128,21 +142,14 @@
                class: 'button',
                id: 'button-again',
                click: function click(event) {
                          isValidating = false;
                  Drupal.commerceAvatax.isValidating = false;
                  confirmationDialog.close();
                }
              }];
            }

                  var confirmationDialog = getConfirmationDialog(response.output, Drupal.t('Confirm your shipping address'), actions);
            var confirmationDialog = Drupal.commerceAvatax.getConfirmationDialog(response.output, Drupal.t('Confirm your shipping address'), actions);
            confirmationDialog.showModal();
                }
                else {
                  // The address had no suggestions.
                  $addressSuggestionEl.val('original');
                  allowFormSubmit = true;
                  submitForm($form);
                }
          },
          error: function error() {
            var actions = [{
@@ -159,10 +166,10 @@
              click: function click(event) {
                $addressSuggestionEl.val('original');
                confirmationDialog.close()
                    submitForm($form);
                Drupal.commerceAvatax.submitForm($form);
              }
            }];
                var confirmationDialog = getConfirmationDialog(
            var confirmationDialog = Drupal.commerceAvatax.getConfirmationDialog(
              Drupal.t('We could not validate the address entered. Please check that you have entered the correct address'),
              Drupal.t('Confirm your shipping address'),
              actions
@@ -170,11 +177,9 @@
            confirmationDialog.showModal();
          },
        });
          }

        return allowFormSubmit;
      });
      });
    },

    detach: function (context, settings, trigger) {