Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c0f37d6

Browse files
committed
wip: account for automatic setting of values in browsers
1 parent 39b91e3 commit c0f37d6

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

src/ng/directive/input.js

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,15 +1482,86 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
14821482
}
14831483

14841484
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+
}
14861547

14871548
var originalRender = ctrl.$render;
14881549

14891550
ctrl.$render = function() {
14901551
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;
14921563
originalRender();
1493-
ctrl.$setViewValue('50');
1564+
ctrl.$setViewValue(ctrl.$viewValue);
14941565
} else {
14951566
originalRender();
14961567
}

test/ng/directive/inputSpec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,6 +3039,15 @@ describe('input', function() {
30393039
expect(scope.value).toBe(0);
30403040
expect(scope.form.alias.$error.max).toBeFalsy();
30413041
});
3042+
3043+
it('should set the model to the max val if it is more than the max val', function() {
3044+
scope.value = 90;
3045+
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" max="10" />');
3046+
3047+
expect(inputElm).toBeValid();
3048+
expect(inputElm.val()).toBe('10');
3049+
expect(scope.value).toBe(10);
3050+
});
30423051
}
30433052

30443053
it('should validate if "range" is not implemented', function() {
@@ -3125,6 +3134,15 @@ describe('input', function() {
31253134
});
31263135
});
31273136

3137+
it('should set the correct initial view and model when min and max are specified', function() {
3138+
scope.max = 80;
3139+
scope.min = 40;
3140+
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" max="{{max}}" min="{{min}}" />');
3141+
3142+
expect(inputElm.val()).toBe('60');
3143+
expect(scope.value).toBe(60);
3144+
});
3145+
31283146
});
31293147

31303148
describe('email', function() {

0 commit comments

Comments
 (0)