From a437bc5bbee6431d65c3fa719e516312bfc0ccf9 Mon Sep 17 00:00:00 2001 From: Joris de Wit Date: Sun, 6 Oct 2013 12:45:57 -0700 Subject: [PATCH] add ability to clear time Enables timepicker to be reset with clear or setTime(null) ex. $('.timepicker').timepicker('setTime', null); or $('.timepicker').timepicker('clear'); fixes #155 --- js/bootstrap-timepicker.js | 22 ++++++++++++++++++---- spec/js/TimepickerSpec.js | 12 ++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/js/bootstrap-timepicker.js b/js/bootstrap-timepicker.js index c4007943..16823cc3 100644 --- a/js/bootstrap-timepicker.js +++ b/js/bootstrap-timepicker.js @@ -90,6 +90,15 @@ this.updateFromElementVal(); }, + clear: function() { + this.hour = null; + this.minute = null; + this.second = null; + this.meridian = null; + + this.$element.val(''); + }, + decrementHour: function() { if (this.showMeridian) { if (this.hour === 1) { @@ -228,11 +237,11 @@ }, formatTime: function(hour, minute, second, meridian) { - hour = hour < 10 ? '0' + hour : hour; - minute = minute < 10 ? '0' + minute : minute; - second = second < 10 ? '0' + second : second; + hour = hour !== null ? (hour < 10 ? '0' + hour : hour) : ''; + minute = minute !== null ? (minute < 10 ? '0' + minute : minute) : ''; + second = second !== null ? (second < 10 ? '0' + second : second) : ''; - return hour + ':' + minute + (this.showSeconds ? ':' + second : '') + (this.showMeridian ? ' ' + meridian : ''); + return hour + (hour !== '' ? ':' : '') + minute + (this.showSeconds && second !== '' ? ':' + second : '') + (this.showMeridian && meridian !== null ? ' ' + meridian : ''); }, getCursorPosition: function() { @@ -666,6 +675,11 @@ var arr, timeArray; + if (!time) { + this.clear(); + return; + } + if (this.showMeridian) { arr = time.split(' '); timeArray = arr[0].split(':'); diff --git a/spec/js/TimepickerSpec.js b/spec/js/TimepickerSpec.js index 2cada324..7a68d91b 100644 --- a/spec/js/TimepickerSpec.js +++ b/spec/js/TimepickerSpec.js @@ -389,6 +389,18 @@ describe('Timepicker feature', function() { expect(hideEvents).toBe(1); }); + it('should be able to reset time by using setTime 0/null', function() { + tp1.hour = 10; + tp1.minute = 30; + tp1.meridian = 'PM'; + tp1.updateElement(); + + $input1.timepicker('setTime', null); + tp1.update(); + expect(tp1.getTime()).toBe(''); + }); + + it('should not have the widget in the DOM if remove method is called', function() { expect($('body')).toContain('.bootstrap-timepicker-widget'); tp1.remove();