41
42
43
44
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
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/toastify/application_helper.rb', line 41
def toastify_script_tag
config = Rails.application.config.try(:toastify) || {}
default_position = config[:position] || "top-right"
default_auto_close = config[:auto_close] || 5000
default_theme = config[:theme] || "light"
default_transition = config[:transition] || "slide"
default_close_button = config.key?(:close_button) ? config[:close_button] : true
default_pause_on_hover = config.key?(:pause_on_hover) ? config[:pause_on_hover] : true
default_draggable = config.key?(:draggable) ? config[:draggable] : true
type_map = {
"notice" => "info",
"success" => "success",
"alert" => "warning",
"error" => "error",
"info" => "info",
"warning" => "warning",
}
script_lines = []
flash.each do |flash_type, message|
next if flash_type.to_s.start_with?("toast_")
type = type_map.fetch(flash_type.to_s, "default")
position = flash[:toast_position] || default_position
auto_close = flash[:toast_duration] || default_auto_close
theme = flash[:toast_theme] || default_theme
transition = flash[:toast_transition] || default_transition
close_button = flash[:toast_close_button].nil? ? default_close_button : flash[:toast_close_button]
pause_on_hover = flash[:toast_pause_on_hover].nil? ? default_pause_on_hover : flash[:toast_pause_on_hover]
draggable = flash[:toast_draggable].nil? ? default_draggable : flash[:toast_draggable]
safe_message = j(message.to_s)
script_lines << "window.Toastify && window.Toastify.show('#{safe_message}', { type: '#{j(type.to_s)}', position: '#{j(position.to_s)}', autoClose: #{auto_close.to_i}, theme: '#{j(theme.to_s)}', transition: '#{j(transition.to_s)}', closeButton: #{!!close_button}, pauseOnHover: #{!!pause_on_hover}, draggable: #{!!draggable} });"
end
flash.keys.reject { |type| type.to_s.start_with?("toast_") }.each do |type|
flash.discard(type)
end
return nil if script_lines.empty?
html = []
html << "<script type=\"module\">"
html << " " + script_lines.join("\n ")
html << "</script>"
html.join("\n").html_safe
end
|