Module: Storefront::Helpers::DomHelper

Included in:
Form::Base
Defined in:
lib/storefront/helpers/dom_helper.rb

Overview

HTML Element Attribute Helper

Instance Method Summary collapse

Instance Method Details

#aria(key) ⇒ Object

Examples:

Aria role main.


aria(:main) #=> {:role => :main}


57
58
59
# File 'lib/storefront/helpers/dom_helper.rb', line 57

def aria(key)
  {:role => key}
end

#clone_attributes(hash) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/storefront/helpers/dom_helper.rb', line 5

def clone_attributes(hash)
  result = {}
  return result if hash.blank?
  hash.each do |key, value|
    result[key] = value.is_a?(::Hash) ? clone_attributes(value) : value
  end
  result
end

#dom(attributes = {}) ⇒ Object

Examples:

Customize <body> tag attributes from inside a partial


# app/views/posts/_post.haml
- dom :body => {:class => "admin", :id => "admin"}

# app/views/layouts/application.haml
%body{dom[:body]}


68
69
70
71
72
# File 'lib/storefront/helpers/dom_helper.rb', line 68

def dom(attributes = {})
  @dom ||= {}
  @dom.deep_merge!(attributes)
  @dom
end

#index_class(index, array_or_length) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/storefront/helpers/dom_helper.rb', line 41

def index_class(index, array_or_length)
  length = array_or_length.is_a?(::Array) ? array_or_length.length : array_or_length
  return nil if length.blank? || (length <= 1)
  
  if index == 0
    "first"
  elsif index == length - 1
    "last"
  else
    nil
  end
end

#merge_class(attributes, *values) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/storefront/helpers/dom_helper.rb', line 21

def merge_class(attributes, *values)
  if values.present?
    if attributes[:class].present?
      classes = attributes[:class].split(/\s+/)
    else
      classes = []
    end
    classes += values.compact.map(&:to_s)
    classes.uniq!
  else
    classes = nil
  end

  if classes.present?
    attributes.merge(:class => classes.join(" "))
  else
    attributes
  end
end

#merge_class!(attributes, *values) ⇒ Object



14
15
16
17
18
19
# File 'lib/storefront/helpers/dom_helper.rb', line 14

def merge_class!(attributes, *values)
  return {} if attributes.nil?
  hash = merge_class(attributes, *values)
  attributes[:class] = hash[:class] if hash
  attributes
end

#page(*args) ⇒ Object

calls model.to_title



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
# File 'lib/storefront/helpers/dom_helper.rb', line 75

def page(*args)
  options = args.extract_options!
  title   = args.shift
  options[:action] ||= storefront_config.current_scope[:action]

  title = case title
  when ::String
    title
  when ::Symbol
    t?(title, :scope => :"pages.titles")
  else
    if options[:title].present?
      options[:title]
    else
      parent_name = !!options[:parent] ? resource_to_title(storefront_config.current_scope[:parent], options) : nil
      resource_name = resource_to_title(storefront_config.current_scope[:resource], options)
      [parent_name, resource_name].compact.join(storefront_config.breadcrumb)
    end
  end

  self.title(options[:browser] || title, options)

  options                                    = header_widget_options(options.merge(:title => title))

  options[:header_html].reverse_merge!(:id   => storefront_config.page_header_id) if storefront_config.page_header_id.present?
  options[:title_html].reverse_merge!(:id    => storefront_config.page_title_id) if storefront_config.page_title_id.present?
  options[:subtitle_html].reverse_merge!(:id => storefront_config.page_subtitle_id) if storefront_config.page_subtitle_id.present?
  options[:level]                            = 1
  options[:skip_format]                      = true

  header_widget options.merge(:title => title)
end

#resource_to_title(resource, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/storefront/helpers/dom_helper.rb', line 119

def resource_to_title(resource, options = {})
  if resource.present?
  
    # handle subdomains as well
    t?(options[:action].to_sym, 
      :scope       => :"pages.titles",
      :model       => Storefront::ModelProxy.model_names(resource), 
      :namespaces  => storefront_config.current_scope[:namespace],
      :allow_blank => true,
      :action      => "",
      :name        => resource.is_a?(::Class) ? resource.name.titleize.pluralize : resource.to_title,
      :clazz       => resource.is_a?(::Class) ? resource.name.titleize : resource.class.name
    )
  end
end

#title_widget(*args, &block) ⇒ Object



108
109
110
111
112
113
# File 'lib/storefront/helpers/dom_helper.rb', line 108

def title_widget(*args, &block)
  options = args.extract_options!
  options[:header_html] ||= {}
  merge_class! options[:header_html], *widget_classes("title-widget")
  header_widget(options, &block)
end

#title_widget_optionsObject



115
116
117
# File 'lib/storefront/helpers/dom_helper.rb', line 115

def title_widget_options
  
end