Module: CamaleonCms::HtmlHelper

Defined in:
app/helpers/camaleon_cms/html_helper.rb

Instance Method Summary collapse

Instance Method Details

#append_asset_content(content) ⇒ Object

add asset content into custom assets content may be: content may be: this will be printed with <%raw cama_draw_custom_assets %>



65
66
67
# File 'app/helpers/camaleon_cms/html_helper.rb', line 65

def append_asset_content(content)
  camaleon_html_helper_state[:assets_content] << content
end

#append_asset_libraries(libraries) ⇒ Object Also known as: cama_load_custom_assets

add custom asset libraries (js, css or both) for the current request, also you can add extra css or js files for existent libraries sample: (add a new library) append_asset_libraries( { "my_library_key"=> { js: [plugin_asset("js/my_js"), "plugins/myplugin/assets/js/my_js2"], css: [plugin_asset("css/my_css"), "plugins/myplugin/assets/css/my_css2"] } } ) sample: (update existent library) append_asset_libraries({"colorpicker"=>{js: [plugin_asset("js/my_custom_js")] } }) return nil



49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/camaleon_cms/html_helper.rb', line 49

def append_asset_libraries(libraries)
  state = camaleon_html_helper_state
  libraries.each do |key, library|
    state[:assets_libraries][key.to_sym] = if state[:assets_libraries].include?(key)
                                             state[:assets_libraries][key.to_sym].merge(library)
                                           else
                                             library
                                           end
  end
end

#append_pre_asset_content(content) ⇒ Object

add pre asset content into custom assets content may be: content may be: this will be printed before assets_library with <%raw cama_draw_pre_asset_contents %>



73
74
75
# File 'app/helpers/camaleon_cms/html_helper.rb', line 73

def append_pre_asset_content(content)
  camaleon_html_helper_state[:pre_assets_content] << content
end

#cama_assets_library_register(key, assets = {}) ⇒ Object

register a new asset library to be included on demand calling by: cama_load_libraries(...) sample: cama_assets_library_register("my_library", ["url_js", "url_js2"], css: ["url_css1", "url_css2"]) cama_load_libraries("my_library")



13
14
15
16
17
18
19
20
# File 'app/helpers/camaleon_cms/html_helper.rb', line 13

def cama_assets_library_register(key, assets = {})
  key = key.to_sym
  cama_assets_libraries
  state = camaleon_html_helper_state
  state[:cama_assets_libraries][key] = { css: [], js: [] } if state[:cama_assets_libraries][key].blank?
  state[:cama_assets_libraries][key][:css] += assets[:css] if assets[:css].present?
  state[:cama_assets_libraries][key][:js] += assets[:js] if assets[:js].present?
end

#cama_draw_custom_assetsObject

return all js libraries added [aa.js, bb,js, ..] def get_assets_js



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
# File 'app/helpers/camaleon_cms/html_helper.rb', line 86

def cama_draw_custom_assets
  state = camaleon_html_helper_state
  cama_html_helpers_init if state[:assets_libraries].blank?
  libs = []
  state[:assets_libraries].each_value do |assets|
    libs += assets[:css] if assets[:css].present?
  end
  stylesheets = libs.uniq
  css = stylesheet_link_tag(*stylesheets, media: 'all')

  libs = []
  state[:assets_libraries].each_value do |assets|
    libs += assets[:js] if assets[:js].present?
  end
  javascripts = libs.uniq
  js = javascript_include_tag(*javascripts)

  args = { stylesheets: stylesheets, javascripts: javascripts, js_html: js, css_html: css }
  hooks_run('draw_custom_assets', args)
  # rubocop:disable Rails/OutputSafety -- Asset helper output and appended fragments are trusted framework/plugin markup.
  trusted_fragments = [args[:css_html], args[:js_html], *state[:assets_content]].filter_map do |fragment|
    next if fragment.blank?

    fragment.is_a?(ActiveSupport::SafeBuffer) ? fragment : fragment.to_s.html_safe
  end
  # rubocop:enable Rails/OutputSafety

  safe_join(trusted_fragments, "\n")
end

#cama_draw_pre_asset_contentsObject

return all scripts to be executed before import the js libraries(cama_draw_custom_assets)



78
79
80
81
82
# File 'app/helpers/camaleon_cms/html_helper.rb', line 78

def cama_draw_pre_asset_contents
  # rubocop:disable Rails/OutputSafety -- Callers append trusted script/style fragments that must render as markup.
  camaleon_html_helper_state[:pre_assets_content].join('').html_safe
  # rubocop:enable Rails/OutputSafety
end

#cama_html_helpers_initObject



3
4
5
6
7
8
# File 'app/helpers/camaleon_cms/html_helper.rb', line 3

def cama_html_helpers_init
  state = camaleon_html_helper_state
  state[:pre_assets_content] = [] # Assets contents before the libraries import
  state[:assets_libraries] = {}
  state[:assets_content] = []
end

#cama_html_tooltip(text = 'Tooltip', location = 'left') ⇒ Object

create an HTML tooltip to include anywhere text: text of the tooltip location: location of the tooltip (left | right | top |bottom)



119
120
121
122
123
# File 'app/helpers/camaleon_cms/html_helper.rb', line 119

def cama_html_tooltip(text = 'Tooltip', location = 'left')
  tag.a(href: 'javascript:;', title: text, data: { toggle: 'tooltip', placement: location }) do
    tag.i(class: 'fa fa-info-circle')
  end
end

#cama_load_libraries(*keys) ⇒ Object Also known as: add_asset_library

enable to load admin or registered libraries (colorpicker, datepicker, tinymce, form_ajax, cropper) sample: add_asset_library("datepicker", "colorpicker") This will add this assets library in the admin head or in a custom place by calling: cama_draw_custom_assets()



25
26
27
28
29
30
31
# File 'app/helpers/camaleon_cms/html_helper.rb', line 25

def cama_load_libraries(*keys)
  state = camaleon_html_helper_state
  keys.each do |key|
    library = cama_assets_libraries[key.to_sym]
    state[:assets_libraries][key.to_sym] = library if library.present?
  end
end