Commit 1293ab5a authored by Ryan McVeigh's avatar Ryan McVeigh
Browse files

Issue #3211488 by rymcveigh: Webform Autosave 2.0.0 stable release plan

parent e5ba6ed9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@
    "source": "https://git.drupalcode.org/project/webformautosave"
  },
  "require": {
    "drupal/core": "^8.8 || ^9.0 || ^10.0",
    "drupal/core": "^9.0 || ^10.0",
    "drupal/webform": "^5.16 || ^6.0"
  }
}
+55 −32
Original line number Diff line number Diff line
@@ -2,25 +2,26 @@
 * @file
 * Webform Autosave behaviors.
 */
(function ($, Drupal, drupalSettings) {
  'use strict';

// eslint-disable-next-line func-names
(function ($, Drupal, drupalSettings) {
  // Set our primary store.
  const store = Object.assign(
    {}, {
  const store = {
    csrfToken: null,
    activeClass: 'active',
    focusedElement: null,
    webform: null,
      submit: null
    },
    drupalSettings.webformautosave
  );
    submit: null,
    ...drupalSettings.webformautosave,
  };

  /**
   * The handler that triggers after ajax is complete.
   *
   * @return {Boolean}
   *   True if the element already has the active class.
   */
  const ajaxCompleteHandler = function () {
  function ajaxCompleteHandler() {
    // Get outta here if we didn't trigger the ajax.
    if (!$(store.submit).hasClass(store.activeClass)) {
      return true;
@@ -29,14 +30,17 @@
    $(store.submit).removeClass(store.activeClass);
    // Ensure our focus doesn't change.
    $(store.focusedElement).focus();
  };
  }

  /**
   * The handler bound to inputs on the form.
   *
   * @return {Boolean}
   *   True if the element already has the active class.
   */
  const inputHandler = function () {
    let webformId = store.webform.data('webform-id');
    let formStore = store.forms[webformId];
  function inputHandler() {
    const webformId = store.webform.data('webform-id');
    const formStore = store.forms[webformId];
    store.submit = $(store.webform).find('[data-autosave-trigger="submit"]');
    // Get out of here if the submit is already happening.
    if ($(store.submit).hasClass(store.activeClass)) {
@@ -46,6 +50,7 @@
    if (formStore) {
      // Prevent propagation by adding the active class.
      $(store.submit).addClass(store.activeClass);
      // eslint-disable-next-line func-names
      setTimeout(function () {
        // Submit our draft after the timeout.
        $(store.submit).click();
@@ -53,48 +58,66 @@
        $(store.focusedElement).focus();
      }, formStore.autosaveTime);
    }
  };
  }

  /**
   * Bind event handlers to input fields.
   *
   * @param {object} form
   *   The form element.
   * @param {HTMLDocument | HTMLElement} context
   *   The current document.
   */
  const bindAutosaveHandlers = function (form, context) {
  function bindAutosaveHandlers(form, context) {
    store.webform = $('form.webform-submission-form');
    store.submit = $(form).find('[data-autosave-trigger="submit"]');

    // Add input and focus event listeners to each input.
    $(once('webformAutosaveBehavior', 'input:not([data-autosave-trigger="submit"]), select:not([data-autosave-trigger="submit"]), textarea:not([data-autosave-trigger="submit"])', context))
    $(
      once(
        'webformAutosaveBehavior',
        'input:not([data-autosave-trigger="submit"]), select:not([data-autosave-trigger="submit"]), textarea:not([data-autosave-trigger="submit"])',
        context,
      ),
    )
      .on('input', inputHandler)
      // eslint-disable-next-line func-names
      .on('focus', function () {
        store.focusedElement = $(this);
      });

    // Remove the active class and perform other actions when ajax is complete.
    $(once('webformAutosaveBehaviorAjaxComplete', 'body')).ajaxComplete(ajaxCompleteHandler);
  };
    $(once('webformAutosaveBehaviorAjaxComplete', 'body')).ajaxComplete(
      ajaxCompleteHandler,
    );
  }

  /**
   * Setup our default behaviors for the webformautosave module.
   *
   * @type {Drupal~behavior}
   *
   * @prop {Drupal~behaviorAttach} attach
   *   Specific description of this attach function goes here.
   */
  Drupal.behaviors.webformautosave = {
    attach: function (context, settings) {
    attach(context, settings) {
      $(document, context).find('form.webform-submission-form');
      // This runs every time we attach (on backend ajax callback).
      store.forms = settings.webformautosave.forms
      let webformForm = $('form.webform-submission-form')
      store.forms = settings.webformautosave.forms;
      const webformForm = $('form.webform-submission-form');
      // Let's bind an input event to our inputs once.
      if ($(webformForm).length) {
        // eslint-disable-next-line func-names
        $(webformForm).each(function (form) {
          bindAutosaveHandlers(form, context);
        });
      }
      // Ensure the wrapper for our draft submit is hidden.
      $(once('webformAutosaveHideWrapper', webformForm))
        .each(function () {
      // eslint-disable-next-line func-names
      $(once('webformAutosaveHideWrapper', webformForm)).each(function () {
        // Ensure the wrapper is hidden.
          $(webformForm)
            .find('.webformautosave-trigger--wrapper')
            .hide();
        $(webformForm).find('.webformautosave-trigger--wrapper').hide();
      });
    },
  };

phpcs.xml.dist

0 → 100644
+31 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Module">
  <file>.</file>
  <arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,md,yml"/>
  <config name="drupal_core_version" value="8"/>

  <!-- Initially include all Drupal and DrupalPractice sniffs. -->
  <rule ref="vendor/drupal/coder/coder_sniffer/Drupal"/>
  <rule ref="vendor/drupal/coder/coder_sniffer/DrupalPractice"/>

  <!-- Use 's' to print the full sniff name in the report. -->
  <!-- A '-' is prefixed to each of these, so s becomes -s, etc. -->
  <arg value="s"/>
  <arg value="-colors"/>
  <arg name='report-width' value='120'/>

  <!-- Ignore all files that match these patterns. They are matched against -->
  <!-- the full file path and there is an implied wildcard at each end. -->
  <!-- Periods must be escaped using \. -->
  <exclude-pattern>_ignore</exclude-pattern>
  <exclude-pattern>\.patch</exclude-pattern>
  <exclude-pattern>interdif</exclude-pattern>

  <!-- Examples for how you disable rules you do not like. -->
  <!-- Exclude a sniff from running on specific files. -->
  <rule ref="Drupal.Files.TxtFileLineLength.TooLong">
    <!-- Exclude .md files from the line limit rule. -->
    <exclude-pattern>\.md</exclude-pattern>
  </rule>

</ruleset>
+16 −5
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_submission_log\WebformSubmissionLogManager;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * A helper class that houses helper functions for the webformautosave module.
@@ -38,6 +39,13 @@ class AutosaveHelper {
   */
  protected $entityTypeManager;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * AutosaveHelper constructor.
   *
@@ -47,11 +55,14 @@ class AutosaveHelper {
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(WebformSubmissionLogManager $webform_submission_log_manager, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
  public function __construct(WebformSubmissionLogManager $webform_submission_log_manager, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, RequestStack $request_stack) {
    $this->webformSubmissionLogManager = $webform_submission_log_manager;
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
    $this->requestStack = $request_stack;
  }

  /**
@@ -66,7 +77,7 @@ class AutosaveHelper {
  public function getSubmissionUrl(WebformSubmissionInterface $webform_submission) {
    $submission_url = $webform_submission->getTokenUrl();
    $submission_url->setAbsolute(FALSE);
    $current_params = (array) \Drupal::request()->query->all();
    $current_params = $this->requestStack->getCurrentRequest()->query->all();
    // Add the current params to the submission url.
    foreach ($current_params as $key => $param) {
      $submission_url->setRouteParameter($key, $param);
@@ -92,8 +103,8 @@ class AutosaveHelper {

    // Clean and return the record if available.
    if (!empty($submission_log)) {
      $submission_log->variables = unserialize($submission_log->variables);
      $submission_log->data = unserialize($submission_log->data);
      $submission_log->variables = unserialize($submission_log->variables, ['allowed_classes' => FALSE]);
      $submission_log->data = unserialize($submission_log->data, ['allowed_classes' => FALSE]);
      return $submission_log;
    }

@@ -102,7 +113,7 @@ class AutosaveHelper {
    $log_data = [
      'webform_id' => $webform_submission->getWebform()->id(),
      'sid' => $webform_submission->id(),
      'uid' => \Drupal::currentUser()->id(),
      'uid' => $this->currentUser->id(),
      'message' => 'initial log by webform_autosave',
      'timestamp' => $now->getTimestamp(),
    ];
+1 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ class WebformAutosaveThirdPartySettingsTest extends WebformBrowserTestBase {
   */
  protected function setUp(): void {
    parent::setUp();
    $this->adminUser = $this->drupalCreateUser(['access administration pages']);
  }

  /**
Loading