Skip to content

Commit ea7b040

Browse files
committed
Remove eager animations from forms
1 parent b6b5568 commit ea7b040

5 files changed

Lines changed: 59 additions & 38 deletions

File tree

@types/directive/form/form.d.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
export function setupValidity(instance: any): void;
2+
/**
3+
* @param {FormController|ng.NgModelController} ctrl
4+
* @param {string} className
5+
* @param {boolean} switchValue
6+
*/
7+
export function cachedToggleClass(
8+
ctrl: FormController | ng.NgModelController,
9+
className: string,
10+
switchValue: boolean,
11+
): void;
212
/**
313
* @type {{
414
* $nonscope: boolean,
@@ -74,19 +84,21 @@ export class FormController {
7484
static $nonscope: boolean;
7585
static $inject: string[];
7686
/**
77-
* @param {Element} $element
87+
* @param {HTMLFormElement} $element
7888
* @param {ng.Attributes} $attrs
7989
* @param {ng.Scope} $scope
8090
* @param {ng.AnimateService} $animate
8191
* @param {ng.InterpolateService} $interpolate
8292
*/
8393
constructor(
84-
$element: Element,
94+
$element: HTMLFormElement,
8595
$attrs: ng.Attributes,
8696
$scope: ng.Scope,
8797
$animate: ng.AnimateService,
8898
$interpolate: ng.InterpolateService,
8999
);
100+
/** @type {boolean} */
101+
_isAnimated: boolean;
90102
_controls: any[];
91103
$name: any;
92104
/**
@@ -102,7 +114,7 @@ export class FormController {
102114
$submitted: boolean;
103115
/** @type {FormController|Object} */
104116
_parentForm: FormController | any;
105-
_element: Element;
117+
_element: HTMLFormElement;
106118
_animate: import("../../animations/interface.ts").AnimateService;
107119
$error: {};
108120
_success: {};

