Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AM/PM configurable designators #167

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
*.swp *.swp
_site/ _site/
.grunt .grunt
.idea
node_modules node_modules
gruntfile.js gruntfile.js
_site _site
Expand Down
10 changes: 10 additions & 0 deletions index.html
Expand Up @@ -342,6 +342,16 @@ <h3>template <small>The picker widget template</small></h3>
<td>false</td> <td>false</td>
<td>Show modal backdrop.</td> <td>Show modal backdrop.</td>
</tr> </tr>
<tr>
<td>amDesignator</td>
<td>AM</td>
<td>AM designator</td>
</tr>
<tr>
<td>pmDesignator</td>
<td>PM</td>
<td>PM designator</td>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>
Expand Down
24 changes: 13 additions & 11 deletions js/bootstrap-timepicker.js
Expand Up @@ -28,6 +28,8 @@
this.template = options.template; this.template = options.template;
this.appendWidgetTo = options.appendWidgetTo; this.appendWidgetTo = options.appendWidgetTo;
this.showWidgetOnAddonClick = options.showWidgetOnAddonClick; this.showWidgetOnAddonClick = options.showWidgetOnAddonClick;
this.amDesignator = options.amDesignator ? options.amDesignator : 'AM';
this.pmDesignator = options.pmDesignator ? options.pmDesignator : 'PM';


this._init(); this._init();
}; };
Expand Down Expand Up @@ -618,7 +620,7 @@
hours = dTime.getHours(), hours = dTime.getHours(),
minutes = dTime.getMinutes(), minutes = dTime.getMinutes(),
seconds = dTime.getSeconds(), seconds = dTime.getSeconds(),
meridian = 'AM'; meridian = this.amDesignator;


if (seconds !== 0) { if (seconds !== 0) {
seconds = Math.ceil(dTime.getSeconds() / this.secondStep) * this.secondStep; seconds = Math.ceil(dTime.getSeconds() / this.secondStep) * this.secondStep;
Expand All @@ -643,9 +645,9 @@
if (hours > 12) { if (hours > 12) {
hours = hours - 12; hours = hours - 12;
} }
meridian = 'PM'; meridian = this.pmDesignator;
} else { } else {
meridian = 'AM'; meridian = this.amDesignator;
} }
} }


Expand All @@ -660,7 +662,7 @@
this.hour = 0; this.hour = 0;
this.minute = 0; this.minute = 0;
this.second = 0; this.second = 0;
this.meridian = 'AM'; this.meridian = this.amDesignator;
} else { } else {
this.setTime(defaultTime); this.setTime(defaultTime);
} }
Expand Down Expand Up @@ -688,21 +690,21 @@
second = time.getSeconds(); second = time.getSeconds();


if (this.showMeridian){ if (this.showMeridian){
meridian = 'AM'; meridian = this.amDesignator;
if (hour > 12){ if (hour > 12){
meridian = 'PM'; meridian = this.pmDesignator;
hour = hour % 12; hour = hour % 12;
} }


if (hour === 12){ if (hour === 12){
meridian = 'PM'; meridian = this.pmDesignator;
} }
} }
} else { } else {
if (time.match(/p/i) !== null) { if (time.match(/p/i) !== null) {
meridian = 'PM'; meridian = this.pmDesignator;
} else { } else {
meridian = 'AM'; meridian = this.amDesignator;
} }


time = time.replace(/[^0-9\:]/g, ''); time = time.replace(/[^0-9\:]/g, '');
Expand Down Expand Up @@ -755,7 +757,7 @@
} else if (hour < 0) { } else if (hour < 0) {
hour = 0; hour = 0;
} }
if (hour < 13 && meridian === 'PM') { if (hour < 13 && meridian === this.pmDesignator) {
hour = hour + 12; hour = hour + 12;
} }
} }
Expand Down Expand Up @@ -838,7 +840,7 @@
}, },


toggleMeridian: function() { toggleMeridian: function() {
this.meridian = this.meridian === 'AM' ? 'PM' : 'AM'; this.meridian = this.meridian === this.amDesignator ? this.pmDesignator : this.amDesignator;
}, },


