From 0843ea52bebfec70201df592e90cb9ac0600ad1d Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 5 Feb 2015 00:34:32 +0100 Subject: [PATCH] Fixed now() selector (was with - timezone). --- .../_application_controller_form.js.coffee | 12 +-- public/assets/tests/form-validation.js | 80 +++++++++++++++++++ 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee index 0f6752308..b7702955b 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee @@ -320,12 +320,13 @@ class App.ControllerForm extends App.Controller number if !reset && (year isnt '' && month isnt '' && day isnt '') time = new Date( Date.parse( "#{year}-#{format(month)}-#{format(day)}T00:00:00Z" ) ) + time.setMinutes( time.getMinutes() + diff + time.getTimezoneOffset() ) else time = new Date() - #time.setMinutes( time.getMinutes() + diff + time.getTimezoneOffset() ) - item.closest('.form-group').find("[name=\"{date}#{name}___day\"]").val( time.getUTCDate() ) - item.closest('.form-group').find("[name=\"{date}#{name}___month\"]").val( time.getUTCMonth()+1 ) - item.closest('.form-group').find("[name=\"{date}#{name}___year\"]").val( time.getUTCFullYear() ) + time.setMinutes( time.getMinutes() + diff ) + item.closest('.form-group').find("[name=\"{date}#{name}___day\"]").val( time.getDate() ) + item.closest('.form-group').find("[name=\"{date}#{name}___month\"]").val( time.getMonth()+1 ) + item.closest('.form-group').find("[name=\"{date}#{name}___year\"]").val( time.getFullYear() ) item.find('.js-today').bind('click', (e) -> e.preventDefault() @@ -462,9 +463,10 @@ class App.ControllerForm extends App.Controller number if !reset && (year isnt '' && month isnt '' && day isnt '' && hour isnt '' && day isnt '') time = new Date( Date.parse( "#{year}-#{format(month)}-#{format(day)}T#{format(hour)}:#{format(minute)}:00Z" ) ) + time.setMinutes( time.getMinutes() + diff + time.getTimezoneOffset() ) else time = new Date() - time.setMinutes( time.getMinutes() + diff + time.getTimezoneOffset() ) + time.setMinutes( time.getMinutes() + diff ) #console.log('T', time, time.getHours(), time.getMinutes()) item.closest('.form-group').find("[name=\"{datetime}#{name}___day\"]").val( time.getDate() ) item.closest('.form-group').find("[name=\"{datetime}#{name}___month\"]").val( time.getMonth()+1 ) diff --git a/public/assets/tests/form-validation.js b/public/assets/tests/form-validation.js index 6c3b4bb7f..8a9dd6884 100644 --- a/public/assets/tests/form-validation.js +++ b/public/assets/tests/form-validation.js @@ -248,4 +248,84 @@ test( "date validation check", function() { equal( el.find('[data-name="date1"]').closest('.form-group').hasClass('has-error'), true, 'check date1 has-error') equal( el.find('[data-name="date1"]').closest('.form-group').find('.help-inline').text(), '', 'check date1 error message') +}); + +test( "datetime selector check", function() { + + $('#forms').append('

datetime selector check

') + + var el = $('#form4') + var defaults = {} + var form = new App.ControllerForm({ + el: el, + model: { + configure_attributes: [ + { name: 'datetime1', display: 'Datetime1', tag: 'datetime', null: false, default: defaults['datetime1'] }, + ], + }, + params: defaults, + }); + + // check params + params = App.ControllerForm.params( el ) + test_params = { + datetime1: undefined, + } + deepEqual( params, test_params, 'params check' ) + + el.find('.js-today').click() + + // check params + timeStamp = new Date() + currentTime = timeStamp.toISOString() + currentTime = currentTime.replace(/(\d\d\.\d\d\dZ)$/, '00.000Z') + params = App.ControllerForm.params( el ) + test_params = { + datetime1: currentTime, + } + deepEqual( params, test_params, 'params check' ) + +}); + +test( "date selector check", function() { + + $('#forms').append('

date selector check

') + + var el = $('#form5') + var defaults = {} + var form = new App.ControllerForm({ + el: el, + model: { + configure_attributes: [ + { name: 'date1', display: 'Datet1', tag: 'date', null: false, default: defaults['date1'] }, + ], + }, + params: defaults, + }); + + // check params + params = App.ControllerForm.params( el ) + test_params = { + date1: undefined, + } + deepEqual( params, test_params, 'params check' ) + + el.find('.js-today').click() + + // check params + format = function (number) { + if ( parseInt(number) < 10 ) { + number = '0' + number.toString() + } + return number + } + + timeStamp = new Date() + currentTime = timeStamp.getFullYear() + '-' + format(timeStamp.getMonth()+1) + '-' + format(timeStamp.getDate()) + params = App.ControllerForm.params( el ) + test_params = { + date1: currentTime, + } + deepEqual( params, test_params, 'params check' ) + }); \ No newline at end of file