@klodd/ds 5.1.0 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/js/hero-roll.js +33 -6
- package/package.json +1 -1
package/js/hero-roll.js
CHANGED
|
@@ -107,7 +107,10 @@
|
|
|
107
107
|
function buildAndAnimate ( el ) {
|
|
108
108
|
if ( ! el ) return;
|
|
109
109
|
if ( el.dataset.rolled === 'true' ) return;
|
|
110
|
-
|
|
110
|
+
// parseFloat sa decimaler stods. 'data-animate-roll="8.5"' rullar
|
|
111
|
+
// "8" som digit-roller och "5" som digit-roller, med "." mellan
|
|
112
|
+
// som static-char.
|
|
113
|
+
const target = parseFloat( el.dataset.animateRoll );
|
|
111
114
|
if ( isNaN( target ) ) return;
|
|
112
115
|
|
|
113
116
|
// No-op for 0
|
|
@@ -121,8 +124,14 @@
|
|
|
121
124
|
const originalText = el.textContent;
|
|
122
125
|
const isNegative = target < 0;
|
|
123
126
|
const absTarget = Math.abs( target );
|
|
124
|
-
|
|
125
|
-
|
|
127
|
+
// Split bevarar decimal-punkt som '.' i array. Heltalsdel separat
|
|
128
|
+
// fran decimaldel for thousand-separator-injektion (1 234 567).
|
|
129
|
+
const targetStr = String( absTarget );
|
|
130
|
+
const dotIndex = targetStr.indexOf( '.' );
|
|
131
|
+
const intPart = dotIndex >= 0 ? targetStr.slice( 0, dotIndex ) : targetStr;
|
|
132
|
+
const decPart = dotIndex >= 0 ? targetStr.slice( dotIndex + 1 ) : '';
|
|
133
|
+
const intDigits = intPart.split( '' ).map( Number );
|
|
134
|
+
const intLen = intDigits.length;
|
|
126
135
|
|
|
127
136
|
const fragment = document.createDocumentFragment();
|
|
128
137
|
|
|
@@ -131,17 +140,35 @@
|
|
|
131
140
|
}
|
|
132
141
|
|
|
133
142
|
const rollers = [];
|
|
134
|
-
|
|
143
|
+
// Heltalsdel: digit-rollers med thousand-separator var 3:e siffra
|
|
144
|
+
// fran hoger.
|
|
145
|
+
intDigits.forEach( function ( d, i ) {
|
|
135
146
|
const r = buildRoller( d );
|
|
136
147
|
fragment.appendChild( r.roller );
|
|
137
148
|
rollers.push( r );
|
|
138
|
-
const fromRight =
|
|
149
|
+
const fromRight = intLen - 1 - i;
|
|
139
150
|
if ( fromRight > 0 && fromRight % 3 === 0 ) {
|
|
140
151
|
fragment.appendChild( buildStaticChar( ' ' ) );
|
|
141
152
|
}
|
|
142
153
|
} );
|
|
143
154
|
|
|
144
|
-
|
|
155
|
+
// Decimal-del: punkt + digit-rollers per decimalsiffra.
|
|
156
|
+
if ( decPart.length > 0 ) {
|
|
157
|
+
fragment.appendChild( buildStaticChar( '.' ) );
|
|
158
|
+
decPart.split( '' ).map( Number ).forEach( function ( d ) {
|
|
159
|
+
const r = buildRoller( d );
|
|
160
|
+
fragment.appendChild( r.roller );
|
|
161
|
+
rollers.push( r );
|
|
162
|
+
} );
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Suffix konfigurerbart via data-roll-suffix. Default ' kr'
|
|
166
|
+
// for Ekonom-bakatkompat - appar utan kr-suffix sattler
|
|
167
|
+
// data-roll-suffix="" pa elementet.
|
|
168
|
+
const suffix = el.dataset.rollSuffix !== undefined ? el.dataset.rollSuffix : ' kr';
|
|
169
|
+
if ( suffix ) {
|
|
170
|
+
fragment.appendChild( buildStaticChar( suffix ) );
|
|
171
|
+
}
|
|
145
172
|
|
|
146
173
|
// Mat font-line-height for digit-roller-hojd.
|
|
147
174
|
const computed = window.getComputedStyle( el );
|