Module: Decidim::LayoutHelper

Overview

View helpers related to the layout.

Instance Method Summary collapse

Instance Method Details

#_icon_classes(options = {}) ⇒ Object


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

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

#apple_faviconObject


18
19
20
21
22
23
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 18

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


99
100
101
102
103
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 99

def application_path(path)
  img_path = asset_pack_path(path)
  img_path = URI(img_path).path if Decidim.cors_enabled
  Rails.root.join("public/#{img_path}")
end

#extended_navigation_bar(items, max_items: 5) ⇒ Object


118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 118

def extended_navigation_bar(items, max_items: 5)
  return unless items.any?

  extra_items = items.slice((max_items + 1)..-1) || []
  active_item = items.find { |item| item[:active] }

  controller.view_context.render partial: "decidim/shared/extended_navigation_bar", locals: {
    items: items,
    extra_items: extra_items,
    active_item: active_item,
    max_items: max_items
  }
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.


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

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

  if path.split(".").last == "svg"
    attributes = { class: classes.join(" ") }.merge(options)
    asset = File.read(application_path(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.


10
11
12
13
14
15
16
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 10

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.


45
46
47
48
49
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
75
76
77
78
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 45

def icon(name, options = {})
  options = options.with_indifferent_access
  html_properties = {}

  html_properties["width"] = options[:width]
  html_properties["height"] = options[:height]
  html_properties["aria-label"] = options[:aria_label] || options[:"aria-label"]
  html_properties["role"] = options[:role] || "img"
  html_properties["aria-hidden"] = options[:aria_hidden] || options[:"aria-hidden"]

  html_properties["class"] = (["icon--#{name}"] + _icon_classes(options)).join(" ")

  title = options["title"] || html_properties["aria-label"]
  if title.blank? && html_properties["role"] == "img"
    # This will make the accessibility audit tools happy as with the "img"
    # role, the alternative text (aria-label) and title are required for the
    # element. This will also force the SVG to be hidden because otherwise
    # the screen reader would announce the icon name which can be in
    # different language (English) than the page language which is not
    # allowed.
    title = name
    html_properties["aria-label"] = title
    html_properties["aria-hidden"] = true
  end

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

   :svg, html_properties do
    inner =  :title, title
    inner +=  :use, nil, "href" => "#{href}#icon-#{name}"

    inner
  end
end

#legacy_faviconObject


25
26
27
28
29
30
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 25

def legacy_favicon
  icon_image = current_organization.attached_uploader(:favicon).variant_url(:small, host: current_organization.host)
  return unless icon_image

  favicon_link_tag(icon_image.gsub(".png", ".ico"), rel: "icon", sizes: "any", type: nil)
end

#organization_colorsObject

Renders a view with the customizable CSS variables in two flavours:

  1. as a hexadecimal valid CSS color (ie: #ff0000)

  2. as a disassembled RGB components (ie: 255,0,0)

Example:

–primary: #ff0000; –primary-rgb: 255,0,0

Hexadecimal variables can be used as a normal CSS color:

color: var(–primary)

While the disassembled variant can be used where you need to manipulate the color somehow (ie: adding a background transparency):

background-color: rgba(var(–primary-rgb), 0.5)


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

def organization_colors
  css = current_organization.colors.each.map { |k, v| "--#{k}: #{v};--#{k}-rgb: #{v[1..2].hex},#{v[3..4].hex},#{v[5..6].hex};" }.join
  render partial: "layouts/decidim/organization_colors", locals: { css: css }
end

#role(options = {}) ⇒ Object

Allows to create role attribute according to accessibility rules

Returns role attribute string if role option is specified


108
109
110
# File 'decidim-core/app/helpers/decidim/layout_helper.rb', line 108

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