Module: Decidim::LayoutHelper

Overview

View helpers related to the layout.

Instance Method Summary collapse

Methods included from TooltipHelper

#with_tooltip

Methods included from ModalHelper

#decidim_drawer, #decidim_modal

Methods included from OrganizationHelper

#current_organization_name, #organization_colors, #organization_description_label, #organization_name

Methods included from TranslatableAttributes

#attachment?, #default_locale?

Instance Method Details

#_icon_classes(options = {}) ⇒ Object



119
120
121
122
123
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 119

def _icon_classes(options = {})
  classes = options[:remove_icon_class] ? [] : ["icon"]
  classes += [options[:class]]
  classes.compact
end

#apple_faviconObject



22
23
24
25
26
27
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 22

def apple_favicon
  icon_image = current_organization.attached_uploader(:favicon).variant_url(:medium, host: current_organization.host)
  return unless icon_image

  favicon_link_tag(icon_image, rel: "apple-touch-icon", type: "image/png")
end

#application_path(path) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 98

def application_path(path)
  # Force the path to be returned without the protocol and host even when a
  # custom asset host has been defined. The host parameter needs to be a
  # non-nil because otherwise it will be set to the asset host at
  # ActionView::Helpers::AssetUrlHelper#compute_asset_host.
  img_path = asset_pack_path(path, host: "", protocol: :relative)
  path = Rails.public_path.join(img_path.sub(%r{^/}, ""))
  return unless File.exist?(path)

  path
rescue ::Shakapacker::Manifest::MissingEntryError
  nil
end

#current_url(params = request.parameters) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 135

def current_url(params = request.parameters)
  return url_for(params) if respond_to?(:current_participatory_space) || respond_to?(:current_component)

  each_decidim_engine do |helpers|
    return helpers.url_for(params)
  rescue ActionController::UrlGenerationError
    # Continue to next engine in case the URL is not available.
  end

  main_app.url_for(params)
rescue ActionController::UrlGenerationError
  "#{request.base_url}#{"?#{params.to_query}" unless params.empty?}"
end

#current_user_unread_dataObject



125
126
127
128
129
130
131
132
133
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 125

def current_user_unread_data
  return {} if current_user.blank?

  {}.tap do |d|
    d.merge!(unread_notifications: true) if current_user.notifications.any?
    d.merge!(unread_conversations: true) if current_user.unread_conversations.any?
    d.merge!(unread_items: d.present?)
  end
end

#external_icon(path, options = {}) ⇒ Object

Outputs a SVG icon from an external file. It apparently renders an image tag, but then a JS script kicks in and replaces it with an inlined SVG version.

path - The asset’s path

Returns an <img /> tag with the SVG icon.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 83

def external_icon(path, options = {})
  classes = _icon_classes(options) + ["external-icon"]

  if path.split(".").last == "svg"
    icon_path = application_path(path)
    return unless icon_path

    attributes = { class: classes.join(" ") }.merge(options)
    asset = File.read(icon_path)
    asset.gsub("<svg ", "<svg#{tag_builder.tag_options(attributes)} ").html_safe
  else
    image_pack_tag(path, class: classes.join(" "), style: "display: none")
  end
end

#faviconObject

Public: Generates a set of meta tags that generate the different favicon versions for an organization.

Returns a safe String with the versions.



14
15
16
17
18
19
20
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 14

def favicon
  return if current_organization.favicon.blank?

  safe_join(Decidim::OrganizationFaviconUploader::SIZES.map do |version, size|
    favicon_link_tag(current_organization.attached_uploader(:favicon).variant_url(version, host: current_organization.host), sizes: "#{size}x#{size}")
  end)
end

#icon(name, options = {}) ⇒ Object

Outputs an SVG-based icon.

name - The String with the icon name. options - The Hash options used to customize the icon (default {}):

:width  - The Number of width in pixels (optional).
:height - The Number of height in pixels (optional).
:title - The title for the SVG element (optional, similar to alt for img)
:aria_label - The String to set as aria label (optional).
:aria_hidden - The Truthy value to enable aria_hidden (optional).
:role - The String to set as the role (optional).
:class - The String to add as a CSS class (optional).

Returns a String.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 50

def icon(name, options = {})
  name = Decidim.icons.find(name)["icon"] unless options[:ignore_missing]

  default_html_properties = {
    "width" => "1em",
    "height" => "1em",
    "role" => "img",
    "aria-hidden" => "true"
  }

  html_properties = options.with_indifferent_access.transform_keys(&:dasherize).slice("width", "height", "aria-label", "role", "aria-hidden", "class", "style")
  html_properties = default_html_properties.merge(html_properties)

  if name == "wechat-line"
    html_properties = html_properties.merge({ "aria-label" => I18n.t("decidim.author.comments.other") }).except("aria-hidden")
  elsif name == "heart-line"
    html_properties = html_properties.merge({ "aria-label" => I18n.t("decidim.author.likes.other") }).except("aria-hidden")
  end

  href = Decidim.cors_enabled ? "" : asset_pack_path("media/images/remixicon.symbol.svg")

   :svg, html_properties do
     :use, nil, "href" => "#{href}#ri-#{name}"
  end
end

#legacy_faviconObject



29
30
31
32
33
34
35
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 29

def legacy_favicon
  variant = :favicon if current_organization.favicon.content_type != "image/vnd.microsoft.icon"
  icon_image = current_organization.attached_uploader(:favicon).variant_url(variant, host: current_organization.host)
  return unless icon_image

  favicon_link_tag(icon_image, rel: "icon", sizes: "any", type: nil)
end

#role(options = {}) ⇒ Object

Allows to create role attribute according to accessibility rules

Returns role attribute string if role option is specified



115
116
117
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 115

def role(options = {})
  "role=\"#{options[:role]}\" " if options[:role]
end

#root_urlObject



149
150
151
152
153
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 149

def root_url
  return onboarding_manager.root_path if current_user&.ephemeral?

  decidim.root_url(host: current_organization.host)
end