forked from Granze/accounting-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounting-element.html
218 lines (197 loc) · 4.88 KB
/
accounting-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
218
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="accounting-import.html">
<!--
Element wrapper for the [accounting.js](http://openexchangerates.github.io/accounting.js/) library.
#### Example:
thousand="."
decimal=","></accounting-element> -> €500.000,00
<accounting-element format-numb
<accounting-element formatMoney="1234566"></accounting-element> -> $1,234,566.00
<accounting-element
format-money="500000"
symbol="€"er="11987612"></accounting-element> -> 11,987,612
<accounting-element to-fixed="22987612.1234" precision="2"></accounting-element> -> 22987612.12
<accounting-element unformat="998,761.2"></accounting-element> -> 998761.2
@element accounting-element
@homepage http://Granze.github.io/accounting-element
@author Maurizio Mangione
@demo
-->
<dom-module id="accounting-element">
<style>
:host{
display: inline-block;
font-family: 'Roboto Mono', monospace;
}
</style>
<template>
<div id="content"></div>
</template>
</dom-module>
<script>
Polymer({
is: 'accounting-element',
properties: {
/**
* @property formatMoney
* @type Number
*/
formatMoney: Number,
/**
* @property formatColumn
* @type Array
*/
formatColumn: Array,
/**
* @property formatNumber
* @type Number
*/
formatNumber: Number,
/**
* @property toFixed
* @type Number
*/
toFixed: Number,
/**
* @property unformat
* @type Number
* @default null
*/
unformat: {
type: String,
value: null,
observer: '_unformat'
},
/**
* @property symbol
* @type String
* @default $
*/
symbol: {
type: String,
value: '$'
},
/**
* @property precision
* @type Number
* @default null
*/
precision: {
type: Number,
value: null
},
/**
* @property thousand
* @type String
* @default ,
*/
thousand: {
type: String,
value: ','
},
/**
* @property decimal
* @type String
* @default .
*/
decimal: {
type: String,
value: '.'
},
/**
* @property format
* @type String
* @default %s%v
*/
formatOpt: {
type: String,
value: '%s%v'
},
formattedValue: {
type: String,
notify: true
},
/**
* in europe usually a comma "," is used for representing decimals
*/
useCommaForDecimal: {
type: Boolean
}
},
observers: [
'_formatMoney(formatMoney, symbol, precision, thousand, decimal, formatOpt)',
'_formatColumn(formatColumn, symbol, precision, thousand, decimal, formatOpt)',
'_formatNumber(formatNumber, precision, thousand, decimal)',
'_toFixed(toFixed, precision)'
],
/**
* @method _formatMoney
* @param newValue
* @private
*/
_formatMoney: function(newValue) {
if(newValue) {
if(this.useCommaForDecimal){
newValue=newValue.replace(",", ".");
}
if(this.precision === null) {this.precision = 2}
this._render(accounting.formatMoney(newValue, this.symbol, this.precision, this.thousand, this.decimal, this.formatOpt));
}
},
/**
* @method _formatColumn
* @param newValue
* @private
*/
_formatColumn: function(newValue) {
if(newValue) {
if(this.precision === null) {this.precision = 2}
var tableData = accounting.formatColumn(newValue, this.symbol, this.precision, this.thousand, this.decimal, this.formatOpt);
var column = tableData.map(function(row) {
return '<tr><td style="white-space: pre;">' + row + '</td></tr>';
});
this._render('<table class="table-data">' + column.join('') + '</table>');
}
},
/**
* @method _formatNumber
* @param newValue
* @private
*/
_formatNumber: function(newValue) {
if(newValue) {
if(this.precision === null) {this.precision = 0}
this._render(accounting.formatNumber(newValue, this.precision));
}
},
/**
* @method _toFixed
* @param newValue
* @private
*/
_toFixed: function(newValue) {
if(newValue) {
this._render(accounting.toFixed(newValue, this.precision));
}
},
/**
* @method _unformat
* @param newValue
* @private
*/
_unformat: function(newValue) {
if(newValue) {
this._render(accounting.unformat(newValue));
}
},
/**
* @method _render
* @param value
* @private
*/
_render: function(value) {
this.$.content.innerHTML = value;
this.formattedValue = value;
}
});
</script>