-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime_selector.coffee
334 lines (282 loc) · 12.1 KB
/
datetime_selector.coffee
1
2
3
4
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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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
# Copyright (C) 2012 Mark Huetsch
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'cream'
TagHelper = require 'tag-helper'
class DateTimeSelector
constructor: (datetime, options = {}, html_options = {}) ->
#console.log "new datetime selector created, with datetime passed as #{datetime}"
@options = Object.clone options
#console.log "new datetime selector cloned options:"
#console.log JSON.stringify(@options)
@html_options = Object.clone html_options
if datetime instanceof Date
@datetime = datetime
else if datetime
@datetime = new Date(new String(datetime))
unless @options.datetime_separator?
@options.datetime_separator = ' — '
unless @options.time_separator?
@options.time_separator = ' : '
#console.log @datetime
@sec = -> @datetime?.getSeconds()
@min = -> @datetime?.getMinutes()
@hour = -> @datetime?.getHours()
@day = -> @datetime?.getDate()
@month = -> if @datetime then ((@datetime.getMonth() % 12) + 1)
@year = -> @datetime?.getFullYear()
# no localization for now
date_order: ->
@options.order or ['year', 'month', 'day'] or []
select_datetime: ->
#console.log 'select_datetime called'
order = Object.clone @date_order()
##console.log JSON.stringify(order)
order = order.filter (x) -> x not in ['hour', 'minute', 'second']
##console.log JSON.stringify(order)
@options.discard_year ||= unless 'year' in order then true
@options.discard_month ||= unless 'month' in order then true
@options.discard_day ||= if @options.discard_month or ('day' not in order) then true
@options.discard_minute ||= if @options.discard_hour then true
@options.discard_second ||= unless @options.include_seconds and (not @options.discard_minute) then true
if @datetime and @options.discard_day and not @options.discard_month
@datetime.setDate(1)
if @options.tag and @options.ignore_date
@select_time()
else
for o in ['day', 'month', 'year']
unless o in order
order.unshift(o)
unless @options.discard_hour
order = order.concat ['hour', 'minute', 'second']
(@build_selects_from_types order).valueOf()
select_date: ->
#console.log "select_date called"
#console.log "@datetime"
#console.log @datetime?.toString()
order = Object.clone @date_order()
@options.discard_hour = true
@options.discard_minute = true
@options.discard_second = true
##console.log JSON.stringify @options
##console.log "order: #{JSON.stringify order}"
@options.discard_year ||= (unless 'year' in order then true)
@options.discard_month ||= (unless 'month' in order then true)
@options.discard_day ||= if @options.discard_month or 'day' not in order then true
##console.log "order again: #{JSON.stringify order}"
##console.log @options
# If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
# valid (otherwise it could be 31 and February wouldn't be a valid date)
if @datetime and @options.discard_day and not @options.discard_month
@datetime.setDate(1)
for o in ['day', 'month', 'year']
unless o in order
order.unshift(o)
ret = (@build_selects_from_types order).valueOf()
##console.log ret
ret
select_time: ->
order = []
@options.discard_month = true
@options.discard_year = true
@options.discard_day = true
@options.discard_second ||= unless @options.include_seconds then true
unless @options.ignore_date
order = order.concat ['year', 'month', 'day']
order = order.concat ['hour', 'minute']
if @options.include_seconds
order.push 'second'
@build_selects_from_types order
select_second: ->
##console.log 'select_second called'
if @options.use_hidden or @options.discard_second
##console.log JSON.stringify @options
if @options.include_seconds
##console.log 'including seconds'
@build_hidden('second', @sec())
else
''
else
##console.log 'not using hidden seconds'
@build_options_and_select('second', @sec())
select_minute: ->
if @options.use_hidden or @options.discard_minute
@build_hidden('minute', @min())
else
@build_options_and_select('minute', @min(), step: @options.minute_step)
select_hour: ->
#console.log 'select_hour called'
if @options.use_hidden or @options.discard_hour
#console.log 'select_hour hidden or discarded'
@build_hidden('hour', @hour())
else
#console.log "select_hour not hidden or discarded: #{@hour()}"
@build_options_and_select('hour', @hour(), end: 23, ampm: @options.ampm)
select_day: ->
#console.log "select_day called"
#console.log "@datetime"
#console.log @datetime?.toString()
if @options.use_hidden or @options.discard_day
@build_hidden('day', @day())
else
@build_options_and_select('day', @day(), start: 1, end: 31, leading_zeros: false)
select_month: ->
if @options.use_hidden or @options.discard_month
@build_hidden('month', @month())
else
month_options = []
for month_number in [1..12]
options = value: month_number
if @month() is month_number
options.selected = "selected"
month_options.push TagHelper.content_tag('option', @month_name(month_number), options) + "\n"
@build_select('month', month_options.join(''))
select_year: ->
#console.log "select_year called"
if (not @datetime) or @datetime is 0
val = ''
middle_year = (new Date()).getFullYear()
else
val = middle_year = @year()
#console.log "@datetime"
#console.log @datetime?.toString()
#console.log "middle year: #{middle_year}"
if @options.use_hidden or @options.discard_year
@build_hidden('year', val)
else
options = {}
options.start = @options.start_year or (middle_year - 5)
options.end = @options.end_year or (middle_year + 5)
options.step = if options.start < options.end then 1 else -1
options.leading_zeros = false
@build_options_and_select('year', val, options)
build_selects_from_types: (order) ->
#console.log "build_selects_from_types called"
#console.log "@datetime"
#console.log @datetime?.toString()
select = ''
for type in Object.clone(order).reverse()
if type is order.first() # don't add on last field
separator = ''
else
separator = @separator(type)
new_select = @["select_#{type}"]()
select = "#{separator}#{new_select}#{select}"
##console.log "select_#{type}"
##console.log new_select.valueOf()
select.html_safe()
separator: (type) ->
ret = switch type
when 'year'
if @options.discard_year then '' else @options.date_separator
when 'month'
if @options.discard_month then '' else @options.date_separator
when 'day'
if @options.discard_day then '' else @options.date_separator
when 'hour'
if @options.discard_year and @options.discard_day then '' else @options.datetime_separator
when 'minute'
if @options.discard_minute then '' else @options.time_separator
when 'second'
if @options.include_seconds then @options.time_separator else ''
ret ||= ''
# Returns translated month names, but also ensures that a custom month
# name array has a leading null element.
month_names: ->
month_names = @options.use_month_names or @translated_month_names()
if month_names.length < 13
month_names.unshift(null)
month_names
month_name: (number) ->
if @options.use_month_numbers
number
else if @options.add_month_numbers
"#{number} - #{@month_names()[number]}"
else
@month_names()[number]
# i18n currently unsupported
translated_month_names: ->
if @options.use_short_month
[null, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
else
["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
build_options_and_select: (type, selected, options = {}) ->
@build_select(type, @build_options(selected, options))
AMPM_TRANSLATION:
0: "12 AM", 1: "01 AM", 2: "02 AM", 3: "03 AM",
4: "04 AM", 5: "05 AM", 6: "06 AM", 7: "07 AM",
8: "08 AM", 9: "09 AM", 10: "10 AM", 11: "11 AM",
12: "12 PM", 13: "01 PM", 14: "02 PM", 15: "03 PM",
16: "04 PM", 17: "05 PM", 18: "06 PM", 19: "07 PM",
20: "08 PM", 21: "09 PM", 22: "10 PM", 23: "11 PM"
DEFAULT_PREFIX: 'date'
POSITION: {year: 1, month: 2, day: 3, hour: 4, minute: 5, second: 6}
build_options: (selected, options = {}) ->
start = 0
if options.start
start = options.start
delete options.start
stop = 59
if options.end
stop = options.end
delete options.end
step = 1
if options.step
step = options.step
delete options.step
unless options.leading_zeros?
options.leading_zeros = true
leading_zeros = options.leading_zeros
delete options.leading_zeros
unless options.ampm?
options.ampm = false
select_options = []
for i in [start..stop] by step
value = String i
# we don't have sprintf..., otherwise value = =sprint("%02d", i)
if leading_zeros
if value.length is 1
value = "0#{value}"
else if value.length is 0
value = "00"
tag_options = value: value
if selected is i
tag_options.selected = "selected"
##console.log @AMPM_TRANSLATION
text = if options.ampm then @AMPM_TRANSLATION[i] else value
select_options.push TagHelper.content_tag('option', text, tag_options)
(select_options.join("\n") + "\n").html_safe()
build_select: (type, select_options_as_html) ->
select_options = id: @input_id_from_type(type), name: @input_name_from_type(type)
for k, v of @html_options
select_options[k] = v
if @options.disabled
select_options.disabled = 'disabled'
select_html = "\n"
if @options.include_blank
select_html += TagHelper.content_tag('option', '', value: '') + "\n"
if @options.prompt
select_html += @prompt_option_tag(type, @options.prompt) + "\n"
select_html += select_options_as_html
(TagHelper.content_tag('select', select_html.html_safe(), select_options) + "\n").html_safe()
build_hidden: (type, value) ->
html_options = type: 'hidden', id: @input_id_from_type(type), name: @input_name_from_type(type), value: value
if @html_options.disabled
html_options = @html_options.disabled
(TagHelper.tag('input', html_options) + "\n").html_safe()
input_name_from_type: (type) ->
prefix = @options.prefix or @DEFAULT_PREFIX
if 'index' in (k for k, v of @options)
prefix += "[#{@options.index}]"
field_name = @options.field_name or type
##console.log "input_name_from_type: #{type}"
if @options.include_position
field_name += "(#{@POSITION[type]}i)"
if @options.discard_type then prefix else "#{prefix}[#{field_name}]"
input_id_from_type: (type) ->
@input_name_from_type(type).replace(/([\[\(])|(\]\[)/g, '_').replace(/[\]\)]/g, '')
module.exports = DateTimeSelector