File tree

4 files changed

+75
-88
lines changed

4 files changed

+75
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ export default class AnimationHandler {
99

1010
static async getModule(modulePath) {
1111
if (!(modulePath in this.modules)) {
12+
const toCamelCase = path => {
13+
const fileName = path.match(/\/([^\.]+)(?:\.js)?$/)[1];
14+
return fileName.replace(/-([a-z])/g, (_, letter) =>
15+
letter.toUpperCase()
16+
);
17+
};
1218
const module = await import(modulePath);
13-
this.modules[modulePath] = module;
19+
this.modules[toCamelCase(modulePath)] = module;
1420
}
1521

1622
return this.modules[modulePath];
1723
}
1824

25+
async initDependencies() {
26+
return Promise.resolve();
27+
}
28+
1929
begin() {
2030
throw new TypeError("Method 'begin' is not implemented.");
2131
}
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ export default class MotionAnimationHandler extends AnimationHandler {
77
this.currentTransition = null;
88
}
99

10-
static #transitionsModule = null;
11-
1210
static #removeMotionCssClass(element) {
1311
const className = [...element.classList].find(cl =>
1412
cl.match(/js\-anim\-\-(rotate|scale)/)
@@ -20,27 +18,20 @@ export default class MotionAnimationHandler extends AnimationHandler {
2018
}
2119
}
2220

23-
static async #initTransitionsModule() {
24-
if (!this.#transitionsModule) {
25-
this.#transitionsModule = await AnimationHandler.getModule(
26-
'./transitions.js'
27-
);
28-
}
21+
async initDependencies() {
22+
await AnimationHandler.getModule('./transitions.js');
2923
}
3024

31-
async begin() {
32-
await MotionAnimationHandler.#initTransitionsModule();
33-
this.currentTransition =
34-
MotionAnimationHandler.#transitionsModule.getCurrentTransition(
35-
this.element
36-
);
25+
begin() {
26+
const { getCurrentTransition } = AnimationHandler.modules.transitions;
27+
this.currentTransition = getCurrentTransition(this.element);
3728
MotionAnimationHandler.#removeMotionCssClass(this.element);
3829
}
3930

