5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/date_picker/form_tag_helper.rb', line 5
def date_picker_tag(name, value, options = {})
options[:type]||= :date
options[:time_zone]||= false
options[:data]||= {}
options[:id]||= "date_picker_" + Digest::SHA1.hexdigest(name.to_s)[8..16]
type = options[:type]
if value.blank?
case type
when :date
value = Date.today
when :datetime
value = DateTime.now
when :time
value = Time.now
end
end
format = nil
if options[:format].present?
format = options[:format]
elsif DatePicker.config.formats.present?
format = DatePicker.config.formats[type]
end
if format.blank? || !format.is_a?(String)
case type
when :date
format_id = format.present? && format.is_a?(Symbol) ? format.to_s : 'default'
format = I18n.t('date.formats.' + format_id, default: "%Y-%m-%d")
when :datetime
format_id = format.present? && format.is_a?(Symbol) ? format.to_s : 'default'
format = I18n.t('time.formats.' + format_id, default: "%a, %d %b %Y %H:%M:%S")
when :time
format_id = format.present? && format.is_a?(Symbol) ? format.to_s : 'only_time'
format = I18n.t('time.formats.' + format_id, default: "%H:%M:%S")
end
else
format = format.to_s
end
if options[:style].present?
style = options[:style]
elsif DatePicker.config.style.present?
style = DatePicker.config.style
else
style = :bootstrap
end
path = File.join(File.dirname(__FILE__), "styles", style.to_s)
require path
obj = Object::const_get('DatePicker::Styles::' + style.to_s.classify).new
types = [:date]
if obj.respond_to?(:types)
types = obj.send(:types)
end
formatted_value = value.present? ? value.strftime(format) : nil
input_options = options.except(:time_zone, :format, :input_tag, :type, :pattern)
if obj.respond_to?(:options)
input_options = input_options.merge(obj.send(:options))
end
if obj.mapping.present?
mapping = obj.mapping
end
if obj.mapping.is_a? Symbol
mapping = DatePicker::Mappings.send(obj.mapping)
end
input_options[:type] = :text
input_options[:value] = formatted_value
input_tag = options[:tag] || :input
input_id = options[:id]
input_html = content_tag(input_tag, nil, input_options)
if mapping[:_].present?
format.gsub!(/(?<!%)([a-z]+)/i, mapping[:_].gsub(/\*/, "\\\\1"))
end
if options[:time_zone]
format.gsub!("%Z", value.present? ? value.to_datetime.strftime("%Z") : "")
format.gsub!("%z", mapping[:z])
else
format.gsub!("%Z", "")
format.gsub!("%z", "")
format.strip!
end
mapping.each_pair do |k, v|
format.gsub!("%" + k.to_s, v)
end
if type == :date
data_format = "%Y-%m-%d"
else
data_format = "%Y-%m-%d %H:%M:%S %z"
end
if options[:time_zone]
data_format.gsub!("%Z", value.present? ? value.to_datetime.strftime("%Z") : "")
data_format.gsub!("%z", mapping[:z])
else
format.gsub!("%Z", "")
format.gsub!("%z", "")
format.strip!
end
mapping.each_pair do |k, v|
data_format.gsub!("%" + k.to_s, v)
end
object_name = name.gsub(/\[\w*\]$/, "")
attribute_name = name.gsub(/.*\[(\w*)\]$/, "\\1")
is_mobile = (request.["HTTP_USER_AGENT"].present? && request.["HTTP_USER_AGENT"] =~ /\b(Mobile|webOS|Android|iPhone|iPad|iPod|Windows Phone|Opera Mobi|Kindle|BackBerry|PlayBook)\b/i).present?
if is_mobile
case options[:type]
when :date
return date_field(object_name, attribute_name, input_options.merge({type: 'date', value: value}))
when :datetime
return datetime_field(object_name, attribute_name, input_options.merge({type: 'datetime-local', value: value.strftime("%Y-%m-%dT%H:%M:%S")}))
when :time
return time_field(object_name, attribute_name, input_options.merge({type: 'time', value: value.strftime("%H:%M")}))
end
end
if !types.include?(options[:type])
case options[:type]
when :date
return date_select(object_name, attribute_name, {default: value}, input_options)
when :datetime
return datetime_select(object_name, attribute_name, {default: value}, input_options)
when :time
return time_select(object_name, attribute_name, {default: value}, input_options)
end
end
input_options[:type] = :text
tmpl = obj.template
locale = I18n.locale
month_names = I18n.t('date.month_names', default: '')
month_names = month_names.present? ? month_names.slice(1, month_names.length - 1) : Date::MONTHNAMES
abbr_month_names = I18n.t('date.abbr_month_names', default: '')
abbr_month_names = abbr_month_names.present? ? abbr_month_names.slice(1, abbr_month_names.length - 1) : Date::MONTHNAMES
day_names = I18n.t('date.day_names', default: Date::DAYNAMES)
abbr_day_names = I18n.t('date.abbr_day_names', default: Date::ABBR_DAYNAMES)
time = value.present? ? value.to_datetime.utc.to_i * 1000 : 0
camelized_keys =
lambda do |h|
Hash === h ?
Hash[
h.map do |k, v|
puts k
key = k.to_s().camelize
key = key[0, 1].downcase + key[1..-1]
[key, camelized_keys[v]]
end
] : h
end
json_options = camelized_keys[input_options[:data]].to_json
vars = {
type: type,
value: value,
locale: locale,
format: format,
data_format: data_format,
name: name,
input_id: input_id,
input_options: input_options,
time: time,
input_html: input_html,
month_names: month_names,
abbr_month_names: abbr_month_names,
day_names: day_names,
abbr_day_names: abbr_day_names
}
result = ERB.new(tmpl).result(OpenStruct.new(vars).instance_eval { binding })
return result.html_safe
end
|