Debian Codegraph @

addons/web/static/lib/hoot/mock/animation.js

raw
1 /** @odoo-module */
2
3 import { on } from "@odoo/hoot-dom";
4 import { MockEventTarget } from "../hoot_utils";
5 import { ensureTest } from "../main_runner";
6
7 //-----------------------------------------------------------------------------
8 // Global
9 //-----------------------------------------------------------------------------
10
11 const {
12 Array: { isArray: $isArray },
13 Element,
14 Object: { assign: $assign, entries: $entries },
15 scroll: windowScroll,
16 scrollBy: windowScrollBy,
17 scrollTo: windowScrollTo,
18 } = globalThis;
19
20 const { animate, scroll, scrollBy, scrollIntoView, scrollTo } = Element.prototype;
21
22 //-----------------------------------------------------------------------------
23 // Internal
24 //-----------------------------------------------------------------------------
25
26 function forceInstantScroll(args) {
27 return !allowAnimations && args[0] && typeof args[0] === "object"
28 ? [{ ...args[0], behavior: "instant" }, ...args.slice(1)]
29 : args;
30 }
31
32 const animationChangeBus = new MockEventTarget();
33 const animationChangeCleanups = [];
34
35 let allowAnimations = true;
36 let allowTransitions = false;
37
38 //-----------------------------------------------------------------------------
39 // Exports
40 //-----------------------------------------------------------------------------
41
42 export class MockAnimation extends MockEventTarget {
43 static publicListeners = ["cancel", "finish", "remove"];
44
45 currentTime = null;
46 effect = null;
47 finished = Promise.resolve(this);
48 id = "";
49 pending = false;
50 playState = "idle";
51 playbackRate = 1;
52 ready = Promise.resolve(this);
53 replaceState = "active";
54 startTime = null;
55 timeline = {
56 currentTime: this.currentTime,
57 duration: null,
58 };
59
60 cancel() {
61 this.dispatchEvent(new AnimationPlaybackEvent("cancel"));
62 }
63
64 commitStyles() {}
65
66 finish() {
67 this.dispatchEvent(new AnimationPlaybackEvent("finish"));
68 }
69
70 pause() {}
71
72 persist() {}
73
74 play() {
75 this.dispatchEvent(new AnimationPlaybackEvent("finish"));
76 }
77
78 reverse() {}
79
80 updatePlaybackRate() {}
81 }
82
83 export function cleanupAnimations() {
84 allowAnimations = true;
85 allowTransitions = false;
86
87 while (animationChangeCleanups.length) {
88 animationChangeCleanups.pop()();
89 }
90 }
91
92 /**
93 * Turns off all animations triggered programmatically (such as with `animate`),
94 * as well as smooth scrolls.
95 *
96 * @param {boolean} [enable=false]
97 */
98 export function disableAnimations(enable = false) {
99 ensureTest("disableAnimations");
100 allowAnimations = enable;
101 }
102
103 /**
104 * Restores all suppressed "animation" and "transition" properties for the current
105 * test, as they are turned off by default.
106 *
107 * @param {boolean} [enable=true]
108 */
109 export function enableTransitions(enable = true) {
110 ensureTest("enableTransitions");
111 allowTransitions = enable;
112 animationChangeBus.dispatchEvent(new CustomEvent("toggle-transitions"));
113 }
114
115 /** @type {Element["animate"]} */
116 export function mockedAnimate(...args) {
117 if (allowAnimations) {
118 return animate.call(this, ...args);
119 }
120
121 // Apply style properties immediatly
122 const keyframesList = $isArray(args[0]) ? args[0] : [args[0]];
123 const style = {};
124 for (const kf of keyframesList) {
125 for (const [key, value] of $entries(kf)) {
126 style[key] = $isArray(value) ? value.at(-1) : value;
127 }
128 }
129 $assign(this.style, style);
130
131 // Return mock animation
132 return new MockAnimation();
133 }
134
135 /** @type {Element["scroll"]} */
136 export function mockedScroll(...args) {
137 return scroll.call(this, ...forceInstantScroll(args));
138 }
139
140 /** @type {Element["scrollBy"]} */
141 export function mockedScrollBy(...args) {
142 return scrollBy.call(this, ...forceInstantScroll(args));
143 }
144
145 /** @type {Element["scrollIntoView"]} */
146 export function mockedScrollIntoView(...args) {
147 return scrollIntoView.call(this, ...forceInstantScroll(args));
148 }
149
150 /** @type {Element["scrollTo"]} */
151 export function mockedScrollTo(...args) {
152 return scrollTo.call(this, ...forceInstantScroll(args));
153 }
154
155 /** @type {typeof window["scroll"]} */
156 export function mockedWindowScroll(...args) {
157 return windowScroll.call(this, ...forceInstantScroll(args));
158 }
159
160 /** @type {typeof window["scrollBy"]} */
161 export function mockedWindowScrollBy(...args) {
162 return windowScrollBy.call(this, ...forceInstantScroll(args));
163 }
164
165 /** @type {typeof window["scrollTo"]} */
166 export function mockedWindowScrollTo(...args) {
167 return windowScrollTo.call(this, ...forceInstantScroll(args));
168 }
169
170 /**
171 * @param {(allowTransitions: boolean) => any} onChange
172 */
173 export function subscribeToTransitionChange(onChange) {
174 onChange(allowTransitions);
175 animationChangeCleanups.push(
176 on(animationChangeBus, "toggle-transitions", () => onChange(allowTransitions))
177 );
178 }