-
-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathbase_helper.rb
More file actions
307 lines (287 loc) · 10.9 KB
/
base_helper.rb
File metadata and controls
307 lines (287 loc) · 10.9 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# frozen_string_literal: true
module Alchemy
module Admin
# This module contains helper methods for rendering dialogs and confirmation windows.
#
# The most important helpers for module developers are:
#
# * {#link_to_dialog}
# * {#link_to_confirm_dialog}
#
module BaseHelper
include Alchemy::BaseHelper
include Alchemy::Admin::NavigationHelper
# Returns a string showing the name of the currently logged in user.
#
# In order to represent your own +User+'s class instance,
# you should add a +alchemy_display_name+ method to your +User+ class
#
def current_alchemy_user_name
name = current_alchemy_user.try(:alchemy_display_name)
if name.present?
content_tag :span, class: "current-user-name" do
"#{render_icon(:user, size: "1x")} #{name}".html_safe
end
end
end
# This helper renders the link to an dialog.
#
# We use this for our fancy modal dialogs in the Alchemy cockpit.
#
# == Example
#
# <%= link_to_dialog('Edit', edit_product_path, {size: '200x300'}, {class: 'icon_button'}) %>
#
# @param [String] content
# The string inside the link tag
# @param [String or Hash] url
# The url of the action displayed inside the dialog.
# @param [Hash] options
# options for the dialog.
# @param [Hash] html_options
# HTML options passed to the <tt>link_to</tt> helper
#
# @option options [String] :size
# String with format of "WidthxHeight". I.E. ("420x280")
# @option options [String] :title
# Text for the dialog title bar.
# @option options [Boolean] :modal (true)
# Show as modal window.
#
def link_to_dialog(content, url, options = {}, html_options = {})
default_options = {modal: true}
options = default_options.merge(options)
if html_options[:title]
tooltip = html_options.delete(:title)
end
anchor = if url.nil?
tag.a(content, class: "disabled #{html_options[:class]}".strip, tabindex: "-1")
else
link_to(content, url, html_options.merge(
"data-dialog-options" => options.to_json,
:is => "alchemy-dialog-link"
))
end
if tooltip
content_tag("sl-tooltip", anchor, content: tooltip)
else
anchor
end
end
def alchemy_admin_js_translations(locale = ::I18n.locale)
render partial: "alchemy/admin/translations/#{locale}", formats: [:js]
rescue ActionView::MissingTemplate
# Fallback to default translations
render partial: "alchemy/admin/translations/en", formats: [:js]
end
# Used for site selector in Alchemy cockpit.
def sites_for_select
Alchemy::Site.all.map do |site|
[site.name, site.id]
end
end
# Returns a link that opens a modal confirmation to delete window.
#
# === Example:
#
# <%= link_to_confirm_dialog('delete', 'Do you really want to delete this comment?', '/admin/comments/1') %>
#
# @param [String] link_string
# The content inside the <a> tag
# @param [String] message
# The message that is displayed in the dialog
# @param [String] url
# The url that gets opened after confirmation (Note: This is an Ajax request with a method of DELETE!)
# @param [Hash] html_options
# HTML options get passed to the link
#
# @option html_options [String] :title (Alchemy.t(:please_confirm))
# The dialog title
# @option html_options [String] :message (message)
# The message displayed in the dialog
# @option html_options [String] :ok_label (Alchemy.t("Yes"))
# The label for the ok button
# @option html_options [String] :cancel_label (Alchemy.t("No"))
# The label for the cancel button
#
def link_to_confirm_dialog(link_string = "", message = "", url = "", html_options = {})
link_to(link_string, url,
html_options.merge(
data: {
"turbo-method": :delete,
"turbo-confirm": message
}
))
end
# Returns a form and a button that opens a modal confirm dialog.
#
# After confirmation it proceeds to send the form's action.
#
# === Example:
#
# <%= button_with_confirm('pay', '/admin/orders/1/pay', message: 'Do you really want to mark this order as payed?') %>
#
# @param [String] value
# The content inside the <tt><a></tt> tag
# @param [String] url
# The url that gets opened after confirmation
# @param [Hash] options
# Options for the Alchemy confirm dialog (see also +app/assets/javascripts/alchemy/alchemy.confirm_dialog.js.coffee+)
# @param [Hash] html_options
# HTML options that get passed to the +button_tag+ helper.
#
# @note The method option in the <tt>html_options</tt> hash gets passed to the <tt>form_tag</tt> helper!
#
def button_with_confirm(value = "", url = "", options = {}, html_options = {})
options = {
message: Alchemy.t(:confirm_to_proceed),
title: Alchemy.t(:please_confirm)
}.merge(options)
form_tag url, {method: html_options.delete(:method), class: "button-with-confirm"} do
button_tag value, html_options.merge("data-turbo-confirm" => options[:message])
end
end
# A delete button with confirmation window.
#
# @option title [String]
# The title for the confirm dialog
# @option message [String]
# The message for the confirm dialog
# @option icon [String]
# The icon class for the button
#
def delete_button(url, options = {}, html_options = {})
options = {
title: Alchemy.t("Delete"),
message: Alchemy.t("Are you sure?"),
icon: "delete-bin-2"
}.merge(options)
if html_options[:title]
tooltip = html_options.delete(:title)
end
button = button_with_confirm(
render_icon(options[:icon]),
url, options, {
method: "delete",
class: "icon_button #{html_options.delete(:class)}".strip
}.merge(html_options)
)
if tooltip
content_tag("sl-tooltip", button, content: tooltip)
else
button
end
end
# (internal) Renders translated Module Names for html title element.
def render_alchemy_title
title = if content_for?(:title)
content_for(:title)
else
Alchemy.t(controller_name, scope: :modules)
end
"Alchemy CMS - #{title}"
end
# Renders a textfield ready to display a datepicker
#
# A Javascript observer converts this into a fancy Datepicker.
# If you pass +'datetime'+ as +:type+ the datepicker will also have a Time select.
# If you pass +'time'+ as +:type+ the datepicker will only have a Time select.
#
# This helper always renders "text" as input type because:
# HTML5 supports input types like 'date' but Browsers are using the users OS settings
# to validate the input format. Since Alchemy is localized in the backend the date formats
# should be aligned with the users locale setting in the backend but not the OS settings.
#
# === Date Example
#
# <%= alchemy_datepicker(@person, :birthday) %>
#
# === Datetime Example
#
# <%= alchemy_datepicker(@page, :public_on, type: 'datetime') %>
#
# === Time Example
#
# <%= alchemy_datepicker(@meeting, :starts_at, type: 'time') %>
#
# @param [ActiveModel::Base] object
# An instance of a model
# @param [String or Symbol] method
# The attribute method to be called for the date value
#
# @option html_options [String] :data-datepicker-type (type)
# The value of the data attribute for the type
# @option html_options [String] :class (type)
# CSS classes of the input field
# @option html_options [String] :value (value of method on object)
# The value the input displays. If you pass a String its parsed with +Time.parse+
#
def alchemy_datepicker(object, method, html_options = {})
type = html_options.delete(:type) || "date"
date = html_options.delete(:value) || object.send(method.to_sym).presence
date = Time.zone.parse(date) if date.is_a?(String)
value = date&.iso8601
input_field = text_field object.class.name.demodulize.underscore.to_sym,
method.to_sym, {type: "text", class: type, value: value}.merge(html_options)
content_tag("alchemy-datepicker", input_field, "input-type" => type)
end
# Render a hint icon with tooltip for given object.
# The model class needs to include the hints module
def render_hint_for(element, icon_options = {})
return unless element.has_hint?
content_tag "sl-tooltip", class: "like-hint-tooltip", placement: "bottom-start" do
render_icon("question", icon_options) +
content_tag(:span, element.hint.html_safe, slot: "content")
end
end
# Appends the current controller and action to body as css class.
def alchemy_body_class
[
controller_name,
action_name,
content_for(:main_menu_style),
content_for(:alchemy_body_class),
"elements-window-visible"
].compact
end
# (internal) Returns options for the clipboard select tag
def clipboard_select_tag_options(items)
options = items.map do |item|
name = if item.respond_to?(:display_name_with_preview_text)
item.display_name_with_preview_text
else
item.name
end
[name, item.id]
end
options_for_select(options)
end
# Returns the regular expression used for external url validation in link dialog.
def link_url_regexp
Alchemy.config.format_matchers.link_url || /^(mailto:|\/|[a-z]+:\/\/)/
end
# Renders a hint with tooltip
#
# == Example
#
# <%= hint_with_tooltip('Page layout is missing', icon: 'info') %>
#
# @param text [String] - The text displayed in the tooltip
# @param icon: 'alert' [String] - Icon name
#
# @return [String]
def hint_with_tooltip(text, icon: "alert", icon_class: nil)
content_tag :"sl-tooltip", class: "like-hint-tooltip", content: text, placement: "bottom" do
render_icon(icon, class: icon_class)
end
end
# Renders a warning icon with a hint
# that explains the user that the page layout is missing
def page_layout_missing_warning
hint_with_tooltip(
Alchemy.t(:page_definition_missing)
)
end
end
end
end