Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
Default time can be setted to now
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Apr 29, 2014
1 parent ce1c417 commit d0469d9
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 31 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ if (something) {

| Name | Default | Description |
| ---- | ------- | ----------- |
| default | '' | default time, '13:14' e.g. |
| default | '' | default time, 'now' 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 |
| vibrate | true | vibrate the device when dragging clock hand |
| fromnow | 0 | set default time to * milliseconds from now (using with default = 'now') |

## Operations

Expand Down Expand Up @@ -117,16 +118,22 @@ gulp

## Todo

- [] Compiling CSS and JavaScript.
- [] Comments in code.
- [] Add tests.
- [] Add documentation and more examples.
- [] Functional operations, since v0.0.5.
- [ ] Auto placement and align.
- [ ] Events.
- [ ] Customize format.
- [ ] Seconds View ?

## Change log

0.0.6

* Default time can be setted to `now`.
* Registered as a bower package.

0.0.5

* Functional operations.

## License

MIT
29 changes: 29 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "clockpicker",
"description": "A clock-style timepicker for Bootstrap (or jQuery)",
"version": "0.0.6",
"main": [
"dist/jquery-clockpicker.js",
"dist/jquery-clockpicker.css"],
"license": "MIT",
"ignore": [
"*",
"!dist/*"
],
"keywords": ["timepicker", "jquery"],
"authors": [
{
"name": "Wang Shenwei",
"email": "[email protected]",
"homepage": "http://wangshenwei.com/"
}
],
"homepage": "http://weareoutman.github.io/clockpicker/",
"repository": {
"type": "git",
"url": "git://github.com/weareoutman/clockpicker.git"
},
"dependencies": {
"jquery" : ">=1.7"
}
}
2 changes: 1 addition & 1 deletion dist/bootstrap-clockpicker.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* ClockPicker v0.0.5 for Bootstrap (http://weareoutman.github.io/clockpicker/)
* ClockPicker v0.0.6 for Bootstrap (http://weareoutman.github.io/clockpicker/)
* Copyright 2014 Wang Shenwei.
* Licensed under MIT (https://github.com/weareoutman/clockpicker/blob/master/LICENSE)
*/
Expand Down
16 changes: 12 additions & 4 deletions dist/bootstrap-clockpicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* ClockPicker v0.0.5 (http://weareoutman.github.io/clockpicker/)
* ClockPicker v0.0.6 (http://weareoutman.github.io/clockpicker/)
* Copyright 2014 Wang Shenwei.
* Licensed under MIT (https://github.com/weareoutman/clockpicker/blob/master/LICENSE)
*/
Expand Down Expand Up @@ -284,10 +284,11 @@

// Default options
ClockPicker.DEFAULTS = {
'default': '', // default value, '13:14' e.g.
'default': '', // default time, 'now' or '13:14' e.g.
fromnow: 0, // set default time to * milliseconds from now (using with default = 'now')
placement: 'bottom', // clock popover placement
align: 'left', // popover arrow align
donetext: '完成', // done button text
donetext: '完成', // done button text
autoclose: false, // auto close when minute is selected
vibrate: true // vibrate the device when dragging clock hand
};
Expand Down Expand Up @@ -372,6 +373,13 @@

// 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;
this.spanHours.html(leadingZero(this.hours));
Expand Down Expand Up @@ -500,7 +508,7 @@

// Once hours or minutes changed, vibrate the device
if (this[this.currentView] !== value) {
if (vibrate) {
if (vibrate && this.options.vibrate) {
// Do not vibrate too frequently
if (! this.vibrateTimer) {
navigator[vibrate](10);
Expand Down
2 changes: 1 addition & 1 deletion dist/bootstrap-clockpicker.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/bootstrap-clockpicker.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/jquery-clockpicker.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* ClockPicker v0.0.5 for jQuery (http://weareoutman.github.io/clockpicker/)
* ClockPicker v0.0.6 for jQuery (http://weareoutman.github.io/clockpicker/)
* Copyright 2014 Wang Shenwei.
* Licensed under MIT (https://github.com/weareoutman/clockpicker/blob/master/LICENSE)
*
Expand Down
16 changes: 12 additions & 4 deletions dist/jquery-clockpicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* ClockPicker v0.0.5 (http://weareoutman.github.io/clockpicker/)
* ClockPicker v0.0.6 (http://weareoutman.github.io/clockpicker/)
* Copyright 2014 Wang Shenwei.
* Licensed under MIT (https://github.com/weareoutman/clockpicker/blob/master/LICENSE)
*/
Expand Down Expand Up @@ -284,10 +284,11 @@

// Default options
ClockPicker.DEFAULTS = {
'default': '', // default value, '13:14' e.g.
'default': '', // default time, 'now' or '13:14' e.g.
fromnow: 0, // set default time to * milliseconds from now (using with default = 'now')
placement: 'bottom', // clock popover placement
align: 'left', // popover arrow align
donetext: '完成', // done button text
donetext: '完成', // done button text
autoclose: false, // auto close when minute is selected
vibrate: true // vibrate the device when dragging clock hand
};
Expand Down Expand Up @@ -372,6 +373,13 @@

// 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;
this.spanHours.html(leadingZero(this.hours));
Expand Down Expand Up @@ -500,7 +508,7 @@

// Once hours or minutes changed, vibrate the device
if (this[this.currentView] !== value) {
if (vibrate) {
if (vibrate && this.options.vibrate) {
// Do not vibrate too frequently
if (! this.vibrateTimer) {
navigator[vibrate](10);
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery-clockpicker.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/jquery-clockpicker.min.js

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,18 @@ <h4>Set default value, input without addon, and manual operations:</h4>
<div class="clearfix">
<div class="pull-center clearfix" style="margin-bottom:10px;">
<button type="button" class="btn btn-default pull-right" id="check-minutes">Check the minutes</button>
<input class="form-control pull-right" id="single-input" value="" placeholder="现在" style="width:80px;margin-right:20px;">
<input class="form-control pull-right" id="single-input" value="" placeholder="Now" style="width:80px;margin-right:20px;">
</div>
</div>
<pre class="hljs-pre"><code class="html">&lt;input class="form-control" id="single-input" value="" placeholder="现在"&gt;
<pre class="hljs-pre"><code class="html">&lt;input class="form-control" id="single-input" value="" placeholder="Now"&gt;
&lt;button type="button" id="check-minutes"&gt;Check the minutes&lt;/button&gt;

&lt;script type="text/javascript"&gt;
var input = $('#single-input').clockpicker({
placement: 'bottom',
align: 'left',
autoclose: true,
'default': '20:48'
'default': 'now'
});

// Manually toggle to the minutes view
Expand All @@ -195,7 +195,7 @@ <h3>Options</h3>
<tr>
<td>default</td>
<td>''</td>
<td>default time, '13:14' e.g.</td>
<td>default time, 'now' or '13:14' e.g.</td>
</tr>
<tr>
<td>placement</td>
Expand All @@ -222,6 +222,11 @@ <h3>Options</h3>
<td>true</td>
<td>vibrate the device when dragging clock hand</td>
</tr>
<tr>
<td>fromnow</td>
<td>0</td>
<td>set default time to * milliseconds from now (using with default = 'now')</td>
</tr>
</tbody>
</table>

Expand Down Expand Up @@ -297,7 +302,7 @@ <h3>License</h3>
placement: 'bottom',
align: 'left',
autoclose: true,
'default': '20:48'
'default': 'now'
});

// Manually toggle to the minutes view
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "clockpicker",
"description": "A clock-style timepicker for Bootstrap (or jQuery)",
"version": "0.0.5",
"version": "0.0.6",
"author": {
"name": "Wang Shenwei",
"email": "[email protected]",
Expand Down
14 changes: 11 additions & 3 deletions src/clockpicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,11 @@

// Default options
ClockPicker.DEFAULTS = {
'default': '', // default value, '13:14' e.g.
'default': '', // default time, 'now' or '13:14' e.g.
fromnow: 0, // set default time to * milliseconds from now (using with default = 'now')
placement: 'bottom', // clock popover placement
align: 'left', // popover arrow align
donetext: '完成', // done button text
donetext: '完成', // done button text
autoclose: false, // auto close when minute is selected
vibrate: true // vibrate the device when dragging clock hand
};
Expand Down Expand Up @@ -372,6 +373,13 @@

// 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;
this.spanHours.html(leadingZero(this.hours));
Expand Down Expand Up @@ -500,7 +508,7 @@

// Once hours or minutes changed, vibrate the device
if (this[this.currentView] !== value) {
if (vibrate) {
if (vibrate && this.options.vibrate) {
// Do not vibrate too frequently
if (! this.vibrateTimer) {
navigator[vibrate](10);
Expand Down
29 changes: 29 additions & 0 deletions test/suites/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,32 @@ test('clockpicker manual operations', function(){
ok(picker.popover.parent().length === 0, 'clockpicker popover is removed');
ok(! input.data('clockpicker'), 'clockpicker is removed manually');
});

test('clockpicker default time is now', function(){
var input = $('<input />')
.appendTo('#qunit-fixture');

input.clockpicker({
'default': 'now'
});
var now = new Date();
var picker = input.data('clockpicker');
input.clockpicker('show');
strictEqual(picker.hours, now.getHours(), 'hours is setted to now');
strictEqual(picker.minutes, now.getMinutes(), 'minutes is setted to now');

input.remove();
input = $('<input />')
.appendTo('#qunit-fixture');

var fromnow = 9e4;
input.clockpicker({
'default': 'now',
fromnow: fromnow
});
now = new Date(+ new Date() + fromnow);
picker = input.data('clockpicker');
input.clockpicker('show');
strictEqual(picker.hours, now.getHours(), 'hours is setted to now');
strictEqual(picker.minutes, now.getMinutes(), 'minutes is setted to now');
});

0 comments on commit d0469d9

Please sign in to comment.