Skip to content

Commit

Permalink
VBID-2114 End times should be :59
Browse files Browse the repository at this point in the history
Change-Id: I2e125377a5921c038e35ad47f1771ee1a1591cef
  • Loading branch information
jawsthegame committed Nov 5, 2013
1 parent c325829 commit 451e038
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 26 deletions.
62 changes: 43 additions & 19 deletions lib/lib/forms.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ dateField =
el.val(moment(date).format('MM/DD/YYYY'))


populateTimes = (select, isEnd) ->
return if select.children().length
mins = if isEnd then '59' else '00'

for tt in ['AM', 'PM']
for hour in [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
label = "#{hour}:#{mins} #{tt}"
jQuery('<option>')
.attr('value', label)
.text(label)
.appendTo(select)
.prop('selected', hour is 11 and isEnd)


dateTimeField =
get: (el) ->
dateStr = el.filter('.date').val()
Expand All @@ -200,34 +214,44 @@ dateTimeField =

set: (el, value) ->
timeEl = el.filter 'select.time'
@populate(timeEl)
populateTimes(timeEl, false)
return unless value?
date = new Date(value)
if _.isNaN(date.getTime())
throw new TypeError('Invalid date string')

el.filter('.date').val(moment(date).format('MM/DD/YYYY'))

timeEl.val(moment(date).format('h:00 A'))

populate: (select) ->
return if select.children().length

for tt in ['AM', 'PM']
for hour in [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
label = "#{hour}:00 #{tt}"
jQuery('<option>')
.attr('value', label)
.text(label)
.appendTo(select)
endDateTimeField =
get: (el) ->
dateStr = el.filter('.date').val()
timeStr = el.filter('.time').val() or ''
if dateStr is '' or timeStr is ''
return null

dateToString(new Date("#{dateStr} #{timeStr}"))

set: (el, value) ->
timeEl = el.filter 'select.time'
populateTimes(timeEl, true)
return unless value?
date = new Date(value)
if _.isNaN(date.getTime())
throw new TypeError('Invalid date string')

el.filter('.date').val(moment(date).format('MM/DD/YYYY'))
timeEl.val(moment(date).format('h:59 A'))


module.exports =
FormView: FormView
stringField: stringField
intField: intField
floatField: floatField
moneyField: moneyField
boolField: boolField
dateField: dateField
dateTimeField: dateTimeField
FormView: FormView
stringField: stringField
intField: intField
floatField: floatField
moneyField: moneyField
boolField: boolField
dateField: dateField
dateTimeField: dateTimeField
endDateTimeField: endDateTimeField
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quips",
"version": "0.2.14",
"version": "0.2.15",
"dependencies": {
"backbone": "1.0.0",
"coffee-script": "1.6.x",
Expand Down
51 changes: 45 additions & 6 deletions test/lib/forms_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ describe 'Money Field', ->
@el.val('5,000,000')
expect(forms.moneyField.get(@el)).to.equal '5000000.00'


describe 'Date Time Field', ->
beforeEach ->
test.create()
Expand All @@ -433,13 +432,16 @@ describe 'Date Time Field', ->
test.destroy()

it 'should populate the time choices on set', ->
expect(@input.filter('.date')).to.have.val ''
expect(@input.filter('.time')).to.have.val null
$date = @input.filter('.date')
$time = @input.filter('.time')

expect($date).to.have.val ''
expect($time).to.have.val null

forms.dateTimeField.set(@input, @date)
expect(@input.filter('.date')).to.have.val '09/02/1945'
expect(@input.filter('.time').find('option')).to.have.length 24
expect(@input.filter('.time')).to.have.val '8:00 AM'
expect($date).to.have.val '09/02/1945'
expect(($time).find('option')).to.have.length 24
expect($time).to.have.val '8:00 AM'

it 'should populate the time choices on set with null date', ->
forms.dateTimeField.set(@input, null)
Expand All @@ -449,3 +451,40 @@ describe 'Date Time Field', ->
forms.dateTimeField.set(@input, @date)
datetime = forms.dateTimeField.get(@input)
expect(datetime).to.equal '1945-09-02T13:00:00Z'

describe 'End Date Time Field', ->
beforeEach ->
test.create()
@doc = $ """
<p>
<input type="text" name="start_date" class="date"/>
<select name="start_date" class="time"></select>
</p>
"""

@input = @doc.find '[name=start_date]'
@date = '1945-09-02T13:45:55Z'

afterEach ->
test.destroy()

it 'should populate the time choices on set', ->
$date = @input.filter('.date')
$time = @input.filter('.time')

expect($date).to.have.val ''
expect($time).to.have.val null

forms.endDateTimeField.set(@input, @date)
expect($date).to.have.val '09/02/1945'
expect(($time).find('option')).to.have.length 24
expect($time).to.have.val '8:59 AM'

it 'should populate the time choices on set with null date', ->
forms.endDateTimeField.set(@input, null)
expect(@input.filter('.time').find('option')).to.have.length 24

it 'should the value from a form', ->
forms.endDateTimeField.set(@input, @date)
datetime = forms.dateTimeField.get(@input)
expect(datetime).to.equal '1945-09-02T13:59:00Z'

0 comments on commit 451e038

Please sign in to comment.