Commit b2c9073b authored by Travis Tomka's avatar Travis Tomka
Browse files

Issue #3285732: Provide a notification message dismiss feature

parent e42fddcd
Loading
Loading
Loading
Loading
+84 −0
Changes for assets/js/notification-message.dismiss.js: 84 added lines, 0 removed lines.
Original line number Diff line number Diff line
(function ($, Drupal, Cookie, once) {
  'use strict';

  /**
   * Read cookie data.
   *
   * @param name
   *   The cookie name.
   *
   * @returns {any}
   */
  function readCookieData(name) {
    let cookieData = Cookie.get(name);
    if (typeof cookieData === "string") {
      return JSON.parse(cookieData);
    }
    return cookieData;
  }

  /**
   * Merge cookie data.
   *
   * @param name
   *   The cookie name.
   * @param data
   *   The cookie data.
   */
  function mergeCookieData(name, data) {
    let cookieData = readCookieData(name) || [];
    let mergedData = $.merge([data], cookieData);

    Cookie.set(
      name,
      JSON.stringify(mergedData.filter(function (element, index, self) {
        return index === self.indexOf(element);
      }))
    );
  }

  Drupal.behaviors.notificationMessage = {
    attach: function (context) {

      once('notificationMessage', 'html', context).forEach(function () {
        let cookieData = readCookieData('notificationMessagesClosed') || [];
        $('.notification-messages .message').each(function () {
          let messageId = $(this).data('message-id');
          if ($.inArray(messageId, cookieData) === -1) {
            if ($(this).parent().is(':hidden')) {
              $(this).parent().show();
            }
            $(this).show();
          } else {
            $(this).remove();
          }
        });
      })

      $('.notification-messages .message a.message__close', context).click(function (event) {
        event.preventDefault();
        let $parent = $(this).parent();
        let messageId = $parent.data('message-id');
        let notificationCount = $('.notification-messages > div').length;

        $('#notification-message-aria-announcement').text(
          'Notification message closed'
        );

        if(notificationCount > 1){
          $parent.remove();
        } else {
          $('.notification-messages').remove();
        }

        if (messageId !== undefined) {
          mergeCookieData(
            'notificationMessagesClosed',
            messageId
          );
        }
      });

    }
  }
}(jQuery, Drupal, window.Cookies, once));
+10 −0
Changes for config/schema/notification_message.schema.yml: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -23,6 +23,16 @@ notification_message.type.*:
      sequence:
        type: string
        label: Data type
    notification_dismiss:
      type: mapping
      label: Notification Dismiss
      mapping:
        show:
          type: boolean
          label: Show notification dismiss
        button_text:
          type: string
          label: Notification dismiss button text

block.settings.notification_message:
  type: block_settings
+7 −0
Changes for notification_message.libraries.yml: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
notification.dismiss:
  version: VERSION
  js:
    assets/js/notification-message.dismiss.js: { minified: false }
  dependencies:
    - core/once
    - core/js-cookie
+29 −0
Changes for src/Entity/NotificationMessageType.php: 29 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
 *     "description",
 *     "allow_condition",
 *     "condition_datatype",
 *     "notification_dismiss",
 *   },
 *   handlers = {
 *     "form" = {
@@ -89,6 +90,13 @@ class NotificationMessageType extends ConfigEntityBundleBase implements Notifica
   */
  protected $condition_datatype;

  /**
   * Notification message type dismiss settings.
   *
   * @var array
   */
  protected $notification_dismiss;

  /**
   * {@inheritDoc}
   */
@@ -124,6 +132,27 @@ class NotificationMessageType extends ConfigEntityBundleBase implements Notifica
    return $this->condition_datatype ?? [];
  }

  /**
   * {@inheritDoc}
   */
  public function getNotificationDismiss() {
    return $this->notification_dismiss ?? [];
  }

  /**
   * {@inheritDoc}
   */
  public function getNotificationDismissShow(): bool {
    return (bool) ($this->getNotificationDismiss()['show'] ?? FALSE);
  }

  /**
   * {@inheritDoc}
   */
  public function getNotificationDismissButtonText(): ?string {
    return $this->getNotificationDismiss()['button_text'] ?? 'Close';
  }

  /**
   * {@inheritDoc}
   */
+24 −0
Changes for src/Entity/NotificationMessageTypeInterface.php: 24 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -34,6 +34,30 @@ interface NotificationMessageTypeInterface extends ConfigEntityInterface, Entity
   */
  public function getConditionDatatype();

  /**
   * Get the notification message type dismiss settings.
   *
   * @return array
   *   An array of the notification dismiss settings.
   */
  public function getNotificationDismiss();

  /**
   * Get notification dismiss show value.
   *
   * @return bool
   *   Return a boolean based on if notification dismiss is enabled.
   */
  public function getNotificationDismissShow(): bool;

  /**
   * Get notification dismiss button text.
   *
   * @return string|null
   *   The notification dismiss button text.
   */
  public function getNotificationDismissButtonText(): ?string;

  /**
   * Has associated notification messages based on the message type.
   *
Loading