@angular-wave/angular.ts 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/.eslintignore +1 -0
  2. package/.eslintrc.cjs +29 -0
  3. package/.github/workflows/playwright.yml +27 -0
  4. package/CHANGELOG.md +17974 -0
  5. package/CODE_OF_CONDUCT.md +3 -0
  6. package/CONTRIBUTING.md +246 -0
  7. package/DEVELOPERS.md +488 -0
  8. package/LICENSE +22 -0
  9. package/Makefile +31 -0
  10. package/README.md +115 -0
  11. package/RELEASE.md +98 -0
  12. package/SECURITY.md +16 -0
  13. package/TRIAGING.md +135 -0
  14. package/css/angular.css +22 -0
  15. package/dist/angular-ts.cjs.js +36843 -0
  16. package/dist/angular-ts.esm.js +36841 -0
  17. package/dist/angular-ts.umd.js +36848 -0
  18. package/dist/build/angular-animate.js +4272 -0
  19. package/dist/build/angular-aria.js +426 -0
  20. package/dist/build/angular-message-format.js +1072 -0
  21. package/dist/build/angular-messages.js +829 -0
  22. package/dist/build/angular-mocks.js +3757 -0
  23. package/dist/build/angular-parse-ext.js +1275 -0
  24. package/dist/build/angular-resource.js +911 -0
  25. package/dist/build/angular-route.js +1266 -0
  26. package/dist/build/angular-sanitize.js +891 -0
  27. package/dist/build/angular-touch.js +368 -0
  28. package/dist/build/angular.js +36600 -0
  29. package/e2e/unit.spec.ts +15 -0
  30. package/images/android-chrome-192x192.png +0 -0
  31. package/images/android-chrome-512x512.png +0 -0
  32. package/images/apple-touch-icon.png +0 -0
  33. package/images/favicon-16x16.png +0 -0
  34. package/images/favicon-32x32.png +0 -0
  35. package/images/favicon.ico +0 -0
  36. package/images/site.webmanifest +1 -0
  37. package/index.html +104 -0
  38. package/package.json +47 -0
  39. package/playwright.config.ts +78 -0
  40. package/public/circle.html +1 -0
  41. package/public/my_child_directive.html +1 -0
  42. package/public/my_directive.html +1 -0
  43. package/public/my_other_directive.html +1 -0
  44. package/public/test.html +1 -0
  45. package/rollup.config.js +31 -0
  46. package/src/animations/animateCache.js +55 -0
  47. package/src/animations/animateChildrenDirective.js +105 -0
  48. package/src/animations/animateCss.js +1139 -0
  49. package/src/animations/animateCssDriver.js +291 -0
  50. package/src/animations/animateJs.js +367 -0
  51. package/src/animations/animateJsDriver.js +67 -0
  52. package/src/animations/animateQueue.js +851 -0
  53. package/src/animations/animation.js +506 -0
  54. package/src/animations/module.js +779 -0
  55. package/src/animations/ngAnimateSwap.js +119 -0
  56. package/src/animations/rafScheduler.js +50 -0
  57. package/src/animations/shared.js +378 -0
  58. package/src/constants.js +20 -0
  59. package/src/core/animate.js +845 -0
  60. package/src/core/animateCss.js +73 -0
  61. package/src/core/animateRunner.js +195 -0
  62. package/src/core/attributes.js +199 -0
  63. package/src/core/cache.js +45 -0
  64. package/src/core/compile.js +4727 -0
  65. package/src/core/controller.js +225 -0
  66. package/src/core/exceptionHandler.js +63 -0
  67. package/src/core/filter.js +146 -0
  68. package/src/core/interpolate.js +442 -0
  69. package/src/core/interval.js +188 -0
  70. package/src/core/intervalFactory.js +57 -0
  71. package/src/core/location.js +1086 -0
  72. package/src/core/parser/parse.js +2562 -0
  73. package/src/core/parser/parse.md +13 -0
  74. package/src/core/q.js +746 -0
  75. package/src/core/rootScope.js +1596 -0
  76. package/src/core/sanitizeUri.js +85 -0
  77. package/src/core/sce.js +1161 -0
  78. package/src/core/taskTrackerFactory.js +125 -0
  79. package/src/core/timeout.js +121 -0
  80. package/src/core/urlUtils.js +187 -0
  81. package/src/core/utils.js +1349 -0
  82. package/src/directive/a.js +37 -0
  83. package/src/directive/attrs.js +283 -0
  84. package/src/directive/bind.js +51 -0
  85. package/src/directive/bind.md +142 -0
  86. package/src/directive/change.js +12 -0
  87. package/src/directive/change.md +25 -0
  88. package/src/directive/cloak.js +12 -0
  89. package/src/directive/cloak.md +24 -0
  90. package/src/directive/events.js +75 -0
  91. package/src/directive/events.md +166 -0
  92. package/src/directive/form.js +725 -0
  93. package/src/directive/init.js +15 -0
  94. package/src/directive/init.md +41 -0
  95. package/src/directive/input.js +1783 -0
  96. package/src/directive/list.js +46 -0
  97. package/src/directive/list.md +22 -0
  98. package/src/directive/ngClass.js +249 -0
  99. package/src/directive/ngController.js +64 -0
  100. package/src/directive/ngCsp.js +82 -0
  101. package/src/directive/ngIf.js +134 -0
  102. package/src/directive/ngInclude.js +217 -0
  103. package/src/directive/ngModel.js +1356 -0
  104. package/src/directive/ngModelOptions.js +509 -0
  105. package/src/directive/ngOptions.js +670 -0
  106. package/src/directive/ngRef.js +90 -0
  107. package/src/directive/ngRepeat.js +650 -0
  108. package/src/directive/ngShowHide.js +255 -0
  109. package/src/directive/ngSwitch.js +178 -0
  110. package/src/directive/ngTransclude.js +98 -0
  111. package/src/directive/non-bindable.js +11 -0
  112. package/src/directive/non-bindable.md +17 -0
  113. package/src/directive/script.js +30 -0
  114. package/src/directive/select.js +624 -0
  115. package/src/directive/style.js +25 -0
  116. package/src/directive/style.md +23 -0
  117. package/src/directive/validators.js +329 -0
  118. package/src/exts/aria.js +544 -0
  119. package/src/exts/messages.js +852 -0
  120. package/src/filters/filter.js +207 -0
  121. package/src/filters/filter.md +69 -0
  122. package/src/filters/filters.js +239 -0
  123. package/src/filters/json.md +16 -0
  124. package/src/filters/limit-to.js +43 -0
  125. package/src/filters/limit-to.md +19 -0
  126. package/src/filters/order-by.js +183 -0
  127. package/src/filters/order-by.md +83 -0
  128. package/src/index.js +13 -0
  129. package/src/injector.js +1034 -0
  130. package/src/jqLite.js +1117 -0
  131. package/src/loader.js +1320 -0
  132. package/src/public.js +215 -0
  133. package/src/routeToRegExp.js +41 -0
  134. package/src/services/anchorScroll.js +135 -0
  135. package/src/services/browser.js +321 -0
  136. package/src/services/cacheFactory.js +398 -0
  137. package/src/services/cookieReader.js +72 -0
  138. package/src/services/document.js +64 -0
  139. package/src/services/http.js +1537 -0
  140. package/src/services/httpBackend.js +206 -0
  141. package/src/services/log.js +160 -0
  142. package/src/services/templateRequest.js +139 -0
  143. package/test/angular.spec.js +2153 -0
  144. package/test/aria/aria.spec.js +1245 -0
  145. package/test/binding.spec.js +504 -0
  146. package/test/build-test.html +14 -0
  147. package/test/injector.spec.js +2327 -0
  148. package/test/jasmine/jasmine-5.1.2/boot0.js +65 -0
  149. package/test/jasmine/jasmine-5.1.2/boot1.js +133 -0
  150. package/test/jasmine/jasmine-5.1.2/jasmine-html.js +963 -0
  151. package/test/jasmine/jasmine-5.1.2/jasmine.css +320 -0
  152. package/test/jasmine/jasmine-5.1.2/jasmine.js +10824 -0
  153. package/test/jasmine/jasmine-5.1.2/jasmine_favicon.png +0 -0
  154. package/test/jasmine/jasmine-browser.json +17 -0
  155. package/test/jasmine/jasmine.json +9 -0
  156. package/test/jqlite.spec.js +2133 -0
  157. package/test/loader.spec.js +219 -0
  158. package/test/messages/messages.spec.js +1146 -0
  159. package/test/min-err.spec.js +174 -0
  160. package/test/mock-test.html +13 -0
  161. package/test/module-test.html +15 -0
  162. package/test/ng/anomate.spec.js +606 -0
  163. package/test/ng/cache-factor.spec.js +334 -0
  164. package/test/ng/compile.spec.js +17956 -0
  165. package/test/ng/controller-provider.spec.js +227 -0
  166. package/test/ng/cookie-reader.spec.js +98 -0
  167. package/test/ng/directive/a.spec.js +192 -0
  168. package/test/ng/directive/bind.spec.js +334 -0
  169. package/test/ng/directive/boolean.spec.js +136 -0
  170. package/test/ng/directive/change.spec.js +71 -0
  171. package/test/ng/directive/class.spec.js +858 -0
  172. package/test/ng/directive/click.spec.js +38 -0
  173. package/test/ng/directive/cloak.spec.js +44 -0
  174. package/test/ng/directive/constoller.spec.js +194 -0
  175. package/test/ng/directive/element-style.spec.js +92 -0
  176. package/test/ng/directive/event.spec.js +282 -0
  177. package/test/ng/directive/form.spec.js +1518 -0
  178. package/test/ng/directive/href.spec.js +143 -0
  179. package/test/ng/directive/if.spec.js +402 -0
  180. package/test/ng/directive/include.spec.js +828 -0
  181. package/test/ng/directive/init.spec.js +68 -0
  182. package/test/ng/directive/input.spec.js +3810 -0
  183. package/test/ng/directive/list.spec.js +170 -0
  184. package/test/ng/directive/model-options.spec.js +1008 -0
  185. package/test/ng/directive/model.spec.js +1905 -0
  186. package/test/ng/directive/non-bindable.spec.js +55 -0
  187. package/test/ng/directive/options.spec.js +3583 -0
  188. package/test/ng/directive/ref.spec.js +575 -0
  189. package/test/ng/directive/repeat.spec.js +1675 -0
  190. package/test/ng/directive/script.spec.js +52 -0
  191. package/test/ng/directive/scrset.spec.js +67 -0
  192. package/test/ng/directive/select.spec.js +2541 -0
  193. package/test/ng/directive/show-hide.spec.js +253 -0
  194. package/test/ng/directive/src.spec.js +157 -0
  195. package/test/ng/directive/style.spec.js +178 -0
  196. package/test/ng/directive/switch.spec.js +647 -0
  197. package/test/ng/directive/validators.spec.js +717 -0
  198. package/test/ng/document.spec.js +52 -0
  199. package/test/ng/filter/filter.spec.js +714 -0
  200. package/test/ng/filter/filters.spec.js +35 -0
  201. package/test/ng/filter/limit-to.spec.js +251 -0
  202. package/test/ng/filter/order-by.spec.js +891 -0
  203. package/test/ng/filter.spec.js +149 -0
  204. package/test/ng/http-backend.spec.js +398 -0
  205. package/test/ng/http.spec.js +4071 -0
  206. package/test/ng/interpolate.spec.js +642 -0
  207. package/test/ng/interval.spec.js +343 -0
  208. package/test/ng/location.spec.js +3488 -0
  209. package/test/ng/on.spec.js +229 -0
  210. package/test/ng/parse.spec.js +4655 -0
  211. package/test/ng/prop.spec.js +805 -0
  212. package/test/ng/q.spec.js +2904 -0
  213. package/test/ng/root-element.spec.js +16 -0
  214. package/test/ng/sanitize-uri.spec.js +249 -0
  215. package/test/ng/sce.spec.js +660 -0
  216. package/test/ng/scope.spec.js +3442 -0
  217. package/test/ng/template-request.spec.js +236 -0
  218. package/test/ng/timeout.spec.js +351 -0
  219. package/test/ng/url-utils.spec.js +156 -0
  220. package/test/ng/utils.spec.js +144 -0
  221. package/test/original-test.html +21 -0
  222. package/test/public.spec.js +34 -0
  223. package/test/sanitize/bing-html.spec.js +36 -0
  224. package/test/server/express.js +158 -0
  225. package/test/test-utils.js +11 -0
  226. package/tsconfig.json +17 -0
  227. package/types/angular.d.ts +138 -0
  228. package/types/global.d.ts +9 -0
  229. package/types/index.d.ts +2357 -0
  230. package/types/jqlite.d.ts +558 -0
  231. package/vite.config.js +14 -0
