-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathform.js
177 lines (134 loc) · 5.41 KB
/
form.js
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
(function (angular) {
'use strict';
// Extending Angular.js module.
angular.module('ngInputModified')
.directive('form', function ($animate, inputModifiedConfig) {
return formDirectiveFactory($animate, inputModifiedConfig, false);
})
.directive('ngForm', function ($animate, inputModifiedConfig) {
return formDirectiveFactory($animate, inputModifiedConfig, true);
})
;
function formDirectiveFactory ($animate, inputModifiedConfig, isNgForm) {
// Shortcut.
var config = inputModifiedConfig;
return {
name: 'form',
restrict: isNgForm ? 'EAC' : 'E',
require: ['?form'],
link: function ($scope, $element, attrs, controllers) {
// Handling controllers.
var formCtrl = controllers[0];
var parentFormCtrl = (formCtrl.$$parentForm || $element.parent().controller('form'));
// Form controller is required for this directive to operate.
// Parent form is optional.
if (!formCtrl) {
return;
}
formCtrl.modified = false;
formCtrl.reset = reset;
// Modified models.
formCtrl.modifiedCount = 0;
formCtrl.modifiedModels = [];
formCtrl.$$onChildModelModifiedStateChanged = function (modelCtrl) {
onModifiedStateChanged(modelCtrl, formCtrl.modifiedModels);
};
// Modified child forms.
formCtrl.modifiedChildFormsCount = 0;
formCtrl.modifiedChildForms = [];
formCtrl.$$onChildFormModifiedStateChanged = function (childFormCtrl) {
onModifiedStateChanged(childFormCtrl, formCtrl.modifiedChildForms);
};
formCtrl.$$onChildFormDestroyed = onChildFormDestroyed;
$element.on('$destroy', function () {
if (parentFormCtrl && 'function' === typeof parentFormCtrl.$$onChildFormDestroyed) {
parentFormCtrl.$$onChildFormDestroyed(formCtrl);
}
});
/**
* Resets all form inputs to it's master values.
*/
function reset () {
// Resetting modified models.
angular.forEach(formCtrl.modifiedModels, function (modelCtrl) {
modelCtrl.reset();
});
// Resetting modified child forms.
angular.forEach(formCtrl.modifiedChildForms, function (childFormCtrl) {
childFormCtrl.reset();
});
}
/**
* This universal function is called when child model or child form is modified
* by the modified component itself.
* It will update the corresponding tracking list, the number of modified components
* and the form itself if required.
*
* @param {FormController|NgModelController} ctrl The modified model or modified form controller
* @param {FormController[]|NgModelController[]} list The tracking list of modified controllers (models or forms)
*/
function onModifiedStateChanged (ctrl, list) {
var listIndex = list.indexOf(ctrl);
var presentInList = (-1 !== listIndex);
if (ctrl.modified && !presentInList) {
// Adding model to the internal list of modified models.
list.push(ctrl);
updateFormState();
} else if (!ctrl.modified && presentInList) {
// Removing model from the internal list of modified models.
list.splice(listIndex, 1);
updateFormState();
}
}
/**
* Updates form state and notifies it's parents.
*/
function updateFormState () {
updateModifiedState();
// Notifying the parent form if it presents.
if (parentFormCtrl && 'function' === typeof parentFormCtrl.$$onChildFormModifiedStateChanged) {
parentFormCtrl.$$onChildFormModifiedStateChanged(formCtrl);
}
updateCssClasses();
// Firing event to parent scopes.
$scope.$emit('inputModified.formChanged', formCtrl.modified, formCtrl);
}
/**
* Updates form modified state.
*
* Form is considered modified when it has at least one
* modified element or child form.
*/
function updateModifiedState () {
formCtrl.modifiedCount = formCtrl.modifiedModels.length;
formCtrl.modifiedChildFormsCount = formCtrl.modifiedChildForms.length;
formCtrl.modified =
(formCtrl.modifiedCount + formCtrl.modifiedChildFormsCount) > 0
;
}
/**
* Decorates element with proper CSS classes.
*/
function updateCssClasses () {
$animate.addClass($element, (formCtrl.modified ? config.modifiedClassName : config.notModifiedClassName));
$animate.removeClass($element, (formCtrl.modified ? config.notModifiedClassName : config.modifiedClassName));
}
/**
* Called when one of child forms is removed from DOM.
* Updates internal list of modified child forms in order to keep correct state.
*
* @param {FormController} childFormCtrl
*/
function onChildFormDestroyed (childFormCtrl) {
var index = formCtrl.modifiedChildForms.indexOf(childFormCtrl);
if (-1 === index) {
return;
}
// Removing form from the list.
formCtrl.modifiedChildForms.splice(index, 1);
updateFormState();
}
}
};
}
})(angular);