Module: Sibu::PagesHelper

Defined in:
app/helpers/sibu/pages_helper.rb

Instance Method Summary collapse

Instance Method Details

#available_docsObject



27
28
29
# File 'app/helpers/sibu/pages_helper.rb', line 27

def available_docs
  options_from_collection_for_select(Sibu::Document.for_user(sibu_user), :file_url, :file_name, @element["value"])
end


23
24
25
# File 'app/helpers/sibu/pages_helper.rb', line 23

def available_links
  options_from_collection_for_select(@site.pages.order(:name), :id, :name, @element["value"])
end

#current_elt(elt) ⇒ Object



365
366
367
# File 'app/helpers/sibu/pages_helper.rb', line 365

def current_elt(elt)
  (elt.is_a?(Hash) ? elt : (select_element(elt) || {})).except('elements')
end

#default_content(type) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'app/helpers/sibu/pages_helper.rb', line 369

def default_content(type)
  case type
  when "text"
    {"text" => Sibu::DEFAULT_TEXT}
  when "link"
    {"value" => "", "text" => Sibu::DEFAULT_TEXT}
  when "paragraph"
    {"text" => Sibu::DEFAULT_PARAGRAPH}
  when "media"
    {"src" => Sibu::DEFAULT_IMG}
  when "embed"
    {"code" => '<p>Contenu HTML personnalisé (iframe, etc...)</p>'}
  end
end

#each_element(elt_or_id) ⇒ Object



122
123
124
125
126
127
# File 'app/helpers/sibu/pages_helper.rb', line 122

def each_element(elt_or_id)
  element_id = elt_id(elt_or_id)
  push_section_ids(*element_id.split("|")) unless element_id.blank?
  (@sb_entity.elements(*instance_variable_get("@sb_section")) || []).map {|el| yield(el)}
  pop_section_id unless element_id.blank?
end

#elements(id = nil) ⇒ Object



133
134
135
136
# File 'app/helpers/sibu/pages_helper.rb', line 133

def elements(id = nil)
  items = id ? select_element(id)["elements"] : @sb_entity.find_or_init(*@sb_section)["elements"]
  items.blank? ? [{"id" => "el#{Time.current.to_i}"}] : items
end

#elt_id(elt) ⇒ Object



361
362
363
# File 'app/helpers/sibu/pages_helper.rb', line 361

def elt_id(elt)
  elt.is_a?(Hash) ? (elt["data-id"] || elt["id"]) : elt
end

#elts(id) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
# File 'app/helpers/sibu/pages_helper.rb', line 308

def elts(id)
  items = []
  element_id = elt_id(id)
  elemnts = @sb_entity.elements(*(@sb_section + element_id.split("|")).uniq)
  if elemnts
    elemnts.each do |e|
      e["data-id"] = [element_id, e["id"]].join("|")
      items << e
    end
  end
  items.blank? ? [{"id" => element_id.split("|").last, "data-id" => [element_id, "#{element_id}0"].join("|")}] : items
end

#embed(elt, opts = {}) ⇒ Object



257
258
259
260
261
262
# File 'app/helpers/sibu/pages_helper.rb', line 257

def embed(elt, opts = {})
  defaults = {"id" => elt.is_a?(Hash) ? elt["id"] : elt, "code" => '<p>Contenu HTML personnalisé (iframe, etc...)</p>'}
  content = defaults.merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {}))
  opts.merge!({data: {id: elt_id(elt), type: "embed"}}) if action_name != 'show'
  (:div, (:p, raw(content["code"]).html_safe), opts)
end

#empty_tag(elt, tag, type, opts = {}) ⇒ Object



197
198
199
200
201
202
203
# File 'app/helpers/sibu/pages_helper.rb', line 197

def empty_tag(elt, tag, type, opts = {})
  repeat = opts.delete(:repeat)
  defaults = {"id" => elt.is_a?(Hash) ? elt["id"] : elt}.merge!(default_content(type))
  opts = defaults.merge(opts)
  opts.merge!({data: {id: elt_id(elt), type: type, repeat: repeat}}) if action_name != 'show'
  (tag, nil, opts)
end

#form_input(elt, html_opts = {}) ⇒ Object



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
# File 'app/helpers/sibu/pages_helper.rb', line 216

