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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
# File 'lib/date_picker/form_tag_helper.rb', line 6
def date_picker_tag(name, value, options = {}, html_options = nil)
object_name = name.gsub(/\[\w*\]$/, "")
attribute_name = name.gsub(/.*\[(\w*)\]$/, "\\1")
option_names = [:time_zone, :format, :type, :default, :min, :max]
opts = options.clone
html_opts = html_options.clone
options||= {}
options = opts.slice(*option_names)
options = ({
type: :date,
time_zone: false,
}).merge(options)
html_options||= {}
html_options = {
type: :text,
id: "date_picker_" + Digest::SHA1.hexdigest(name.to_s)[8..16],
name: name,
data: html_options[:data] || options[:data] || {}
}.merge(opts.except(*option_names)).merge(html_options)
type = options[:type]
if !options.key?(:default)
case type
when :date
default = Date.current
when :datetime
default = DateTime.current
when :time
default = Time.current
end
else
default = options[:default]
end
if html_options.key?(:value)
value = html_options[:value]
end
if value.blank?
value = default
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 = :none
end
path = File.join(File.dirname(__FILE__), "styles", style.to_s)
if style && style != 'none' && File.exist?(path + '.rb')
require path
obj = Object::const_get('DatePicker::Styles::' + style.to_s.classify).new
end
types = [:date]
if obj && obj.respond_to?(:types)
types = obj.send(:types)
end
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, html_options.merge({type: 'date', value: value.present? ? value : ''}))
when :datetime
return datetime_field(object_name, attribute_name, html_options.merge({type: 'datetime-local', value: value.present? ? value.strftime("%Y-%m-%dT%H:%M:%S") : ''}))
when :time
return time_field(object_name, attribute_name, html_options.merge({type: 'time', value: value.present? ? value.strftime("%H:%M") : ''}))
end
end
if !obj || !types.include?(options[:type])
html_options.delete(:name)
case options[:type]
when :date
return date_select(object_name, attribute_name, {default: value}, html_options)
when :datetime
return datetime_select(object_name, attribute_name, {default: value}, html_options)
when :time
return time_select(object_name, attribute_name, {default: value}, html_options)
end
end
if obj.respond_to?(:options)
html_options = html_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
picker_format = format.clone
if mapping[:__].present?
replace = mapping[:__].gsub(/\*/, "\\\\1")
keys = mapping.keys.select{ |key| !mapping[key].blank? && !key.to_s.start_with?('__') }
values = keys.map{ |key| mapping[key] }
picker_format.gsub!(/(?<!%)(#{(values.map { |string| Regexp.escape(string) }).join('|')})/i, replace)
picker_format.gsub!(/(?<!%)(#{(keys.map { |string| Regexp.escape(string) }).join('|')})/i, replace)
end
if options[:time_zone]
picker_format.gsub!("%Z", value.present? ? value.to_datetime.strftime("%Z") : "")
picker_format.gsub!("%z", mapping[:z])
else
picker_format.gsub!("%Z", "")
picker_format.gsub!("%z", "")
picker_format.strip!
end
mapping.each_pair do |k, v|
picker_format.gsub!("%" + k.to_s, v)
end
if type == :date
data_format = "%Y-%m-%d"
else
data_format = "%Y-%m-%d %H:%M:%S" + ((format.include?("%z") || format.include?("%z")) && options[:time_zone] ? ' %z' : '')
end
if options[:time_zone]
data_format.gsub!("%Z", value.present? ? value.to_datetime.strftime("%Z") : "")
data_format.gsub!("%z", mapping[:z])
end
mapping.each_pair do |k, v|
data_format.gsub!("%" + k.to_s, v)
end
formatted_value = value.present? ? I18n.l(value, format: format.to_s) : nil
html_options[:value] = formatted_value
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_time.utc.to_i * 1000 : 0
tz = Time.zone
if value.present?
utc_offset = (value.is_a?(Date) ? value.to_datetime : value).utc_offset
tz = ActiveSupport::TimeZone.all.select {
|zone|
zone.tzinfo.current_period.offset.utc_total_offset === utc_offset
}.first
end
timezone = type === :time && utc_offset === 0 ? 'Etc/UTC' : ActiveSupport::TimeZone::MAPPING[tz.name]
camelized_keys =
lambda do |h|
Hash === h ?
Hash[
h.map do |k, v|
key = k.to_s().camelize
key = key[0, 1].downcase + key[1..-1]
[key, camelized_keys[v]]
end
] : h
end
picker_options = camelized_keys[html_options[:data]].to_json
input_tag = :input
input_id = html_options[:id]
input_html = content_tag(input_tag, nil, html_options)
vars = options.merge({
type: type,
value: value,
locale: locale,
picker_format: picker_format,
data_format: data_format,
name: name,
input_id: input_id,
html_options: html_options,
time: time,
time_zone: timezone,
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
|