1
|
|
|
import _ from 'underscore'; |
|
|
|
|
2
|
|
|
import { |
3
|
|
|
CellFormatter |
4
|
|
|
} from './cell.js'; |
5
|
|
|
import { |
6
|
|
|
NumberFormatter |
7
|
|
|
} from './number.js'; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
A number formatter that converts a floating point number, optionally |
11
|
|
|
multiplied by a multiplier, to a percentage string and vice versa. |
12
|
|
|
|
13
|
|
|
@class Backgrid.PercentFormatter |
14
|
|
|
@extends Backgrid.NumberFormatter |
15
|
|
|
@constructor |
16
|
|
|
@throws {RangeError} If decimals < 0 or > 20. |
17
|
|
|
*/ |
18
|
|
|
var PercentFormatter = function () { |
19
|
|
|
Backgrid.NumberFormatter.apply(this, arguments); |
|
|
|
|
20
|
|
|
}; |
21
|
|
|
|
22
|
|
|
PercentFormatter.prototype = new NumberFormatter(), |
|
|
|
|
23
|
|
|
|
24
|
|
|
_.extend(PercentFormatter.prototype, { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
@member Backgrid.PercentFormatter |
28
|
|
|
@cfg {Object} options |
29
|
|
|
|
30
|
|
|
@cfg {number} [options.multiplier=1] The number used to multiply the model |
31
|
|
|
value for display. |
32
|
|
|
|
33
|
|
|
@cfg {string} [options.symbol='%'] The symbol to append to the percentage |
34
|
|
|
string. |
35
|
|
|
*/ |
36
|
|
|
defaults: _.extend({}, NumberFormatter.prototype.defaults, { |
37
|
|
|
multiplier: 1, |
38
|
|
|
symbol: "%" |
39
|
|
|
}), |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
Takes a floating point number, where the number is first multiplied by |
43
|
|
|
`multiplier`, then converted to a formatted string like |
44
|
|
|
NumberFormatter#fromRaw, then finally append `symbol` to the end. |
45
|
|
|
|
46
|
|
|
@member Backgrid.PercentFormatter |
47
|
|
|
@param {number} rawValue |
|
|
|
|
48
|
|
|
@param {Backbone.Model} model Used for more complicated formatting |
49
|
|
|
@return {string} |
50
|
|
|
*/ |
51
|
|
|
fromRaw: function (number, model) { |
|
|
|
|
52
|
|
|
var args = [].slice.call(arguments, 1); |
53
|
|
|
args.unshift(number * this.multiplier); |
54
|
|
|
return (NumberFormatter.prototype.fromRaw.apply(this, args) || "0") + this.symbol; |
55
|
|
|
}, |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
Takes a string, possibly appended with `symbol` and/or `decimalSeparator`, |
59
|
|
|
and convert it back to a number for the model like NumberFormatter#toRaw, |
60
|
|
|
and then dividing it by `multiplier`. |
61
|
|
|
|
62
|
|
|
@member Backgrid.PercentFormatter |
63
|
|
|
@param {string} formattedData |
|
|
|
|
64
|
|
|
@param {Backbone.Model} model Used for more complicated formatting |
65
|
|
|
@return {number|undefined} Undefined if the string cannot be converted to |
66
|
|
|
a number. |
67
|
|
|
*/ |
68
|
|
|
toRaw: function (formattedValue, model) { |
|
|
|
|
69
|
|
|
var tokens = formattedValue.split(this.symbol); |
70
|
|
|
if (tokens && tokens[0] && tokens[1] === "" || tokens[1] == null) { |
|
|
|
|
71
|
|
|
var rawValue = NumberFormatter.prototype.toRaw.call(this, tokens[0]); |
72
|
|
|
if (_.isUndefined(rawValue)) return rawValue; |
|
|
|
|
73
|
|
|
return rawValue / this.multiplier; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
}); |
78
|
|
|
export { |
79
|
|
|
PercentFormatter |
80
|
|
|
}; |
81
|
|
|
|