update: function(ignoreWidget) { update: function(ignoreWidget) {
Expand Down
10 changes: 5 additions & 5 deletions js/bootstrap-timepicker.min.js

Large diffs are not rendered by default.

44 changes: 39 additions & 5 deletions spec/js/TimepickerSpec.js
Expand Up @@ -4,12 +4,15 @@ describe('Timepicker feature', function() {
var $input1, var $input1,
$input2, $input2,
$input3, $input3,
$input4,
$timepicker1, $timepicker1,
$timepicker2, $timepicker2,
$timepicker3, $timepicker3,
$timepicker4,
tp1, tp1,
tp2, tp2,
tp3; tp3,
tp4;


beforeEach(function () { beforeEach(function () {
loadFixtures('timepicker.html'); loadFixtures('timepicker.html');
Expand All @@ -35,6 +38,14 @@ describe('Timepicker feature', function() {
defaultTime: '13:25:15' defaultTime: '13:25:15'
}); });
tp3 = $timepicker3.data('timepicker'); tp3 = $timepicker3.data('timepicker');

$input4 = $('#timepicker4');
$timepicker4 = $input4.timepicker({
showMeridian: true,
amDesignator: 'xAM',
pmDesignator: 'xPM'
});
tp4 = $timepicker4.data('timepicker');
}); });


afterEach(function () { afterEach(function () {
Expand All @@ -47,9 +58,13 @@ describe('Timepicker feature', function() {
if ($input3.data('timepicker') !== undefined) { if ($input3.data('timepicker') !== undefined) {
$input3.data('timepicker').remove(); $input3.data('timepicker').remove();
} }
if ($input4.data('timepicker') !== undefined) {
$input4.data('timepicker').remove();
}
$input1.remove(); $input1.remove();
$input2.remove(); $input2.remove();
$input3.remove(); $input3.remove();
$input4.remove();
}); });


it('should be available on the jquery object', function() { it('should be available on the jquery object', function() {
Expand All @@ -73,6 +88,8 @@ describe('Timepicker feature', function() {
expect(tp1.modalBackdrop).toBe(false); expect(tp1.modalBackdrop).toBe(false);
expect(tp1.isOpen).toBe(false); expect(tp1.isOpen).toBe(false);
expect(tp1.showWidgetOnAddonClick).toBe(true); expect(tp1.showWidgetOnAddonClick).toBe(true);
expect(tp1.amDesignator).toBe('AM');
expect(tp1.pmDesignator).toBe('PM');
}); });


it('should allow user to configure defaults', function() { it('should allow user to configure defaults', function() {
Expand All @@ -81,7 +98,7 @@ describe('Timepicker feature', function() {
}); });


it('should be configurable with data attributes', function() { it('should be configurable with data attributes', function() {
$('body').append('<div id="hi" class="bootstrap-timepicker"><input id="customTimepicker" data-template="modal" data-minute-step="30" data-modal-backdrop="true" data-show-meridian="true" type="text"/></div'); $('body').append('<div id="hi" class="bootstrap-timepicker"><input id="customTimepicker" data-am-designator="xAM" data-pm-designator="xPM" data-template="modal" data-minute-step="30" data-modal-backdrop="true" data-show-meridian="true" type="text"/></div>');


var $customInput = $('body').find('#customTimepicker'), var $customInput = $('body').find('#customTimepicker'),
tpCustom = $customInput.timepicker().data('timepicker'); tpCustom = $customInput.timepicker().data('timepicker');
Expand All @@ -91,6 +108,8 @@ describe('Timepicker feature', function() {
expect(tpCustom.minuteStep).toBe(30, 'data-minute-step not working'); expect(tpCustom.minuteStep).toBe(30, 'data-minute-step not working');
expect(tpCustom.modalBackdrop).toBe(true, 'data-modal-backdrop not working'); expect(tpCustom.modalBackdrop).toBe(true, 'data-modal-backdrop not working');
expect(tpCustom.showMeridian).toBe(true, 'data-show-meridian not working'); expect(tpCustom.showMeridian).toBe(true, 'data-show-meridian not working');
expect(tpCustom.amDesignator).toBe('xAM', 'data-am-designator not working');
expect(tpCustom.pmDesignator).toBe('xPM', 'data-pm-designator not working');


tpCustom.remove(); tpCustom.remove();
}); });
Expand Down Expand Up @@ -378,7 +397,6 @@ describe('Timepicker feature', function() {
expect(tp2.minute).toBe(30); expect(tp2.minute).toBe(30);
}); });



it('should increment hour if minutes increment past 59', function() { it('should increment hour if minutes increment past 59', function() {
$input1.val('11:55 AM'); $input1.val('11:55 AM');
tp1.updateFromElementVal(); tp1.updateFromElementVal();
Expand Down Expand Up @@ -417,7 +435,6 @@ describe('Timepicker feature', function() {
expect(tp2.second).toBe(30); expect(tp2.second).toBe(30);
}); });



it('should increment minute by 1 if seconds increment past 59', function() { it('should increment minute by 1 if seconds increment past 59', function() {
$input2.val('11:55:30 AM'); $input2.val('11:55:30 AM');
tp2.updateFromElementVal(); tp2.updateFromElementVal();
Expand Down Expand Up @@ -458,12 +475,12 @@ describe('Timepicker feature', function() {
expect(tp1.getTime()).toBe(''); expect(tp1.getTime()).toBe('');
}); });



it('should not have the widget in the DOM if remove method is called', function() { it('should not have the widget in the DOM if remove method is called', function() {
expect($('body')).toContain('.bootstrap-timepicker-widget'); expect($('body')).toContain('.bootstrap-timepicker-widget');
tp1.remove(); tp1.remove();
tp2.remove(); tp2.remove();
tp3.remove(); tp3.remove();
tp4.remove();
expect($('body')).not.toContain('.bootstrap-timepicker-widget'); expect($('body')).not.toContain('.bootstrap-timepicker-widget');
}); });


Expand All @@ -479,4 +496,21 @@ describe('Timepicker feature', function() {
expect(tp1.isOpen).toBe(true); expect(tp1.isOpen).toBe(true);
}); });


it('should use AM designator specified in initialization options', function() {
$input4.val('11:00 xAM');
tp4.updateFromElementVal();

expect(tp4.hour).toBe(11);
expect(tp4.minute).toBe(0);
expect(tp4.meridian).toBe('xAM');
});

it('should use PM designator specified in initialization options', function() {
$input4.val('11:00 xPM');
tp4.updateFromElementVal();

expect(tp4.hour).toBe(11);
expect(tp4.minute).toBe(0);
expect(tp4.meridian).toBe('xPM');
});
}); });