@types/directive/model/model.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class NgModelController {
5252
* @param {ng.Scope} $scope
5353
* @param {ng.ExceptionHandlerService} $exceptionHandler
5454
* @param {ng.Attributes} $attr
55-
* @param {Element} $element
55+
* @param {HTMLElement} $element
5656
* @param {ng.ParseService} $parse
5757
* @param {ng.AnimateService} $animate
5858
* @param {ng.InterpolateService} $interpolate
@@ -61,11 +61,13 @@ export class NgModelController {
6161
$scope: ng.Scope,
6262
$exceptionHandler: ng.ExceptionHandlerService,
6363
$attr: ng.Attributes,
64-
$element: Element,
64+
$element: HTMLElement,
6565
$parse: ng.ParseService,
6666
$animate: ng.AnimateService,
6767
$interpolate: ng.InterpolateService,
6868
);
69+
/** @type {boolean} */
70+
_isAnimated: boolean;
6971
/** @type {any} The actual value from the control's view */
7072
$viewValue: any;
7173
/** @type {any} The value in the model that the control is bound to. */
@@ -144,7 +146,7 @@ export class NgModelController {
144146
/** @type {ng.Scope} */
145147
_scope: ng.Scope;
146148
_attr: ng.Attributes;
147-
_element: Element;
149+
_element: HTMLElement;
148150
_animate: import("../../animations/interface.ts").AnimateService;
149151
_parse: import("../../core/parse/interface.ts").ParseService;
150152
_exceptionHandler: import("../../services/exception/interface.ts").ExceptionHandler;

src/core/compile/compile.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,6 +2661,10 @@ export class CompileProvider {
26612661
}
26622662
}
26632663

2664+
/**
2665+
* @param {ng.Directive[]} directives
2666+
* @param {string} text
2667+
*/
26642668
function addTextInterpolateDirective(directives, text) {
26652669
const interpolateFn = $interpolate(text, true);
26662670

src/directive/form/form.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,16 @@ export class FormController {
121121
];
122122

123123
/**
124-
* @param {Element} $element
124+
* @param {HTMLFormElement} $element
125125
* @param {ng.Attributes} $attrs
126126
* @param {ng.Scope} $scope
127127
* @param {ng.AnimateService} $animate
128128
* @param {ng.InterpolateService} $interpolate
129129
*/
130130
constructor($element, $attrs, $scope, $animate, $interpolate) {
131+
/** @type {boolean} */
132+
this._isAnimated = hasAnimate($element);
133+
131134
this._controls = [];
132135

133136
this.$name = $interpolate($attrs.name || $attrs.ngForm || "")($scope);
@@ -491,16 +494,6 @@ export class FormController {
491494
}
492495
}
493496

494-
function cachedToggleClass(ctrl, className, switchValue) {
495-
if (switchValue && !ctrl._classCache[className]) {
496-
ctrl._animate.addClass(ctrl._element, className);
497-
ctrl._classCache[className] = true;
498-
} else if (!switchValue && ctrl._classCache[className]) {
499-
ctrl._animate.removeClass(ctrl._element, className);
500-
ctrl._classCache[className] = false;
501-
}
502-
}
503-
504497
function toggleValidationCss(ctrl, validationErrorKeyParam, isValid) {
505498
validationErrorKeyParam = validationErrorKeyParam
506499
? `-${snakeCase(validationErrorKeyParam, "-")}`
@@ -732,3 +725,26 @@ export function setupValidity(instance) {
732725
instance._classCache[INVALID_CLASS] = !(instance._classCache[VALID_CLASS] =
733726
instance._element.classList.contains(VALID_CLASS));
734727
}
728+
729+
/**
730+
* @param {FormController|ng.NgModelController} ctrl
731+
* @param {string} className
732+
* @param {boolean} switchValue
733+
*/
734+
export function cachedToggleClass(ctrl, className, switchValue) {
735+
if (switchValue && !ctrl._classCache[className]) {
736+
if (ctrl._isAnimated) {
737+
ctrl._animate.addClass(ctrl._element, className);
738+
} else {
739+
ctrl._element.classList.add(className);
740+
}
741+
ctrl._classCache[className] = true;
742+
} else if (!switchValue && ctrl._classCache[className]) {
743+
if (ctrl._isAnimated) {
744+
ctrl._animate.removeClass(ctrl._element, className);
745+
} else {
746+
ctrl._element.classList.remove(className);
747+
}
748+
ctrl._classCache[className] = false;
749+
}
750+
}

src/directive/model/model.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import {
2424
minErr,
2525
snakeCase,
2626
} from "../../shared/utils.js";
27-
import { PENDING_CLASS, nullFormCtrl } from "../form/form.js";
27+
import {
28+
PENDING_CLASS,
29+
cachedToggleClass,
30+
nullFormCtrl,
31+
} from "../form/form.js";
2832
import { defaultModelOptions } from "../model-options/model-options.js";
2933
import { startingTag } from "../../shared/dom.js";
3034
import { $injectTokens as $t } from "../../injection-tokens.js";
@@ -84,7 +88,7 @@ export class NgModelController {
8488
* @param {ng.Scope} $scope
8589
* @param {ng.ExceptionHandlerService} $exceptionHandler
8690
* @param {ng.Attributes} $attr
87-
* @param {Element} $element
91+
* @param {HTMLElement} $element
8892
* @param {ng.ParseService} $parse
8993
* @param {ng.AnimateService} $animate
9094
* @param {ng.InterpolateService} $interpolate
@@ -98,6 +102,8 @@ export class NgModelController {
98102
$animate,
99103
$interpolate,
100104
) {
105+
/** @type {boolean} */
106+
this._isAnimated = hasAnimate($element);
101107
/** @type {any} The actual value from the control's view */
102108
this.$viewValue = Number.NaN;
103109

@@ -216,25 +222,6 @@ export class NgModelController {
216222
}
217223
}
218224

219-
function cachedToggleClass(ctrl, className, switchValue) {
220-
if (switchValue && !ctrl._classCache[className]) {
221-
if (hasAnimate(ctrl._element)) {
222-
ctrl._animate.addClass(ctrl._element, className);
223-
} else {
224-
ctrl._element.classList.add(className);
225-
}
226-
227-
ctrl._classCache[className] = true;
228-
} else if (!switchValue && ctrl._classCache[className]) {
229-
if (hasAnimate(ctrl._element)) {
230-
ctrl._animate.removeClass(ctrl._element, className);
231-
} else {
232-
ctrl._element.classList.remove(className);
233-
}
234-
ctrl._classCache[className] = false;
235-
}
236-
}
237-
238225
function toggleValidationCss(ctrl, validationErrorKeyParam, isValid) {
239226
validationErrorKeyParam = validationErrorKeyParam
240227
? `-${snakeCase(validationErrorKeyParam, "-")}`

0 commit comments

Comments
 (0)