Skip to content

Commit b5640b1

Browse files
author
Slava Fomin II
committed
Version 1.1.0
1 parent bba13d7 commit b5640b1

File tree

4 files changed

+80
-28
lines changed

4 files changed

+80
-28
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-input-modified",
33
"description": "AngularJS module to detect and indicate input modifications",
4-
"version": "1.0.0",
4+
"version": "1.1.0",
55
"main": "src/angular-input-modified.js",
66
"dependencies": {
77
"angular": "1.2.16"

changelog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# angular-input-modified changelog
22

3+
## Version 1.1.0
4+
(30 May 2014)
5+
6+
- Improved initialization code
7+
- Updated comparison function to allow more weak comparison like `"1"` (string) and `1` (integer)
8+
- Fixed bug with `$setPristine` method, now it should work correctly in all cases
9+
- Improved modification tracking
10+
- Added property `ngForm.modifiedModels` (list of names for modified models)
11+
- Demo updated
12+
313
## Version 1.0.0
414
(18 May 2014)
515

readme.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# angular-input-modified 1.0.0
1+
# angular-input-modified 1.1.0
22

33
This AngularJS module adds additional properties and methods to the `ngModel` and `ngForm` controllers
44
to provide end-user with facilities to detect and indicate form input modifications.
@@ -39,23 +39,26 @@ It also supports animations if `ngAnimate` module is present.
3939

4040
### ngModel
4141

42-
**masterValue** -
42+
{\*} **masterValue** -
4343
initial value of the input field.
4444

45-
**modified** -
45+
{boolean} **modified** -
4646
flag that indicates whether the input value was modified.
4747

4848
**reset()** -
4949
method to reset input value to it's initial state.
5050

5151
### ngForm
5252

53-
**modifiedCount** -
53+
{integer} **modifiedCount** -
5454
number of modified input types inside of the form.
5555

56-
**modified** -
56+
{boolean} **modified** -
5757
flag that indicates whether the form is modified (i.e. at least one element is modified).
5858

59+
{string[]} **modifiedModels** -
60+
list of names for modified models.
61+
5962
**reset()** -
6063
method to reset all input values of the form to their initial states.
6164

src/angular-input-modified.js

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* AngularJS module "angular-input-modified".
3+
*
4+
* @version 1.1.0
5+
* @author Slava Fomin II <[email protected]>
6+
* @licence MIT
7+
* @copyright Slava Fomin II, Better Solutions, 2014
8+
*/
19
(function(window, angular) {
210
'use strict';
311

@@ -20,6 +28,8 @@
2028
require: ['?ngModel', '?^form'],
2129
link: function($scope, $element, attrs, controllers) {
2230

31+
var modelName = attrs.ngModel;
32+
2333
// Handling controllers.
2434
var ngModel = controllers[0];
2535
var ngForm = controllers[1];
@@ -30,6 +40,33 @@
3040
return;
3141
}
3242

43+
/**
44+
* Decorates element with proper CSS classes.
45+
*/
46+
var toggleCssClasses = function() {
47+
$animate.addClass($element, (ngModel.modified ? modifiedClassName : notModifiedClassName));
48+
$animate.removeClass($element, (ngModel.modified ? notModifiedClassName : modifiedClassName));
49+
};
50+
51+
var updateModified = function(modelName, modified) {
52+
if (ngForm) {
53+
var index = ngForm.modifiedModels.indexOf(modelName);
54+
var exists = (-1 !== index);
55+
56+
if (modified && !exists) {
57+
// Adding model name to the list of modified models.
58+
ngForm.modifiedModels.push(modelName);
59+
ngForm.modifiedCount++;
60+
} else if (!modified && exists) {
61+
// Removing model name from the list of modified models.
62+
ngForm.modifiedModels.splice(index, 1);
63+
ngForm.modifiedCount--;
64+
}
65+
66+
ngForm.modified = (ngForm.modifiedCount > 0);
67+
}
68+
};
69+
3370
// Saving handle to original $setPristine method.
3471
var originalSetPristine = ngModel.$setPristine;
3572

@@ -51,9 +88,14 @@
5188
* Augmentation for original $setPristine method.
5289
*/
5390
ngModel.$setPristine = function() {
91+
5492
// Calling original $setPristine method.
5593
originalSetPristine.apply(this, arguments);
5694

95+
if (ngModel.modified) {
96+
updateModified(modelName, false);
97+
}
98+
5799
// Updating parameters.
58100
ngModel.masterValue = ngModel.$modelValue;
59101
ngModel.modified = false;
@@ -66,11 +108,19 @@
66108
* Resets input value to the master.
67109
*/
68110
ngModel.reset = function() {
69-
eval('$scope.' + attrs.ngModel + ' = ngModel.masterValue;');
111+
eval('$scope.' + modelName + ' = ngModel.masterValue;');
70112
};
71113

72-
// If parent form element is present for this input.
73-
if (ngForm) {
114+
// If parent form element is present for this input and
115+
// is not yet initialized.
116+
if (ngForm && 'undefined' === typeof ngForm.modified) {
117+
118+
ngForm.modified = false;
119+
ngForm.modifiedCount = 0;
120+
121+
// List of modified models.
122+
ngForm.modifiedModels = [];
123+
74124
/**
75125
* Resets all form inputs to it's master values.
76126
*/
@@ -84,18 +134,6 @@
84134
}
85135
});
86136
};
87-
88-
ngForm.modified = false;
89-
ngForm.modifiedCount = 0;
90-
}
91-
92-
/**
93-
* Decorates element with proper CSS classes.
94-
*/
95-
var toggleCssClasses = function()
96-
{
97-
$animate.addClass($element, (ngModel.modified ? modifiedClassName : notModifiedClassName));
98-
$animate.removeClass($element, (ngModel.modified ? notModifiedClassName : modifiedClassName));
99137
}
100138

101139
/**
@@ -106,7 +144,7 @@
106144
var initial = true;
107145

108146
// Watching for model value changes.
109-
$scope.$watch(attrs.ngModel, function(value) {
147+
$scope.$watch(modelName, function(value) {
110148
// If master value is not set.
111149
if (initial) {
112150
// Preserving master value.
@@ -115,17 +153,15 @@
115153
toggleCssClasses();
116154

117155
} else {
156+
118157
// Comparing current input value with preserved master value
119158
// to determine if it's changed.
120159
var modified = !valuesEqual(value, ngModel.masterValue);
121160

122161
// If modified flag is changed.
123162
if (ngModel.modified !== modified) {
124163

125-
if (ngForm) {
126-
ngForm.modifiedCount += (modified ? 1 : -1);
127-
ngForm.modified = (ngForm.modifiedCount > 0);
128-
}
164+
updateModified(modelName, modified);
129165

130166
// Setting new flag.
131167
ngModel.modified = modified;
@@ -160,11 +196,14 @@
160196
if (value1 instanceof Date && value2 instanceof Date) {
161197
// Comparing two dates.
162198
return (value1.getTime() === value2.getTime());
199+
} else {
200+
// Comparing two generic objects using strong comparison.
201+
return (value1 === value2);
163202
}
164203
}
165204

166-
// In all other cases.
167-
return (value1 === value2);
205+
// In all other cases using weak comparison.
206+
return (value1 == value2);
168207
}
169208

170209
/**

0 commit comments

Comments
 (0)