From 13710dfca9a9e1bed80724e12dd0fcf8460343fd Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 9 Aug 2011 17:30:21 +0200 Subject: [PATCH] [IMP] rename 'dates.js' to 'formats.js', move data-formatting for cells from list to formats so treeview can rely on them bzr revid: xmo@openerp.com-20110809153021-k1wop0j8qjp6rp49 --- addons/base/__openerp__.py | 2 +- addons/base/static/src/js/boot.js | 2 +- .../static/src/js/{dates.js => formats.js} | 72 ++++++++++++++++++- addons/base/static/src/js/list.js | 66 ++--------------- addons/base/static/test/test.html | 2 +- addons/web_mobile/static/src/web_mobile.html | 6 +- 6 files changed, 81 insertions(+), 69 deletions(-) rename addons/base/static/src/js/{dates.js => formats.js} (56%) diff --git a/addons/base/__openerp__.py b/addons/base/__openerp__.py index 9cf8a6c893e..cfe8850ab03 100644 --- a/addons/base/__openerp__.py +++ b/addons/base/__openerp__.py @@ -22,7 +22,7 @@ "static/lib/underscore/underscore.string.js", "static/src/js/boot.js", "static/src/js/core.js", - "static/src/js/dates.js", + "static/src/js/formats.js", "static/src/js/chrome.js", "static/src/js/views.js", "static/src/js/data.js", diff --git a/addons/base/static/src/js/boot.js b/addons/base/static/src/js/boot.js index 5754f5ccd14..7ac8a6bb1e0 100644 --- a/addons/base/static/src/js/boot.js +++ b/addons/base/static/src/js/boot.js @@ -60,7 +60,7 @@ openerp.base = function(instance) { openerp.base.core(instance); - openerp.base.dates(instance); + openerp.base.formats(instance); openerp.base.chrome(instance); openerp.base.data(instance); if (openerp.base.views) { diff --git a/addons/base/static/src/js/dates.js b/addons/base/static/src/js/formats.js similarity index 56% rename from addons/base/static/src/js/dates.js rename to addons/base/static/src/js/formats.js index 8daea39773f..3157997e82d 100644 --- a/addons/base/static/src/js/dates.js +++ b/addons/base/static/src/js/formats.js @@ -1,5 +1,5 @@ -openerp.base.dates = function(openerp) { +openerp.base.formats = function(openerp) { /** * Converts a string to a Date javascript object using OpenERP's @@ -133,5 +133,75 @@ openerp.base.format_time = function(obj) { return fts(obj.getHours(),2) + ":" + fts(obj.getMinutes(),2) + ":" + fts(obj.getSeconds(),2); }; + +/** + * Formats a single atomic value based on a field descriptor + * + * @param {Object} value read from OpenERP + * @param {Object} descriptor union of orm field and view field + * @param {Object} [descriptor.widget] widget to use to display the value + * @param {Object} descriptor.type fallback if no widget is provided, or if the provided widget is unknown + * @param {Object} [descriptor.digits] used for the formatting of floats + * @param {String} [value_if_empty=''] returned if the ``value`` argument is considered empty + */ +openerp.base.format_value = function (value, descriptor, value_if_empty) { + // If NaN value, display as with a `false` (empty cell) + if (typeof value === 'number' && isNaN(value)) { + value = false; + } + switch (value) { + case false: + case Infinity: + case -Infinity: + return value_if_empty === undefined ? '' : value_if_empty; + } + switch (descriptor.widget || descriptor.type) { + case 'integer': + return _.sprintf('%d', value); + case 'float': + var precision = descriptor.digits ? descriptor.digits[1] : 2; + return _.sprintf('%.' + precision + 'f', value); + case 'float_time': + return _.sprintf("%02d:%02d", + Math.floor(value), + Math.round((value % 1) * 60)); + case 'progressbar': + return _.sprintf( + '%.2f%%', + value, value); + case 'many2one': + // name_get value format + return value[1]; + default: + return value; + } +}; + +/** + * Formats a provided cell based on its field type + * + * @param {Object} row_data record whose values should be displayed in the cell + * @param {Object} column column descriptor + * @param {"button"|"field"} column.tag base control type + * @param {String} column.type widget type for a field control + * @param {String} [column.string] button label + * @param {String} [column.icon] button icon + * @param {String} [value_if_empty=''] what to display if the field's value is ``false`` + */ +openerp.base.format_cell = function (row_data, column, value_if_empty) { + var attrs = column.modifiers_for(row_data); + if (attrs.invisible) { return ''; } + if (column.tag === 'button') { + return [ + '' + ].join('') + } + + return openerp.base.format_value( + row_data[column.id].value, column, value_if_empty); +} }; diff --git a/addons/base/static/src/js/list.js b/addons/base/static/src/js/list.js index ea9cdaea1ee..11440a003b5 100644 --- a/addons/base/static/src/js/list.js +++ b/addons/base/static/src/js/list.js @@ -1,63 +1,5 @@ openerp.base.list = function (openerp) { openerp.base.views.add('list', 'openerp.base.ListView'); -openerp.base.list = { - /** - * Formats the rendring of a given value based on its field type - * - * @param {Object} row_data record whose values should be displayed in the cell - * @param {Object} column column descriptor - * @param {"button"|"field"} column.tag base control type - * @param {String} column.type widget type for a field control - * @param {String} [column.string] button label - * @param {String} [column.icon] button icon - * @param {String} [value_if_empty=''] what to display if the field's value is ``false`` - */ - render_cell: function (row_data, column, value_if_empty) { - var attrs = column.modifiers_for(row_data); - if (attrs.invisible) { return ''; } - if (column.tag === 'button') { - return [ - '' - ].join('') - } - - var value = row_data[column.id].value; - - // If NaN value, display as with a `false` (empty cell) - if (typeof value === 'number' && isNaN(value)) { - value = false; - } - switch (value) { - case false: - case Infinity: - case -Infinity: - return value_if_empty === undefined ? '' : value_if_empty; - } - switch (column.widget || column.type) { - case 'integer': - return _.sprintf('%d', value); - case 'float': - var precision = column.digits ? column.digits[1] : 2; - return _.sprintf('%.' + precision + 'f', value); - case 'float_time': - return _.sprintf("%02d:%02d", - Math.floor(value), - Math.round((value % 1) * 60)); - case 'progressbar': - return _.sprintf( - '%.2f%%', - value, value); - case 'many2one': - // name_get value format - return value[1]; - default: - return value; - } - } -}; openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListView# */ { defaults: { // records can be selected one by one @@ -637,7 +579,7 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi } $footer_cells.filter(_.sprintf('[data-field=%s]', column.id)) - .html(openerp.base.list.render_cell(aggregation, column)); + .html(openerp.base.format_cell(aggregation, column)); }); } // TODO: implement reorder (drag and drop rows) @@ -725,7 +667,7 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base. this.$current = this.$_element.clone(true); this.$current.empty().append( QWeb.render('ListView.rows', _.extend({ - render_cell: openerp.base.list.render_cell}, this))); + render_cell: openerp.base.format_cell}, this))); }, /** * Gets the ids of all currently selected records, if any @@ -856,7 +798,7 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base. row: this.rows[record_index], row_parity: (record_index % 2 === 0) ? 'even' : 'odd', row_index: record_index, - render_cell: openerp.base.list.render_cell + render_cell: openerp.base.format_cell }); }, /** @@ -1016,7 +958,7 @@ openerp.base.ListView.Groups = openerp.base.Class.extend( /** @lends openerp.bas row_data[group.grouped_on] = group; var group_column = _(self.columns).detect(function (column) { return column.id === group.grouped_on; }); - $group_column.html(openerp.base.list.render_cell( + $group_column.html(openerp.base.format_cell( row_data, group_column, "Undefined" )); if (group.openable) { diff --git a/addons/base/static/test/test.html b/addons/base/static/test/test.html index 83920b6ddb1..97a73865041 100644 --- a/addons/base/static/test/test.html +++ b/addons/base/static/test/test.html @@ -18,7 +18,7 @@ - + diff --git a/addons/web_mobile/static/src/web_mobile.html b/addons/web_mobile/static/src/web_mobile.html index f30ead90e63..bd74e5f4f66 100755 --- a/addons/web_mobile/static/src/web_mobile.html +++ b/addons/web_mobile/static/src/web_mobile.html @@ -19,10 +19,10 @@ - + - - + +