From 11400f4dd833d89cd8f5fd2bc72094ea12952a42 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 23 Sep 2015 11:08:19 +0200 Subject: [PATCH] Set time to now if + or - is used and no time is available. --- .../controllers/_ui_element/date.js.coffee | 25 ++++++++-------- .../_ui_element/datetime.js.coffee | 29 ++++++++++--------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_ui_element/date.js.coffee b/app/assets/javascripts/app/controllers/_ui_element/date.js.coffee index 7256d2e54..fb83e95d0 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/date.js.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/date.js.coffee @@ -33,22 +33,22 @@ class App.UiElement.date ) item.find('.js-plus-day').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, 60 * 24) + @setNewTime(item, attribute, 60 * 24, false, true) @validation(item, attribute) ) item.find('.js-minus-day').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, -60 * 24) + @setNewTime(item, attribute, -60 * 24, false, true) @validation(item, attribute) ) item.find('.js-plus-week').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, 60 * 24 * 7) + @setNewTime(item, attribute, 60 * 24 * 7, false, true) @validation(item, attribute) ) item.find('.js-minus-week').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, -60 * 24 * 7) + @setNewTime(item, attribute, -60 * 24 * 7, false, true) @validation(item, attribute) ) item.find('input').bind('keyup blur focus change', (e) => @@ -68,20 +68,21 @@ class App.UiElement.date number = "0#{number}" number - @setNewTime: (item, attribute, diff, reset = false) -> + @setNewTime: (item, attribute, diff, reset = false, tolerant = false) -> - # remove old validation - #item.find('.has-error').removeClass('has-error') - #item.closest('.form-group').find('.help-inline').html('') - - if reset + resetTimeToToday = => time = new Date() time.setMinutes( time.getMinutes() + diff ) @setParams(item, attribute, time) - return + + return resetTimeToToday() if reset params = @getParams(item) - return if params.year is '' && params.month is '' && params.day is '' && params.hour is '' && params.day is '' + if params.year is '' && params.month is '' && params.day is '' + return if !tolerant + resetTimeToToday() + params = @getParams(item) + time = new Date( Date.parse( "#{params.year}-#{@format(params.month)}-#{@format(params.day)}T00:00:00Z" ) ) time.setMinutes( time.getMinutes() + diff ) return if !time diff --git a/app/assets/javascripts/app/controllers/_ui_element/datetime.js.coffee b/app/assets/javascripts/app/controllers/_ui_element/datetime.js.coffee index 2f1c8decb..ff1e64c2f 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/datetime.js.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/datetime.js.coffee @@ -37,32 +37,32 @@ class App.UiElement.datetime ) item.find('.js-plus-hour').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, 60) + @setNewTime(item, attribute, 60, false, true) @validation(item, attribute) ) item.find('.js-minus-hour').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, -60) + @setNewTime(item, attribute, -60, false, true) @validation(item, attribute) ) item.find('.js-plus-day').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, 60 * 24) + @setNewTime(item, attribute, 60 * 24, false, true) @validation(item, attribute) ) item.find('.js-minus-day').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, -60 * 24) + @setNewTime(item, attribute, -60 * 24, false, true) @validation(item, attribute) ) item.find('.js-plus-week').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, 60 * 24 * 7) + @setNewTime(item, attribute, 60 * 24 * 7, false, true) @validation(item, attribute) ) item.find('.js-minus-week').bind('click', (e) => e.preventDefault() - @setNewTime(item, attribute, -60 * 24 * 7) + @setNewTime(item, attribute, -60 * 24 * 7, false, true) @validation(item, attribute) ) item.find('input').bind('keyup blur focus change', (e) => @@ -82,20 +82,21 @@ class App.UiElement.datetime number = "0#{number}" number - @setNewTime: (item, attribute, diff, reset = false) -> + @setNewTime: (item, attribute, diff, reset = false, tolerant = false) -> - # remove old validation - #item.find('.has-error').removeClass('has-error') - #item.closest('.form-group').find('.help-inline').html('') - - if reset + resetTimeToToday = => time = new Date() time.setMinutes( time.getMinutes() + diff ) @setParams(item, attribute, time) - return + + return resetTimeToToday() if reset params = @getParams(item) - return if params.year is '' && params.month is '' && params.day is '' && params.hour is '' && params.day is '' + if params.year is '' && params.month is '' && params.day is '' && params.hour is '' && params.minute is '' + return if !tolerant + resetTimeToToday() + params = @getParams(item) + time = new Date( Date.parse( "#{params.year}-#{@format(params.month)}-#{@format(params.day)}T#{@format(params.hour)}:#{@format(params.minute)}:00Z" ) ) time.setMinutes( time.getMinutes() + diff + time.getTimezoneOffset() ) return if !time