fix utc and clear

This commit is contained in:
vitalets 2013-04-01 14:44:17 +04:00
parent 5d1a5c8fd3
commit 79f2293e44
2 changed files with 29 additions and 21 deletions
package.json
src/inputs/datetime

@ -2,7 +2,7 @@
"name": "X-editable", "name": "X-editable",
"title": "X-editable", "title": "X-editable",
"description": "In-place editing with Twitter Bootstrap, jQuery UI or pure jQuery", "description": "In-place editing with Twitter Bootstrap, jQuery UI or pure jQuery",
"version": "1.4.4", "version": "1.4.3",
"homepage": "http://github.com/vitalets/x-editable", "homepage": "http://github.com/vitalets/x-editable",
"author": { "author": {
"name": "Vitaliy Potapov", "name": "Vitaliy Potapov",

@ -78,20 +78,26 @@ $(function(){
}, },
value2html: function(value, element) { value2html: function(value, element) {
var text = value ? this.dpg.formatDate(value, this.parsedViewFormat, this.options.datetimepicker.language, this.options.formatType) : ''; //formatDate works with UTCDate!
var text = value ? this.dpg.formatDate(this.toUTC(value), this.parsedViewFormat, this.options.datetimepicker.language, this.options.formatType) : '';
DateTime.superclass.value2html(text, element); DateTime.superclass.value2html(text, element);
}, },
html2value: function(html) { html2value: function(html) {
return html ? this.dpg.parseDate(html, this.parsedViewFormat, this.options.datetimepicker.language, this.options.formatType) : null; //parseDate return utc date!
var value = html ? this.dpg.parseDate(html, this.parsedViewFormat, this.options.datetimepicker.language, this.options.formatType) : null;
return value ? this.fromUTC(value) : null;
}, },
value2str: function(value) { value2str: function(value) {
return value ? this.dpg.formatDate(value, this.parsedFormat, this.options.datetimepicker.language, this.options.formatType) : ''; //formatDate works with UTCDate!
return value ? this.dpg.formatDate(this.toUTC(value), this.parsedFormat, this.options.datetimepicker.language, this.options.formatType) : '';
}, },
str2value: function(str) { str2value: function(str) {
return str ? this.dpg.parseDate(str, this.parsedFormat, this.options.datetimepicker.language, this.options.formatType) : null; //parseDate return utc date!
var value = str ? this.dpg.parseDate(str, this.parsedFormat, this.options.datetimepicker.language, this.options.formatType) : null;
return value ? this.fromUTC(value) : null;
}, },
value2submit: function(value) { value2submit: function(value) {
@ -99,11 +105,15 @@ $(function(){
}, },
value2input: function(value) { value2input: function(value) {
this.$input.datetimepicker('update', value); if(value) {
this.$input.data('datetimepicker').setDate(value);
}
}, },
input2value: function() { input2value: function() {
return this.$input.data('datetimepicker').date; //date may be cleared, in that case getDate() triggers error
var dt = this.$input.data('datetimepicker');
return dt.date ? dt.getDate() : null;
}, },
activate: function() { activate: function() {
@ -115,24 +125,22 @@ $(function(){
}, },
autosubmit: function() { autosubmit: function() {
this.$input.on('mouseup', '.day', function(e){ this.$input.on('mouseup', '.minute', function(e){
if($(e.currentTarget).is('.old') || $(e.currentTarget).is('.new')) {
return;
}
var $form = $(this).closest('form'); var $form = $(this).closest('form');
setTimeout(function() { setTimeout(function() {
$form.submit(); $form.submit();
}, 200); }, 200);
}); });
//changedate is not suitable as it triggered when showing datetimepicker. see #149 },
/*
this.$input.on('changeDate', function(e){ //convert date from local to utc
var $form = $(this).closest('form'); toUTC: function(value) {
setTimeout(function() { return value ? new Date(value.valueOf() - value.getTimezoneOffset() * 60000) : value;
$form.submit(); },
}, 200);
}); //convert date from utc to local
*/ fromUTC: function(value) {
return value ? new Date(value.valueOf() + value.getTimezoneOffset() * 60000) : value;
} }
}); });
@ -201,4 +209,4 @@ $(function(){
$.fn.editabletypes.datetime = DateTime; $.fn.editabletypes.datetime = DateTime;
}(window.jQuery)); }(window.jQuery));