Module: Sibu::PagesHelper

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

Instance Method Summary collapse

Instance Method Details

#available_docsObject



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

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


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

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

#current_elt(elt) ⇒ Object



350
351
352
# File 'app/helpers/sibu/pages_helper.rb', line 350

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

#default_content(type) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'app/helpers/sibu/pages_helper.rb', line 354

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



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

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

#elements(id = nil) ⇒ Object



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

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



346
347
348
# File 'app/helpers/sibu/pages_helper.rb', line 346

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

#elts(id) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
# File 'app/helpers/sibu/pages_helper.rb', line 294

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



246
247
248
249
250
251
# File 'app/helpers/sibu/pages_helper.rb', line 246

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



186
187
188
189
190
191
192
# File 'app/helpers/sibu/pages_helper.rb', line 186

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



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

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



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

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



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/helpers/sibu/pages_helper.rb', line 172

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/sibu/pages_helper.rb', line 137

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) || {}))
  @sb_section = (@sb_section || []) + [t_id]
  if action_name == 'show'
    content["src"] = ("/#{conf[:deployment_path]}" + content["src"]) if @online && conf[:deployment_path]
  else
    opts.merge!({data: {id: @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})
  @sb_section -= [t_id]
  html_output
end

#input_categoriesObject



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

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



339
340
341
342
343
344
# File 'app/helpers/sibu/pages_helper.rb', line 339

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



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

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

#join_tokens(tokens, suffix) ⇒ Object



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

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


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'app/helpers/sibu/pages_helper.rb', line 307

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")
  @sb_section = (@sb_section || []) + [t_id]
  html_opts.merge!({data: {id: @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
  @sb_section -= [t_id]
  html_output
end


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

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


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

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



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

def nested_elements(elt_or_id)
  element_id = elt_id(elt_or_id)
  element_id_tokens = (@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

#render_page_section(s, set_id = false) ⇒ Object



268
269
270
271
272
273
# File 'app/helpers/sibu/pages_helper.rb', line 268

def render_page_section(s, set_id = false)
  @sb_section = [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



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

def sb
  self
end

#sb_pageObject Also known as: page

Note : see ActionView::OutputBuffer



254
255
256
257
# File 'app/helpers/sibu/pages_helper.rb', line 254

def sb_page
  @sb_entity = @page
  self
end

#sb_siteObject Also known as: site



261
262
263
264
# File 'app/helpers/sibu/pages_helper.rb', line 261

def sb_site
  @sb_entity = @site
  self
end

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



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

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



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

def sections_templates
  @site.site_template.available_sections
end

#select_element(id) ⇒ Object



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

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

#sibu_attributes(section, set_id) ⇒ Object

Page sections attrs



276
277
278
279
# File 'app/helpers/sibu/pages_helper.rb', line 276

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



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

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

#site_imagesObject



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

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

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/helpers/sibu/pages_helper.rb', line 156

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



240
241
242
243
244
# File 'app/helpers/sibu/pages_helper.rb', line 240

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