From 765704f6a42885ff15583a90049d033448090dbd Mon Sep 17 00:00:00 2001 From: csharpler Date: Thu, 17 Oct 2013 17:07:27 +0200 Subject: [PATCH] Configuration options for hour/minute/second stepping Added the configuration options hourStep (defaults to 1), minuteStep (defaults to 1), secondStep (defaults to 1). Each of them determines the number of units the arrows will change the hours, minutes and seconds, respectively. That is, if hourStep is 4 and the time in the datetimepicker currently has a hour value of 17, clicking on the up arrow will change that value to 17+4=21, and clicking on the down arrow will change it to 17-4=13. This also wraps around 0 and 24, so that decrementing 00:05:00 with a minuteStep of 6 will give 23:59:00. Also adjusted three test cases to reflect the new behavior. Those test cases expected the click to effect a change of 1, which is the default value, but they now expect a change of the step values given in the setup function. --- src/js/bootstrap-datetimepicker.js | 20 +++++++++++++------- test/specs.coffee | 18 +++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/js/bootstrap-datetimepicker.js b/src/js/bootstrap-datetimepicker.js index c566f890e..7fcca4ca2 100644 --- a/src/js/bootstrap-datetimepicker.js +++ b/src/js/bootstrap-datetimepicker.js @@ -52,6 +52,9 @@ this.language = options.language in dates ? options.language : 'en' this.pickDate = options.pickDate; this.pickTime = options.pickTime; + this.hourStep = options.hourStep; + this.minuteStep = options.minuteStep; + this.secondStep = options.secondStep; this.isInput = this.$element.is('input'); this.component = false; if (this.$element.find('.input-append') || this.$element.find('.input-prepend')) @@ -639,27 +642,27 @@ actions: { incrementHours: function(e) { - this._date.setUTCHours(this._date.getUTCHours() + 1); + this._date.setUTCHours(this._date.getUTCHours() + this.hourStep); }, incrementMinutes: function(e) { - this._date.setUTCMinutes(this._date.getUTCMinutes() + 1); + this._date.setUTCMinutes(this._date.getUTCMinutes() + this.minuteStep); }, incrementSeconds: function(e) { - this._date.setUTCSeconds(this._date.getUTCSeconds() + 1); + this._date.setUTCSeconds(this._date.getUTCSeconds() + this.secondStep); }, decrementHours: function(e) { - this._date.setUTCHours(this._date.getUTCHours() - 1); + this._date.setUTCHours(this._date.getUTCHours() - this.hourStep); }, decrementMinutes: function(e) { - this._date.setUTCMinutes(this._date.getUTCMinutes() - 1); + this._date.setUTCMinutes(this._date.getUTCMinutes() - this.minuteStep); }, decrementSeconds: function(e) { - this._date.setUTCSeconds(this._date.getUTCSeconds() - 1); + this._date.setUTCSeconds(this._date.getUTCSeconds() - this.secondStep); }, togglePeriod: function(e) { @@ -1097,7 +1100,10 @@ pickSeconds: true, startDate: -Infinity, endDate: Infinity, - collapse: true + collapse: true, + hourStep: 1, + minuteStep: 1, + secondStep: 1 }; $.fn.datetimepicker.Constructor = DateTimePicker; var dpgId = 0; diff --git a/test/specs.coffee b/test/specs.coffee index 8497b032b..2e3463417 100644 --- a/test/specs.coffee +++ b/test/specs.coffee @@ -1,6 +1,10 @@ describe 'datetimepicker', -> - beforeEach setupDateTimePicker() + beforeEach setupDateTimePicker({ + hourStep: 2, + minuteStep: 5, + secondStep: 15 + }) afterEach teardownDateTimePicker() @@ -118,29 +122,29 @@ describe 'datetimepicker', -> .to.be.true done() - it 'increments/decrements hour', -> + it 'adds/subtracts the hours step', -> @addon.click() @widget.find('.picker-switch a').click() @timeWidget.find('[data-action=incrementHours]').click() - @dateShouldEqual 1905, 4, 1, 22, 52, 14 + @dateShouldEqual 1905, 4, 1, 23, 52, 14 @timeWidget.find('[data-action=decrementHours]').click() @dateShouldEqual 1905, 4, 1, 21, 52, 14 - it 'increments/decrements minutes', -> + it 'adds/subtracts the minutes step', -> @addon.click() @widget.find('.picker-switch a').click() @timeWidget.find('[data-action=incrementMinutes]').click() # 15 minutes step is the default - @dateShouldEqual 1905, 4, 1, 21, 53, 14 + @dateShouldEqual 1905, 4, 1, 21, 57, 14 @timeWidget.find('[data-action=decrementMinutes]').click() @dateShouldEqual 1905, 4, 1, 21, 52, 14 - it 'increments/decrements minutes', -> + it 'adds/subtracts the seconds step', -> @addon.click() @widget.find('.picker-switch a').click() @timeWidget.find('[data-action=incrementSeconds]').click() # 30 seconds step is the default - @dateShouldEqual 1905, 4, 1, 21, 52, 15 + @dateShouldEqual 1905, 4, 1, 21, 52, 29 @timeWidget.find('[data-action=decrementSeconds]').click() @dateShouldEqual 1905, 4, 1, 21, 52, 14