storefront 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/storefront.rb +1 -0
- data/lib/storefront/components/form.rb +2 -4
- data/lib/storefront/components/form/base.rb +40 -38
- data/lib/storefront/components/form/builder.rb +98 -96
- data/lib/storefront/components/form/errors.rb +22 -20
- data/lib/storefront/components/form/field.rb +75 -73
- data/lib/storefront/components/form/fieldset.rb +32 -30
- data/lib/storefront/components/form/hint.rb +23 -21
- data/lib/storefront/components/form/input.rb +143 -141
- data/lib/storefront/components/form/inputs.rb +4 -2
- data/lib/storefront/components/form/inputs/checkbox.rb +8 -6
- data/lib/storefront/components/form/inputs/date.rb +9 -7
- data/lib/storefront/components/form/inputs/file.rb +8 -6
- data/lib/storefront/components/form/inputs/hidden.rb +7 -5
- data/lib/storefront/components/form/inputs/radio.rb +6 -4
- data/lib/storefront/components/form/inputs/range.rb +8 -6
- data/lib/storefront/components/form/inputs/select.rb +52 -50
- data/lib/storefront/components/form/inputs/string.rb +64 -62
- data/lib/storefront/components/form/inputs/submit.rb +17 -15
- data/lib/storefront/components/form/inputs/textarea.rb +13 -11
- data/lib/storefront/components/form/inputs/value.rb +11 -9
- data/lib/storefront/components/form/label.rb +24 -22
- metadata +1 -1
@@ -1,40 +1,42 @@
|
|
1
1
|
module Storefront
|
2
|
-
|
3
|
-
class
|
4
|
-
|
2
|
+
module Components
|
3
|
+
class Form
|
4
|
+
class Fieldset < Storefront::Components::Form::Base
|
5
|
+
attr_reader :builder
|
5
6
|
|
6
|
-
|
7
|
-
|
7
|
+
def initialize(options = {})
|
8
|
+
super
|
8
9
|
|
9
|
-
|
10
|
+
@label = localize(:titles, options[:label], nil, (attributes[:locale_options] || {}).merge(:allow_blank => true)) if options[:label].present?
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
merge_class! attributes, *[
|
13
|
+
config.fieldset_class
|
14
|
+
]
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
attributes[:id] ||= label.underscore.strip.gsub(/[_\s]+/, config.separator) if label.present?
|
17
|
+
attributes.delete(:index)
|
18
|
+
attributes.delete(:parent_index)
|
19
|
+
attributes.delete(:label)
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
21
|
+
@builder = Storefront::Components::Form::Builder.new(
|
22
|
+
:template => template,
|
23
|
+
:model => model,
|
24
|
+
:attribute => attribute,
|
25
|
+
:index => index,
|
26
|
+
:parent_index => parent_index
|
27
|
+
)
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
30
|
+
# form.inputs :basic_info, :locale_options => {:count => 1, :past => true}
|
31
|
+
def render(&block)
|
32
|
+
template.capture_haml do
|
33
|
+
template.haml_tag :fieldset, attributes do
|
34
|
+
template.haml_tag :legend, :class => config.legend_class do
|
35
|
+
template.haml_tag :span, label
|
36
|
+
end if label.present?
|
37
|
+
template.haml_tag config.field_list_tag, :class => config.list_class do
|
38
|
+
builder.render(&block)
|
39
|
+
end
|
38
40
|
end
|
39
41
|
end
|
40
42
|
end
|
@@ -1,31 +1,33 @@
|
|
1
1
|
module Storefront
|
2
|
-
|
3
|
-
class
|
4
|
-
|
5
|
-
|
2
|
+
module Components
|
3
|
+
class Form
|
4
|
+
class Hint < Storefront::Components::Form::Base
|
5
|
+
def initialize(options = {})
|
6
|
+
super
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
attributes.merge!(options[:hint_html]) if options[:hint_html]
|
9
|
+
attributes.delete :hint_html
|
10
|
+
attributes.delete :hint
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
unless options[:hint] == false
|
13
|
+
merge_class! attributes, config.hint_class
|
14
|
+
attributes[:id] ||= attribute.to_id(:type => :hint, :index => index, :parent_index => parent_index) if config.id_enabled_on.include?("hint")
|
15
|
+
if config.include_aria && config.hint_is_popup
|
16
|
+
attributes[:role] ||= :tooltip
|
17
|
+
end
|
18
|
+
@value = localize(:hints, attribute.name, options[:hint].is_a?(::String) ? options[:hint] : nil, :allow_blank => true)
|
16
19
|
end
|
17
|
-
@value = localize(:hints, attribute.name, options[:hint].is_a?(::String) ? options[:hint] : nil, :allow_blank => true)
|
18
20
|
end
|
19
|
-
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def render(&block)
|
23
|
+
template.capture_haml do
|
24
|
+
if value.present?
|
25
|
+
template.haml_tag config.hint_tag, attributes do
|
26
|
+
template.haml_concat value.html_safe.gsub(/\n$/, "")
|
27
|
+
end
|
28
|
+
elsif config.always_include_hint_tag
|
29
|
+
template.haml_tag config.hint_tag, attributes
|
26
30
|
end
|
27
|
-
elsif config.always_include_hint_tag
|
28
|
-
template.haml_tag config.hint_tag, attributes
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
@@ -1,192 +1,194 @@
|
|
1
1
|
module Storefront
|
2
|
-
|
3
|
-
class
|
4
|
-
class
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
2
|
+
module Components
|
3
|
+
class Form
|
4
|
+
class Input < Storefront::Components::Form::Base
|
5
|
+
class << self
|
6
|
+
attr_accessor :resolves
|
7
|
+
|
8
|
+
def resolves(*input_types)
|
9
|
+
@resolves ||= []
|
10
|
+
@resolves = @resolves.concat(input_types.flatten.compact).uniq
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
def resolves?(name)
|
14
|
+
@resolves ||= []
|
15
|
+
@resolves.include?(name)
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
def registry
|
19
|
+
@registry ||= Dir[::File.join(::File.dirname(__FILE__), "inputs", "*")].map do |path|
|
20
|
+
next unless ::File.extname(path) == ".rb"
|
21
|
+
"Storefront::Components::Form::#{::File.basename(path, ::File.extname(path)).camelize}".constantize
|
22
|
+
end.compact
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
def find(name)
|
26
|
+
registry.detect do |input_class|
|
27
|
+
input_class.resolves?(name)
|
28
|
+
end
|
27
29
|
end
|
28
30
|
end
|
29
|
-
end
|
30
31
|
|
31
|
-
|
32
|
-
|
32
|
+
def initialize(options = {})
|
33
|
+
super
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
# input type
|
36
|
+
options[:as] ||= attribute.input_type(options)
|
37
|
+
@input_type = options[:as]
|
38
|
+
@include_blank = options[:include_blank] != false
|
39
|
+
@value = options[:value]
|
40
|
+
@dynamic = options[:dynamic] == true
|
41
|
+
@rich_input = options.has_key?(:rich_input) ? !!options[:rich_input] : config.rich_input
|
41
42
|
|
42
|
-
|
43
|
+
@validate = attributes.delete(:validate) != false
|
43
44
|
|
44
|
-
|
45
|
+
classes = [input_type, self.class.name.split("::").last.underscore, attribute.name.to_s.underscore.strip.gsub(/[_\s]+/, config.separator)]
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
47
|
+
if options[:input_html].present?
|
48
|
+
classes << options[:input_html].delete(:class)
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
unless [:submit].include?(input_type)
|
52
|
+
classes += [
|
53
|
+
attribute.required? ? config.required_class : config.optional_class,
|
54
|
+
attribute.errors? ? config.error_class : config.valid_class,
|
55
|
+
"input"
|
56
|
+
]
|
56
57
|
|
57
|
-
|
58
|
-
|
58
|
+
if @validate && attribute.validations.present?
|
59
|
+
classes << config.validate_class
|
60
|
+
end
|
59
61
|
end
|
60
|
-
end
|
61
62
|
|
62
|
-
|
63
|
-
|
63
|
+
# class
|
64
|
+
merge_class! attributes, *classes.compact.uniq.map(&:to_s)
|
64
65
|
|
65
|
-
|
66
|
-
|
66
|
+
# id
|
67
|
+
attributes[:id] ||= attribute.to_id(:index => index, :parent_index => parent_index) if config.id_enabled_on.include?("input")
|
67
68
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
69
|
+
# validations
|
70
|
+
if @validate
|
71
|
+
attributes.merge!(attribute.validations) if config.inline_validations && attribute.validations.present?
|
72
|
+
end
|
73
|
+
attributes[:placeholder] = options[:placeholder] if options[:placeholder].present?
|
73
74
|
|
74
|
-
|
75
|
-
|
75
|
+
# name
|
76
|
+
attributes[:name] ||= attribute.to_param(:index => index, :parent_index => parent_index)
|
76
77
|
|
77
|
-
|
78
|
-
|
78
|
+
# value
|
79
|
+
attributes[:value] ||= attribute.value(options[:value])
|
79
80
|
|
80
|
-
|
81
|
-
|
81
|
+
# attributes[:tabindex] = @tabindex
|
82
|
+
attributes[:maxlength] = options[:max] if options[:max].present?
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
84
|
+
# expressions
|
85
|
+
pattern = options[:match]
|
86
|
+
pattern = pattern.source if pattern.is_a?(::Regexp)
|
87
|
+
attributes[:"data-match"] = pattern if pattern.present?
|
88
|
+
attributes[:"aria-required"] = attribute.required.to_s if attribute.required?
|
88
89
|
|
89
|
-
|
90
|
-
|
91
|
-
|
90
|
+
# access key
|
91
|
+
access_key = attributes[:accesskey] || attribute.access_key
|
92
|
+
attributes[:accesskey] = access_key
|
92
93
|
|
93
|
-
|
94
|
+
attributes.merge!(options[:input_html]) if options[:input_html]
|
94
95
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
96
|
+
attributes.delete :include_blank
|
97
|
+
attributes.delete :input_html
|
98
|
+
attributes.delete :include_template
|
99
|
+
attributes.delete :as
|
100
|
+
attributes.delete :dynamic
|
101
|
+
attributes.delete :parent_index
|
101
102
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
103
|
+
attributes[:required] = "true" if attributes.delete(:required) == true
|
104
|
+
attributes[:disabled] = "true" if attributes.delete(:disabled) == true
|
105
|
+
attributes[:autofocus] = "true" if attributes.delete(:autofocus) == true
|
106
|
+
attributes[:"data-dynamic"] = "true" if @dynamic
|
106
107
|
|
107
|
-
|
108
|
+
attributes[:title] ||= attributes[:placeholder] if attributes[:placeholder].present?
|
108
109
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
110
|
+
autocomplete = attributes.delete(:autocomplete)
|
111
|
+
if autocomplete && config.include_aria
|
112
|
+
attributes[:"aria-autocomplete"] = case autocomplete
|
113
|
+
when :inline, :list, :both
|
114
|
+
autocomplete.to_s
|
115
|
+
else
|
116
|
+
"both"
|
117
|
+
end
|
116
118
|
end
|
117
119
|
end
|
118
|
-
end
|
119
120
|
|
120
|
-
|
121
|
-
|
122
|
-
|
121
|
+
def render(&block)
|
122
|
+
send(:"#{@input_type}_input")
|
123
|
+
end
|
123
124
|
|
124
|
-
|
125
|
-
|
125
|
+
protected
|
126
|
+
attr_reader :include_blank
|
126
127
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
128
|
+
def simple_input(type, options = {}, &block)
|
129
|
+
template.capture_haml do
|
130
|
+
template.haml_tag :input, extract_attributes!(type, options), &block
|
131
|
+
if @dynamic
|
132
|
+
template.haml_tag :ul, :class => "buttons" do
|
133
|
+
template.haml_tag :li do
|
134
|
+
template.haml_tag :a, config.add_label, :class => :add, :role => :button, :type => :button, :href => "#"
|
135
|
+
end
|
136
|
+
template.haml_tag :li do
|
137
|
+
template.haml_tag :a, config.remove_label, :class => :remove, :role => :button, :type => :button, :href => "#"
|
138
|
+
end
|
137
139
|
end
|
138
140
|
end
|
139
141
|
end
|
140
142
|
end
|
141
|
-
end
|
142
143
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
144
|
+
def rich_input(type, options = {}, &block)
|
145
|
+
template.capture_haml do
|
146
|
+
result = template.rich_button :inner_html => extract_attributes!(type, options), :as => :input
|
147
|
+
template.haml_concat result.gsub(/\n$/, "") if result
|
148
|
+
end
|
147
149
|
end
|
148
|
-
end
|
149
150
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
151
|
+
def extract_attributes!(type, options = {})
|
152
|
+
if [:numeric, :string, :password, :text, :phone, :url, :email].include?(type)
|
153
|
+
attributes = model.default_string_options(attribute.name, type).merge(self.attributes)
|
154
|
+
else
|
155
|
+
attributes = self.attributes
|
156
|
+
end
|
156
157
|
|
157
|
-
|
158
|
+
merge_class! attributes, options.delete(:class)
|
158
159
|
|
159
|
-
|
160
|
+
attributes[:type] = input_type_for(type)
|
160
161
|
|
161
|
-
|
162
|
-
|
162
|
+
attributes.merge(options)
|
163
|
+
end
|
163
164
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
else
|
173
|
-
if text.present?
|
174
|
-
template.haml_tag tag, text, attributes
|
165
|
+
def base_input(tag, *args, &block)
|
166
|
+
options = args.extract_options!
|
167
|
+
attributes = self.attributes
|
168
|
+
text = args.shift
|
169
|
+
|
170
|
+
template.capture_haml do
|
171
|
+
if block_given?
|
172
|
+
template.haml_tag tag, attributes, &block
|
175
173
|
else
|
176
|
-
|
174
|
+
if text.present?
|
175
|
+
template.haml_tag tag, text, attributes
|
176
|
+
else
|
177
|
+
template.haml_tag tag, attributes
|
178
|
+
end
|
177
179
|
end
|
178
180
|
end
|
179
181
|
end
|
180
|
-
end
|
181
182
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
183
|
+
def input_type_for(type)
|
184
|
+
case type
|
185
|
+
when :password, :text, :url, :email, :file, :textarea, :search, :submit, :radio, :checkbox
|
186
|
+
type
|
187
|
+
when :phone, :tel
|
188
|
+
:tel
|
189
|
+
else
|
190
|
+
:string
|
191
|
+
end
|
190
192
|
end
|
191
193
|
end
|
192
194
|
end
|