def form_input(elt, html_opts = {})
  repeat = html_opts.delete(:repeat)
  children = html_opts.delete(:children)
  t_id = elt.is_a?(Hash) ? elt["id"] : elt
  opts = {"id" => t_id}.merge(html_opts.stringify_keys).merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {}))
  form_name = opts.delete('form_name') || 'contact'
  field_name = opts.delete('field_name') || 'name'
  name_attr = "#{form_name}[#{field_name}]"

  @sb_section = (@sb_section || []) + [t_id]
  opts.merge!({data: {id: @sb_section[1..-1].join('|'), type: "input", repeat: repeat, children: children}}) if action_name != 'show'

  case field_name
  when 'name', 'address'
    html_output = (:input, nil, opts.merge('type' => 'text', 'name' => name_attr))
  when 'email'
    html_output = (:input, nil, opts.merge('type' => 'email', 'name' => name_attr))
  when 'telephone'
    html_output = (:input, nil, opts.merge('type' => 'tel', 'name' => name_attr))
  when 'arrival_date', 'departure_date'
    html_output = (:input, nil, opts.merge('type' => 'date', 'name' => name_attr))
  when 'people'
    html_output = (:input, nil, opts.merge('type' => 'number', 'name' => name_attr))
  when 'message'
    html_output = (:textarea, nil, opts.merge('name' => name_attr))
  when 'newsletter'
    html_output = (:input, nil, opts.merge('type' => 'checkbox', 'name' => name_attr))
  else
    html_output = (:input, nil, opts.merge('type' => 'text', 'name' => name_attr))
  end

  @sb_section -= [t_id]
  html_output
end

#form_label(elt, html_opts = {}, &block) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'app/helpers/sibu/pages_helper.rb', line 205

def form_label(elt, html_opts = {}, &block)
  t_id = elt.is_a?(Hash) ? elt["id"] : elt
  defaults = {"id" => t_id, "text" => Sibu::DEFAULT_TEXT}
  content = defaults.merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {}))
  @sb_section = (@sb_section || []) + [t_id]
  html_opts.merge!({data: {id: @sb_section[1..-1].join('|'), type: "text"}}) if action_name != 'show'
  html_output = (:label, raw(content["text"]).html_safe, html_opts)
  @sb_section -= [t_id]
  html_output
end

#grp(elt, opts = {}, &block) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/helpers/sibu/pages_helper.rb', line 183

def grp(elt, opts = {}, &block)
  wrapper = opts.delete(:wrapper) || :div
  repeat = opts.delete(:repeat)
  children = opts.delete(:children)
  t_id = elt.is_a?(Hash) ? elt["id"] : elt
  @sb_section = (@sb_section || []) + [t_id]
  defaults = {"id" => t_id}
  opts = defaults.merge(opts)
  opts.merge!({data: {id: @sb_section[1..-1].join('|'), type: "group", repeat: repeat, children: children}}) if action_name != 'show'
  html_output = (wrapper, capture(*elts(elt), &block), opts)
  @sb_section -= [t_id]
  html_output
end

#img(elt, opts = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/helpers/sibu/pages_helper.rb', line 148

def img(elt, opts = {})
  wrapper = opts.delete(:wrapper)
  repeat = opts.delete(:repeat)
  size = opts.delete(:size)
  t_id = elt.is_a?(Hash) ? elt["id"] : elt
  defaults = {"id" => t_id, "src" => Sibu::DEFAULT_IMG}
  content = defaults.merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {}))
  push_section_ids(t_id)
  if action_name == 'show'
    content["src"] = ("/#{conf[:deployment_path]}" + content["src"]) if @online && conf[:deployment_path]
  else
    opts.merge!({data: {id: instance_variable_get("@sb_section")[1..-1].join('|'), type: "media", repeat: repeat, size: size}})
  end
  html_output = wrapper ? (wrapper, (:img, nil, content.except("id")), opts)
      : (:img, nil, content.except("id").merge(opts.stringify_keys) {|k, old, new| k == 'class' ? [old, new].join(' ') : new})
  pop_section_id
  html_output
end

#input_categoriesObject



31
32
33
34
35
36
# File 'app/helpers/sibu/pages_helper.rb', line 31

def input_categories
  options_for_select([['Nom', 'name'], ['Adresse', 'address'], ['Email', 'email'], ['Téléphone', 'telephone'],
                      ['Arrivée', 'arrival_date'], ['Départ', 'departure_date'], ['Nombre de personnes', 'people'],
                      ['Message (texte libre)', 'message'], ['Inscription à la newsletter', 'newsletter']],
                     @element["field_name"])
end

#interactive_map(elt, html_opts = {}) ⇒ Object



354
355
356
357
358
359
# File 'app/helpers/sibu/pages_helper.rb', line 354

