oxjs/source/Ox.UI/js/Form/Ox.TimeInput.js

180 lines
5.6 KiB
JavaScript
Raw Normal View History

2011-07-29 18:48:43 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-05-16 10:49:48 +00:00
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
() -> <f> TimeInput Object
(options) -> <f> TimeInput Object
(options, self) -> <f> TimeInput Object
options <o> Options object
ampm <b|false> 24h/ampm
seconds <b|false> show seconds
milliseconds <b|false> show milliseconds
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
self = self || {};
var that = Ox.Element({}, self)
2011-04-22 22:03:10 +00:00
.defaults({
ampm: false,
seconds: false,
milliseconds: false,
value: Ox.formatDate(new Date(), '%T'),
///*
2011-06-01 18:38:58 +00:00
width: {
hours: 32,
minutes: 32,
seconds: 32,
milliseconds: 40,
ampm: 32
}
//*/
2011-04-22 22:03:10 +00:00
})
.options(options || {});
if (self.options.milliseconds) {
self.options.seconds = true;
if (self.options.value.indexOf('.') == -1) {
self.options.value += '.000';
}
}
self.date = getDate();
self.values = getValues();
self.$input = {
hours: Ox.Input({
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',
value: self.values.hours,
width: self.options.width.hours
2011-04-22 22:03:10 +00:00
}),
minutes: Ox.Input({
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',
value: self.values.minutes,
width: self.options.width.minutes
2011-04-22 22:03:10 +00:00
}),
seconds: Ox.Input({
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',
value: self.values.seconds,
width: self.options.width.seconds
2011-04-22 22:03:10 +00:00
}),
milliseconds: Ox.Input({
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',
value: self.values.milliseconds,
width: self.options.width.milliseconds
2011-04-22 22:03:10 +00:00
}),
ampm: Ox.Input({
2011-04-22 22:03:10 +00:00
autocomplete: ['AM', 'PM'],
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
id: 'ampm',
value: self.values.ampm,
width: self.options.width.ampm
2011-04-22 22:03:10 +00:00
})
};
that = Ox.InputGroup(Ox.extend(self.options, {
inputs: Ox.merge([
2011-04-22 22:03:10 +00:00
self.$input.hours,
self.$input.minutes,
], self.options.seconds ? [
self.$input.seconds
] : [], self.options.milliseconds ? [
2011-04-22 22:03:10 +00:00
self.$input.milliseconds
] : [], self.options.ampm ? [
2011-04-22 22:03:10 +00:00
self.$input.ampm
] : []),
separators: Ox.merge([
2011-04-22 22:03:10 +00:00
{title: ':', width: 8},
], self.options.seconds ? [
{title: ':', width: 8}
] : [], self.options.milliseconds ? [
2011-04-22 22:03:10 +00:00
{title: '.', width: 8}
] : [], self.options.ampm ? [
2011-04-22 22:03:10 +00:00
{title: '', width: 8}
] : []),
width: 0
2011-04-22 22:03:10 +00:00
//width: self.options.width || 128
}), self)
.bindEvent('change', setValue);
setValue();
function getDate() {
return new Date('1970/01/01 ' + (
self.options.milliseconds ?
self.options.value.substr(0, self.options.value.length - 4) :
self.options.value
));
}
function getValues() {
self.date = getDate();
return {
ampm: Ox.formatDate(self.date, '%p'),
hours: Ox.formatDate(self.date, self.options.ampm ? '%I' : '%H'),
milliseconds: self.options.milliseconds ? self.options.value.substr(-3) : '000',
minutes: Ox.formatDate(self.date, '%M'),
seconds: Ox.formatDate(self.date, '%S')
};
}
function setValue() {
self.options.value = Ox.formatDate(new Date('1970/01/01 ' + [
self.$input.hours.options('value'),
self.$input.minutes.options('value'),
self.options.seconds ? self.$input.seconds.options('value') : '00'
].join(':') + (self.options.ampm ? ' ' + self.$input.ampm.options('value') : '')),
(self.options.seconds? '%T' : '%H:%M')) +
(self.options.milliseconds ? '.' + self.$input.milliseconds.options('value') : '');
2011-11-04 15:54:28 +00:00
//Ox.Log('Form', 'SETVALUE', self.options.value);
2011-04-22 22:03:10 +00:00
}
function setValues() {
self.values = getValues();
Ox.forEach(self.$input, function(v, k) {
self.$input[k].options({
value: self.values[k]
});
});
}
2011-04-29 12:40:51 +00:00
self.setOption = function(key, value) {
2011-04-22 22:03:10 +00:00
if (key == 'value') {
setValues();
}
};
2011-04-22 22:03:10 +00:00
return that;
};