2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-05-16 10:49:48 +00:00
|
|
|
/*@
|
|
|
|
Ox.TimeInput <f:Ox.Element> TimeInput Object
|
2012-05-22 13:14:40 +00:00
|
|
|
([options[, self]]) -> <f> TimeInput Object
|
2011-05-16 10:49:48 +00:00
|
|
|
options <o> Options object
|
|
|
|
ampm <b|false> 24h/ampm
|
|
|
|
seconds <b|false> show seconds
|
|
|
|
milliseconds <b|false> show milliseconds
|
2011-09-08 08:16:31 +00:00
|
|
|
value <d> value, defaults to current time
|
2011-05-16 10:49:48 +00:00
|
|
|
self <o> shared private variable
|
|
|
|
@*/
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
Ox.TimeInput = function(options, self) {
|
|
|
|
|
|
|
|
// fixme: seconds get set even if options.seconds is false
|
2011-12-21 13:42:47 +00:00
|
|
|
var that;
|
|
|
|
self = Ox.extend(self || {}, {
|
|
|
|
options: Ox.extend({
|
|
|
|
ampm: false,
|
|
|
|
seconds: false,
|
|
|
|
milliseconds: false,
|
|
|
|
value: (function() {
|
|
|
|
var date = new Date();
|
|
|
|
return Ox.formatDate(
|
|
|
|
date,
|
|
|
|
options.seconds || options.milliseconds ? '%T' : '%H:%M'
|
|
|
|
) + (options.milliseconds ? '.' + Ox.pad(date % 1000, 3) : '');
|
|
|
|
}()),
|
|
|
|
width: {
|
|
|
|
hours: 32,
|
|
|
|
minutes: 32,
|
|
|
|
seconds: 32,
|
|
|
|
milliseconds: 40,
|
|
|
|
ampm: 32
|
|
|
|
}
|
|
|
|
}, options || {})
|
|
|
|
});
|
|
|
|
|
|
|
|
self.options.seconds = self.options.seconds || self.options.milliseconds;
|
2011-04-22 22:03:10 +00:00
|
|
|
|
|
|
|
self.$input = {
|
2011-06-19 17:48:32 +00:00
|
|
|
hours: Ox.Input({
|
2011-09-08 08:16:31 +00:00
|
|
|
autocomplete: (self.options.ampm ? Ox.range(1, 13) : Ox.range(0, 24)).map(function(i) {
|
|
|
|
return Ox.pad(i, 2);
|
2011-04-22 22:03:10 +00:00
|
|
|
}),
|
|
|
|
autocompleteReplace: true,
|
|
|
|
autocompleteReplaceCorrect: true,
|
|
|
|
id: 'hours',
|
|
|
|
textAlign: 'right',
|
2011-09-08 08:16:31 +00:00
|
|
|
width: self.options.width.hours
|
2011-04-22 22:03:10 +00:00
|
|
|
}),
|
2011-06-19 17:48:32 +00:00
|
|
|
minutes: Ox.Input({
|
2011-09-08 08:16:31 +00:00
|
|
|
autocomplete: Ox.range(0, 60).map(function(i) {
|
|
|
|
return Ox.pad(i, 2);
|
2011-04-22 22:03:10 +00:00
|
|
|
}),
|
|
|
|
autocompleteReplace: true,
|
|
|
|
autocompleteReplaceCorrect: true,
|
|
|
|
id: 'minutes',
|
|
|
|
textAlign: 'right',
|
2011-09-08 08:16:31 +00:00
|
|
|
width: self.options.width.minutes
|
2011-04-22 22:03:10 +00:00
|
|
|
}),
|
2011-06-19 17:48:32 +00:00
|
|
|
seconds: Ox.Input({
|
2011-09-08 08:16:31 +00:00
|
|
|
autocomplete: Ox.range(0, 60).map(function(i) {
|
|
|
|
return Ox.pad(i, 2);
|
2011-04-22 22:03:10 +00:00
|
|
|
}),
|
|
|
|
autocompleteReplace: true,
|
|
|
|
autocompleteReplaceCorrect: true,
|
|
|
|
id: 'seconds',
|
|
|
|
textAlign: 'right',
|
2011-09-08 08:16:31 +00:00
|
|
|
width: self.options.width.seconds
|
2011-04-22 22:03:10 +00:00
|
|
|
}),
|
2011-06-19 17:48:32 +00:00
|
|
|
milliseconds: Ox.Input({
|
2011-09-08 08:16:31 +00:00
|
|
|
autocomplete: Ox.range(0, 1000).map(function(i) {
|
|
|
|
return Ox.pad(i, 3);
|
2011-04-22 22:03:10 +00:00
|
|
|
}),
|
|
|
|
autocompleteReplace: true,
|
|
|
|
autocompleteReplaceCorrect: true,
|
|
|
|
id: 'milliseconds',
|
|
|
|
textAlign: 'right',
|
2011-09-08 08:16:31 +00:00
|
|
|
width: self.options.width.milliseconds
|
2011-04-22 22:03:10 +00:00
|
|
|
}),
|
2011-06-19 17:48:32 +00:00
|
|
|
ampm: Ox.Input({
|
2011-04-22 22:03:10 +00:00
|
|
|
autocomplete: ['AM', 'PM'],
|
|
|
|
autocompleteReplace: true,
|
|
|
|
autocompleteReplaceCorrect: true,
|
|
|
|
id: 'ampm',
|
2011-09-08 08:16:31 +00:00
|
|
|
width: self.options.width.ampm
|
2011-04-22 22:03:10 +00:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2011-09-08 08:16:31 +00:00
|
|
|
that = Ox.InputGroup(Ox.extend(self.options, {
|
2011-12-21 13:42:47 +00:00
|
|
|
id: self.options.id,
|
|
|
|
inputs: Ox.merge([
|
|
|
|
self.$input.hours,
|
|
|
|
self.$input.minutes,
|
|
|
|
], self.options.seconds ? [
|
|
|
|
self.$input.seconds
|
|
|
|
] : [], self.options.milliseconds ? [
|
|
|
|
self.$input.milliseconds
|
|
|
|
] : [], self.options.ampm ? [
|
|
|
|
self.$input.ampm
|
|
|
|
] : []),
|
|
|
|
join: join,
|
|
|
|
separators: Ox.merge([
|
|
|
|
{title: ':', width: 8},
|
|
|
|
], self.options.seconds ? [
|
|
|
|
{title: ':', width: 8}
|
|
|
|
] : [], self.options.milliseconds ? [
|
|
|
|
{title: '.', width: 8}
|
|
|
|
] : [], self.options.ampm ? [
|
|
|
|
{title: '', width: 8}
|
|
|
|
] : []),
|
|
|
|
split: split,
|
|
|
|
value: self.options.value,
|
|
|
|
width: 0
|
|
|
|
}), self);
|
2011-04-22 22:03:10 +00:00
|
|
|
|
|
|
|
function getDate() {
|
|
|
|
return new Date('1970/01/01 ' + (
|
2011-12-21 13:42:47 +00:00
|
|
|
self.options.milliseconds
|
|
|
|
? self.options.value.substr(0, self.options.value.length - 4)
|
|
|
|
: self.options.value
|
2011-04-22 22:03:10 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getValues() {
|
2011-12-21 13:42:47 +00:00
|
|
|
var date = getDate();
|
2011-04-22 22:03:10 +00:00
|
|
|
return {
|
2011-12-21 13:42:47 +00:00
|
|
|
ampm: Ox.formatDate(date, '%p'),
|
|
|
|
hours: Ox.formatDate(date, self.options.ampm ? '%I' : '%H'),
|
2011-04-22 22:03:10 +00:00
|
|
|
milliseconds: self.options.milliseconds ? self.options.value.substr(-3) : '000',
|
2011-12-21 13:42:47 +00:00
|
|
|
minutes: Ox.formatDate(date, '%M'),
|
|
|
|
seconds: Ox.formatDate(date, '%S')
|
2011-04-22 22:03:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
function join() {
|
|
|
|
return Ox.formatDate(
|
|
|
|
new Date(
|
|
|
|
'1970/01/01 ' + [
|
2011-12-21 15:33:52 +00:00
|
|
|
self.$input.hours.value(),
|
|
|
|
self.$input.minutes.value(),
|
|
|
|
self.options.seconds ? self.$input.seconds.value() : '00'
|
2011-12-21 13:42:47 +00:00
|
|
|
].join(':') + (
|
2011-12-21 15:33:52 +00:00
|
|
|
self.options.ampm ? ' ' + self.$input.ampm.value() : ''
|
2011-12-21 13:42:47 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
self.options.seconds ? '%T' : '%H:%M'
|
|
|
|
) + (
|
2011-12-21 15:33:52 +00:00
|
|
|
self.options.milliseconds ? '.' + self.$input.milliseconds.value() : ''
|
2011-12-21 13:42:47 +00:00
|
|
|
)
|
|
|
|
);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
function split(value) {
|
|
|
|
var values = getValues();
|
|
|
|
return Ox.merge([
|
|
|
|
values.hours,
|
|
|
|
values.minutes,
|
|
|
|
], self.options.seconds ? [
|
|
|
|
values.seconds
|
|
|
|
] : [], self.options.milliseconds ? [
|
|
|
|
values.milliseconds
|
|
|
|
] : [], self.options.ampm ? [
|
|
|
|
values.ampm
|
|
|
|
] : []);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|