def interactive_map(elt, html_opts = {})
  defaults = {"data-lat" => "46.1988027", "data-lng" => "5.1748288"}
  content = defaults.merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {"id" => elt}))
  html_opts.merge!({data: {id: elt_id(elt), type: "map"}}) if action_name != 'show'
  (:div, nil, content.merge(html_opts.stringify_keys))
end

#is_current_page(link_val) ⇒ Object



11
12
13
# File 'app/helpers/sibu/pages_helper.rb', line 11

def is_current_page(link_val)
  /\d+/.match?(link_val) && link_val.to_i == @page.id
end

#join_tokens(tokens, suffix) ⇒ Object



106
107
108
# File 'app/helpers/sibu/pages_helper.rb', line 106

def join_tokens(tokens, suffix)
  (tokens + [suffix]).select {|t| !t.blank?}.join("|")
end


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'app/helpers/sibu/pages_helper.rb', line 321

def link(elt, html_opts = {}, &block)
  repeat = html_opts.delete(:repeat)
  children = html_opts.delete(:children)
  t_id = elt.is_a?(Hash) ? elt["id"] : elt
  defaults = {"id" => t_id, "value" => "", "text" => Sibu::DEFAULT_TEXT}
  link_elt = current_elt(elt)
  content = defaults.merge(link_elt)
  val = content.delete("value") || ""
  text = content.delete("text")
  push_section_ids(t_id)

  html_opts.merge!({data: {id: instance_variable_get("@sb_section")[1..-1].join('|'), type: "link", repeat: repeat, children: children}}) if action_name != 'show'
  if val.to_s.start_with?('http')
    content["href"] = val
  elsif val.to_s.start_with?('#')
    content["href"] = val
    content.delete("target")
  elsif val.to_s.start_with?('/')
    content["href"] = val
  elsif val.to_s.include?('@')
    content["href"] = 'mailto:' + val
  else
    content["href"] = @links.keys.include?(val.to_s) ? (action_name == 'show' ? link_path(val) : site_page_edit_content_path(@site.id, val)) : '#'
  end
  if block_given?
    html_output = (:a, capture(link_elt, nested_elements(elt), &block), content.merge(html_opts).except("elements"))
  else
    html_output = (:a, raw(text), content.merge(html_opts).except("elements"))
  end
  pop_section_id
  html_output
end


6
7
8
9
# File 'app/helpers/sibu/pages_helper.rb', line 6

def link_path(page_id)
  p = @site.page_by_id(page_id)
  p ? (request.host == conf[:host] ? site_page_path(@site.id, p.id) : (conf[:deployment_path] ? "/#{conf[:deployment_path]}/#{p.path}" : "/#{p.path}")) : "#"
end


38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/sibu/pages_helper.rb', line 38

def link_type(val)
  if val.blank? || /^\d+$/.match(val.to_s)
    'internal'
  elsif Sibu::Document.for_user(sibu_user).map {|d| d.file_url}.include?(val)
    'document'
  elsif val.to_s.start_with?('http', '#', '/')
    'external'
  else
    'email'
  end
end

#nested_elements(elt_or_id) ⇒ Object

Note : the best option is probably a “section” helper that is instantiated from the section hash and that provides all the logic for elements manipulation (ex: section.h3(), section.elements, section.elements(nested_id), etc…) That will also remove the entity type references (site / page) from the partials (but is still has to be figured out by Sibu) Note : add “each_with_elements” and “elements” on section/elts enumerables



114
115
116
117
118
119
120
# File 'app/helpers/sibu/pages_helper.rb', line 114

def nested_elements(elt_or_id)
  element_id = elt_id(elt_or_id)
  element_id_tokens = (instance_variable_get("@sb_section") + element_id.split("|")).uniq
  nested_elts = @sb_entity.elements(*element_id_tokens)
  nested_elts.blank? ? [{"id" => element_id.split("|").last, "data-id" => join_tokens(element_id_tokens[1..-1], "#{element_id}0")}] :
      nested_elts.map {|e| e.merge({"data-id" => join_tokens(element_id_tokens[1..-1], e['id'])})}
end

#pop_section_idObject



143
144
145
146
# File 'app/helpers/sibu/pages_helper.rb', line 143

def pop_section_id
  instance_variable_set("@sb_section", instance_variable_get("@sb_section")[0..-2])
  # Rails.logger.debug "popped section - now #{instance_variable_get("@sb_section")}"
end

#push_section_ids(*section_ids) ⇒ Object



138
139
140
141
# File 'app/helpers/sibu/pages_helper.rb', line 138

def push_section_ids(*section_ids)
  # Rails.logger.debug "push section ids #{section_ids}"
  instance_variable_set("@sb_section", (instance_variable_get("@sb_section") || []) + section_ids)
