forked from theforeman/foreman
/
layout_helper.rb
179 lines (152 loc) · 6.83 KB
/
layout_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
module LayoutHelper
def title(page_title, page_header = nil)
content_for(:title, page_title.to_s)
@page_header ||= page_header || @content_for_title || page_title.to_s
end
def title_actions(*elements)
content_for(:title_actions) { elements.join(" ").html_safe }
end
def button_group(*elements)
content_tag(:div, :class => "btn-group") { elements.join(" ").html_safe }
end
def search_bar(*elements)
content_for(:search_bar) { elements.join(" ").html_safe }
end
def mount_breadcrumbs(options = {}, &block)
options = BreadcrumbsOptions.new(@page_header, controller, action_name, block_given? ? yield : options)
mount_react_component("BreadcrumbBar", "#breadcrumb", options.bar_props.to_json)
end
def breadcrumbs(options = {}, &block)
content_for(:breadcrumbs) do
mount_breadcrumbs(options, &block)
end
end
def stylesheet(*args)
content_for(:stylesheets) { stylesheet_link_tag(*args.push("data-turbolinks-track" => true)) }
end
def javascript(*args)
content_for(:javascripts) { javascript_include_tag(*args.push("data-turbolinks-track" => true)) }
end
# The target should have class="collapse [out|in]" out means collapsed on load and in means expanded.
# Target must also have a unique id.
def collapsing_header(title, target, collapsed = '')
content_tag(:h2, :class => "expander #{collapsed}", :data => {:toggle => 'collapse', :target => target}) do
content_tag(:span, '', :class => 'caret') + title
end
end
def base_errors_for(obj)
if obj.errors[:base].present?
alert :header => _("Unable to save"),
:class => 'alert-danger base in fade',
:text => obj.errors[:base].map { |e| '<li>'.html_safe + e + '</li>'.html_safe }.join.html_safe
end
end
def popover(title, msg, options = {})
options[:icon] ||= 'info'
options[:kind] ||= 'pficon'
content_tag(:a, icon_text(options[:icon], title, :kind => options[:kind]), { :rel => "popover",
:data => { :content => msg,
:"original-title" => title,
:trigger => "focus",
:container => 'body',
:html => true },
:role => 'button',
:tabindex => '-1' }.deep_merge(options))
end
def icon_text(i, text = "", opts = {})
opts[:kind] ||= "glyphicon"
(content_tag(:span, "", :class => "#{opts[:kind] + ' ' + opts[:kind]}-#{i} #{opts[:class]}", :title => opts[:title]) + " " + text).html_safe
end
def alert(opts = {})
opts[:close] = true if opts[:close].nil?
opts[:header] ||= _("Warning!")
opts[:text] ||= _("Alert")
html_class = "alert #{opts[:class]} "
html_class += 'alert-dismissable' if opts[:close]
content_tag :div, :class => html_class, :id => opts[:id] do
result = "".html_safe
result += alert_close if opts[:close]
result += alert_header(opts[:header], opts[:class])
result += content_tag(:span, opts[:text], :class => 'text')
result += alert_actions(opts[:actions]) if opts[:actions].present?
result
end
end
def alert_header(text, html_class = nil)
case html_class
when /alert-success/
icon = icon_text("ok", "", :kind => "pficon")
text ||= _("Notice")
when /alert-warning/
icon = icon_text("warning-triangle-o", "", :kind => "pficon")
text ||= _("Warning")
when /alert-info/
icon = icon_text("info", "", :kind => "pficon")
text ||= _("Notice")
when /alert-danger/
icon = icon_text("error-circle-o", "", :kind => "pficon")
text ||= _("Error")
end
header = icon.to_s
header += content_tag(:strong, text + ' ') if text.present?
header.html_safe
end
def alert_close(data_dismiss = 'alert')
"<button type='button' class='close' data-dismiss='#{data_dismiss}' aria-hidden='true'>×</button>".html_safe
end
def trunc_with_tooltip(text, length = 32, tooltip_text = "", shorten = true)
text = text.to_s.empty? ? tooltip_text.to_s : text.to_s
tooltip_text = tooltip_text.to_s.empty? ? text : tooltip_text.to_s
options = (shorten && (text.size < length)) ? {} : { :'data-original-title' => tooltip_text, :rel => 'twipsy' }
if shorten
content_tag(:span, truncate(text, :length => length), options).html_safe
else
content_tag(:span, text, options).html_safe
end
end
def alert_actions(actions)
content_tag :div, :class => 'alert-actions' do
'<hr>'.html_safe + actions
end
end
def modal_close(text = _('Close'))
button_tag(text, :class => 'btn btn-default', :data => { :dismiss => 'modal' })
end
def last_days(days)
content_tag(:h6, n_("last %s day", "last %s days", days) % days, :class => 'ca')
end
def fullscreen_button(element = "$(this).prev()")
button_tag(:type => 'button', :class => 'btn btn-default btn-md btn-fullscreen', :onclick => "set_fullscreen(#{element})", :title => _("Full screen")) do
icon_text('expand', '', :kind => 'fa')
end
end
def fullscreen_input(element = "$(this).closest('.input-group').find('input,textarea')")
content_tag(:span, fullscreen_button(element), :class => 'input-group-btn')
end
def new_child_fields_template(form_builder, association, options = { })
unless options[:object].present?
association_object = form_builder.object.class.reflect_on_association(association)
options[:object] = association_object.klass.new(association_object.foreign_key => form_builder.object.id)
end
options[:partial] ||= association.to_s.singularize
options[:form_builder_local] ||= :f
options[:form_builder_attrs] ||= {}
content_tag(:div, :class => "#{association}_fields_template form_template", :style => "display: none;") do
form_builder.fields_for(association, options[:object], :child_index => "new_#{association}") do |f|
render(:partial => options[:partial], :layout => options[:layout],
:locals => { options[:form_builder_local] => f }.merge(options[:form_builder_attrs]))
end
end
end
def render_if_partial_exists(path, f)
render path, :f => f if lookup_context.exists?(path, [], true)
end
def per_page(collection)
per_page = params[:per_page] ? params[:per_page].to_i : Setting[:entries_per_page]
[per_page, collection.total_entries].min
end
private
def table_css_classes(classes = '')
"table table-bordered table-striped table-hover " + classes
end
end