From 1caba189307eb4c475a5aba29b7be3f21001871f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 26 Jan 2015 11:32:47 +0100 Subject: [PATCH] Simplify date, time and datetime widgets - Remove datetime widget in list view - Allow to use widget attribute for date/time issue4457 review10011002 --- src/action.js | 5 +++ src/common.js | 57 +++++++++++++++++++++++----------- src/model.js | 66 +++++++++++++++------------------------- src/sao.js | 6 ++++ src/view.js | 84 +++++++++++++++++++++++++++++++++++++++------------ 5 files changed, 139 insertions(+), 79 deletions(-) diff --git a/src/action.js b/src/action.js index 54820161b4..ab55160932 100644 --- a/src/action.js +++ b/src/action.js @@ -50,6 +50,11 @@ ctx = jQuery.extend(ctx, params.context); ctx = jQuery.extend(ctx, context); params.context = jQuery.extend(params.context, context); + if (!('date_format' in params.context)) { + if (session.context.locale && session.context.locale.date) { + params.context.date_format = session.context.locale.date; + } + } var domain_context = jQuery.extend({}, ctx); domain_context.context = ctx; diff --git a/src/common.js b/src/common.js index fb5449b667..db94a150a8 100644 --- a/src/common.js +++ b/src/common.js @@ -78,22 +78,25 @@ return prm.fail(); }; - Sao.common.date_format = function() { - if (Sao.Session.current_session) { + Sao.common.date_format = function(format) { + if (jQuery.isEmptyObject(format) && Sao.Session.current_session) { var context = Sao.Session.current_session.context; if (context.locale && context.locale.date) { - return context.locale.date - .replace('%d', 'dd') - .replace('%j', 'oo') - .replace('%a', 'D') - .replace('%A', 'DD') - .replace('%m', 'mm') - .replace('%b', 'M') - .replace('%B', 'MM') - .replace('%y', 'y') - .replace('%Y', 'yy'); + format = context.locale.date; } } + if (format) { + return format + .replace('%d', 'dd') + .replace('%j', 'oo') + .replace('%a', 'D') + .replace('%A', 'DD') + .replace('%m', 'mm') + .replace('%b', 'M') + .replace('%B', 'MM') + .replace('%y', 'y') + .replace('%Y', 'yy'); + } return jQuery.datepicker.W3C; }; @@ -123,7 +126,30 @@ getNumber('%f')); }; + Sao.common.format_date = function(date_format, date) { + if (!date) { + return ''; + } + return jQuery.datepicker.formatDate(date_format, date); + }; + + Sao.common.parse_date = function(value) { + try { + value = jQuery.datepicker.parseDate( + Sao.common.date_format(), value); + if (value) { + value = Sao.Date(value); + } + } catch (e) { + value = null; + } + return value; + }; + Sao.common.format_datetime = function(date_format, time_format, date) { + if (!date) { + return ''; + } return (jQuery.datepicker.formatDate(date_format, date) + ' ' + Sao.common.format_time(time_format, date)); }; @@ -1220,7 +1246,7 @@ !(value.getHours() || value.getMinutes() || value.getSeconds())) { - return jQuery.datepicker.formatDate( + return Sao.common.format_date( Sao.common.date_format(), value); } @@ -1230,10 +1256,7 @@ value); }.bind(this), 'date': function() { - if (!value) { - return ''; - } - return jQuery.datepicker.formatDate( + return Sao.common.format_date( Sao.common.date_format(), value); }, diff --git a/src/model.js b/src/model.js index 058e9b4f71..ef2424c8dd 100644 --- a/src/model.js +++ b/src/model.js @@ -1241,53 +1241,42 @@ return record.expr_eval(this.description.format); }, set_client: function(record, value, force_change) { - if (typeof value == 'string') { - try { - value = Sao.common.parse_datetime( - Sao.common.date_format(), - this.time_format(record), - value); - } catch (e) { - value = this._default; + var current_value; + if (value instanceof Sao.Time) { + current_value = this.get(record); + if (current_value) { + value = Sao.DateTime.combine(current_value, value); + } else { + value = null; + } + } else if ((value instanceof Date) && value.isDate) { + current_value = this.get(record); + if (current_value) { + value = Sao.DateTime.combine(value, current_value); } } Sao.field.DateTime._super.set_client.call(this, record, value, force_change); }, - get_client: function(record) { - var value = Sao.field.Date._super.get_client.call(this, record); - if (value) { - return Sao.common.format_datetime(Sao.common.date_format(), - this.time_format(record), value); - } - return ''; + date_format: function(record) { + var context = this.get_context(record); + return Sao.common.date_format(context.date_format); } }); Sao.field.Date = Sao.class_(Sao.field.Field, { _default: null, set_client: function(record, value, force_change) { - if (typeof value == 'string') { - try { - value = jQuery.datepicker.parseDate( - Sao.common.date_format(), value); - if (value) { - value = Sao.Date(value); - } - } catch (e) { - value = this._default; - } + if (value && !value.isDate) { + value.isDate = true; + value.isDateTime = false; } Sao.field.Date._super.set_client.call(this, record, value, force_change); }, - get_client: function(record) { - var value = Sao.field.Date._super.get_client.call(this, record); - if (value) { - return jQuery.datepicker.formatDate(Sao.common.date_format(), - value); - } - return ''; + date_format: function(record) { + var context = this.get_context(record); + return Sao.common.date_format(context.date_format); } }); @@ -1297,19 +1286,12 @@ return record.expr_eval(this.description.format); }, set_client: function(record, value, force_change) { - if (typeof value == 'string') { - value = Sao.common.parse_time(this.time_format(record), value); + if (value && value instanceof Date) { + value = new Sao.Time(value.getHours(), value.getMinutes(), + value.getSeconds(), value.getMilliseconds()); } Sao.field.Time._super.set_client.call(this, record, value, force_change); - }, - get_client: function(record) { - var value = Sao.field.Time._super.get_client.call(this, record); - if (value) { - return Sao.common.format_time(this.time_format(record), - value); - } - return ''; } }); diff --git a/src/sao.js b/src/sao.js index dc69ed94d2..71c3e3db2a 100644 --- a/src/sao.js +++ b/src/sao.js @@ -118,6 +118,12 @@ var Sao = {}; return datetime; }; + Sao.DateTime.combine = function(date, time) { + return new Sao.DateTime(date.year, date.month, date.day, + time.getHours(), time.getMinutes, + time.getSeconds(), time.getMilliseconds()); + }; + Sao.Time = Sao.class_(Object, { init: function(hour, minute, second, millisecond) { this.date = new Date(0, 0, 0, hour, minute, second, diff --git a/src/view.js b/src/view.js index 38c557e203..341ed7b16b 100644 --- a/src/view.js +++ b/src/view.js @@ -55,8 +55,6 @@ return Sao.View.Tree.Many2OneColumn; case 'date': return Sao.View.Tree.DateColumn; - case 'datetime': - return Sao.View.Tree.DateTimeColumn; case 'time': return Sao.View.Tree.TimeColumn; case 'one2many': @@ -1040,15 +1038,20 @@ }); Sao.View.Tree.DateColumn = Sao.class_(Sao.View.Tree.CharColumn, { - class_: 'column-date' - }); - - Sao.View.Tree.DateTimeColumn = Sao.class_(Sao.View.Tree.CharColumn, { - class_: 'column-datetime' + class_: 'column-date', + update_text: function(cell, record) { + var value = this.field.get_client(record); + var date_format = this.field.date_format(record); + cell.text(Sao.common.format_date(date_format, value)); + } }); Sao.View.Tree.TimeColumn = Sao.class_(Sao.View.Tree.CharColumn, { - class_: 'column-time' + class_: 'column-time', + update_text: function(cell, record) { + var value = this.field.get_client(record); + cell.text(Soa.common.format_time(field.time_format(record), value)); + } }); Sao.View.Tree.One2ManyColumn = Sao.class_(Sao.View.Tree.CharColumn, { @@ -1743,6 +1746,8 @@ return Sao.View.Form.Date; case 'datetime': return Sao.View.Form.DateTime; + case 'time': + return Sao.View.Form.Time; case 'integer': case 'biginteger': return Sao.View.Form.Integer; @@ -1936,7 +1941,12 @@ }.bind(this)); }, get_format: function(record, field) { - return Sao.common.date_format(); + return field.date_format(record); + }, + format: function(record, field) { + var value = field.get_client(record); + var date_format = field.date_format(record); + return Sao.common.format_date(date_format, value); }, display: function(record, field) { if (record && field) { @@ -1945,11 +1955,14 @@ } Sao.View.Form.Date._super.display.call(this, record, field); if (record) { - this.date.val(record.field_get_client(this.field_name)); + this.date.val(this.format(record, field)); + } else { + this.date.val(''); } }, set_value: function(record, field) { - field.set_client(record, this.date.val()); + var value = Sao.common.parse_date(this.date.val()); + field.set_client(record, value); } }); @@ -1963,7 +1976,7 @@ this.field().time_format(this.record()), this._get_time()); this.date.datepicker('option', 'dateFormat', - Sao.common.date_format() + time); + this.field().date_format(this.record()) + time); this.date.prop('disabled', true); }.bind(this)); this.date.datepicker('option', 'onClose', function() { @@ -1971,7 +1984,8 @@ }.bind(this)); }, _get_time: function() { - return Sao.common.parse_datetime(Sao.common.date_format(), + return Sao.common.parse_datetime( + this.field().date_format(this.record()), this.field().time_format(this.record()), this.date.val()); }, get_format: function(record, field) { @@ -1981,12 +1995,44 @@ time = ' ' + Sao.common.format_time(field.time_format(record), value); } - return Sao.common.date_format() + time; + return field.date_format(record) + time; + }, + format: function(record, field) { + var value = field.get_client(record); + return Sao.common.format_datetime( + field.date_format(record), field.time_format(record), + value); + }, + set_value: function(record, field) { + var value = Sao.common.parse_datetime( + field.date_format(record), + field.time_format(record), + this.date.val()); + field.set_client(record, value); } }); Sao.View.Form.Time = Sao.class_(Sao.View.Form.Char, { - class_: 'form-time' + class_: 'form-time', + format: function(record, field) { + var value = field.get_client(record); + return Sao.common.format_time(field.time_format(record), value); + }, + display: function(record, field) { + // Skip Char + Sao.View.Form.Widget.display.call(this, record, field); + if (record) { + this.el.val(this.format(record, field)); + } else { + this.el.val(''); + } + }, + set_value: function(record, field) { + var value = Sao.common.parse_time( + field.time_format(record), + this.date.val()); + field.set_client(record, value); + } }); Sao.View.Form.Integer = Sao.class_(Sao.View.Form.Char, { @@ -3318,8 +3364,6 @@ return Sao.View.EditableTree.Char; case 'date': return Sao.View.EditableTree.Date; - case 'datetime': - return Sao.View.EditableTree.DateTime; case 'integer': case 'biginteger': return Sao.View.EditableTree.Integer; @@ -3371,10 +3415,10 @@ } }); - Sao.View.EditableTree.DateTime = Sao.class_(Sao.View.Form.DateTime, { - class_: 'editabletree-datetime', + Sao.View.EditableTree.Time = Sao.class_(Sao.View.Form.Time, { + class_: 'editabletree-time', init: function(field_name, model, attributes) { - Sao.View.EditableTree.DateTime._super.init.call(this, field_name, + Sao.View.EditableTree.Time._super.init.call(this, field_name, model, attributes); Sao.View.EditableTree.editable_mixin(this); }