end

#render_page_section(s, set_id = false) ⇒ Object

Resets section ids to clean up remaining ids



280
281
282
283
284
285
286
# File 'app/helpers/sibu/pages_helper.rb', line 280

def render_page_section(s, set_id = false)
  instance_variable_set("@sb_section", [])
  push_section_ids(s['id'])
  @sb_entity = @page
  render partial: "shared/#{@site.section_template(s)}",
         locals: {sibu: self, sibu_section: s, sibu_attrs: sibu_attributes(s, set_id).html_safe}
end

#sbObject



102
103
104
# File 'app/helpers/sibu/pages_helper.rb', line 102

def sb
  self
end

#sb_pageObject Also known as: page

Note : see ActionView::OutputBuffer



265
266
267
268
# File 'app/helpers/sibu/pages_helper.rb', line 265

def sb_page
  @sb_entity = @page
  self
end

#sb_siteObject Also known as: site



272
273
274
275
# File 'app/helpers/sibu/pages_helper.rb', line 272

def sb_site
  @sb_entity = @site
  self
end

#section(id, tag, html_opts = {}, &block) ⇒ Object



302
303
304
305
306
# File 'app/helpers/sibu/pages_helper.rb', line 302

def section(id, tag, html_opts = {}, &block)
  @sb_section = [id]
  opts = action_name != 'show' ? html_opts.merge({"data-sb-id" => id, "data-sb-entity" => @sb_entity == @site ? 'site' : 'page'}) : html_opts
  (tag, capture(self, &block), opts)
end

#sections_templatesObject



15
16
17
# File 'app/helpers/sibu/pages_helper.rb', line 15

def sections_templates
  @site.site_template.available_sections
end

#select_element(id) ⇒ Object



129
130
131
# File 'app/helpers/sibu/pages_helper.rb', line 129

def select_element(id)
  @sb_entity.element(*instance_variable_get("@sb_section"), id)
end

#sibu_attributes(section, set_id) ⇒ Object

Page sections attrs



289
290
291
292
# File 'app/helpers/sibu/pages_helper.rb', line 289

def sibu_attributes(section, set_id)
  attrs = set_id ? ('id="' + section['id'] + '" ') : ''
  attrs + (action_name != 'show' ? ('data-sb-id="' + section['id'] + '" data-sb-entity="page"') : '')
end

#sibu_attrs(section_id) ⇒ Object

Site sections attrs - resets section ids



295
296
297
298
299
300
# File 'app/helpers/sibu/pages_helper.rb', line 295

def sibu_attrs(section_id)
  instance_variable_set("@sb_section", [])
  push_section_ids(section_id)
  @sb_entity = @site
  action_name != 'show' ? ('data-sb-id="' + section_id + '" data-sb-entity="site"').html_safe : ''
end

#site_imagesObject



19
20
21
# File 'app/helpers/sibu/pages_helper.rb', line 19

def site_images
  Sibu::Image.for_user(sibu_user).uniq
end

#svg_image(elt, opts = {}) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/helpers/sibu/pages_helper.rb', line 167

def svg_image(elt, opts = {})
  size = opts.delete(:size)
  t_id = elt.is_a?(Hash) ? elt["id"] : elt
  defaults = {"id" => t_id, "xlink:href" => Sibu::DEFAULT_IMG}
  content = defaults.merge((elt.is_a?(Hash) ? elt : (select_element(elt) || {})).transform_keys {|k| k == 'src' ? 'xlink:href' : k})
  @sb_section = (@sb_section || []) + [t_id]
  if action_name == 'show'
    content["xlink:href"] = ("/#{conf[:deployment_path]}" + content["xlink:href"]) if @online && conf[:deployment_path]
  else
    opts.merge!({data: {id: @sb_section[1..-1].join('|'), type: "media", repeat: false, size: size}})
  end
  html_output = (:image, nil, content.except("id").merge(opts.stringify_keys) {|k, old, new| k == 'class' ? [old, new].join(' ') : new})
  @sb_section -= [t_id]
  html_output
end

#widget(elt, widget_type, opts = {}, &block) ⇒ Object



251
252
253
254
255
# File 'app/helpers/sibu/pages_helper.rb', line 251

def widget(elt, widget_type, opts = {}, &block)
  content = elt.is_a?(Hash) ? elt : (select_element(elt) || {})
  opts.merge!({data: {id: elt_id(elt), type: "widget_#{widget_type.to_s.split('::').last.underscore}"}}) if action_name != 'show'
  (:div, capture(widget_type.new(content), &block), opts)
end