@@ -0,0 +1,1356 @@
1
+ import {
2
+ DIRTY_CLASS,
3
+ EMPTY_CLASS,
4
+ NOT_EMPTY_CLASS,
5
+ PRISTINE_CLASS,
6
+ TOUCHED_CLASS,
7
+ UNTOUCHED_CLASS,
8
+ VALID_CLASS,
9
+ } from "../constants";
10
+ import {
11
+ minErr,
12
+ forEach,
13
+ isNumber,
14
+ isNumberNaN,
15
+ isPromiseLike,
16
+ isUndefined,
17
+ isFunction,
18
+ } from "../core/utils";
19
+ import { addSetValidityMethod, nullFormCtrl, setupValidity } from "./form";
20
+ import { defaultModelOptions } from "./ngModelOptions";
21
+ import { startingTag } from "../jqLite";
22
+ export const ngModelMinErr = minErr("ngModel");
23
+
24
+ /**
25
+ * @ngdoc type
26
+ * @name ngModel.NgModelController
27
+ * @property {*} $viewValue The actual value from the control's view. For `input` elements, this is a
28
+ * String. See {@link ngModel.NgModelController#$setViewValue} for information about when the $viewValue
29
+ * is set.
30
+ *
31
+ * @property {*} $modelValue The value in the model that the control is bound to.
32
+ *
33
+ * @property {Array.<Function>} $parsers Array of functions to execute, as a pipeline, whenever
34
+ * the control updates the ngModelController with a new {@link ngModel.NgModelController#$viewValue
35
+ `$viewValue`} from the DOM, usually via user input.
36
+ See {@link ngModel.NgModelController#$setViewValue `$setViewValue()`} for a detailed lifecycle explanation.
37
+ Note that the `$parsers` are not called when the bound ngModel expression changes programmatically.
38
+
39
+ The functions are called in array order, each passing
40
+ its return value through to the next. The last return value is forwarded to the
41
+ {@link ngModel.NgModelController#$validators `$validators`} collection.
42
+
43
+ Parsers are used to sanitize / convert the {@link ngModel.NgModelController#$viewValue
44
+ `$viewValue`}.
45
+
46
+ Returning `undefined` from a parser means a parse error occurred. In that case,
47
+ no {@link ngModel.NgModelController#$validators `$validators`} will run and the `ngModel`
48
+ will be set to `undefined` unless {@link ngModelOptions `ngModelOptions.allowInvalid`}
49
+ is set to `true`. The parse error is stored in `ngModel.$error.parse`.
50
+
51
+ This simple example shows a parser that would convert text input value to lowercase:
52
+ * ```js
53
+ * function parse(value) {
54
+ * if (value) {
55
+ * return value.toLowerCase();
56
+ * }
57
+ * }
58
+ * ngModelController.$parsers.push(parse);
59
+ * ```
60
+
61
+ *
62
+ * @property {Array.<Function>} $formatters Array of functions to execute, as a pipeline, whenever
63
+ the bound ngModel expression changes programmatically. The `$formatters` are not called when the
64
+ value of the control is changed by user interaction.
65
+
66
+ Formatters are used to format / convert the {@link ngModel.NgModelController#$modelValue
67
+ `$modelValue`} for display in the control.
68
+
69
+ The functions are called in reverse array order, each passing the value through to the
70
+ next. The last return value is used as the actual DOM value.
71
+
72
+ This simple example shows a formatter that would convert the model value to uppercase:
73
+
74
+ * ```js
75
+ * function format(value) {
76
+ * if (value) {
77
+ * return value.toUpperCase();
78
+ * }
79
+ * }
80
+ * ngModel.$formatters.push(format);
81
+ * ```
82
+ *
83
+ * @property {Object.<string, function>} $validators A collection of validators that are applied
84
+ * whenever the model value changes. The key value within the object refers to the name of the
85
+ * validator while the function refers to the validation operation. The validation operation is
86
+ * provided with the model value as an argument and must return a true or false value depending
87
+ * on the response of that validation.
88
+ *
89
+ * ```js
90
+ * ngModel.$validators.validCharacters = function(modelValue, viewValue) {
91
+ * let value = modelValue || viewValue;
92
+ * return /[0-9]+/.test(value) &&
93
+ * /[a-z]+/.test(value) &&
94
+ * /[A-Z]+/.test(value) &&
95
+ * /\W+/.test(value);
96
+ * };
97
+ * ```
98
+ *
99
+ * @property {Object.<string, function>} $asyncValidators A collection of validations that are expected to
100
+ * perform an asynchronous validation (e.g. a HTTP request). The validation function that is provided
101
+ * is expected to return a promise when it is run during the model validation process. Once the promise
102
+ * is delivered then the validation status will be set to true when fulfilled and false when rejected.
103
+ * When the asynchronous validators are triggered, each of the validators will run in parallel and the model
104
+ * value will only be updated once all validators have been fulfilled. As long as an asynchronous validator
105
+ * is unfulfilled, its key will be added to the controllers `$pending` property. Also, all asynchronous validators
106
+ * will only run once all synchronous validators have passed.
107
+ *
108
+ * Please note that if $http is used then it is important that the server returns a success HTTP response code
109
+ * in order to fulfill the validation and a status level of `4xx` in order to reject the validation.
110
+ *
111
+ * ```js
112
+ * ngModel.$asyncValidators.uniqueUsername = function(modelValue, viewValue) {
113
+ * let value = modelValue || viewValue;
114
+ *
115
+ * // Lookup user by username
116
+ * return $http.get('/api/users/' + value).
117
+ * then(function resolved() {
118
+ * //username exists, this means validation fails
119
+ * return $q.reject('exists');
120
+ * }, function rejected() {
121
+ * //username does not exist, therefore this validation passes
122
+ * return true;
123
+ * });
124
+ * };
125
+ * ```
126
+ *
127
+ * @property {Array.<Function>} $viewChangeListeners Array of functions to execute whenever
128
+ * a change to {@link ngModel.NgModelController#$viewValue `$viewValue`} has caused a change
129
+ * to {@link ngModel.NgModelController#$modelValue `$modelValue`}.
130
+ * It is called with no arguments, and its return value is ignored.
131
+ * This can be used in place of additional $watches against the model value.
132
+ *
133
+ * @property {Object} $error An object hash with all failing validator ids as keys.
134
+ * @property {Object} $pending An object hash with all pending validator ids as keys.
135
+ *
136
+ * @property {boolean} $untouched True if control has not lost focus yet.
137
+ * @property {boolean} $touched True if control has lost focus.
138
+ * @property {boolean} $pristine True if user has not interacted with the control yet.
139
+ * @property {boolean} $dirty True if user has already interacted with the control.
140
+ * @property {boolean} $valid True if there is no error.
141
+ * @property {boolean} $invalid True if at least one error on the control.
142
+ * @property {string} $name The name attribute of the control.
143
+ *
144
+ * @description
145
+ *
146
+ * `NgModelController` provides API for the {@link ngModel `ngModel`} directive.
147
+ * The controller contains services for data-binding, validation, CSS updates, and value formatting
148
+ * and parsing. It purposefully does not contain any logic which deals with DOM rendering or
149
+ * listening to DOM events.
150
+ * Such DOM related logic should be provided by other directives which make use of
151
+ * `NgModelController` for data-binding to control elements.
152
+ * AngularJS provides this DOM logic for most {@link input `input`} elements.
153
+ * At the end of this page you can find a {@link ngModel.NgModelController#custom-control-example
154
+ * custom control example} that uses `ngModelController` to bind to `contenteditable` elements.
155
+ *
156
+ * @example
157
+ * ### Custom Control Example
158
+ * This example shows how to use `NgModelController` with a custom control to achieve
159
+ * data-binding. Notice how different directives (`contenteditable`, `ng-model`, and `required`)
160
+ * collaborate together to achieve the desired result.
161
+ *
162
+ * `contenteditable` is an HTML5 attribute, which tells the browser to let the element
163
+ * contents be edited in place by the user.
164
+ *
165
+ * We are using the {@link ng.service:$sce $sce} service here and include the {@link ngSanitize $sanitize}
166
+ * module to automatically remove "bad" content like inline event listener (e.g. `<span onclick="...">`).
167
+ * However, as we are using `$sce` the model can still decide to provide unsafe content if it marks
168
+ * that content using the `$sce` service.
169
+ *
170
+ */
171
+ NgModelController.$inject = [
172
+ "$scope",
173
+ "$exceptionHandler",
174
+ "$attrs",
175
+ "$element",
176
+ "$parse",
177
+ "$animate",
178
+ "$timeout",
179
+ "$q",
180
+ "$interpolate",
181
+ ];
182
+ export function NgModelController(
183
+ $scope,
184
+ $exceptionHandler,
185
+ $attr,
186
+ $element,
187
+ $parse,
188
+ $animate,
189
+ $timeout,
190
+ $q,
191
+ $interpolate,
192
+ ) {
193
+ this.$viewValue = Number.NaN;
194
+ this.$modelValue = Number.NaN;
195
+ this.$$rawModelValue = undefined; // stores the parsed modelValue / model set from scope regardless of validity.
196
+ this.$validators = {};
197
+ this.$asyncValidators = {};
198
+ this.$parsers = [];
199
+ this.$formatters = [];
200
+ this.$viewChangeListeners = [];
201
+ this.$untouched = true;
202
+ this.$touched = false;
203
+ this.$pristine = true;
204
+ this.$dirty = false;
205
+ this.$valid = true;
206
+ this.$invalid = false;
207
+ this.$error = {}; // keep invalid keys here
208
+ this.$$success = {}; // keep valid keys here
209
+ this.$pending = undefined; // keep pending keys here
210
+ this.$name = $interpolate($attr.name || "", false)($scope);
211
+ this.$$parentForm = nullFormCtrl;
212
+ this.$options = defaultModelOptions;
213
+ this.$$updateEvents = "";
214
+ // Attach the correct context to the event handler function for updateOn
215
+ this.$$updateEventHandler = this.$$updateEventHandler.bind(this);
216
+
217
+ this.$$parsedNgModel = $parse($attr.ngModel);
218
+ this.$$parsedNgModelAssign = this.$$parsedNgModel.assign;
219
+ this.$$ngModelGet = this.$$parsedNgModel;
220
+ this.$$ngModelSet = this.$$parsedNgModelAssign;
221
+ this.$$pendingDebounce = null;
222
+ this.$$parserValid = undefined;
223
+ this.$$parserName = "parse";
224
+
225
+ this.$$currentValidationRunId = 0;
226
+
227
+ this.$$scope = $scope;
228
+ this.$$rootScope = $scope.$root;
229
+ this.$$attr = $attr;
230
+ this.$$element = $element;
231
+ this.$$animate = $animate;
232
+ this.$$timeout = $timeout;
233
+ this.$$parse = $parse;
234
+ this.$$q = $q;
235
+ this.$$exceptionHandler = $exceptionHandler;
236
+
237
+ setupValidity(this);
238
+ setupModelWatcher(this);
239
+ }
240
+
241
+ NgModelController.prototype = {
242
+ $$initGetterSetters() {
243
+ if (this.$options.getOption("getterSetter")) {
244
+ const invokeModelGetter = this.$$parse(`${this.$$attr.ngModel}()`);
245
+ const invokeModelSetter = this.$$parse(`${this.$$attr.ngModel}($$$p)`);
246
+
247
+ this.$$ngModelGet = function ($scope) {
248
+ let modelValue = this.$$parsedNgModel($scope);
249
+ if (isFunction(modelValue)) {
250
+ modelValue = invokeModelGetter($scope);
251
+ }
252
+ return modelValue;
253
+ };
254
+ this.$$ngModelSet = function ($scope, newValue) {
255
+ if (isFunction(this.$$parsedNgModel($scope))) {
256
+ invokeModelSetter($scope, { $$$p: newValue });
257
+ } else {
258
+ this.$$parsedNgModelAssign($scope, newValue);
259
+ }
260
+ };
261
+ } else if (!this.$$parsedNgModel.assign) {
262
+ throw ngModelMinErr(
263
+ "nonassign",
264
+ "Expression '{0}' is non-assignable. Element: {1}",
265
+ this.$$attr.ngModel,
266
+ startingTag(this.$$element),
267
+ );
268
+ }
269
+ },
270
+
271
+ /**
272
+ * @ngdoc method
273
+ * @name ngModel.NgModelController#$render
274
+ *
275
+ * @description
276
+ * Called when the view needs to be updated. It is expected that the user of the ng-model
277
+ * directive will implement this method.
278
+ *
279
+ * The `$render()` method is invoked in the following situations:
280
+ *
281
+ * * `$rollbackViewValue()` is called. If we are rolling back the view value to the last
282
+ * committed value then `$render()` is called to update the input control.
283
+ * * The value referenced by `ng-model` is changed programmatically and both the `$modelValue` and
284
+ * the `$viewValue` are different from last time.
285
+ *
286
+ * Since `ng-model` does not do a deep watch, `$render()` is only invoked if the values of
287
+ * `$modelValue` and `$viewValue` are actually different from their previous values. If `$modelValue`
288
+ * or `$viewValue` are objects (rather than a string or number) then `$render()` will not be
289
+ * invoked if you only change a property on the objects.
290
+ */
291
+ $render: () => {},
292
+
293
+ /**
294
+ * @ngdoc method
295
+ * @name ngModel.NgModelController#$isEmpty
296
+ *
297
+ * @description
298
+ * This is called when we need to determine if the value of an input is empty.
299
+ *
300
+ * For instance, the required directive does this to work out if the input has data or not.
301
+ *
302
+ * The default `$isEmpty` function checks whether the value is `undefined`, `''`, `null` or `NaN`.
303
+ *
304
+ * You can override this for input directives whose concept of being empty is different from the
305
+ * default. The `checkboxInputType` directive does this because in its case a value of `false`
306
+ * implies empty.
307
+ *
308
+ * @param {*} value The value of the input to check for emptiness.
309
+ * @returns {boolean} True if `value` is "empty".
310
+ */
311
+ $isEmpty(value) {
312
+ // eslint-disable-next-line no-self-compare
313
+ return (
314
+ isUndefined(value) || value === "" || value === null || value !== value
315
+ );
316
+ },
317
+
318
+ $$updateEmptyClasses(value) {
319
+ if (this.$isEmpty(value)) {
320
+ this.$$animate.removeClass(this.$$element, NOT_EMPTY_CLASS);
321
+ this.$$animate.addClass(this.$$element, EMPTY_CLASS);
322
+ } else {
323
+ this.$$animate.removeClass(this.$$element, EMPTY_CLASS);
324
+ this.$$animate.addClass(this.$$element, NOT_EMPTY_CLASS);
325
+ }
326
+ },
327
+
328
+ /**
329
+ * @ngdoc method
330
+ * @name ngModel.NgModelController#$setPristine
331
+ *
332
+ * @description
333
+ * Sets the control to its pristine state.
334
+ *
335
+ * This method can be called to remove the `ng-dirty` class and set the control to its pristine
336
+ * state (`ng-pristine` class). A model is considered to be pristine when the control
337
+ * has not been changed from when first compiled.
338
+ */
339
+ $setPristine() {
340
+ this.$dirty = false;
341
+ this.$pristine = true;
342
+ this.$$animate.removeClass(this.$$element, DIRTY_CLASS);
343
+ this.$$animate.addClass(this.$$element, PRISTINE_CLASS);
344
+ },
345
+
346
+ /**
347
+ * @ngdoc method
348
+ * @name ngModel.NgModelController#$setDirty
349
+ *
350
+ * @description
351
+ * Sets the control to its dirty state.
352
+ *
353
+ * This method can be called to remove the `ng-pristine` class and set the control to its dirty
354
+ * state (`ng-dirty` class). A model is considered to be dirty when the control has been changed
355
+ * from when first compiled.
356
+ */
357
+ $setDirty() {
358
+ this.$dirty = true;
359
+ this.$pristine = false;
360
+ this.$$animate.removeClass(this.$$element, PRISTINE_CLASS);
361
+ this.$$animate.addClass(this.$$element, DIRTY_CLASS);
362
+ this.$$parentForm.$setDirty();
363
+ },
364
+
365
+ /**
366
+ * @ngdoc method
367
+ * @name ngModel.NgModelController#$setUntouched
368
+ *
369
+ * @description
370
+ * Sets the control to its untouched state.
371
+ *
372
+ * This method can be called to remove the `ng-touched` class and set the control to its
373
+ * untouched state (`ng-untouched` class). Upon compilation, a model is set as untouched
374
+ * by default, however this function can be used to restore that state if the model has
375
+ * already been touched by the user.
376
+ */
377
+ $setUntouched() {
378
+ this.$touched = false;
379
+ this.$untouched = true;
380
+ this.$$animate.setClass(this.$$element, UNTOUCHED_CLASS, TOUCHED_CLASS);
381
+ },
382
+
383
+ /**
384
+ * @ngdoc method
385
+ * @name ngModel.NgModelController#$setTouched
386
+ *
387
+ * @description
388
+ * Sets the control to its touched state.
389
+ *
390
+ * This method can be called to remove the `ng-untouched` class and set the control to its
391
+ * touched state (`ng-touched` class). A model is considered to be touched when the user has
392
+ * first focused the control element and then shifted focus away from the control (blur event).
393
+ */
394
+ $setTouched() {
395
+ this.$touched = true;
396
+ this.$untouched = false;
397
+ this.$$animate.setClass(this.$$element, TOUCHED_CLASS, UNTOUCHED_CLASS);
398
+ },
399
+
400
+ /**
401
+ * @ngdoc method
402
+ * @name ngModel.NgModelController#$rollbackViewValue
403
+ *
404
+ * @description
405
+ * Cancel an update and reset the input element's value to prevent an update to the `$modelValue`,
406
+ * which may be caused by a pending debounced event or because the input is waiting for some
407
+ * future event.
408
+ *
409
+ * If you have an input that uses `ng-model-options` to set up debounced updates or updates that
410
+ * depend on special events such as `blur`, there can be a period when the `$viewValue` is out of
411
+ * sync with the ngModel's `$modelValue`.
412
+ *
413
+ * In this case, you can use `$rollbackViewValue()` to manually cancel the debounced / future update
414
+ * and reset the input to the last committed view value.
415
+ *
416
+ * It is also possible that you run into difficulties if you try to update the ngModel's `$modelValue`
417
+ * programmatically before these debounced/future events have resolved/occurred, because AngularJS's
418
+ * dirty checking mechanism is not able to tell whether the model has actually changed or not.
419
+ *
420
+ * The `$rollbackViewValue()` method should be called before programmatically changing the model of an
421
+ * input which may have such events pending. This is important in order to make sure that the
422
+ * input field will be updated with the new model value and any pending operations are cancelled.
423
+ *
424
+ * @example
425
+ * <example name="ng-model-cancel-update" module="cancel-update-example">
426
+ * <file name="app.js">
427
+ * angular.module('cancel-update-example', [])
428
+ *
429
+ * .controller('CancelUpdateController', ['$scope', function($scope) {
430
+ * $scope.model = {value1: '', value2: ''};
431
+ *
432
+ * $scope.setEmpty = function(e, value, rollback) {
433
+ * if (e.keyCode === 27) {
434
+ * e.preventDefault();
435
+ * if (rollback) {
436
+ * $scope.myForm[value].$rollbackViewValue();
437
+ * }
438
+ * $scope.model[value] = '';
439
+ * }
440
+ * };
441
+ * }]);
442
+ * </file>
443
+ * <file name="index.html">
444
+ * <div ng-controller="CancelUpdateController">
445
+ * <p>Both of these inputs are only updated if they are blurred. Hitting escape should
446
+ * empty them. Follow these steps and observe the difference:</p>
447
+ * <ol>
448
+ * <li>Type something in the input. You will see that the model is not yet updated</li>
449
+ * <li>Press the Escape key.
450
+ * <ol>
451
+ * <li> In the first example, nothing happens, because the model is already '', and no
452
+ * update is detected. If you blur the input, the model will be set to the current view.
453
+ * </li>
454
+ * <li> In the second example, the pending update is cancelled, and the input is set back
455
+ * to the last committed view value (''). Blurring the input does nothing.
456
+ * </li>
457
+ * </ol>
458
+ * </li>
459
+ * </ol>
460
+ *
461
+ * <form name="myForm" ng-model-options="{ updateOn: 'blur' }">
462
+ * <div>
463
+ * <p id="inputDescription1">Without $rollbackViewValue():</p>
464
+ * <input name="value1" aria-describedby="inputDescription1" ng-model="model.value1"
465
+ * ng-keydown="setEmpty($event, 'value1')">
466
+ * value1: "{{ model.value1 }}"
467
+ * </div>
468
+ *
469
+ * <div>
470
+ * <p id="inputDescription2">With $rollbackViewValue():</p>
471
+ * <input name="value2" aria-describedby="inputDescription2" ng-model="model.value2"
472
+ * ng-keydown="setEmpty($event, 'value2', true)">
473
+ * value2: "{{ model.value2 }}"
474
+ * </div>
475
+ * </form>
476
+ * </div>
477
+ * </file>
478
+ <file name="style.css">
479
+ div {
480
+ display: table-cell;
481
+ }
482
+ div:nth-child(1) {
483
+ padding-right: 30px;
484
+ }
485
+
486
+ </file>
487
+ * </example>
488
+ */
489
+ $rollbackViewValue() {
490
+ this.$$timeout.cancel(this.$$pendingDebounce);
491
+ this.$viewValue = this.$$lastCommittedViewValue;
492
+ this.$render();
493
+ },
494
+
495
+ /**
496
+ * @ngdoc method
497
+ * @name ngModel.NgModelController#$validate
498
+ *
499
+ * @description
500
+ * Runs each of the registered validators (first synchronous validators and then
501
+ * asynchronous validators).
502
+ * If the validity changes to invalid, the model will be set to `undefined`,
503
+ * unless {@link ngModelOptions `ngModelOptions.allowInvalid`} is `true`.
504
+ * If the validity changes to valid, it will set the model to the last available valid
505
+ * `$modelValue`, i.e. either the last parsed value or the last value set from the scope.
506
+ */
507
+ $validate() {
508
+ // ignore $validate before model is initialized
509
+ if (isNumberNaN(this.$modelValue)) {
510
+ return;
511
+ }
512
+
513
+ const viewValue = this.$$lastCommittedViewValue;
514
+ // Note: we use the $$rawModelValue as $modelValue might have been
515
+ // set to undefined during a view -> model update that found validation
516
+ // errors. We can't parse the view here, since that could change
517
+ // the model although neither viewValue nor the model on the scope changed
518
+ const modelValue = this.$$rawModelValue;
519
+
520
+ const prevValid = this.$valid;
521
+ const prevModelValue = this.$modelValue;
522
+
523
+ const allowInvalid = this.$options.getOption("allowInvalid");
524
+
525
+ const that = this;
526
+ this.$$runValidators(modelValue, viewValue, (allValid) => {
527
+ // If there was no change in validity, don't update the model
528
+ // This prevents changing an invalid modelValue to undefined
529
+ if (!allowInvalid && prevValid !== allValid) {
530
+ // Note: Don't check this.$valid here, as we could have
531
+ // external validators (e.g. calculated on the server),
532
+ // that just call $setValidity and need the model value
533
+ // to calculate their validity.
534
+ that.$modelValue = allValid ? modelValue : undefined;
535
+
536
+ if (that.$modelValue !== prevModelValue) {
537
+ that.$$writeModelToScope();
538
+ }
539
+ }
540
+ });
541
+ },
542
+
543
+ $$runValidators(modelValue, viewValue, doneCallback) {
544
+ this.$$currentValidationRunId++;
545
+ const localValidationRunId = this.$$currentValidationRunId;
546
+ const that = this;
547
+
548
+ // check parser error
549
+ if (!processParseErrors()) {
550
+ validationDone(false);
551
+ return;
552
+ }
553
+ if (!processSyncValidators()) {
554
+ validationDone(false);
555
+ return;
556
+ }
557
+ processAsyncValidators();
558
+
559
+ function processParseErrors() {
560
+ const errorKey = that.$$parserName;
561
+
562
+ if (isUndefined(that.$$parserValid)) {
563
+ setValidity(errorKey, null);
564
+ } else {
565
+ if (!that.$$parserValid) {
566
+ forEach(that.$validators, (v, name) => {
567
+ setValidity(name, null);
568
+ });
569
+ forEach(that.$asyncValidators, (v, name) => {
570
+ setValidity(name, null);
571
+ });
572
+ }
573
+
574
+ // Set the parse error last, to prevent unsetting it, should a $validators key == parserName
575
+ setValidity(errorKey, that.$$parserValid);
576
+ return that.$$parserValid;
577
+ }
578
+ return true;
579
+ }
580
+
581
+ function processSyncValidators() {
582
+ let syncValidatorsValid = true;
583
+ forEach(that.$validators, (validator, name) => {
584
+ const result = Boolean(validator(modelValue, viewValue));
585
+ syncValidatorsValid = syncValidatorsValid && result;
586
+ setValidity(name, result);
587
+ });
588
+ if (!syncValidatorsValid) {
589
+ forEach(that.$asyncValidators, (v, name) => {
590
+ setValidity(name, null);
591
+ });
592
+ return false;
593
+ }
594
+ return true;
595
+ }
596
+
597
+ function processAsyncValidators() {
598
+ const validatorPromises = [];
599
+ let allValid = true;
600
+ forEach(that.$asyncValidators, (validator, name) => {
601
+ const promise = validator(modelValue, viewValue);
602
+ if (!isPromiseLike(promise)) {
603
+ throw ngModelMinErr(
604
+ "nopromise",
605
+ "Expected asynchronous validator to return a promise but got '{0}' instead.",
606
+ promise,
607
+ );
608
+ }
609
+ setValidity(name, undefined);
610
+ validatorPromises.push(
611
+ promise.then(
612
+ () => {
613
+ setValidity(name, true);
614
+ },
615
+ () => {
616
+ allValid = false;
617
+ setValidity(name, false);
618
+ },
619
+ ),
620
+ );
621
+ });
622
+ if (!validatorPromises.length) {
623
+ validationDone(true);
624
+ } else {
625
+ that.$$q.all(validatorPromises).then(
626
+ () => {
627
+ validationDone(allValid);
628
+ },
629
+ () => {},
630
+ );
631
+ }
632
+ }
633
+
634
+ function setValidity(name, isValid) {
635
+ if (localValidationRunId === that.$$currentValidationRunId) {
636
+ that.$setValidity(name, isValid);
637
+ }
638
+ }
639
+
640
+ function validationDone(allValid) {
641
+ if (localValidationRunId === that.$$currentValidationRunId) {
642
+ doneCallback(allValid);
643
+ }
644
+ }
645
+ },
646
+
647
+ /**
648
+ * @ngdoc method
649
+ * @name ngModel.NgModelController#$commitViewValue
650
+ *
651
+ * @description
652
+ * Commit a pending update to the `$modelValue`.
653
+ *
654
+ * Updates may be pending by a debounced event or because the input is waiting for a some future
655
+ * event defined in `ng-model-options`. this method is rarely needed as `NgModelController`
656
+ * usually handles calling this in response to input events.
657
+ */
658
+ $commitViewValue() {
659
+ const viewValue = this.$viewValue;
660
+
661
+ this.$$timeout.cancel(this.$$pendingDebounce);
662
+
663
+ // If the view value has not changed then we should just exit, except in the case where there is
664
+ // a native validator on the element. In this case the validation state may have changed even though
665
+ // the viewValue has stayed empty.
666
+ if (
667
+ this.$$lastCommittedViewValue === viewValue &&
668
+ (viewValue !== "" || !this.$$hasNativeValidators)
669
+ ) {
670
+ return;
671
+ }
672
+
673
+ if (
674
+ this.$$lastCommittedViewValue === undefined &&
675
+ Number.isNaN(viewValue)
676
+ ) {
677
+ return;
678
+ }
679
+
680
+ this.$$updateEmptyClasses(viewValue);
681
+ this.$$lastCommittedViewValue = viewValue;
682
+
683
+ // change to dirty
684
+ if (this.$pristine) {
685
+ this.$setDirty();
686
+ }
687
+ this.$$parseAndValidate();
688
+ },
689
+
690
+ $$parseAndValidate() {
691
+ const viewValue = this.$$lastCommittedViewValue;
692
+ let modelValue = viewValue;
693
+ const that = this;
694
+
695
+ this.$$parserValid = isUndefined(modelValue) ? undefined : true;
696
+
697
+ // Reset any previous parse error
698
+ this.$setValidity(this.$$parserName, null);
699
+ this.$$parserName = "parse";
700
+
701
+ if (this.$$parserValid) {
702
+ for (let i = 0; i < this.$parsers.length; i++) {
703
+ modelValue = this.$parsers[i](modelValue);
704
+ if (isUndefined(modelValue)) {
705
+ this.$$parserValid = false;
706
+ break;
707
+ }
708
+ }
709
+ }
710
+ if (isNumberNaN(this.$modelValue)) {
711
+ // this.$modelValue has not been touched yet...
712
+ this.$modelValue = this.$$ngModelGet(this.$$scope);
713
+ }
714
+ const prevModelValue = this.$modelValue;
715
+ const allowInvalid = this.$options.getOption("allowInvalid");
716
+ this.$$rawModelValue = modelValue;
717
+
718
+ if (allowInvalid) {
719
+ this.$modelValue = modelValue;
720
+ writeToModelIfNeeded();
721
+ }
722
+
723
+ // Pass the $$lastCommittedViewValue here, because the cached viewValue might be out of date.
724
+ // This can happen if e.g. $setViewValue is called from inside a parser
725
+ this.$$runValidators(
726
+ modelValue,
727
+ this.$$lastCommittedViewValue,
728
+ (allValid) => {
729
+ if (!allowInvalid) {
730
+ // Note: Don't check this.$valid here, as we could have
731
+ // external validators (e.g. calculated on the server),
732
+ // that just call $setValidity and need the model value
733
+ // to calculate their validity.
734
+ that.$modelValue = allValid ? modelValue : undefined;
735
+ writeToModelIfNeeded();
736
+ }
737
+ },
738
+ );
739
+
740
+ function writeToModelIfNeeded() {
741
+ if (that.$modelValue !== prevModelValue) {
742
+ that.$$writeModelToScope();
743
+ }
744
+ }
745
+ },
746
+
747
+ $$writeModelToScope() {
748
+ this.$$ngModelSet(this.$$scope, this.$modelValue);
749
+ forEach(
750
+ this.$viewChangeListeners,
751
+ function (listener) {
752
+ try {
753
+ listener();
754
+ } catch (e) {
755
+ // eslint-disable-next-line no-invalid-this
756
+ this.$$exceptionHandler(e);
757
+ }
758
+ },
759
+ this,
760
+ );
761
+ },
762
+
763
+ /**
764
+ * @ngdoc method
765
+ * @name ngModel.NgModelController#$setViewValue
766
+ *
767
+ * @description
768
+ * Update the view value.
769
+ *
770
+ * This method should be called when a control wants to change the view value; typically,
771
+ * this is done from within a DOM event handler. For example, the {@link ng.directive:input input}
772
+ * directive calls it when the value of the input changes and {@link ng.directive:select select}
773
+ * calls it when an option is selected.
774
+ *
775
+ * When `$setViewValue` is called, the new `value` will be staged for committing through the `$parsers`
776
+ * and `$validators` pipelines. If there are no special {@link ngModelOptions} specified then the staged
777
+ * value is sent directly for processing through the `$parsers` pipeline. After this, the `$validators` and
778
+ * `$asyncValidators` are called and the value is applied to `$modelValue`.
779
+ * Finally, the value is set to the **expression** specified in the `ng-model` attribute and
780
+ * all the registered change listeners, in the `$viewChangeListeners` list are called.
781
+ *
782
+ * In case the {@link ng.directive:ngModelOptions ngModelOptions} directive is used with `updateOn`
783
+ * and the `default` trigger is not listed, all those actions will remain pending until one of the
784
+ * `updateOn` events is triggered on the DOM element.
785
+ * All these actions will be debounced if the {@link ng.directive:ngModelOptions ngModelOptions}
786
+ * directive is used with a custom debounce for this particular event.
787
+ * Note that a `$digest` is only triggered once the `updateOn` events are fired, or if `debounce`
788
+ * is specified, once the timer runs out.
789
+ *
790
+ * When used with standard inputs, the view value will always be a string (which is in some cases
791
+ * parsed into another type, such as a `Date` object for `input[date]`.)
792
+ * However, custom controls might also pass objects to this method. In this case, we should make
793
+ * a copy of the object before passing it to `$setViewValue`. This is because `ngModel` does not
794
+ * perform a deep watch of objects, it only looks for a change of identity. If you only change
795
+ * the property of the object then ngModel will not realize that the object has changed and
796
+ * will not invoke the `$parsers` and `$validators` pipelines. For this reason, you should
797
+ * not change properties of the copy once it has been passed to `$setViewValue`.
798
+ * Otherwise you may cause the model value on the scope to change incorrectly.
799
+ *
800
+ * <div class="alert alert-info">
801
+ * In any case, the value passed to the method should always reflect the current value
802
+ * of the control. For example, if you are calling `$setViewValue` for an input element,
803
+ * you should pass the input DOM value. Otherwise, the control and the scope model become
804
+ * out of sync. It's also important to note that `$setViewValue` does not call `$render` or change
805
+ * the control's DOM value in any way. If we want to change the control's DOM value
806
+ * programmatically, we should update the `ngModel` scope expression. Its new value will be
807
+ * picked up by the model controller, which will run it through the `$formatters`, `$render` it
808
+ * to update the DOM, and finally call `$validate` on it.
809
+ * </div>
810
+ *
811
+ * @param {*} value value from the view.
812
+ * @param {string} trigger Event that triggered the update.
813
+ */
814
+ $setViewValue(value, trigger) {
815
+ this.$viewValue = value;
816
+ if (this.$options.getOption("updateOnDefault")) {
817
+ this.$$debounceViewValueCommit(trigger);
818
+ }
819
+ },
820
+
821
+ $$debounceViewValueCommit(trigger) {
822
+ let debounceDelay = this.$options.getOption("debounce");
823
+
824
+ if (isNumber(debounceDelay[trigger])) {
825
+ debounceDelay = debounceDelay[trigger];
826
+ } else if (
827
+ isNumber(debounceDelay.default) &&
828
+ this.$options.getOption("updateOn").indexOf(trigger) === -1
829
+ ) {
830
+ debounceDelay = debounceDelay.default;
831
+ } else if (isNumber(debounceDelay["*"])) {
832
+ debounceDelay = debounceDelay["*"];
833
+ }
834
+
835
+ this.$$timeout.cancel(this.$$pendingDebounce);
836
+ const that = this;
837
+ if (debounceDelay > 0) {
838
+ // this fails if debounceDelay is an object
839
+ this.$$pendingDebounce = this.$$timeout(() => {
840
+ that.$commitViewValue();
841
+ }, debounceDelay);
842
+ } else if (this.$$rootScope.$$phase) {
843
+ this.$commitViewValue();
844
+ } else {
845
+ this.$$scope.$apply(() => {
846
+ that.$commitViewValue();
847
+ });
848
+ }
849
+ },
850
+
851
+ /**
852
+ * @ngdoc method
853
+ *
854
+ * @name ngModel.NgModelController#$overrideModelOptions
855
+ *
856
+ * @description
857
+ *
858
+ * Override the current model options settings programmatically.
859
+ *
860
+ * The previous `ModelOptions` value will not be modified. Instead, a
861
+ * new `ModelOptions` object will inherit from the previous one overriding
862
+ * or inheriting settings that are defined in the given parameter.
863
+ *
864
+ * See {@link ngModelOptions} for information about what options can be specified
865
+ * and how model option inheritance works.
866
+ *
867
+ * <div class="alert alert-warning">
868
+ * **Note:** this function only affects the options set on the `ngModelController`,
869
+ * and not the options on the {@link ngModelOptions} directive from which they might have been
870
+ * obtained initially.
871
+ * </div>
872
+ *
873
+ * <div class="alert alert-danger">
874
+ * **Note:** it is not possible to override the `getterSetter` option.
875
+ * </div>
876
+ *
877
+ * @param {Object} options a hash of settings to override the previous options
878
+ *
879
+ */
880
+ $overrideModelOptions(options) {
881
+ this.$options = this.$options.createChild(options);
882
+ this.$$setUpdateOnEvents();
883
+ },
884
+
885
+ /**
886
+ * @ngdoc method
887
+ *
888
+ * @name ngModel.NgModelController#$processModelValue
889
+
890
+ * @description
891
+ *
892
+ * Runs the model -> view pipeline on the current
893
+ * {@link ngModel.NgModelController#$modelValue $modelValue}.
894
+ *
895
+ * The following actions are performed by this method:
896
+ *
897
+ * - the `$modelValue` is run through the {@link ngModel.NgModelController#$formatters $formatters}
898
+ * and the result is set to the {@link ngModel.NgModelController#$viewValue $viewValue}
899
+ * - the `ng-empty` or `ng-not-empty` class is set on the element
900
+ * - if the `$viewValue` has changed:
901
+ * - {@link ngModel.NgModelController#$render $render} is called on the control
902
+ * - the {@link ngModel.NgModelController#$validators $validators} are run and
903
+ * the validation status is set.
904
+ *
905
+ * This method is called by ngModel internally when the bound scope value changes.
906
+ * Application developers usually do not have to call this function themselves.
907
+ *
908
+ * This function can be used when the `$viewValue` or the rendered DOM value are not correctly
909
+ * formatted and the `$modelValue` must be run through the `$formatters` again.
910
+ *
911
+ * @example
912
+ * Consider a text input with an autocomplete list (for fruit), where the items are
913
+ * objects with a name and an id.
914
+ * A user enters `ap` and then selects `Apricot` from the list.
915
+ * Based on this, the autocomplete widget will call `$setViewValue({name: 'Apricot', id: 443})`,
916
+ * but the rendered value will still be `ap`.
917
+ * The widget can then call `ctrl.$processModelValue()` to run the model -> view
918
+ * pipeline again, which formats the object to the string `Apricot`,
919
+ * then updates the `$viewValue`, and finally renders it in the DOM.
920
+ *
921
+ * <example module="inputExample" name="ng-model-process">
922
+ <file name="index.html">
923
+ <div ng-controller="inputController" style="display: flex;">
924
+ <div style="margin-right: 30px;">
925
+ Search Fruit:
926
+ <basic-autocomplete items="items" on-select="selectedFruit = item"></basic-autocomplete>
927
+ </div>
928
+ <div>
929
+ Model:<br>
930
+ <pre>{{selectedFruit | json}}</pre>
931
+ </div>
932
+ </div>
933
+ </file>
934
+ <file name="app.js">
935
+ angular.module('inputExample', [])
936
+ .controller('inputController', function($scope) {
937
+ $scope.items = [
938
+ {name: 'Apricot', id: 443},
939
+ {name: 'Clementine', id: 972},
940
+ {name: 'Durian', id: 169},
941
+ {name: 'Jackfruit', id: 982},
942
+ {name: 'Strawberry', id: 863}
943
+ ];
944
+ })
945
+ .component('basicAutocomplete', {
946
+ bindings: {
947
+ items: '<',
948
+ onSelect: '&'
949
+ },
950
+ templateUrl: 'autocomplete.html',
951
+ controller: function($element, $scope) {
952
+ let that = this;
953
+ let ngModel;
954
+
955
+ that.$postLink = function() {
956
+ ngModel = $element.find('input').controller('ngModel');
957
+
958
+ ngModel.$formatters.push(function(value) {
959
+ return (value && value.name) || value;
960
+ });
961
+
962
+ ngModel.$parsers.push(function(value) {
963
+ let match = value;
964
+ for (let i = 0; i < that.items.length; i++) {
965
+ if (that.items[i].name === value) {
966
+ match = that.items[i];
967
+ break;
968
+ }
969
+ }
970
+
971
+ return match;
972
+ });
973
+ };
974
+
975
+ that.selectItem = function(item) {
976
+ ngModel.$setViewValue(item);
977
+ ngModel.$processModelValue();
978
+ that.onSelect({item: item});
979
+ };
980
+ }
981
+ });
982
+ </file>
983
+ <file name="autocomplete.html">
984
+ <div>
985
+ <input type="search" ng-model="$ctrl.searchTerm" />
986
+ <ul>
987
+ <li ng-repeat="item in $ctrl.items | filter:$ctrl.searchTerm">
988
+ <button ng-click="$ctrl.selectItem(item)">{{ item.name }}</button>
989
+ </li>
990
+ </ul>
991
+ </div>
992
+ </file>
993
+ * </example>
994
+ *
995
+ */
996
+ $processModelValue() {
997
+ const viewValue = this.$$format();
998
+
999
+ if (this.$viewValue !== viewValue) {
1000
+ this.$$updateEmptyClasses(viewValue);
1001
+ this.$viewValue = this.$$lastCommittedViewValue = viewValue;
1002
+ this.$render();
1003
+ // It is possible that model and view value have been updated during render
1004
+ this.$$runValidators(this.$modelValue, this.$viewValue, () => {});
1005
+ }
1006
+ },
1007
+
1008
+ /**
1009
+ * This method is called internally to run the $formatters on the $modelValue
1010
+ */
1011
+ $$format() {
1012
+ const formatters = this.$formatters;
1013
+ let idx = formatters.length;
1014
+
1015
+ let viewValue = this.$modelValue;
1016
+ while (idx--) {
1017
+ viewValue = formatters[idx](viewValue);
1018
+ }
1019
+
1020
+ return viewValue;
1021
+ },
1022
+
1023
+ /**
1024
+ * This method is called internally when the bound scope value changes.
1025
+ */
1026
+ $$setModelValue(modelValue) {
1027
+ this.$modelValue = this.$$rawModelValue = modelValue;
1028
+ this.$$parserValid = undefined;
1029
+ this.$processModelValue();
1030
+ },
1031
+
1032
+ $$setUpdateOnEvents() {
1033
+ if (this.$$updateEvents) {
1034
+ this.$$element.off(this.$$updateEvents, this.$$updateEventHandler);
1035
+ }
1036
+
1037
+ this.$$updateEvents = this.$options.getOption("updateOn");
1038
+ if (this.$$updateEvents) {
1039
+ this.$$element.on(this.$$updateEvents, this.$$updateEventHandler);
1040
+ }
1041
+ },
1042
+
1043
+ $$updateEventHandler(ev) {
1044
+ this.$$debounceViewValueCommit(ev && ev.type);
1045
+ },
1046
+ };
1047
+
1048
+ function setupModelWatcher(ctrl) {
1049
+ // model -> value
1050
+ // Note: we cannot use a normal scope.$watch as we want to detect the following:
1051
+ // 1. scope value is 'a'
1052
+ // 2. user enters 'b'
1053
+ // 3. ng-change kicks in and reverts scope value to 'a'
1054
+ // -> scope value did not change since the last digest as
1055
+ // ng-change executes in apply phase
1056
+ // 4. view should be changed back to 'a'
1057
+ ctrl.$$scope.$watch((scope) => {
1058
+ const modelValue = ctrl.$$ngModelGet(scope);
1059
+
1060
+ // if scope model value and ngModel value are out of sync
1061
+ // This cannot be moved to the action function, because it would not catch the
1062
+ // case where the model is changed in the ngChange function or the model setter
1063
+ if (
1064
+ modelValue !== ctrl.$modelValue &&
1065
+ // checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator
1066
+ // eslint-disable-next-line no-self-compare
1067
+ (ctrl.$modelValue === ctrl.$modelValue || modelValue === modelValue)
1068
+ ) {
1069
+ ctrl.$$setModelValue(modelValue);
1070
+ }
1071
+
1072
+ return modelValue;
1073
+ });
1074
+ }
1075
+
1076
+ /**
1077
+ * @ngdoc method
1078
+ * @name ngModel.NgModelController#$setValidity
1079
+ *
1080
+ * @description
1081
+ * Change the validity state, and notify the form.
1082
+ *
1083
+ * This method can be called within $parsers/$formatters or a custom validation implementation.
1084
+ * However, in most cases it should be sufficient to use the `ngModel.$validators` and
1085
+ * `ngModel.$asyncValidators` collections which will call `$setValidity` automatically.
1086
+ *
1087
+ * @param {string} validationErrorKey Name of the validator. The `validationErrorKey` will be assigned
1088
+ * to either `$error[validationErrorKey]` or `$pending[validationErrorKey]`
1089
+ * (for unfulfilled `$asyncValidators`), so that it is available for data-binding.
1090
+ * The `validationErrorKey` should be in camelCase and will get converted into dash-case
1091
+ * for class name. Example: `myError` will result in `ng-valid-my-error` and `ng-invalid-my-error`
1092
+ * classes and can be bound to as `{{ someForm.someControl.$error.myError }}`.
1093
+ * @param {boolean} isValid Whether the current state is valid (true), invalid (false), pending (undefined),
1094
+ * or skipped (null). Pending is used for unfulfilled `$asyncValidators`.
1095
+ * Skipped is used by AngularJS when validators do not run because of parse errors and
1096
+ * when `$asyncValidators` do not run because any of the `$validators` failed.
1097
+ */
1098
+ addSetValidityMethod({
1099
+ clazz: NgModelController,
1100
+ set(object, property) {
1101
+ object[property] = true;
1102
+ },
1103
+ unset(object, property) {
1104
+ delete object[property];
1105
+ },
1106
+ });
1107
+
1108
+ /**
1109
+ * @ngdoc directive
1110
+ * @name ngModel
1111
+ * @restrict A
1112
+ * @priority 1
1113
+ * @param {string} ngModel assignable {@link guide/expression Expression} to bind to.
1114
+ *
1115
+ * @description
1116
+ * The `ngModel` directive binds an `input`,`select`, `textarea` (or custom form control) to a
1117
+ * property on the scope using {@link ngModel.NgModelController NgModelController},
1118
+ * which is created and exposed by this directive.
1119
+ *
1120
+ * `ngModel` is responsible for:
1121
+ *
1122
+ * - Binding the view into the model, which other directives such as `input`, `textarea` or `select`
1123
+ * require.
1124
+ * - Providing validation behavior (i.e. required, number, email, url).
1125
+ * - Keeping the state of the control (valid/invalid, dirty/pristine, touched/untouched, validation errors).
1126
+ * - Setting related css classes on the element (`ng-valid`, `ng-invalid`, `ng-dirty`, `ng-pristine`, `ng-touched`,
1127
+ * `ng-untouched`, `ng-empty`, `ng-not-empty`) including animations.
1128
+ * - Registering the control with its parent {@link ng.directive:form form}.
1129
+ *
1130
+ * Note: `ngModel` will try to bind to the property given by evaluating the expression on the
1131
+ * current scope. If the property doesn't already exist on this scope, it will be created
1132
+ * implicitly and added to the scope.
1133
+ *
1134
+ * For best practices on using `ngModel`, see:
1135
+ *
1136
+ * - [Understanding Scopes](https://github.com/angular/angular.js/wiki/Understanding-Scopes)
1137
+ *
1138
+ * For basic examples, how to use `ngModel`, see:
1139
+ *
1140
+ * - {@link ng.directive:input input}
1141
+ * - {@link input[text] text}
1142
+ * - {@link input[checkbox] checkbox}
1143
+ * - {@link input[radio] radio}
1144
+ * - {@link input[number] number}
1145
+ * - {@link input[email] email}
1146
+ * - {@link input[url] url}
1147
+ * - {@link input[date] date}
1148
+ * - {@link input[datetime-local] datetime-local}
1149
+ * - {@link input[time] time}
1150
+ * - {@link input[month] month}
1151
+ * - {@link input[week] week}
1152
+ * - {@link ng.directive:select select}
1153
+ * - {@link ng.directive:textarea textarea}
1154
+ *
1155
+ * ## Complex Models (objects or collections)
1156
+ *
1157
+ * By default, `ngModel` watches the model by reference, not value. This is important to know when
1158
+ * binding inputs to models that are objects (e.g. `Date`) or collections (e.g. arrays). If only properties of the
1159
+ * object or collection change, `ngModel` will not be notified and so the input will not be re-rendered.
1160
+ *
1161
+ * The model must be assigned an entirely new object or collection before a re-rendering will occur.
1162
+ *
1163
+ * Some directives have options that will cause them to use a custom `$watchCollection` on the model expression
1164
+ * - for example, `ngOptions` will do so when a `track by` clause is included in the comprehension expression or
1165
+ * if the select is given the `multiple` attribute.
1166
+ *
1167
+ * The `$watchCollection()` method only does a shallow comparison, meaning that changing properties deeper than the
1168
+ * first level of the object (or only changing the properties of an item in the collection if it's an array) will still
1169
+ * not trigger a re-rendering of the model.
1170
+ *
1171
+ * ## CSS classes
1172
+ * The following CSS classes are added and removed on the associated input/select/textarea element
1173
+ * depending on the validity of the model.
1174
+ *
1175
+ * - `ng-valid`: the model is valid
1176
+ * - `ng-invalid`: the model is invalid
1177
+ * - `ng-valid-[key]`: for each valid key added by `$setValidity`
1178
+ * - `ng-invalid-[key]`: for each invalid key added by `$setValidity`
1179
+ * - `ng-pristine`: the control hasn't been interacted with yet
1180
+ * - `ng-dirty`: the control has been interacted with
1181
+ * - `ng-touched`: the control has been blurred
1182
+ * - `ng-untouched`: the control hasn't been blurred
1183
+ * - `ng-pending`: any `$asyncValidators` are unfulfilled
1184
+ * - `ng-empty`: the view does not contain a value or the value is deemed "empty", as defined
1185
+ * by the {@link ngModel.NgModelController#$isEmpty} method
1186
+ * - `ng-not-empty`: the view contains a non-empty value
1187
+ *
1188
+ * Keep in mind that ngAnimate can detect each of these classes when added and removed.
1189
+ *
1190
+ * @animations
1191
+ * Animations within models are triggered when any of the associated CSS classes are added and removed
1192
+ * on the input element which is attached to the model. These classes include: `.ng-pristine`, `.ng-dirty`,
1193
+ * `.ng-invalid` and `.ng-valid` as well as any other validations that are performed on the model itself.
1194
+ * The animations that are triggered within ngModel are similar to how they work in ngClass and
1195
+ * animations can be hooked into using CSS transitions, keyframes as well as JS animations.
1196
+ *
1197
+ * The following example shows a simple way to utilize CSS transitions to style an input element
1198
+ * that has been rendered as invalid after it has been validated:
1199
+ *
1200
+ * <pre>
1201
+ * //be sure to include ngAnimate as a module to hook into more
1202
+ * //advanced animations
1203
+ * .my-input {
1204
+ * transition:0.5s linear all;
1205
+ * background: white;
1206
+ * }
1207
+ * .my-input.ng-invalid {
1208
+ * background: red;
1209
+ * color:white;
1210
+ * }
1211
+ * </pre>
1212
+ *
1213
+ * @example
1214
+ * ### Basic Usage
1215
+ * <example deps="angular-animate.js" animations="true" fixBase="true" module="inputExample" name="ng-model">
1216
+ <file name="index.html">
1217
+ <script>
1218
+ angular.module('inputExample', [])
1219
+ .controller('ExampleController', ['$scope', function($scope) {
1220
+ $scope.val = '1';
1221
+ }]);
1222
+ </script>
1223
+ <style>
1224
+ .my-input {
1225
+ transition:all linear 0.5s;
1226
+ background: transparent;
1227
+ }
1228
+ .my-input.ng-invalid {
1229
+ color:white;
1230
+ background: red;
1231
+ }
1232
+ </style>
1233
+ <p id="inputDescription">
1234
+ Update input to see transitions when valid/invalid.
1235
+ Integer is a valid value.
1236
+ </p>
1237
+ <form name="testForm" ng-controller="ExampleController">
1238
+ <input ng-model="val" ng-pattern="/^\d+$/" name="anim" class="my-input"
1239
+ aria-describedby="inputDescription" />
1240
+ </form>
1241
+ </file>
1242
+ * </example>
1243
+ *
1244
+ * @example
1245
+ * ### Binding to a getter/setter
1246
+ *
1247
+ * Sometimes it's helpful to bind `ngModel` to a getter/setter function. A getter/setter is a
1248
+ * function that returns a representation of the model when called with zero arguments, and sets
1249
+ * the internal state of a model when called with an argument. It's sometimes useful to use this
1250
+ * for models that have an internal representation that's different from what the model exposes
1251
+ * to the view.
1252
+ *
1253
+ * <div class="alert alert-success">
1254
+ * **Best Practice:** It's best to keep getters fast because AngularJS is likely to call them more
1255
+ * frequently than other parts of your code.
1256
+ * </div>
1257
+ *
1258
+ * You use this behavior by adding `ng-model-options="{ getterSetter: true }"` to an element that
1259
+ * has `ng-model` attached to it. You can also add `ng-model-options="{ getterSetter: true }"` to
1260
+ * a `<form>`, which will enable this behavior for all `<input>`s within it. See
1261
+ * {@link ng.directive:ngModelOptions `ngModelOptions`} for more.
1262
+ *
1263
+ * The following example shows how to use `ngModel` with a getter/setter:
1264
+ *
1265
+ * @example
1266
+ * <example name="ngModel-getter-setter" module="getterSetterExample">
1267
+ <file name="index.html">
1268
+ <div ng-controller="ExampleController">
1269
+ <form name="userForm">
1270
+ <label>Name:
1271
+ <input type="text" name="userName"
1272
+ ng-model="user.name"
1273
+ ng-model-options="{ getterSetter: true }" />
1274
+ </label>
1275
+ </form>
1276
+ <pre>user.name = <span ng-bind="user.name()"></span></pre>
1277
+ </div>
1278
+ </file>
1279
+ <file name="app.js">
1280
+ angular.module('getterSetterExample', [])
1281
+ .controller('ExampleController', ['$scope', function($scope) {
1282
+ let _name = 'Brian';
1283
+ $scope.user = {
1284
+ name: function(newName) {
1285
+ // Note that newName can be undefined for two reasons:
1286
+ // 1. Because it is called as a getter and thus called with no arguments
1287
+ // 2. Because the property should actually be set to undefined. This happens e.g. if the
1288
+ // input is invalid
1289
+ return arguments.length ? (_name = newName) : _name;
1290
+ }
1291
+ };
1292
+ }]);
1293
+ </file>
1294
+ * </example>
1295
+ */
1296
+ export const ngModelDirective = [
1297
+ "$rootScope",
1298
+ ($rootScope) => ({
1299
+ restrict: "A",
1300
+ require: ["ngModel", "^?form", "^?ngModelOptions"],
1301
+ controller: NgModelController,
1302
+ // Prelink needs to run before any input directive
1303
+ // so that we can set the NgModelOptions in NgModelController
1304
+ // before anyone else uses it.
1305
+ priority: 1,
1306
+ compile: function ngModelCompile(element) {
1307
+ // Setup initial state of the control
1308
+ element[0].classList.add(PRISTINE_CLASS, UNTOUCHED_CLASS, VALID_CLASS);
1309
+
1310
+ return {
1311
+ pre: function ngModelPreLink(scope, element, attr, ctrls) {
1312
+ const modelCtrl = ctrls[0];
1313
+ const formCtrl = ctrls[1] || modelCtrl.$$parentForm;
1314
+ const optionsCtrl = ctrls[2];
1315
+
1316
+ if (optionsCtrl) {
1317
+ modelCtrl.$options = optionsCtrl.$options;
1318
+ }
1319
+
1320
+ modelCtrl.$$initGetterSetters();
1321
+
1322
+ // notify others, especially parent forms
1323
+ formCtrl.$addControl(modelCtrl);
1324
+
1325
+ attr.$observe("name", (newValue) => {
1326
+ if (modelCtrl.$name !== newValue) {
1327
+ modelCtrl.$$parentForm.$$renameControl(modelCtrl, newValue);
1328
+ }
1329
+ });
1330
+
1331
+ scope.$on("$destroy", () => {
1332
+ modelCtrl.$$parentForm.$removeControl(modelCtrl);
1333
+ });
1334
+ },
1335
+ post: function ngModelPostLink(scope, element, attr, ctrls) {
1336
+ const modelCtrl = ctrls[0];
1337
+ modelCtrl.$$setUpdateOnEvents();
1338
+
1339
+ function setTouched() {
1340
+ modelCtrl.$setTouched();
1341
+ }
1342
+
1343
+ element.on("blur", () => {
1344
+ if (modelCtrl.$touched) return;
1345
+
1346
+ if ($rootScope.$$phase) {
1347
+ scope.$evalAsync(setTouched);
1348
+ } else {
1349
+ scope.$apply(setTouched);
1350
+ }
1351
+ });
1352
+ },
1353
+ };
1354
+ },
1355
+ }),
1356
+ ];