2017-06-21 16:14:41 +02:00
|
|
|
|
module ApplicationHelper
|
2018-01-11 18:09:01 +01:00
|
|
|
|
include SanitizeUrl
|
|
|
|
|
|
2022-04-21 10:28:23 +02:00
|
|
|
|
def html_lang
|
|
|
|
|
I18n.locale.to_s
|
|
|
|
|
end
|
|
|
|
|
|
2018-01-11 18:09:01 +01:00
|
|
|
|
def sanitize_url(url)
|
2018-01-18 17:24:21 +01:00
|
|
|
|
if !url.nil?
|
|
|
|
|
super(url, schemes: ['http', 'https'], replace_evil_with: root_url)
|
|
|
|
|
end
|
2018-01-11 18:09:01 +01:00
|
|
|
|
end
|
|
|
|
|
|
2018-11-14 16:28:02 +01:00
|
|
|
|
def flash_class(level, sticky: false, fixed: false)
|
2022-01-06 12:43:02 +01:00
|
|
|
|
class_names = []
|
|
|
|
|
|
|
|
|
|
case level
|
2018-11-14 16:28:02 +01:00
|
|
|
|
when 'notice'
|
2022-01-06 12:43:02 +01:00
|
|
|
|
class_names << 'alert-success'
|
2022-01-06 13:21:47 +01:00
|
|
|
|
when 'alert', 'error'
|
2022-01-06 12:43:02 +01:00
|
|
|
|
class_names << 'alert-danger'
|
2017-06-21 16:14:41 +02:00
|
|
|
|
end
|
2022-01-06 12:43:02 +01:00
|
|
|
|
|
2018-11-14 16:28:02 +01:00
|
|
|
|
if sticky
|
|
|
|
|
class_names << 'sticky'
|
|
|
|
|
end
|
|
|
|
|
if fixed
|
|
|
|
|
class_names << 'alert-fixed'
|
|
|
|
|
end
|
|
|
|
|
class_names.join(' ')
|
2017-06-21 16:14:41 +02:00
|
|
|
|
end
|
2017-06-22 16:45:57 +02:00
|
|
|
|
|
2022-04-20 11:14:48 +02:00
|
|
|
|
def react_component(name, props = {}, html = {})
|
|
|
|
|
tag.div(**html.merge(data: { controller: 'react', react_component_value: name, react_props_value: props.to_json }))
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-09 11:15:23 +02:00
|
|
|
|
def render_to_element(selector, partial:, outer: false, locals: {})
|
|
|
|
|
method = outer ? 'outerHTML' : 'innerHTML'
|
|
|
|
|
html = escape_javascript(render partial: partial, locals: locals)
|
|
|
|
|
# rubocop:disable Rails/OutputSafety
|
|
|
|
|
raw("document.querySelector('#{selector}').#{method} = \"#{html}\";")
|
|
|
|
|
# rubocop:enable Rails/OutputSafety
|
|
|
|
|
end
|
|
|
|
|
|
2019-01-30 16:14:15 +01:00
|
|
|
|
def append_to_element(selector, partial:, locals: {})
|
|
|
|
|
html = escape_javascript(render partial: partial, locals: locals)
|
|
|
|
|
# rubocop:disable Rails/OutputSafety
|
|
|
|
|
raw("document.querySelector('#{selector}').insertAdjacentHTML('beforeend', \"#{html}\");")
|
|
|
|
|
# rubocop:enable Rails/OutputSafety
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-14 16:28:02 +01:00
|
|
|
|
def render_flash(timeout: false, sticky: false, fixed: false)
|
2018-08-09 11:15:23 +02:00
|
|
|
|
if flash.any?
|
2018-11-14 16:28:02 +01:00
|
|
|
|
html = render_to_element('#flash_messages', partial: 'layouts/flash_messages', locals: { sticky: sticky, fixed: fixed }, outer: true)
|
2018-08-09 11:15:23 +02:00
|
|
|
|
flash.clear
|
|
|
|
|
if timeout
|
|
|
|
|
html += remove_element('#flash_messages', timeout: timeout, inner: true)
|
|
|
|
|
end
|
|
|
|
|
html
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def remove_element(selector, timeout: 0, inner: false)
|
|
|
|
|
script = "(function() {";
|
|
|
|
|
script << "var el = document.querySelector('#{selector}');"
|
|
|
|
|
method = (inner ? "el.innerHTML = ''" : "el.parentNode.removeChild(el)")
|
2020-08-25 12:47:13 +02:00
|
|
|
|
if timeout.present? && timeout > 0
|
|
|
|
|
script << "if (el) { setTimeout(function() { #{method}; }, #{timeout}); }"
|
|
|
|
|
else
|
|
|
|
|
script << "if (el) { #{method} };"
|
|
|
|
|
end
|
2018-08-09 11:15:23 +02:00
|
|
|
|
script << "})();"
|
|
|
|
|
# rubocop:disable Rails/OutputSafety
|
|
|
|
|
raw(script);
|
|
|
|
|
# rubocop:enable Rails/OutputSafety
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-03 20:42:44 +02:00
|
|
|
|
def show_element(selector)
|
|
|
|
|
# rubocop:disable Rails/OutputSafety
|
|
|
|
|
raw("document.querySelector('#{selector}').classList.remove('hidden');")
|
|
|
|
|
# rubocop:enable Rails/OutputSafety
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-09 18:56:57 +01:00
|
|
|
|
def focus_element(selector)
|
|
|
|
|
# rubocop:disable Rails/OutputSafety
|
|
|
|
|
raw("document.querySelector('#{selector}').focus();")
|
|
|
|
|
# rubocop:enable Rails/OutputSafety
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-28 16:39:42 +02:00
|
|
|
|
def disable_element(selector)
|
|
|
|
|
# rubocop:disable Rails/OutputSafety
|
|
|
|
|
raw("document.querySelector('#{selector}').disabled = true;")
|
|
|
|
|
# rubocop:enable Rails/OutputSafety
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def enable_element(selector)
|
|
|
|
|
# rubocop:disable Rails/OutputSafety
|
|
|
|
|
raw("document.querySelector('#{selector}').disabled = false;")
|
|
|
|
|
# rubocop:enable Rails/OutputSafety
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-14 16:28:02 +01:00
|
|
|
|
def fire_event(event_name, data)
|
|
|
|
|
# rubocop:disable Rails/OutputSafety
|
|
|
|
|
raw("DS.fire('#{event_name}', #{raw(data)});")
|
|
|
|
|
# rubocop:enable Rails/OutputSafety
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-22 16:45:57 +02:00
|
|
|
|
def current_email
|
2018-05-30 18:45:46 +02:00
|
|
|
|
current_user&.email ||
|
2019-08-06 11:02:54 +02:00
|
|
|
|
current_instructeur&.email ||
|
2018-05-30 18:45:46 +02:00
|
|
|
|
current_administrateur&.email
|
2017-06-22 16:45:57 +02:00
|
|
|
|
end
|
2018-03-08 13:54:26 +01:00
|
|
|
|
|
2018-08-21 15:43:06 +02:00
|
|
|
|
def staging?
|
2021-03-24 15:05:24 +01:00
|
|
|
|
Rails.application.config.ds_env == 'staging'
|
2018-08-21 15:43:06 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 15:00:35 +02:00
|
|
|
|
def contact_link(title, options = {})
|
2018-08-30 15:10:18 +02:00
|
|
|
|
tags, type, dossier_id = options.values_at(:tags, :type, :dossier_id)
|
|
|
|
|
options.except!(:tags, :type, :dossier_id)
|
|
|
|
|
|
2019-01-17 11:52:38 +01:00
|
|
|
|
params = { tags: tags, type: type, dossier_id: dossier_id }.compact
|
|
|
|
|
link_to title, contact_url(params), options
|
2018-08-29 15:00:35 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-03-08 13:54:26 +01:00
|
|
|
|
def root_path_for_profile(nav_bar_profile)
|
|
|
|
|
case nav_bar_profile
|
2019-08-06 11:02:54 +02:00
|
|
|
|
when :instructeur
|
|
|
|
|
instructeur_procedures_path
|
2018-03-08 13:54:26 +01:00
|
|
|
|
when :user
|
2018-06-27 14:47:02 +02:00
|
|
|
|
dossiers_path
|
2018-03-08 13:54:26 +01:00
|
|
|
|
else
|
|
|
|
|
root_path
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-04-10 15:49:13 +02:00
|
|
|
|
|
2020-06-03 14:55:36 +02:00
|
|
|
|
def root_path_info_for_profile(nav_bar_profile)
|
|
|
|
|
case nav_bar_profile
|
|
|
|
|
when :administrateur
|
2021-05-26 15:16:30 +02:00
|
|
|
|
[admin_procedures_path, "Aller au panneau d’administration"]
|
2020-06-03 14:55:36 +02:00
|
|
|
|
when :instructeur
|
|
|
|
|
[instructeur_procedures_path, 'Aller à la liste des démarches']
|
|
|
|
|
when :user
|
|
|
|
|
[dossiers_path, 'Aller à la liste des dossiers']
|
|
|
|
|
else
|
2021-05-26 15:16:30 +02:00
|
|
|
|
[root_path, "Aller à la page d’accueil"]
|
2020-06-03 14:55:36 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-10 15:49:13 +02:00
|
|
|
|
def try_format_date(date)
|
2020-08-31 13:47:27 +02:00
|
|
|
|
date.present? ? I18n.l(date, format: :long) : ''
|
2019-04-10 15:49:13 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def try_format_datetime(datetime)
|
|
|
|
|
datetime.present? ? I18n.l(datetime) : ''
|
|
|
|
|
end
|
2019-12-03 16:02:08 +01:00
|
|
|
|
|
2020-04-07 19:29:14 +02:00
|
|
|
|
def try_format_mois_effectif(etablissement)
|
2020-04-21 20:26:07 +02:00
|
|
|
|
if etablissement.entreprise_effectif_mois.present? && etablissement.entreprise_effectif_annee.present?
|
|
|
|
|
[etablissement.entreprise_effectif_mois, etablissement.entreprise_effectif_annee].join('/')
|
2020-04-07 19:29:14 +02:00
|
|
|
|
else
|
|
|
|
|
''
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-03 16:02:08 +01:00
|
|
|
|
def dismiss_outdated_browser_banner
|
|
|
|
|
cookies[:dismissed_outdated_browser_banner] = {
|
|
|
|
|
value: 'true',
|
|
|
|
|
expires: 1.week.from_now
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def has_dismissed_outdated_browser_banner?
|
|
|
|
|
cookies[:dismissed_outdated_browser_banner] == 'true'
|
|
|
|
|
end
|
2020-03-04 16:23:54 +01:00
|
|
|
|
|
|
|
|
|
def supported_browser?
|
|
|
|
|
BrowserSupport.supported?(browser)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show_outdated_browser_banner?
|
|
|
|
|
!supported_browser? && !has_dismissed_outdated_browser_banner?
|
|
|
|
|
end
|
2022-06-23 11:47:59 +02:00
|
|
|
|
|
|
|
|
|
def vite_legacy?
|
|
|
|
|
if ENV['VITE_LEGACY'] == 'disabled'
|
|
|
|
|
false
|
|
|
|
|
else
|
|
|
|
|
Rails.env.production? || ENV['VITE_LEGACY'] == 'enabled'
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-06-30 10:24:42 +02:00
|
|
|
|
|
|
|
|
|
def ds_vite_legacy_javascript_tag(name, asset_type: :javascript)
|
|
|
|
|
legacy_name = name.sub(/(\..+)|$/, '-legacy\1')
|
|
|
|
|
import_tag = tag(:script) do
|
|
|
|
|
# rubocop:disable Rails/OutputSafety
|
|
|
|
|
"System.import('#{vite_asset_path(legacy_name, type: asset_type)}')".html_safe
|
|
|
|
|
# rubocop:enable Rails/OutputSafety
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
safe_join [ds_vite_legacy_polyfill_tag, import_tag]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Internal: Renders the vite-legacy-polyfill to enable code splitting in
|
|
|
|
|
# browsers that do not support modules.
|
|
|
|
|
def ds_vite_legacy_polyfill_tag
|
|
|
|
|
tag(:script, nil, src: vite_asset_path('legacy-polyfills', type: :virtual))
|
|
|
|
|
end
|
2017-06-21 16:14:41 +02:00
|
|
|
|
end
|