Class: DsfrComponent::NoticeComponent

Inherits:
Base
  • Object
show all
Defined in:
app/components/dsfr_component/notice_component.rb

Constant Summary collapse

GENERIC_TYPES =
%w[info warning alert].freeze
WEATHER_TYPES =
%w[weather-orange weather-red weather-purple].freeze
ALERT_TYPES =
%w[kidnapping cyberattack attack witness].freeze
TYPES =
GENERIC_TYPES + WEATHER_TYPES + ALERT_TYPES
WEATHER_ICONS =
%w[windy-fill thunderstorms-fill heavy-showers-fill flood-fill temp-cold-fill sun-fill avalanches-fill snowy-fill].freeze
DESCRIPTION_TAGS =
i[p h1 h2 h3 h4 h5 h6].freeze

Constants inherited from Base

Base::HEADING_LEVELS, Base::SIZES

Instance Attribute Summary

Attributes inherited from Base

#html_attributes

Instance Method Summary collapse

Constructor Details

#initialize(title:, description:, type: "info", description_tag: :p, use_icon: true, icon_name: nil, use_notice: false, dismissible: false, dismiss_label: "Masquer le message", link_label: nil, link_href: nil, link_title: nil, link_blank: true, html_attributes: {}) ⇒ NoticeComponent

Returns a new instance of NoticeComponent.

Parameters:

  • title (String)

    Titre du bandeau

  • description (String)

    Description du bandeau pour apporter du contexte (optionnel)

  • type (String) (defaults to: "info")

    Type de bandeau (info, warning, alert, weather-orange, weather-red, weather-purple, kidnapping, cyberattack, attack, witness)

  • description_tag (Symbol) (defaults to: :p)

    Balise HTML à utiliser pour la description (p, h1, h2, h3, h4, h5, h6)

  • use_icon (Boolean) (defaults to: true)

    Afficher ou non une icône, uniquement pour les bandeaux génériques

  • icon_name (String) (defaults to: nil)

    Nom de l’icône à afficher

  • use_notice (Boolean) (defaults to: false)

    Ajout l’attribut role=“notice” (si insertion du bandeau à la volée)

  • dismissible (Boolean) (defaults to: false)

    Ajouter un bouton de fermeture

  • dismiss_label (String) (defaults to: "Masquer le message")

    Libellé du bouton de fermeture (optionnel)

  • link_label (String) (defaults to: nil)

    Libellé du lien (optionnel)

  • link_href (String) (defaults to: nil)

    URL du lien (optionnel)

  • link_title (String) (defaults to: nil)

    Titre du lien (optionnel)

  • link_blank (Boolean) (defaults to: true)

    Ouvrir le lien dans un nouvel onglet (optionnel)

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/components/dsfr_component/notice_component.rb', line 25

def initialize(title:, description:, type: "info", description_tag: :p, use_icon: true, icon_name: nil, use_notice: false, dismissible: false, dismiss_label: "Masquer le message", link_label: nil, link_href: nil, link_title: nil, link_blank: true,
               html_attributes: {})
  @title = title
  @description = description
  @type = type
  @description_tag = description_tag
  @icon_name = icon_name
  @use_notice = use_notice
  @dismissible = dismissible
  @dismiss_label = dismiss_label
  @link_label = link_label
  @link_href = link_href
  @link_title = link_title
  @link_blank = link_blank

  # D’après les règles du DSFR les types non génériques ont forcément une icône
  @use_icon = if GENERIC_TYPES.include?(type)
            use_icon
          else
            true
          end

  raise ArgumentError, "Invalid type: #{type}. Valid types: #{TYPES.join(', ')}" unless type_valid?

  raise ArgumentError, "Invalid description tag: #{description_tag}" unless DESCRIPTION_TAGS.include?(description_tag)

  if icon_name
    raise ArgumentError, "L’icône n’est pas personnalisable sur les bandeaux d’alertes" if ALERT_TYPES.include?(type)
    raise ArgumentError, "L’icône d’un bandeau de type météo doit être une icône météo (#{WEATHER_ICONS.join(', ')})" if WEATHER_TYPES.include?(type) && WEATHER_ICONS.exclude?(icon_name)
  end

  super(html_attributes: html_attributes)
end