MCPcopy
hub / github.com/autoNumeric/autoNumeric / _parseStyleRules

Method _parseStyleRules

src/AutoNumeric.js:1846–1946  ·  view source on GitHub ↗

* Parse the `styleRules` option and run the test for each given rules, either pre-defined ones like `positive`, `negative` and `ranges`, or user defined callbacks within the `userDefined` attribute. * @private

()

Source from the content-addressed store, hash-verified

1844 * @private
1845 */
1846 _parseStyleRules() {
1847 if (AutoNumericHelper.isUndefinedOrNullOrEmpty(this.settings.styleRules) || this.rawValue === '') {
1848 return;
1849 }
1850
1851 // 'positive' attribute
1852 if (!AutoNumericHelper.isUndefinedOrNullOrEmpty(this.settings.styleRules.positive)) {
1853 if (this.rawValue >= 0) {
1854 this._addCSSClass(this.settings.styleRules.positive);
1855 } else {
1856 this._removeCSSClass(this.settings.styleRules.positive);
1857 }
1858 }
1859
1860 // 'negative' attribute
1861 if (!AutoNumericHelper.isUndefinedOrNullOrEmpty(this.settings.styleRules.negative)) {
1862 if (this.rawValue < 0) {
1863 this._addCSSClass(this.settings.styleRules.negative);
1864 } else {
1865 this._removeCSSClass(this.settings.styleRules.negative);
1866 }
1867 }
1868
1869 // 'ranges' attribute
1870 if (!AutoNumericHelper.isUndefinedOrNullOrEmpty(this.settings.styleRules.ranges) && this.settings.styleRules.ranges.length !== 0) {
1871 this.settings.styleRules.ranges.forEach(range => {
1872 if (this.rawValue >= range.min && this.rawValue < range.max) {
1873 this._addCSSClass(range.class);
1874 } else {
1875 this._removeCSSClass(range.class);
1876 }
1877 });
1878 }
1879
1880 // 'userDefined' attribute
1881 //TODO Also pass the old raw value as a parameter, and not only the new raw value
1882 if (!AutoNumericHelper.isUndefinedOrNullOrEmpty(this.settings.styleRules.userDefined) && this.settings.styleRules.userDefined.length !== 0) {
1883 this.settings.styleRules.userDefined.forEach(userObject => {
1884 if (AutoNumericHelper.isFunction(userObject.callback)) {
1885 // Test for the type of the `classes` attribute, which changes the function behavior
1886 if (AutoNumericHelper.isString(userObject.classes)) {
1887 // If 'classes' is a string, set it if `true`, remove it if `false`
1888 if (userObject.callback(this.rawValue)) {
1889 this._addCSSClass(userObject.classes);
1890 } else {
1891 this._removeCSSClass(userObject.classes);
1892 }
1893 } else if (AutoNumericHelper.isArray(userObject.classes)) {
1894 if (userObject.classes.length === 2) {
1895 // If 'classes' is an array with only 2 elements, set the first class if `true`, the second if `false`
1896 if (userObject.callback(this.rawValue)) {
1897 this._addCSSClass(userObject.classes[0]);
1898 this._removeCSSClass(userObject.classes[1]);
1899 } else {
1900 this._removeCSSClass(userObject.classes[0]);
1901 this._addCSSClass(userObject.classes[1]);
1902 }
1903 } else if (userObject.classes.length > 2) {

Callers 1

_setRawValueMethod · 0.95

Calls 11

_addCSSClassMethod · 0.95
_removeCSSClassMethod · 0.95
isFunctionMethod · 0.80
isStringMethod · 0.80
isArrayMethod · 0.80
isInArrayMethod · 0.80
isIntMethod · 0.80
isNullMethod · 0.80
throwErrorMethod · 0.80
warningMethod · 0.80

Tested by

no test coverage detected