1
1
'use strict' ;
2
2
3
3
angular . module ( 'platformWebApp' )
4
- // TODO: Replace with tested localized version (see below)
5
- . directive ( 'smartFloat' , [ '$filter' , '$compile' , 'platformWebApp.userProfile' , function ( $filter , $compile , userProfile ) {
6
- var INTEGER_REGEXP = / ^ \- ? \d + $ / ; //Integer number
7
- var INTEGER_MAX_VALUE = 2147483647 ;
8
- var INTEGER_MIN_VALUE = - 2147483648 ;
9
- var FLOAT_REGEXP_1 = / ^ [ - + ] ? \$ ? \d { 1 , 3 } ( \. \d { 3 } ) + ( , \d * ) $ / ; //Numbers like: 1.234,5678
10
- var FLOAT_REGEXP_2 = / ^ [ - + ] ? \$ ? \d { 1 , 3 } ( , \d { 3 } ) + ( \. \d * ) $ / ; //Numbers like: 1,234.5678
11
- var FLOAT_REGEXP_3 = / ^ [ - + ] ? \$ ? \d + ( \. \d * ) ? $ / ; //Numbers like: 1234.5678
12
- var FLOAT_REGEXP_4 = / ^ [ - + ] ? \$ ? \d + ( , \d * ) ? $ / ; //Numbers like: 1234,5678
4
+ // TODO: Replace with tested localized version (see below)
5
+ . directive ( 'smartFloat' , [ '$filter' , '$compile' , 'platformWebApp.userProfile' , function ( $filter , $compile , userProfile ) {
6
+ var INTEGER_REGEXP = / ^ \- ? \d + $ / ; //Integer number
7
+ var INTEGER_MAX_VALUE = 2147483647 ;
8
+ var INTEGER_MIN_VALUE = - 2147483648 ;
9
+ var FLOAT_REGEXP_1 = / ^ [ - + ] ? \$ ? \d { 1 , 3 } ( \. \d { 3 } ) + ( , \d * ) $ / ; //Numbers like: 1.234,5678
10
+ var FLOAT_REGEXP_2 = / ^ [ - + ] ? \$ ? \d { 1 , 3 } ( , \d { 3 } ) + ( \. \d * ) $ / ; //Numbers like: 1,234.5678
11
+ var FLOAT_REGEXP_3 = / ^ [ - + ] ? \$ ? \d + ( \. \d * ) ? $ / ; //Numbers like: 1234.5678
12
+ var FLOAT_REGEXP_4 = / ^ [ - + ] ? \$ ? \d + ( , \d * ) ? $ / ; //Numbers like: 1234,5678
13
13
14
- return {
15
- require : 'ngModel' ,
16
- link : function ( scope , elm , attrs , ctrl ) {
14
+ return {
15
+ require : 'ngModel' ,
16
+ link : function ( scope , elm , attrs , ctrl ) {
17
17
// possible values for fraction are: 0, positive number, negative number, none
18
18
// when fraction is a negative number result has maximum length of the fractional part of the value
19
- var fraction = ( attrs . fraction || Number ( attrs . fraction ) === 0 ) ? attrs . fraction : 2 ;
20
- if ( attrs . numType === "float" ) {
21
- ctrl . $parsers . unshift ( function ( viewValue ) {
22
- if ( FLOAT_REGEXP_1 . test ( viewValue ) ) {
23
- ctrl . $setValidity ( 'float' , true ) ;
24
- return parseFloat ( viewValue . replace ( '.' , '' ) . replace ( ',' , '.' ) ) ;
25
- } else if ( FLOAT_REGEXP_2 . test ( viewValue ) ) {
26
- ctrl . $setValidity ( 'float' , true ) ;
27
- return parseFloat ( viewValue . replace ( ',' , '' ) ) ;
28
- } else if ( FLOAT_REGEXP_3 . test ( viewValue ) ) {
29
- ctrl . $setValidity ( 'float' , true ) ;
30
- return parseFloat ( viewValue ) ;
31
- } else if ( FLOAT_REGEXP_4 . test ( viewValue ) ) {
32
- ctrl . $setValidity ( 'float' , true ) ;
33
- return parseFloat ( viewValue . replace ( ',' , '.' ) ) ;
34
- } else {
35
- //Allow to use empty values
36
- ctrl . $setValidity ( 'float' , ! viewValue ) ;
37
- return viewValue ;
38
- }
39
- } ) ;
19
+ var fraction = ( attrs . fraction || Number ( attrs . fraction ) === 0 ) ? attrs . fraction : 2 ;
20
+ if ( attrs . numType === "float" ) {
21
+ ctrl . $parsers . unshift ( function ( viewValue ) {
22
+ if ( FLOAT_REGEXP_1 . test ( viewValue ) ) {
23
+ ctrl . $setValidity ( 'float' , true ) ;
24
+ return parseFloat ( viewValue . replace ( '.' , '' ) . replace ( ',' , '.' ) ) ;
25
+ } else if ( FLOAT_REGEXP_2 . test ( viewValue ) ) {
26
+ ctrl . $setValidity ( 'float' , true ) ;
27
+ return parseFloat ( viewValue . replace ( ',' , '' ) ) ;
28
+ } else if ( FLOAT_REGEXP_3 . test ( viewValue ) ) {
29
+ ctrl . $setValidity ( 'float' , true ) ;
30
+ return parseFloat ( viewValue ) ;
31
+ } else if ( FLOAT_REGEXP_4 . test ( viewValue ) ) {
32
+ ctrl . $setValidity ( 'float' , true ) ;
33
+ return parseFloat ( viewValue . replace ( ',' , '.' ) ) ;
34
+ } else {
35
+ ctrl . $setValidity ( 'float' , false ) ;
36
+ return viewValue ;
37
+ }
38
+ } ) ;
40
39
41
- ctrl . $formatters . unshift (
42
- function ( modelValue ) {
40
+ ctrl . $formatters . unshift (
41
+ function ( modelValue ) {
43
42
if ( modelValue == null ) {
44
43
return modelValue ;
45
44
}
@@ -51,29 +50,29 @@ angular.module('platformWebApp')
51
50
return new Intl . NumberFormat ( userProfile . language || 'default' , { maximumFractionDigits : - fraction } ) . format ( resultValue ) ;
52
51
}
53
52
// default behavior
54
- return $filter ( 'number' ) ( resultValue , fraction ) ;
55
- }
56
- ) ;
57
- }
58
- else if ( attrs . numType === "positiveInteger" ) {
59
- ctrl . $parsers . unshift ( function ( viewValue ) {
60
- ctrl . $setValidity ( 'positiveInteger' , INTEGER_REGEXP . test ( viewValue ) && viewValue > 0 ) ;
61
- return viewValue ;
62
- } ) ;
63
- }
64
- else {
65
- ctrl . $parsers . unshift ( function ( viewValue ) {
66
- if ( INTEGER_REGEXP . test ( viewValue ) && viewValue >= INTEGER_MIN_VALUE && viewValue <= INTEGER_MAX_VALUE ) {
67
- ctrl . $setValidity ( 'integer' , true ) ;
68
- return viewValue ;
69
- }
70
- else {
71
- //Allow to use empty values
72
- ctrl . $setValidity ( 'integer' , ! viewValue ) ;
73
- return viewValue ;
74
- }
75
- } ) ;
76
- }
77
- }
78
- } ;
79
- } ] ) ;
53
+ return $filter ( 'number' ) ( resultValue , fraction ) ;
54
+ }
55
+ ) ;
56
+ }
57
+ else if ( attrs . numType === "positiveInteger" ) {
58
+ ctrl . $parsers . unshift ( function ( viewValue ) {
59
+ ctrl . $setValidity ( 'positiveInteger' , INTEGER_REGEXP . test ( viewValue ) && viewValue > 0 ) ;
60
+ return viewValue ;
61
+ } ) ;
62
+ }
63
+ else {
64
+ ctrl . $parsers . unshift ( function ( viewValue ) {
65
+ if ( INTEGER_REGEXP . test ( viewValue ) && viewValue >= INTEGER_MIN_VALUE && viewValue <= INTEGER_MAX_VALUE ) {
66
+ ctrl . $setValidity ( 'integer' , true ) ;
67
+ return viewValue ;
68
+ }
69
+ else {
70
+ //Allow to use empty values
71
+ ctrl . $setValidity ( 'integer' , ! viewValue ) ;
72
+ return viewValue ;
73
+ }
74
+ } ) ;
75
+ }
76
+ }
77
+ } ;
78
+ } ] ) ;
0 commit comments