Module: Maglev::ApplicationHelper

Defined in:
app/helpers/maglev/application_helper.rb

Constant Summary collapse

RTL_UI_LOCALES =
%i[ar he fa ur].freeze

Instance Method Summary collapse

Instance Method Details

#maglev_button_classesObject

rubocop:disable Metrics/MethodLength



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
# File 'app/helpers/maglev/application_helper.rb', line 136

def maglev_button_classes(...)
  ClassVariants.build(
    base: %(
    rounded-sm transition-colors transition-background duration-200 text-center cursor-pointer
    group-[.is-success]/form:bg-green-500 group-[.is-success]/form:hover:bg-green-500/90
    group-[.is-success]/form:disabled:bg-green-500/75
    group-[.is-error]/form:bg-red-500 group-[.is-error]/form:hover:bg-red-500/90
    group-[.is-error]/form:disabled:bg-red-500/75
    ),
    variants: {
      color: {
        primary: 'text-white bg-editor-primary hover:bg-editor-primary/90 disabled:bg-editor-primary/75',
        secondary: 'text-gray-800 hover:bg-gray-100'
      },
      size: {
        big: 'flex items-center justify-center w-full px-6 h-14',
        medium: 'inline-flex items-center justify-center px-4 h-10'
      }
    },
    defaults: {
      color: :primary,
      size: :medium
    }
  ).render(...)
end

#maglev_button_label(label, options = {}) ⇒ Object

UI helpers



126
127
128
129
130
131
132
133
# File 'app/helpers/maglev/application_helper.rb', line 126

def maglev_button_label(label, options = {})
  options = (options || {}).with_indifferent_access

  render 'maglev/editor/shared/button_label', label: label,
                                              pending_label: options[:pending] || label,
                                              success_label: options[:success] || label,
                                              error_label: options[:error] || label
end

#maglev_client_javascript_tagsObject



18
19
20
21
22
# File 'app/helpers/maglev/application_helper.rb', line 18

def maglev_client_javascript_tags
  return '' unless maglev_rendering_mode == :editor

  maglev_importmap_tags(:client, 'maglev-client')
end

#maglev_delayed_stream_tagObject



57
58
59
60
61
62
63
64
65
# File 'app/helpers/maglev/application_helper.rb', line 57

def maglev_delayed_stream_tag
  # since we set the turbo request id before the build of the fetch request, Turbo will set
  # the X-TURBO-REQUEST-ID header to a comma-separated list of request ids
  # we only want to use the first request id (ours), so we split the header value and take the first part
  safe_join [
    tag.meta(name: 'turbo-request-id', content: ::Turbo.current_request_id.split(',').first),
    tag.meta(name: 'turbo-delayed-stream', content: 'true')
  ], "\n"
end

#maglev_editor_directionObject

Editor helpers



68
69
70
# File 'app/helpers/maglev/application_helper.rb', line 68

def maglev_editor_direction
  RTL_UI_LOCALES.include?(editor_ui_locale.to_sym) ? 'rtl' : 'ltr'
end

#maglev_editor_javascript_tagsObject



14
15
16
# File 'app/helpers/maglev/application_helper.rb', line 14

def maglev_editor_javascript_tags
  maglev_importmap_tags(:editor, 'editor')
end

#maglev_editor_logo_urlObject



83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/maglev/application_helper.rb', line 83

def maglev_editor_logo_url
  case maglev_config.
  when nil
    asset_path('maglev/logo.svg')
  when String
    asset_path(maglev_config.)
  when Proc
    instance_exec(maglev_site, &maglev_config.)
  end
end

#maglev_editor_titleObject



72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/maglev/application_helper.rb', line 72

def maglev_editor_title
  case maglev_config.title
  when nil
    'Maglev - EDITOR'
  when String
    maglev_config.title
  when Proc
    instance_exec(maglev_site, &maglev_config.title)
  end
end

#maglev_favicon_urlObject



94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/maglev/application_helper.rb', line 94

def maglev_favicon_url
  case maglev_config.favicon
  when nil
    asset_path('maglev/favicon.svg')
  when String
    asset_path(maglev_config.favicon)
  when Proc
    instance_exec(maglev_site, &maglev_config.favicon)
  end
end

#maglev_flash_messageObject



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/helpers/maglev/application_helper.rb', line 171

def maglev_flash_message
  message, color, icon_name = if flash[:notice].present?
                                [flash[:notice], :green, 'checkbox_circle']
                              elsif flash[:alert].present?
                                [flash[:alert], :red, 'error_warning']
                              end

  return '' if message.blank?

  render Maglev::Uikit::BadgeComponent.new(color: color,
                                           icon_name: icon_name,
                                           disappear_after: 3.seconds).with_content(message)
end

#maglev_icon_button_classesObject

rubocop:enable Metrics/MethodLength



163
164
165
166
167
168
169
# File 'app/helpers/maglev/application_helper.rb', line 163

def maglev_icon_button_classes(...)
  ClassVariants.build(
    base: %(h-7 w-7 flex items-center justify-center rounded-full focus:outline-none
    transition-colors duration-200 cursor-pointer bg-gray-600/0 text-gray-800
    hover:bg-gray-600/10 hover:text-gray-900)
  ).render(...)
end

#maglev_importmap_tags(namespace, entry_point) ⇒ Object



49
50
51
52
53
54
55
# File 'app/helpers/maglev/application_helper.rb', line 49

def maglev_importmap_tags(namespace, entry_point)
  safe_join [
    javascript_inline_importmap_tag(Maglev::Engine.importmaps[namespace].to_json(resolver: self)),
    javascript_importmap_module_preload_tags(Maglev::Engine.importmaps[namespace]),
    javascript_import_module_tag(entry_point)
  ], "\n"
end

#maglev_javascript_importmap_tags(entry_point = 'application', importmap: Rails.application.importmap) ⇒ Object

Use this helper in your theme layout instead of calling javascript_importmap_tags and maglev_client_javascript_tags separately.

Firefox (unlike Chrome) strictly follows the HTML spec which only allows a single