forked from dkhunt27/angular-directive-percentage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathangular-directive-percentage.js
More file actions
300 lines (243 loc) · 9.57 KB
/
angular-directive-percentage.js
File metadata and controls
300 lines (243 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Heavily adapted from
* https://github.com/fiestah/angular-money-directive
* http://www.chroder.com/2014/02/01/using-ngmodelcontroller-with-custom-directives/
*/
angular.module('angular-directive-percentage', [])
.directive('percentage', function () {
'use strict';
var NUMBER_REGEXP = /^\s*(\-|\+)?(\d+|(\d*(\.\d*)))\s*$/;
var PRECISION_MIN = 2;
var PRECISION_DEFAULT = 4;
var MIN_DEFAULT = 0;
var DEBUG = false;
var min = MIN_DEFAULT;
var precision = PRECISION_DEFAULT;
var max;
var isValidNumber = function (value, ngModelCtrl) {
if (ngModelCtrl.$isEmpty(value)) {
return true;
} else {
return NUMBER_REGEXP.test(value);
}
};
var roundToPrecision = function (value, precision) {
var d = Math.pow(10, precision);
return Math.round(value * d) / d;
};
var formatToPrecision = function (value, precision) {
precision = precision - 2;
if (precision < 0) {
precision = 0;
}
return parseFloat(value).toFixed(precision);
};
var formatViewDataIntoModelData = function (viewValue, ngModelCtrl) {
var parsedValue = "";
// check if valid (one of the validators could have set to be invalid already)
if (ngModelCtrl.$valid || !ngModelCtrl.$error.percentage) {
if (ngModelCtrl.$isEmpty(viewValue)) {
// handle empty view data since we can't covert it back to a decimal
parsedValue = "";
} else {
// handle formatting
//if (viewValue.slice(-1) === "%") {
// viewValue = viewValue.slice(0, -1);
//}
// check for non numeric
var valid = isValidNumber(viewValue, ngModelCtrl);
if (!valid) {
viewValue = '';
ngModelCtrl.$setValidity('percentage', false);
}
if (valid) {
// convert percentage back to decimal
viewValue = viewValue / 100;
}
if (valid) {
// handle precision
if (precision > -1) {
viewValue = roundToPrecision(viewValue, precision);
}
}
parsedValue = viewValue;
}
}
return parsedValue;
};
var formatModelDataIntoViewData = function (modelValue, ngModelCtrl) {
var formattedValue = "";
// check if valid (one of the validators could have set to be invalid already)
if (ngModelCtrl.$valid) {
// handle empty view data
if (ngModelCtrl.$isEmpty(modelValue)) {
formattedValue = "";
} else {
// handle non numeric
var valid = isValidNumber(modelValue, ngModelCtrl);
if (!valid) {
modelValue = '';
ngModelCtrl.$setValidity('percentage', false);
}
if (valid) {
// convert decimal into percentage
modelValue = modelValue * 100;
// handle precision
if (precision > -1) {
modelValue = roundToPrecision(modelValue, precision);
modelValue = formatToPrecision(modelValue, precision);
}
}
// handle formatting
//formattedValue = modelValue.toString() + "%";
formattedValue = modelValue.toString();
}
}
return formattedValue;
};
var minValidator = function (value, ngModelCtrl) {
if (!ngModelCtrl.$isEmpty(value) && value < min) {
ngModelCtrl.$setValidity('percentage', false);
return value;
} else {
return value;
}
};
var maxValidator = function (value, ngModelCtrl) {
if (!ngModelCtrl.$isEmpty(value) && value > max) {
ngModelCtrl.$setValidity('percentage', false);
return value;
} else {
return value;
}
};
var addParsers = function (ngModelCtrl, attrs) {
// when the view data changes, the code executes the parses in order that they are added to the stack
// so add them in order that you want them to execute...use push
// EXECUTE 1st (START)
// reset the validity whenever the view data changes
ngModelCtrl.$parsers.push(function (viewValue) {
ngModelCtrl.$setValidity('percentage', true);
var newModelData = viewValue;
DEBUG ? console.log("ngModelCtrl.$parsers $setValidity newModelData:" + newModelData) : "";
return newModelData;
});
// EXECUTE 2nd
// perform min validation whenever the view data changes
ngModelCtrl.$parsers.push(function (viewValue) {
var newModelData = minValidator(viewValue, ngModelCtrl);
DEBUG ? console.log("ngModelCtrl.$parsers minValidator newModelData:" + newModelData) : "";
return newModelData;
});
// EXECUTE 3rd (if attribute present)
// Max validation (optional)
if (angular.isDefined(attrs.max)) {
// perform max validation whenever the view data changes
ngModelCtrl.$parsers.push(function (viewValue) {
var newModelData = maxValidator(viewValue, ngModelCtrl);
DEBUG ? console.log("ngModelCtrl.$parsers maxValidator newModelData:" + newModelData) : "";
return newModelData;
});
}
// EXECUTE 4th (LAST)
// perform specific formatting for the data
ngModelCtrl.$parsers.push(function (viewValue) {
var newModelData = formatViewDataIntoModelData(viewValue, ngModelCtrl);
DEBUG ? console.log("ngModelCtrl.$parsers formatViewDataIntoModelData newModelData:" + newModelData) : "";
return newModelData;
});
};
var addFormatters = function (ngModelCtrl, attrs) {
// when the model data changes, the code executes the formatters in reverse order that they are added to the stack
// so add them in reverse order that you want them to execute...use unshift instead of push
// EXECUTE 1st (START)
// reset the validity whenever the model data changes
ngModelCtrl.$formatters.unshift(function (modelValue) {
ngModelCtrl.$setValidity('percentage', true);
var newViewData = modelValue;
DEBUG ? console.log("ngModelCtrl.$formatters $setValidity newViewData:" + newViewData) : "";
return newViewData;
});
// EXECUTE 2nd
// perform min validation whenever the model data changes
ngModelCtrl.$formatters.unshift(function (modelValue) {
var newViewData = minValidator(modelValue, ngModelCtrl);
DEBUG ? console.log("ngModelCtrl.$formatters minValidator newViewData:" + newViewData) : "";
return newViewData;
});
// EXECUTE 3rd (if attribute present)
// Max validation (optional)
if (angular.isDefined(attrs.max)) {
// perform max validation whenever the model data changes
ngModelCtrl.$formatters.unshift(function (modelValue) {
var newViewData = maxValidator(modelValue, ngModelCtrl);
DEBUG ? console.log("ngModelCtrl.$formatters maxValidator newViewData:" + newViewData) : "";
return newViewData;
});
}
// EXECUTE 4th (LAST)
// perform specific formatting for the model (parser) or view (formatter)
ngModelCtrl.$formatters.unshift(function (modelValue) {
var newViewData = formatModelDataIntoViewData(modelValue, ngModelCtrl);
DEBUG ? console.log("ngModelCtrl.$formatters formatModelDataIntoViewData newViewData:" + newViewData) : "";
return newViewData;
});
};
function link(scope, el, attrs, ngModelCtrl) {
// when the view model changes, the code executes the parses in order that they are added to the stack
// so add them in order that you want them to execute
// parsers execute whenever the view data changes
// then process that data into the model data
addParsers(ngModelCtrl, attrs);
// formatters execute whenever the model data changes
// then process that data into the view data
addFormatters(ngModelCtrl, attrs);
// Auto-format on blur
el.bind('blur', function () {
var viewValue;
// since the view data has changed...the parseViewToModel has already ran and updated the $modelValue
if (ngModelCtrl.$valid) {
// if the data is valid, run the formatter to update the $viewValue
var modelValue = ngModelCtrl.$modelValue;
viewValue = formatModelDataIntoViewData(modelValue, ngModelCtrl);
ngModelCtrl.$setViewValue(viewValue);
DEBUG ? console.log("ngModelCtrl.onBlur updated-viewValue:" + viewValue) : "";
ngModelCtrl.$render(); // forces the DOM to update
} else {
viewValue = ngModelCtrl.$viewValue;
DEBUG ? console.log("ngModelCtrl.onBlur current-viewValue:" + viewValue) : "";
}
});
attrs.$observe('precision', function (value) {
var parsed = parseFloat(value);
if (isNaN(parsed)) {
parsed = PRECISION_DEFAULT;
} else if (parsed < 0) {
// special case to disable parsing
} else if (parsed < PRECISION_MIN) {
parsed = PRECISION_MIN;
}
precision = parsed;
});
attrs.$observe('min', function (value) {
var parsed = parseFloat(value);
min = !isNaN(parsed) ? parsed : MIN_DEFAULT;
});
// Max validation (optional)
if (angular.isDefined(attrs.max)) {
attrs.$observe('max', function (value) {
var parsed = parseFloat(value);
max = parsed;
});
}
}
return {
restrict: 'A',
require: 'ngModel',
link: link
};
}
);
if (module && module.exports) {
module.exports = 'angular-directive-percentage';
}