Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Coopdevs OCB mirror
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Coopdevs
Odoo
Coopdevs OCB mirror
Commits
8e017ee2
Commit
8e017ee2
authored
7 years ago
by
Mathieu Duckerts-Antoine
Committed by
Géry Debongnie
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[IMP] web: add a percentage formatter
This rev. adds a new formatter for numerical values to fieldsUtils.
parent
dcca2e8b
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
addons/web/static/src/js/fields/field_utils.js
+17
-4
17 additions, 4 deletions
addons/web/static/src/js/fields/field_utils.js
addons/web/static/tests/fields/field_utils_tests.js
+15
-1
15 additions, 1 deletion
addons/web/static/tests/fields/field_utils_tests.js
with
32 additions
and
5 deletions
addons/web/static/src/js/fields/field_utils.js
+
17
−
4
View file @
8e017ee2
...
...
@@ -29,12 +29,12 @@ var _t = core._t;
/**
* Convert binary to bin_size
*
*
* @param {string} [value] base64 representation of the binary (might be already a bin_size!)
* @param {Object} [field]
* a description of the field (note: this parameter is ignored)
* a description of the field (note: this parameter is ignored)
* @param {Object} [options] additional options (note: this parameter is ignored)
*
*
* @returns {string} bin_size (which is human-readable)
*/
function
formatBinary
(
value
,
field
,
options
)
{
...
...
@@ -320,7 +320,19 @@ function formatMonetary(value, field, options) {
return
currency
.
symbol
+
'
'
+
formatted_value
;
}
}
/**
* Returns a string representing the given value (multiplied by 100)
* concatenated with '%'.
*
* @param {number | false} value
* @param {Object} [field]
* @param {Object} [options]
* @returns {string}
*/
function
formatPercentage
(
value
,
field
,
options
)
{
value
=
formatFloat
(
value
*
100
,
field
,
options
)
||
'
0
'
;
return
parseFloat
(
value
)
+
"
%
"
;
}
/**
* Returns a string representing the value of the selection.
*
...
...
@@ -591,6 +603,7 @@ return {
many2one
:
formatMany2one
,
monetary
:
formatMonetary
,
one2many
:
formatX2Many
,
percentage
:
formatPercentage
,
reference
:
formatMany2one
,
selection
:
formatSelection
,
text
:
formatChar
,
...
...
This diff is collapsed.
Click to expand it.
addons/web/static/tests/fields/field_utils_tests.js
+
15
−
1
View file @
8e017ee2
...
...
@@ -4,7 +4,6 @@ odoo.define('web.field_utils_tests', function (require) {
var
core
=
require
(
'
web.core
'
);
var
session
=
require
(
'
web.session
'
);
var
fieldUtils
=
require
(
'
web.field_utils
'
);
var
testUtils
=
require
(
'
web.test_utils
'
);
QUnit
.
module
(
'
fields
'
,
{},
function
()
{
...
...
@@ -133,7 +132,22 @@ QUnit.test('format binary', function (assert) {
// base64 estimated size (bytes) = value.length / 1.37 (http://en.wikipedia.org/wiki/Base64#MIME)
// Here: 4 / 1.37 = 2.91970800 => 2.92 (rounded 2 decimals by utils.human_size)
assert
.
strictEqual
(
fieldUtils
.
format
.
binary
(
'
Cg==
'
),
'
2.92 Bytes
'
);
});
QUnit
.
test
(
'
format percentage
'
,
function
(
assert
)
{
assert
.
expect
(
8
);
assert
.
strictEqual
(
fieldUtils
.
format
.
percentage
(
0
),
'
0%
'
);
assert
.
strictEqual
(
fieldUtils
.
format
.
percentage
(
0.5
),
'
50%
'
);
assert
.
strictEqual
(
fieldUtils
.
format
.
percentage
(
1
),
'
100%
'
);
assert
.
strictEqual
(
fieldUtils
.
format
.
percentage
(
-
0.2
),
'
-20%
'
);
assert
.
strictEqual
(
fieldUtils
.
format
.
percentage
(
2.5
),
'
250%
'
);
assert
.
strictEqual
(
fieldUtils
.
format
.
percentage
(
0.125
),
'
12.5%
'
);
assert
.
strictEqual
(
fieldUtils
.
format
.
percentage
(
0.666666
),
'
66.67%
'
);
assert
.
strictEqual
(
fieldUtils
.
format
.
percentage
(
false
),
'
0%
'
);
});
QUnit
.
test
(
'
parse float
'
,
function
(
assert
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment