diff options
| author | Cedric Snauwaert <csn@openerp.com> | 2014-09-02 11:22:11 +0200 |
|---|---|---|
| committer | Fabien Meghazi <fme@openerp.com> | 2014-11-05 15:27:59 +0100 |
| commit | ef886aed2f4c7d9b78123d1cd980a899bf6b20ea (patch) | |
| tree | 0a51dc66a9ac12d898116721cc990882eb4588b0 /addons/web/static/src/js | |
| parent | 893d7fa8643192aec817478effa44c55d3c1ebc4 (diff) | |
[FIX] web: remove datejs and replace by momentjs
Diffstat (limited to 'addons/web/static/src/js')
| -rw-r--r-- | addons/web/static/src/js/core.js | 8 | ||||
| -rw-r--r-- | addons/web/static/src/js/formats.js | 85 | ||||
| -rw-r--r-- | addons/web/static/src/js/search.js | 2 | ||||
| -rw-r--r-- | addons/web/static/src/js/view_form.js | 4 | ||||
| -rw-r--r-- | addons/web/static/src/js/view_list.js | 2 | ||||
| -rw-r--r-- | addons/web/static/src/js/view_tree.js | 2 |
6 files changed, 29 insertions, 74 deletions
diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index df810bb67b9..ca13482caa8 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -328,13 +328,6 @@ instance.web.Session.include( /** @lends instance.web.Session# */{ }).done(function() { self.on_modules_loaded(); self.trigger('module_loaded'); - if (!Date.CultureInfo.pmDesignator) { - // If no am/pm designator is specified but the openerp - // datetime format uses %i, date.js won't be able to - // correctly format a date. See bug#938497. - Date.CultureInfo.amDesignator = 'AM'; - Date.CultureInfo.pmDesignator = 'PM'; - } }); }); }, @@ -633,6 +626,7 @@ instance.web._lt = function (s) { instance.web.qweb.debug = instance.session.debug; _.extend(instance.web.qweb.default_dict, { '__debug__': instance.session.debug, + 'moment': function(date) { return new moment(date); }, }); instance.web.qweb.preprocess_node = function() { // Note that 'this' is the Qweb Node diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js index 14026f17681..3c044bdff99 100644 --- a/addons/web/static/src/js/formats.js +++ b/addons/web/static/src/js/formats.js @@ -67,28 +67,6 @@ instance.web.insert_thousand_seps = function (num) { }; /** - * removes literal (non-format) text from a date or time pattern, as datejs can - * not deal with literal text in format strings (whatever the format), whereas - * strftime allows for literal characters - * - * @param {String} value original format - */ -instance.web.strip_raw_chars = function (value) { - var isletter = /[a-zA-Z]/, output = []; - for(var index=0; index < value.length; ++index) { - var character = value[index]; - if(isletter.test(character) && (index === 0 || value[index-1] !== '%')) { - continue; - } - output.push(character); - } - return output.join(''); -}; -var normalize_format = function (format) { - return Date.normalizeFormat(instance.web.strip_raw_chars(format)); -}; - -/** * Check with a scary heuristic if the value is a bin_size or not. * If not, compute an approximate size out of the base64 encoded string. * @@ -134,6 +112,9 @@ instance.web.human_size = function(size) { * @param {String} [value_if_empty=''] returned if the ``value`` argument is considered empty */ instance.web.format_value = function (value, descriptor, value_if_empty) { + var l10n = _t.database.parameters; + var date_format = instance.web.normalize_format(l10n.date_format); + var time_format = instance.web.normalize_format(l10n.time_format) // If NaN value, display as with a `false` (empty cell) if (typeof value === 'number' && isNaN(value)) { value = false; @@ -152,7 +133,6 @@ instance.web.format_value = function (value, descriptor, value_if_empty) { case -Infinity: return value_if_empty === undefined ? '' : value_if_empty; } - var l10n = _t.database.parameters; switch (descriptor.widget || descriptor.type || (descriptor.field && descriptor.field.type)) { case 'id': return value.toString(); @@ -190,18 +170,17 @@ instance.web.format_value = function (value, descriptor, value_if_empty) { return _.str.sprintf(_t("(%d records)"), value.length); case 'datetime': if (typeof(value) == "string") - value = instance.web.auto_str_to_date(value); + value = new moment(instance.web.auto_str_to_date(value)); - return value.toString(normalize_format(l10n.date_format) - + ' ' + normalize_format(l10n.time_format)); + return value.format(date_format + ' ' + time_format); case 'date': if (typeof(value) == "string") - value = instance.web.str_to_date(value.substring(0,10)); - return value.toString(normalize_format(l10n.date_format)); + value = new moment(instance.web.str_to_date(value.substring(0,10))); + return value.format(date_format); case 'time': if (typeof(value) == "string") - value = instance.web.auto_str_to_date(value); - return value.toString(normalize_format(l10n.time_format)); + value = new moment(instance.web.auto_str_to_date(value)); + return value.format(time_format); case 'selection': case 'statusbar': // Each choice is [value, label] if(_.isArray(value)) { @@ -218,8 +197,10 @@ instance.web.format_value = function (value, descriptor, value_if_empty) { }; instance.web.parse_value = function (value, descriptor, value_if_empty) { - var date_pattern = normalize_format(_t.database.parameters.date_format), - time_pattern = normalize_format(_t.database.parameters.time_format); + var date_pattern = instance.web.normalize_format(_t.database.parameters.date_format), + time_pattern = instance.web.normalize_format(_t.database.parameters.time_format); + var date_pattern_wo_zero = date_pattern.replace('MM','M').replace('DD','D'), + time_pattern_wo_zero = date_pattern.replace('HH','H').replace('mm','m').replace('ss','s'); switch (value) { case false: case "": @@ -267,39 +248,19 @@ instance.web.parse_value = function (value, descriptor, value_if_empty) { case 'progressbar': return instance.web.parse_value(value, {type: "float"}); case 'datetime': - var datetime = Date.parseExact( - value, (date_pattern + ' ' + time_pattern)); - if (datetime !== null) - return instance.web.datetime_to_str(datetime); - datetime = Date.parseExact(value.toString().replace(/\d+/g, function(m){ - return m.length === 1 ? "0" + m : m ; - }), (date_pattern + ' ' + time_pattern)); - if (datetime !== null) - return instance.web.datetime_to_str(datetime); - datetime = Date.parse(value); - if (datetime !== null) - return instance.web.datetime_to_str(datetime); + var datetime = new moment(value, [date_pattern + ' ' + time_pattern, date_pattern_wo_zero + ' ' + time_pattern_wo_zero], true) + if (datetime.isValid()) + return instance.web.datetime_to_str(datetime.toDate()); throw new Error(_.str.sprintf(_t("'%s' is not a correct datetime"), value)); case 'date': - var date = Date.parseExact(value, date_pattern); - if (date !== null) - return instance.web.date_to_str(date); - date = Date.parseExact(value.toString().replace(/\d+/g, function(m){ - return m.length === 1 ? "0" + m : m ; - }), date_pattern); - if (date !== null) - return instance.web.date_to_str(date); - date = Date.parse(value); - if (date !== null) - return instance.web.date_to_str(date); + var date = new moment(value, [date_pattern, date_pattern_wo_zero], true) + if (date.isValid()) + return instance.web.date_to_str(date.toDate()); throw new Error(_.str.sprintf(_t("'%s' is not a correct date"), value)); case 'time': - var time = Date.parseExact(value, time_pattern); - if (time !== null) - return instance.web.time_to_str(time); - time = Date.parse(value); - if (time !== null) - return instance.web.time_to_str(time); + var time = new moment(value, [time_pattern, time_pattern_wo_zero], true); + if (time.isValid()) + return instance.web.time_to_str(time.toDate()); throw new Error(_.str.sprintf(_t("'%s' is not a correct time"), value)); } return value; @@ -366,7 +327,7 @@ instance.web.round_decimals = function(value, decimals){ + * convert python.strftime format into moment.js format + * inspired from : https://github.com/uruz/moment-datetime/blob/master/moment-datetime.js +*/ -instance.web.convert_to_moment_format = function(format){ +instance.web.normalize_format = function(format){ if (!format) return false; var replacements = { diff --git a/addons/web/static/src/js/search.js b/addons/web/static/src/js/search.js index 86412907f4f..6e5ff2df959 100644 --- a/addons/web/static/src/js/search.js +++ b/addons/web/static/src/js/search.js @@ -1272,7 +1272,7 @@ instance.web.search.DateField = instance.web.search.Field.extend(/** @lends inst return instance.web.date_to_str(facetValue.get('value')); }, complete: function (needle) { - var d = Date.parse(needle); + var d = new moment(needle).toDate(); if (!d) { return $.when(null); } var date_string = instance.web.format_value(d, this.attrs); var label = _.str.sprintf(_.str.escapeHTML( diff --git a/addons/web/static/src/js/view_form.js b/addons/web/static/src/js/view_form.js index 7156fdaba5d..c483e58d998 100644 --- a/addons/web/static/src/js/view_form.js +++ b/addons/web/static/src/js/view_form.js @@ -2640,13 +2640,13 @@ instance.web.DateTimeWidget = instance.web.Widget.extend({ down: 'fa fa-chevron-down' }, language : moment.locale(), - format : instance.web.convert_to_moment_format(l10n.date_format +' '+ l10n.time_format), + format : instance.web.normalize_format(l10n.date_format +' '+ l10n.time_format), }; this.$input = this.$el.find('input.oe_datepicker_master'); if (this.type_of_date === 'date') { options['pickTime'] = false; options['useSeconds'] = false; - options['format'] = instance.web.convert_to_moment_format(l10n.date_format); + options['format'] = instance.web.normalize_format(l10n.date_format); } this.picker = this.$('.oe_datepicker_main').datetimepicker(options); this.set_readonly(false); diff --git a/addons/web/static/src/js/view_list.js b/addons/web/static/src/js/view_list.js index 1e1c967de22..524658f26c9 100644 --- a/addons/web/static/src/js/view_list.js +++ b/addons/web/static/src/js/view_list.js @@ -171,7 +171,7 @@ instance.web.ListView = instance.web.View.extend( /** @lends instance.web.ListVi var context = _.extend({}, record.attributes, { uid: this.session.uid, - current_date: new Date().toString('yyyy-MM-dd') + current_date: new moment(new Date()).format('YYYY-MM-DD') // TODO: time, datetime, relativedelta }); var i; diff --git a/addons/web/static/src/js/view_tree.js b/addons/web/static/src/js/view_tree.js index 2f92042c4d0..027e1b64095 100644 --- a/addons/web/static/src/js/view_tree.js +++ b/addons/web/static/src/js/view_tree.js @@ -138,7 +138,7 @@ instance.web.TreeView = instance.web.View.extend(/** @lends instance.web.TreeVie if (!this.colors) { return ''; } var context = _.extend({}, record, { uid: this.session.uid, - current_date: new Date().toString('yyyy-MM-dd') + current_date: new moment(new Date()).format('YYYY-MM-DD') // TODO: time, datetime, relativedelta }); for(var i=0, len=this.colors.length; i<len; ++i) { |