40-
async middle() {
41-
await MotionAnimationHandler.#initTransitionsModule();
31+
middle() {
4232
if (this.currentTransition) {
43-
MotionAnimationHandler.#transitionsModule.appendTransition(
33+
const { appendTransition } = AnimationHandler.modules.transitions;
34+
appendTransition(
4435
this.element,
4536
CLASS_NAMES[this.action][this.animationId],
4637
this.currentTransition
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import AnimationHandler from './AnimationHandler.js';
12
import VisibilityAnimationHandler from './VisibilityAnimationHandler.js';
23

34
export default class VisibilityAnimationWithParentResizeHandler extends VisibilityAnimationHandler {
@@ -9,76 +10,59 @@ export default class VisibilityAnimationWithParentResizeHandler extends Visibili
910
this.parentMeasures = null;
1011
}
1112

12-
setDimensionsTransition(heightTransition, widthTransition) {
13+
setDimensionsTransition({ heightTransition, widthTransition }) {
1314
this.heightTransition = heightTransition;
1415
this.widthTransition = widthTransition;
1516
}
1617

17-
static #resizeParentModule = null;
18-
static #measurementsModule = null;
19-
20-
static async #initResizeParentModule() {
21-
if (!VisibilityAnimationWithParentResizeHandler.#resizeParentModule) {
22-
VisibilityAnimationWithParentResizeHandler.#resizeParentModule =
23-
await this.getModule('./resize-parent.js');
24-
}
18+
async initDependencies() {
19+
await AnimationHandler.getModule('./resize-parent.js');
20+
await AnimationHandler.getModule('./measurements.js');
2521
}
2622

27-
static async #initMeasurementsModule() {
28-
if (!VisibilityAnimationWithParentResizeHandler.#measurementsModule) {
29-
VisibilityAnimationWithParentResizeHandler.#measurementsModule =
30-
await this.getModule('./measurements.js');
31-
}
32-
}
33-
34-
async begin() {
35-
await VisibilityAnimationWithParentResizeHandler.#initResizeParentModule();
23+
begin() {
3624
super.begin();
3725

3826
if (this.widthTransition || this.heightTransition) {
39-
const parentData =
40-
VisibilityAnimationWithParentResizeHandler.#resizeParentModule.initParentResize(
41-
{
42-
element: this.element,
43-
action: this.action,
44-
widthTransition: this.widthTransition,
45-
heightTransition: this.heightTransition,
46-
}
47-
);
27+
const { initParentResize } = AnimationHandler.modules.resizeParent;
28+
29+
const parentData = initParentResize({
30+
element: this.element,
31+
action: this.action,
32+
widthTransition: this.widthTransition,
33+
heightTransition: this.heightTransition,
34+
});
4835

4936
this.parentMeasures = parentData.parentMeasures;
5037
this.dimension = parentData.dimension;
5138
}
5239
}
5340

54-
async middle() {
55-
await VisibilityAnimationWithParentResizeHandler.#initMeasurementsModule();
41+
middle() {
5642
super.middle();
5743

5844
setTimeout(() => {
59-
if (this.dimension)
60-
VisibilityAnimationWithParentResizeHandler.#measurementsModule.setParentMaxMeasures(
61-
{
62-
parentState: 'final',
63-
element: this.element,
64-
parentMeasures: this.parentMeasures,
65-
action: this.action,
66-
dimension: this.dimension,
67-
}
68-
);
45+
if (this.dimension) {
46+
const { setParentMaxMeasures } = AnimationHandler.modules.measurements;
47+
48+
setParentMaxMeasures({
49+
parentState: 'final',
50+
element: this.element,
51+
parentMeasures: this.parentMeasures,
52+
action: this.action,
53+
dimension: this.dimension,
54+
});
55+
}
6956
}, 0);
7057
}
7158

72-
async end() {
73-
await VisibilityAnimationWithParentResizeHandler.#initResizeParentModule();
59+
end() {
7460
super.end();
7561

7662
const { widthTransition, heightTransition } = this;
7763
if (widthTransition || heightTransition) {
78-
VisibilityAnimationWithParentResizeHandler.#resizeParentModule.endParentResize(
79-
this.element,
80-
{ widthTransition, heightTransition }
81-
);
64+
const { endParentResize } = AnimationHandler.modules.resizeParent;
65+
endParentResize(this.element, { widthTransition, heightTransition });
8266
}
8367
}
8468
}
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Handles all the animation process
33
* @module animate
44
*/
5-
import VisibilityAnimationWithParentResizeHandler from './VisibilityAnimationWithParentResizeHandler.js';
65
import {
76
MOTION_ANIMS_ID,
87
PROPERTY_NAMES,
@@ -343,46 +342,38 @@ const initCallback = (trigger, fn, type) => {
343342

344343
const getAnimationHandler = async (animType, args) => {
345344
const { element, action, id } = args;
345+
let animationHandler = null;
346+
346347
if (animType === 'motion') {
347348
const { default: MotionAnimationHandler } = await import(
348349
'./MotionAnimationHandler.js'
349350
);
350-
return new MotionAnimationHandler(element, action, id);
351-
}
352-
353-
if (animType === 'visibility') {
351+
animationHandler = new MotionAnimationHandler(element, action, id);
352+
} else if (animType === 'visibility') {
354353
const { widthTransition, heightTransition } = args;
355354

356355
if (widthTransition || heightTransition) {
357356
const { default: VisibilityAnimationHandler } = await import(
358357
'./VisibilityAnimationWithParentResizeHandler.js'
359358
);
360-
const animationHandler = new VisibilityAnimationHandler(
361-
element,
362-
action,
363-
id
364-
);
359+
animationHandler = new VisibilityAnimationHandler(element, action, id);
365360
animationHandler.setDimensionsTransition({
366361
heightTransition,
367362
widthTransition,
368363
});
369-
370-
return animationHandler;
364+
} else {
365+
const { default: VisibilityAnimationHandler } = await import(
366+
'./VisibilityAnimationHandler.js'
367+
);
368+
animationHandler = new VisibilityAnimationHandler(element, action, id);
371369
}
372370

373-
const { default: VisibilityAnimationHandler } = await import(
374-
'./VisibilityAnimationHandler.js'
375-
);
376-
const animationHandler = new VisibilityAnimationHandler(
377-
element,
378-
action,
379-
id
380-
);
381371
if (args.overflowHidden) animationHandler.setOverflowHidden(true);
382372
if (args.maintainSpace) animationHandler.setMaintainSpace(true);
383-
384-
return animationHandler;
385373
}
374+
375+
await animationHandler.initDependencies();
376+
return animationHandler;
386377
};
387378

388379
/**
@@ -441,15 +432,15 @@ const animate = async (element, action, id, opts = {}) => {
441432
}
442433
};
443434

444-
await handleAnimation.begin();
435+
handleAnimation.begin();
445436
if (typeof start === 'function') {
446437
initCallback(trigger, start, 'start');
447438
}
448439
element.classList.add(CLASS_NAMES[action][id]);
449440
element.classList.remove(CLASS_NAMES[OPPOSITE_ACTION[action]][id]);
450-
await handleAnimation.middle();
441+
handleAnimation.middle();
451442

452-
setTimeout(() => {
443+
setTimeout(async () => {
453444
handleAnimation.end();
454445
if (typeof complete === 'function') {
455446
initCallback(trigger, complete, 'complete');
@@ -554,6 +545,17 @@ const init = (animationId, opts = {}) => {
554545
cursor,
555546
} = opts;
556547

548+
/** TODO: load dependencies on page load */
549+
// async function loadDependencies() {
550+
// await import('./resize-parent.js');
551+
// await import('./measurements.js');
552+
// await import('./transitions.js');
553+
554+
// removeEventListener('load', loadDependencies);
555+
// }
556+
557+
// addEventListener('load', loadDependencies);
558+
557559
document.querySelectorAll(trigger).forEach(btn => {
558560
btn.classList.add(CLASS_NAMES.btnCursor);
559561
if (typeof cursor === 'string') {

0 commit comments

Comments
 (0)