-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnumbro-element.html
217 lines (183 loc) · 4.87 KB
/
numbro-element.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="numbro-import.html">
<!--
A CSS friendly element wrapper for the [numbro.js](http://numbrojs.com/) library.
### Example
<numbro-element
value="1000"
format="0,0">
</numbro-element>
### Styling
The following mixins are available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--numbro-element` | Mixin applied to the numbro element | `{}`
`--numbro-element-prefix` | Mixin applied to the numbro element prefix | `{}`
`--numbro-element-value` | Mixin applied to the numbro element value | `{}`
`--numbro-element-suffix` | Mixin applied to the numbro element suffix | `{}`
@demo demo/index.html
@hero hero.svg
-->
<dom-module id="numbro-element">
<template>
<style>
:host {
display: inline;
box-sizing: border-box;
@apply --numbro-element;
}
#prefix {
@apply --numbro-element-prefix;
}
#value {
@apply --numbro-element-value;
}
#suffix {
@apply --numbro-element-suffix;
}
</style>
<!--TODO: with update to 2.0, investigate using data-binding-->
<span id="prefix"></span>
<span id="value"></span>
<span id="suffix"></span>
</template>
<script>
Polymer({
is: 'numbro-element',
properties: {
/**
* The number value to be formatted
*/
value: Number,
/**
* The format used to generate the output
*/
format: {
type: String
},
/**
* Specify whether the value should be formatted as a currency
*/
currency: {
type: Boolean,
value: false
},
/**
* Reflect if the value is negative
*/
negative: {
type: Boolean,
readOnly: true,
reflectToAttribute: true
},
/**
* A formatted String to extract a value from
*/
unformat: {
type: String,
observer: '_unformat'
},
/**
* Provide localization for currency formatting
*/
culture: {
type: String,
value: 'en-US',
observer: '_culture'
},
/**
* Provide a new default format
*/
defaultFormat: {
type: String,
observer: '_defaultFormat'
},
/**
* Provide a new default format for currency
*/
defaultCurrencyFormat: {
type: String,
observer: '_defaultCurrencyFormat'
},
/**
* Provide a new default format for `0` value
*/
zeroFormat: {
type: String,
observer: '_zeroFormat'
},
},
observers: [
'_format(value, format, currency)'
],
// Element Lifecycle
ready: function() {
// force a default value for format for the observer to trigger properly
this.format = this.format || (this.currency ? '0$' : '0,0');
},
// Element Behavior
/**
* @param {number} input value.
*/
_format: function(value) {
var formatted;
this._setNegative(value < 0);
if ( this.currency ) {
if (this.format) {
formatted = numbro(value).formatCurrency(this.format);
} else {
formatted = numbro(value).formatCurrency();
}
} else {
formatted = numbro(value).format(this.format);
}
this._render(formatted);
},
/**
* @param {string} formatted value.
*/
_unformat: function(value) {
var unformatted = numbro().unformat(value);
this._render(unformatted);
},
/**
* @param {string} new culture.
*/
_culture: function(culture) {
numbro.culture(culture);
this._format(this.value);
},
/**
* @param {string} new default format.
*/
_defaultFormat: function(format) {
numbro.defaultFormat(format);
this._format(this.value);
},
/**
* @param {string} new default currency format.
*/
_defaultCurrencyFormat: function(format) {
numbro.defaultCurrencyFormat(format);
this._format(this.value);
},
/**
* @param {string} new default zero format.
*/
_zeroFormat: function(format) {
numbro.zeroFormat(format);
this._format(this.value);
},
/**
* @param {string} input value.
*/
_render: function(value) {
var regex = /([^\.,\d]*)([\.,\d]*)(\D*)/i;
var result = regex.exec(value).slice(1);
this.$.prefix.innerText = result[0];
this.$.value.innerText = result[1];
this.$.suffix.innerText = result[2];
},
});
</script>
</dom-module>