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

Commit 8f3daa2

Browse files
committed
feat(input[range]): support step
1 parent 9130166 commit 8f3daa2

File tree

3 files changed

+330
-33
lines changed

3 files changed

+330
-33
lines changed

src/jqLite.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,8 @@ var ALIASED_ATTR = {
577577
'ngMaxlength': 'maxlength',
578578
'ngMin': 'min',
579579
'ngMax': 'max',
580-
'ngPattern': 'pattern'
580+
'ngPattern': 'pattern',
581+
'ngStep': 'step'
581582
};
582583

583584
function getBooleanAttrName(element, name) {

src/ng/directive/input.js

+69-32
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,10 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15561556

15571557
var minVal = 0,
15581558
maxVal = 100,
1559+
stepVal = 1,
1560+
minAttrType = isDefined(attr.ngMin) ? 'ngMin' : isDefined(attr.min) ? 'min' : false,
1561+
maxAttrType = isDefined(attr.ngMax) ? 'ngMax' : isDefined(attr.max) ? 'max' : false,
1562+
stepAttributeType = isDefined(attr.ngStep) ? 'ngStep' : isDefined(attr.step) ? 'step' : false,
15591563
supportsRange = ctrl.$$hasNativeValidators && element[0].type === 'range',
15601564
validity = element[0].validity;
15611565

@@ -1570,6 +1574,55 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15701574
} :
15711575
originalRender;
15721576

1577+
if (minAttrType) {
1578+
ctrl.$validators.min = minAttrType === 'min' && supportsRange ?
1579+
function noopMinValidator(value) {
1580+
// Since all browsers set the input to a valid value, we don't need to check validity
1581+
return true;
1582+
} :
1583+
// ngMin doesn't set the min attr, so the browser doesn't adjust the input value as setting min would
1584+
function minValidator(modelValue, viewValue) {
1585+
return ctrl.$isEmpty(viewValue) || isUndefined(minVal) || viewValue >= minVal;
1586+
};
1587+
1588+
// Assign minVal when the directive is linked. This won't run the validators as the model isn't ready yet
1589+
minChange(attr.min);
1590+
attr.$observe('min', minChange);
1591+
}
1592+
1593+
if (maxAttrType) {
1594+
ctrl.$validators.max = maxAttrType === 'max' && supportsRange ?
1595+
function noopMaxValidator() {
1596+
// Since all browsers set the input to a valid value, we don't need to check validity
1597+
return true;
1598+
} :
1599+
// ngMax doesn't set the max attr, so the browser doesn't adjust the input value as setting max would
1600+
function maxValidator(modelValue, viewValue) {
1601+
return ctrl.$isEmpty(viewValue) || isUndefined(maxVal) || viewValue <= maxVal;
1602+
};
1603+
1604+
// Assign maxVal when the directive is linked. This won't run the validators as the model isn't ready yet
1605+
maxChange(attr.max);
1606+
attr.$observe('max', maxChange);
1607+
1608+
}
1609+
1610+
if (stepAttributeType) {
1611+
ctrl.$validators.step = stepAttributeType === 'step' && supportsRange ?
1612+
function noopStepValidator() {
1613+
// Since all browsers set the input to a valid value, we don't need to check validity
1614+
return true;
1615+
} :
1616+
// ngStep doesn't set the setp attr, so the browser doesn't adjust the input value as setting step would
1617+
function stepValidator(modelValue, viewValue) {
1618+
return ctrl.$isEmpty(viewValue) || isUndefined(stepVal) || viewValue % stepVal === 0;
1619+
};
1620+
1621+
// Assign stepVal when the directive is linked. This won't run the validators as the model isn't ready yet
1622+
stepChange(attr.step);
1623+
attr.$observe('step', stepChange);
1624+
}
1625+
15731626
function minChange(val) {
15741627
if (isDefined(val) && !isNumber(val)) {
15751628
val = parseFloat(val);
@@ -1594,23 +1647,6 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15941647
}
15951648
}
15961649

1597-
var minAttrType = isDefined(attr.ngMin) ? 'ngMin' : isDefined(attr.min) ? 'min' : false;
1598-
if (minAttrType) {
1599-
ctrl.$validators.min = isDefined(attr.min) && supportsRange ?
1600-
function noopMinValidator(value) {
1601-
// Since all browsers set the input to a valid value, we don't need to check validity
1602-
return true;
1603-
} :
1604-
// ngMin doesn't set the min attr, so the browser doesn't adjust the input value as setting min would
1605-
function minValidator(modelValue, viewValue) {
1606-
return ctrl.$isEmpty(viewValue) || isUndefined(minVal) || viewValue >= minVal;
1607-
};
1608-
1609-
// Assign minVal when the directive is linked. This won't run the validators as the model isn't ready yet
1610-
minChange(attr.min);
1611-
attr.$observe('min', minChange);
1612-
}
1613-
16141650
function maxChange(val) {
16151651
if (isDefined(val) && !isNumber(val)) {
16161652
val = parseFloat(val);
@@ -1634,23 +1670,24 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16341670
ctrl.$validate();
16351671
}
16361672
}
1637-
var maxAttrType = isDefined(attr.max) ? 'max' : attr.ngMax ? 'ngMax' : false;
1638-
if (maxAttrType) {
1639-
ctrl.$validators.max = isDefined(attr.max) && supportsRange ?
1640-
function noopMaxValidator() {
1641-
// Since all browsers set the input to a valid value, we don't need to check validity
1642-
return true;
1643-
} :
1644-
// ngMax doesn't set the max attr, so the browser doesn't adjust the input value as setting max would
1645-
function maxValidator(modelValue, viewValue) {
1646-
return ctrl.$isEmpty(viewValue) || isUndefined(maxVal) || viewValue <= maxVal;
1647-
};
16481673

1649-
// Assign maxVal when the directive is linked. This won't run the validators as the model isn't ready yet
1650-
maxChange(attr.max);
1651-
attr.$observe('max', maxChange);
1652-
}
1674+
function stepChange(val) {
1675+
if (isDefined(val) && !isNumber(val)) {
1676+
val = parseFloat(val);
1677+
}
1678+
stepVal = isNumber(val) && !isNaN(val) ? val : undefined;
1679+
// ignore changes before model is initialized
1680+
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
1681+
return;
1682+
}
16531683

1684+
if (supportsRange && stepAttributeType === 'step') {
1685+
ctrl.$setViewValue(element.val());
1686+
} else {
1687+
// TODO(matsko): implement validateLater to reduce number of validations
1688+
ctrl.$validate();
1689+
}
1690+
}
16541691
}
16551692

16561693
function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) {

0 commit comments

Comments
 (0)