Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ if (something) {
// Manual operations (after clockpicker is initialized).
$('#demo-input').clockpicker('show') // Or hide, remove ...
.clockpicker('toggleView', 'minutes');

// Get the time of single clockpicker
var dateObject = $('#demo-input').clockpicker('getTime');
console.log(dateObject);

// Get the time of a clockpicker list
$('.clockpicker').clockpicker('getTime', function(dateObject) {
// The clockpicker element
console.log(this);
console.log(dateObject);
});
}
</script>
```
Expand All @@ -69,14 +80,14 @@ if (something) {

| Name | Default | Description |
| ---- | ------- | ----------- |
| default | '' | default time, 'now' or '13:14' e.g. |
| default | '' | default time, 'now', Date object or '13:14' e.g. |
| placement | 'bottom' | popover placement |
| align | 'left' | popover arrow align |
| donetext | '完成' | done button text |
| autoclose | false | auto close when minute is selected |
| twelvehour | false | enables twelve hour mode with AM & PM buttons |
| vibrate | true | vibrate the device when dragging clock hand |
| fromnow | 0 | set default time to * milliseconds from now (using with default = 'now') |
| fromnow | 0 | set default time to * milliseconds from now (using with default = 'now' or default = Date) |
| init | | callback function triggered after the colorpicker has been initiated |
| beforeShow | | callback function triggered before popup is shown |
| afterShow | | callback function triggered after popup is shown |
Expand All @@ -95,6 +106,7 @@ if (something) {
| hide | | hide the clockpicker |
| remove | | remove the clockpicker (and event listeners) |
| toggleView | 'hours' or 'minutes' | toggle to hours or minutes view |
| getTime | Optional callback (Can be used for list of elements) | Returns Date object. (Will call the callback if callback is given) |

## What's included

Expand Down
76 changes: 61 additions & 15 deletions src/clockpicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,30 @@
popover.css(styles);
};

// The input can be changed by the user
// So before we can use this.hours/this.minutes we must update it
ClockPicker.prototype.parseInputValue = function(){
var value = this.input.prop('value') || this.options['default'] || '';

if (value === 'now') {
value = new Date(+ new Date() + this.options.fromnow);
}
if (value instanceof Date) {
value = value.getHours() + ':' + value.getMinutes();
}

value = value.split(':');

// Minutes can have AM/PM that needs to be removed
this.hours = + value[0] || 0;
this.minutes = + (value[1] + '').replace(/\D/g, '') || 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the first + be there? What does it do?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casts it to a number


if (this.twelvehour) {
var period = (value[1] + '').replace(/\d+/g, '').toLowerCase();
this.amOrPm = this.hours < 12 || period === 'am' ? 'AM' : 'PM';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

12:00AM and 12:00PM both get converted to 12:00PM.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattkrick You are right fixed the issue in this commit
JordyMoos@6f0ac5f

}
};

// Show popover
ClockPicker.prototype.show = function(e){
// Not show again
Expand All @@ -453,17 +477,9 @@
this.isAppended = true;
}

// Get the time
var value = ((this.input.prop('value') || this.options['default'] || '') + '').split(':');
if (value[0] === 'now') {
var now = new Date(+ new Date() + this.options.fromnow);
value = [
now.getHours(),
now.getMinutes()
];
}
this.hours = + value[0] || 0;
this.minutes = + value[1] || 0;
// Get the time from the input field
this.parseInputValue();

this.spanHours.html(leadingZero(this.hours));
this.spanMinutes.html(leadingZero(this.minutes));

Expand Down Expand Up @@ -670,6 +686,23 @@
this.fg.setAttribute('cy', cy);
};

// Allow user to get time time as Date object
ClockPicker.prototype.getTime = function(callback) {
this.parseInputValue();

var hours = this.hours;
if (this.options.twelvehour && hours < 12 && this.amOrPm === 'PM') {
hours += 12;
}

var selectedTime = new Date();
selectedTime.setMinutes(this.minutes)
selectedTime.setHours(hours);
selectedTime.setSeconds(0);

return callback && callback.apply(this.element, selectedTime) || selectedTime;
}

// Hours and minutes are selected
ClockPicker.prototype.done = function() {
raiseCallback(this.options.beforeDone);
Expand Down Expand Up @@ -712,18 +745,31 @@
// Extends $.fn.clockpicker
$.fn.clockpicker = function(option){
var args = Array.prototype.slice.call(arguments, 1);
return this.each(function(){

function handleClockPickerRequest() {
var $this = $(this),
data = $this.data('clockpicker');
if (! data) {
var options = $.extend({}, ClockPicker.DEFAULTS, $this.data(), typeof option == 'object' && option);
$this.data('clockpicker', new ClockPicker($this, options));
} else {
// Manual operatsions. show, hide, remove, e.g.
// Manual operations. show, hide, remove, getTime, e.g.
if (typeof data[option] === 'function') {
data[option].apply(data, args);
return data[option].apply(data, args);
}
}
});
}

// If we explicitly do a call on a single element then we can return the value (if needed)
// This allows us, for example, to return the value of getTime
if (this.length == 1) {
var returnValue = handleClockPickerRequest.apply(this[0]);

// If we do not have any return value then return the object itself so you can chain
return returnValue !== undefined ? returnValue : this;
}

// If we do have a list then we do not care about return values
return this.each(handleClockPickerRequest);
};
}());