@@ -1482,15 +1482,86 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1482
1482
}
1483
1483
1484
1484
function rangeInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
1485
- numberInputType ( scope , element , attr , ctrl , $sniffer , $browser ) ;
1485
+ badInputChecker ( scope , element , attr , ctrl ) ;
1486
+ baseInputType ( scope , element , attr , ctrl , $sniffer , $browser ) ;
1487
+
1488
+ var minVal = 0 , maxVal = 100 ;
1489
+
1490
+ ctrl . $$parserName = 'number' ;
1491
+ ctrl . $parsers . push ( function ( value ) {
1492
+ if ( ctrl . $isEmpty ( value ) ) return null ;
1493
+ if ( NUMBER_REGEXP . test ( value ) ) return parseFloat ( value ) ;
1494
+ return undefined ;
1495
+ } ) ;
1496
+
1497
+ ctrl . $formatters . push ( function ( value ) {
1498
+ if ( ! ctrl . $isEmpty ( value ) ) {
1499
+ if ( ! isNumber ( value ) ) {
1500
+ throw ngModelMinErr ( 'numfmt' , 'Expected `{0}` to be a number' , value ) ;
1501
+ }
1502
+ value = value . toString ( ) ;
1503
+ }
1504
+ return value ;
1505
+ } ) ;
1506
+
1507
+ function minChange ( val ) {
1508
+ if ( isDefined ( val ) && ! isNumber ( val ) ) {
1509
+ val = parseFloat ( val ) ;
1510
+ }
1511
+ minVal = isNumber ( val ) && ! isNaN ( val ) ? val : undefined ;
1512
+ // TODO(matsko): implement validateLater to reduce number of validations
1513
+ ctrl . $validate ( ) ;
1514
+ }
1515
+
1516
+ if ( isDefined ( attr . min ) || attr . ngMin ) {
1517
+ ctrl . $validators . min = function ( value ) {
1518
+ return ctrl . $isEmpty ( value ) || isUndefined ( minVal ) || value >= minVal ;
1519
+ } ;
1520
+
1521
+ minChange ( attr . min ) ;
1522
+ attr . $observe ( 'min' , minChange ) ;
1523
+ }
1524
+
1525
+ function maxChange ( val ) {
1526
+ if ( isDefined ( val ) && ! isNumber ( val ) ) {
1527
+ val = parseFloat ( val ) ;
1528
+ }
1529
+ maxVal = isNumber ( val ) && ! isNaN ( val ) ? val : undefined ;
1530
+ // TODO(matsko): implement validateLater to reduce number of validations
1531
+ ctrl . $validate ( ) ;
1532
+ }
1533
+
1534
+ if ( isDefined ( attr . max ) || attr . ngMax ) {
1535
+ ctrl . $validators . max = ctrl . $$hasNativeValidators && isObject ( element [ 0 ] . validity ) ?
1536
+ function ( value ) {
1537
+ return ! element [ 0 ] . validity . rangeOverflow ;
1538
+ } :
1539
+ function ( modelValue , viewValue ) {
1540
+ return ctrl . $isEmpty ( viewValue ) || isUndefined ( maxVal ) || viewValue <= maxVal ;
1541
+ } ;
1542
+
1543
+ console . log ( 'max initial' , attr . max ) ;
1544
+ maxChange ( attr . max ) ;
1545
+ attr . $observe ( 'max' , maxChange ) ;
1546
+ }
1486
1547
1487
1548
var originalRender = ctrl . $render ;
1488
1549
1489
1550
ctrl . $render = function ( ) {
1490
1551
if ( ctrl . $isEmpty ( ctrl . $viewValue ) ) {
1491
- ctrl . $viewValue = '50' ;
1552
+ // Browsers that implement range do this automatically, but IE9 does not
1553
+ var viewValue = minVal + ( maxVal - minVal ) / 2 ;
1554
+ ctrl . $viewValue = viewValue ;
1555
+ originalRender ( ) ;
1556
+ ctrl . $setViewValue ( viewValue ) ;
1557
+ } else if ( ctrl . $viewValue > maxVal ) {
1558
+ ctrl . $viewValue = maxVal ;
1559
+ originalRender ( ) ;
1560
+ ctrl . $setViewValue ( ctrl . $viewValue ) ;
1561
+ } else if ( ctrl . $viewValue < minVal ) {
1562
+ ctrl . $viewValue = minVal ;
1492
1563
originalRender ( ) ;
1493
- ctrl . $setViewValue ( '50' ) ;
1564
+ ctrl . $setViewValue ( ctrl . $viewValue ) ;
1494
1565
} else {
1495
1566
originalRender ( ) ;
1496
1567
}
0 commit comments