Set customfields "date" and "date-time" widgets value correctly when they get initiated by storage value

This commit is contained in:
Hadi Nategh 2014-05-08 17:48:54 +00:00
parent 82e259c407
commit b6e2460e91

View File

@ -35,8 +35,9 @@ var et2_date = et2_inputWidget.extend(
"ignore": false
},
"data_format": {
"ignore": true,
"description": "Format data is in. This is not used client-side because it's always a timestamp client side."
"ignore": false,
"description": "Date/Time format. Can be set as an options to date widget",
"default": ''
}
},
@ -101,7 +102,7 @@ var et2_date = et2_inputWidget.extend(
this.createInputWidget();
}
},
set_value: function(_value) {
var old_value = this.getValue();
if(_value === null || _value === "" || _value === undefined ||
@ -121,7 +122,8 @@ var et2_date = et2_inputWidget.extend(
}
// Handle just time as a string in the form H:i
if(typeof _value == 'string' && isNaN(_value)) {
if(typeof _value == 'string' && isNaN(_value))
{
// silently fix skiped minutes or times with just one digit, as parser is quite pedantic ;-)
var fix_reg = new RegExp((this._type == "date-timeonly"?'^':' ')+'([0-9]+)(:[0-9]*)?( ?(a|p)m?)?$','i');
var matches = _value.match(fix_reg);
@ -134,37 +136,56 @@ var et2_date = et2_inputWidget.extend(
_value = _value.replace(fix_reg, (this._type == "date-timeonly"?'':' ')+matches[1]+matches[2]+matches[3]);
if (matches[4] !== undefined) matches[3] = matches[4].toLowerCase() == 'a' ? 'am' : 'pm';
}
if(this._type == "date-timeonly") {
var parsed = jQuery.datepicker.parseTime(this.input_date.datepicker('option', 'timeFormat'), _value);
if (!parsed) // parseTime returns false
{
this.set_validation_error(this.egw().lang("'%1' has an invalid format !!!",_value));
return;
}
this.set_validation_error(false);
this.date.setHours(parsed.hour);
this.date.setMinutes(parsed.minute);
if(old_value !== this.getValue())
{
this.input_date.timepicker('setTime',_value);
if (this._oldValue !== et2_no_init)
switch(this._type)
{
case "date-timeonly":
var parsed = jQuery.datepicker.parseTime(this.input_date.datepicker('option', 'timeFormat'), _value);
if (!parsed) // parseTime returns false
{
this.change(this.input_date);
this.set_validation_error(this.egw().lang("'%1' has an invalid format !!!",_value));
return;
}
}
this._oldValue = _value;
return;
} else {
try { // parseDateTime throws exception
var parsed = jQuery.datepicker.parseDateTime(this.input_date.datepicker('option', 'dateFormat'),
this.input_date.datepicker('option', 'timeFormat'), _value);
this.date = new Date(parsed);
this.set_validation_error(false);
}
catch(e) {
this.set_validation_error(this.egw().lang("'%1' has an invalid format !!!",_value));
this.date.setHours(parsed.hour);
this.date.setMinutes(parsed.minute);
if(old_value !== this.getValue())
{
this.input_date.timepicker('setTime',_value);
if (this._oldValue !== et2_no_init)
{
this.change(this.input_date);
}
}
this._oldValue = _value;
return;
}
default:
if (this.id.match(/^#/g) && this.options.value == _value) // Parse customfields's date with storage data_format to date object
{
switch (this._type)
{
case 'date':
var parsed = jQuery.datepicker.parseDate(this.egw().dateTimeFormat(this.options.data_format), _value);
break;
case 'date-time':
var DTformat = this.options.data_format.split(' ');
var parsed = jQuery.datepicker.parseDateTime(this.egw().dateTimeFormat(DTformat[0]),this.egw().dateTimeFormat(DTformat[1]), _value);
}
this.date = new Date(parsed);
}
else if(!jQuery.isEmptyObject(this._oldValue)) // Parse other date widgets date with timepicker date/time format to date onject
{
var parsed = jQuery.datepicker.parseDateTime(this.input_date.datepicker('option', 'dateFormat'),
this.input_date.datepicker('option', 'timeFormat'), _value);
if(!parsed)
{
this.set_validation_error(this.egw().lang("%1' han an invalid format !!!",_value));
return;
}
this.date = new Date(parsed);
}
this.set_validation_error(false);
}
} else if (typeof _value == 'object' && _value.date) {
this.date = _value.date;