Unverified Commit 5030f45e authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3300481 by Murz, smustgrave, catch: Move ScrollTopCommand views Ajax...

Issue #3300481 by Murz, smustgrave, catch: Move ScrollTopCommand views Ajax command to the Drupal Core
parent 7f2f2d10
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Ajax;

/**
 * Provides an AJAX command for scrolling to the top of an element.
 *
 * This command is implemented in Drupal.AjaxCommands.prototype.scrollTop.
 */
class ScrollTopCommand implements CommandInterface {

  /**
   * A CSS selector string.
   *
   * @var string
   */
  protected $selector;

  /**
   * Constructs a \Drupal\Core\Ajax\ScrollTopCommand object.
   *
   * @param string $selector
   *   A CSS selector.
   */
  public function __construct($selector) {
    $this->selector = $selector;
  }

  /**
   * {@inheritdoc}
   */
  public function render(): array {
    return [
      'command' => 'scrollTop',
      'selector' => $this->selector,
    ];
  }

}
+26 −0
Original line number Diff line number Diff line
@@ -1748,6 +1748,32 @@
        });
      });
    },

    /**
     * Command to scroll the page to an html element.
     *
     * @param {Drupal.Ajax} [ajax]
     *   A {@link Drupal.ajax} object.
     * @param {object} response
     *   Ajax response.
     * @param {string} response.selector
     *   Selector to use.
     */
    scrollTop(ajax, response) {
      const offset = $(response.selector).offset();
      // We can't guarantee that the scrollable object should be
      // the body, as the element could be embedded in something
      // more complex such as a modal popup. Recurse up the DOM
      // and scroll the first element that has a non-zero top.
      let scrollTarget = response.selector;
      while ($(scrollTarget).scrollTop() === 0 && $(scrollTarget).parent()) {
        scrollTarget = $(scrollTarget).parent();
      }
      // Only scroll upward.
      if (offset.top - 10 < $(scrollTarget).scrollTop()) {
        $(scrollTarget).animate({ scrollTop: offset.top - 10 }, 500);
      }
    },
  };

  /**
+6 −16
Original line number Diff line number Diff line
@@ -214,23 +214,13 @@
   *   Ajax response.
   * @param {string} response.selector
   *   Selector to use.
   *
   * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0.
   *   Use Drupal.AjaxCommands.prototype.scrollTop().
   *
   * @see https://www.drupal.org/node/3344141
   */
  Drupal.AjaxCommands.prototype.viewsScrollTop = function (ajax, response) {
    // Scroll to the top of the view. This will allow users
    // to browse newly loaded content after e.g. clicking a pager
    // link.
    const offset = $(response.selector).offset();
    // We can't guarantee that the scrollable object should be
    // the body, as the view could be embedded in something
    // more complex such as a modal popup. Recurse up the DOM
    // and scroll the first element that has a non-zero top.
    let scrollTarget = response.selector;
    while ($(scrollTarget).scrollTop() === 0 && $(scrollTarget).parent()) {
      scrollTarget = $(scrollTarget).parent();
    }
    // Only scroll upward.
    if (offset.top - 10 < $(scrollTarget).scrollTop()) {
      $(scrollTarget).animate({ scrollTop: offset.top - 10 }, 500);
    }
    Drupal.AjaxCommands.prototype.scrollTop(ajax, response);
  };
})(jQuery, Drupal, drupalSettings);
+7 −30
Original line number Diff line number Diff line
@@ -2,40 +2,17 @@

namespace Drupal\views\Ajax;

use Drupal\Core\Ajax\CommandInterface;
use Drupal\Core\Ajax\ScrollTopCommand as CoreScrollTopCommand;

/**
 * Provides an AJAX command for scrolling to the top of an element.
 *
 * This command is implemented in Drupal.AjaxCommands.prototype.viewsScrollTop.
 */
class ScrollTopCommand implements CommandInterface {

  /**
   * A CSS selector string.
 *
   * @var string
   */
  protected $selector;

  /**
   * Constructs a \Drupal\views\Ajax\ScrollTopCommand object.
 * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0.
 *   Use \Drupal\Core\Ajax\ScrollTopCommand
 *
   * @param string $selector
   *   A CSS selector.
 * @see https://www.drupal.org/node/3344141
 */
  public function __construct($selector) {
    $this->selector = $selector;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    return [
      'command' => 'viewsScrollTop',
      'selector' => $this->selector,
    ];
  }

class ScrollTopCommand extends CoreScrollTopCommand {
}
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\views\Ajax\ScrollTopCommand;
use Drupal\Core\Ajax\ScrollTopCommand;
use Drupal\views\Ajax\ViewAjaxResponse;
use Drupal\views\ViewExecutableFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
Loading