/
sequence.ts
172 lines (141 loc) · 4.89 KB
/
sequence.ts
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
import { IPlayer, ITrigger } from '../interfaces';
const NUMBER_REGEX = /^\d*(\.\d+)?$/
const MUTATION_OBSERVER_CONFIG = { attributes: true, childList: false, subtree: false };
/**
* The __Sequence__ trigger allows you to define complex playing scenarios with a simple definition.
*
* Example usage:
* ```html
* <lord-icon trigger="sequence" sequence="state:intro-empty,play,state:hover-empty,play,state:morph-fill,play,state:morph-erase,play,state:intro-empty,delay:first:last:500,play:reverse" src="/trash.json"></lord-icon>
* ```
*/
export class Sequence implements ITrigger {
protected sequenceIndex: number = 0;
protected frameState: string | null = null;
protected frameDelayFirst: number | null = null;
protected frameDelayLast: number | null = null;
protected timer: any;
protected observer: MutationObserver;
constructor(
protected player: IPlayer,
protected element: HTMLElement,
protected targetElement: HTMLElement,
) {
this.observer = new MutationObserver((mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === 'attributes' && mutation.attributeName === 'sequence') {
this.reset();
this.step();
}
}
});
}
onReady() {
this.step();
}
onComplete() {
this.timer = setTimeout(() => {
this.timer = null;
this.frameDelayLast = null;
this.step();
}, this.frameDelayLast || 0);
}
onConnected() {
this.observer.observe(this.element, MUTATION_OBSERVER_CONFIG);
}
onDisconnected() {
this.observer.disconnect();
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
}
reset() {
this.player.pause();
this.sequenceIndex = 0;
this.frameState = this.frameDelayFirst = this.frameDelayLast = null;
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
}
takeStep() {
const steps = this.sequence.split(',');
const step = steps[this.sequenceIndex];
this.sequenceIndex++;
if (this.sequenceIndex >= steps.length) {
this.sequenceIndex = 0;
}
const [action, ...params] = step.split(':');
return { action, params };
}
handleStep(action: string, params: string[]) {
if (action === 'play') {
if (this.frameState) {
this.player.state = this.frameState;
this.frameState = null;
}
const hasReverse = params.includes('reverse');
if (hasReverse) {
this.player.goToLastFrame();
this.player.direction = -1;
} else {
this.player.goToFirstFrame();
this.player.direction = 1;
}
this.timer = setTimeout(() => {
this.timer = null;
this.frameDelayFirst = null;
this.player.play();
}, this.frameDelayFirst || 0);
} else if (action === 'frame') {
let frameIndex = 0;
if (params.length && params[0].match(NUMBER_REGEX)) {
frameIndex = Math.max(0, Math.min(this.player.frames, +params[0]));
}
this.player.frame = frameIndex;
this.timer = setTimeout(() => {
this.timer = null;
this.frameDelayFirst = null;
this.step();
}, this.frameDelayFirst || 0);
} else if (action === 'state') {
this.frameState = params[0];
this.step();
} else if (action === 'delay') {
let value: number | null = null;
for (const param of params) {
if (param && param.match(NUMBER_REGEX)) {
value = +param;
}
}
if (value && value > 0) {
if (params.includes('first') && params.includes('last')) {
this.frameDelayFirst = value;
this.frameDelayLast = value;
} else if (params.includes('first')) {
this.frameDelayFirst = value;
} else if (params.includes('last')) {
this.frameDelayLast = value;
} else {
this.frameDelayFirst = value;
}
}
this.step();
} else if (action === 'idle') {
// do nothing
} else {
throw new Error(`Invalid sequence action: ${action}`);
}
}
step() {
const { action, params } = this.takeStep();
if (!action) {
return;
}
this.handleStep(action, params);
}
get sequence() {
return this.element.getAttribute('sequence') || '';
}
}