@egjs/flicking-plugins 4.7.1 → 4.8.0-beta.0

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.
package/src/Arrow.ts CHANGED
@@ -1,4 +1,4 @@
1
- import Flicking, { EVENTS, FlickingError, Plugin } from "@egjs/flicking";
1
+ import Flicking, { EVENTS, FlickingError, Plugin, WillChangeEvent } from "@egjs/flicking";
2
2
 
3
3
  import { BROWSER } from "./event";
4
4
  import { ARROW } from "./const";
@@ -11,6 +11,7 @@ interface ArrowOptions {
11
11
  disabledClass: string;
12
12
  moveCount: number;
13
13
  moveByViewportSize: boolean;
14
+ interruptable: boolean;
14
15
  }
15
16
 
16
17
  /**
@@ -23,6 +24,7 @@ class Arrow implements Plugin {
23
24
  private _flicking: Flicking | null = null;
24
25
  private _prevEl: HTMLElement;
25
26
  private _nextEl: HTMLElement;
27
+ private _nextIndex: number;
26
28
 
27
29
  /* Options */
28
30
  private _parentEl: ArrowOptions["parentEl"];
@@ -31,6 +33,7 @@ class Arrow implements Plugin {
31
33
  private _disabledClass: ArrowOptions["disabledClass"];
32
34
  private _moveCount: ArrowOptions["moveCount"];
33
35
  private _moveByViewportSize: ArrowOptions["moveByViewportSize"];
36
+ private _interruptable: ArrowOptions["interruptable"];
34
37
 
35
38
  public get prevEl() { return this._prevEl; }
36
39
  public get nextEl() { return this._nextEl; }
@@ -41,6 +44,7 @@ class Arrow implements Plugin {
41
44
  public get disabledClass() { return this._disabledClass; }
42
45
  public get moveCount() { return this._moveCount; }
43
46
  public get moveByViewportSize() { return this._moveByViewportSize; }
47
+ public get interruptable() { return this._interruptable; }
44
48
 
45
49
  public set parentEl(val: ArrowOptions["parentEl"]) { this._parentEl = val; }
46
50
  public set prevElSelector(val: ArrowOptions["prevElSelector"]) { this._prevElSelector = val; }
@@ -48,6 +52,7 @@ class Arrow implements Plugin {
48
52
  public set disabledClass(val: ArrowOptions["disabledClass"]) { this._disabledClass = val; }
49
53
  public set moveCount(val: ArrowOptions["moveCount"]) { this._moveCount = val; }
50
54
  public set moveByViewportSize(val: ArrowOptions["moveByViewportSize"]) { this._moveByViewportSize = val; }
55
+ public set interruptable(val: ArrowOptions["interruptable"]) { this._interruptable = val; }
51
56
 
52
57
  public constructor({
53
58
  parentEl = null,
@@ -55,7 +60,8 @@ class Arrow implements Plugin {
55
60
  nextElSelector = ARROW.NEXT_SELECTOR,
56
61
  disabledClass = ARROW.DISABLED_CLASS,
57
62
  moveCount = 1,
58
- moveByViewportSize = false
63
+ moveByViewportSize = false,
64
+ interruptable = false
59
65
  }: Partial<ArrowOptions> = {}) {
60
66
  this._parentEl = parentEl;
61
67
  this._prevElSelector = prevElSelector;
@@ -63,6 +69,7 @@ class Arrow implements Plugin {
63
69
  this._disabledClass = disabledClass;
64
70
  this._moveCount = moveCount;
65
71
  this._moveByViewportSize = moveByViewportSize;
72
+ this._interruptable = interruptable;
66
73
  }
67
74
 
68
75
  public init(flicking: Flicking): void {
@@ -73,6 +80,7 @@ class Arrow implements Plugin {
73
80
  this._flicking = flicking;
74
81
 
75
82
  flicking.on(EVENTS.MOVE, this._onAnimation);
83
+ flicking.on(EVENTS.WILL_CHANGE, this._onWillChange);
76
84
 
77
85
  const parentEl = this._parentEl ? this._parentEl : flicking.element;
78
86
  const prevEl = getElement(this._prevElSelector, parentEl, "Arrow");
@@ -100,6 +108,7 @@ class Arrow implements Plugin {
100
108
  }
101
109
 
102
110
  flicking.off(EVENTS.MOVE, this._onAnimation);
111
+ flicking.off(EVENTS.WILL_CHANGE, this._onWillChange);
103
112
 
104
113
  const prevEl = this._prevEl;
105
114
  const nextEl = this._nextEl;
@@ -124,10 +133,16 @@ class Arrow implements Plugin {
124
133
 
125
134
  private _onPrevClick = () => {
126
135
  const flicking = this._flicking!;
136
+ const index = flicking.animating ? this._nextIndex : flicking.index;
137
+ const currentPanel = flicking.animating ? flicking.panels[this._nextIndex] : flicking.currentPanel;
127
138
  const camera = flicking.camera;
128
139
  const anchorPoints = camera.anchorPoints;
129
140
 
130
- if (flicking.animating || anchorPoints.length <= 0) return;
141
+ if ((flicking.animating && !this.interruptable) || anchorPoints.length <= 0) return;
142
+
143
+ if (flicking.animating) {
144
+ flicking.stopAnimation();
145
+ }
131
146
 
132
147
  const firstAnchor = anchorPoints[0];
133
148
  const moveCount = this._moveCount;
@@ -137,18 +152,18 @@ class Arrow implements Plugin {
137
152
  .catch(this._onCatch);
138
153
  } else {
139
154
  if (flicking.circularEnabled) {
140
- let targetPanel = flicking.currentPanel;
155
+ let targetPanel = currentPanel;
141
156
 
142
157
  for (let i = 0; i < moveCount; i++) {
143
158
  targetPanel = targetPanel.prev()!;
144
159
  }
145
160
 
146
161
  targetPanel.focus().catch(this._onCatch);
147
- } else if (flicking.index > firstAnchor.panel.index) {
148
- flicking.moveTo(Math.max(flicking.index - moveCount, firstAnchor.panel.index))
162
+ } else if (index > firstAnchor.panel.index) {
163
+ flicking.moveTo(Math.max(index - moveCount, firstAnchor.panel.index))
149
164
  .catch(this._onCatch);
150
165
  } else if (camera.position > camera.range.min) {
151
- flicking.moveTo(flicking.index)
166
+ flicking.moveTo(index)
152
167
  .catch(this._onCatch);
153
168
  }
154
169
  }
@@ -156,11 +171,16 @@ class Arrow implements Plugin {
156
171
 
157
172
  private _onNextClick = () => {
158
173
  const flicking = this._flicking!;
159
-
174
+ const index = flicking.animating ? this._nextIndex : flicking.index;
175
+ const currentPanel = flicking.animating ? flicking.panels[this._nextIndex] : flicking.currentPanel;
160
176
  const camera = flicking.camera;
161
177
  const anchorPoints = camera.anchorPoints;
162
178
 
163
- if (flicking.animating || anchorPoints.length <= 0) return;
179
+ if ((flicking.animating && !this.interruptable) || anchorPoints.length <= 0) return;
180
+
181
+ if (flicking.animating) {
182
+ flicking.stopAnimation();
183
+ }
164
184
 
165
185
  const lastAnchor = anchorPoints[anchorPoints.length - 1];
166
186
  const moveCount = this._moveCount;
@@ -170,18 +190,18 @@ class Arrow implements Plugin {
170
190
  .catch(this._onCatch);
171
191
  } else {
172
192
  if (flicking.circularEnabled) {
173
- let targetPanel = flicking.currentPanel;
193
+ let targetPanel = currentPanel;
174
194
 
175
195
  for (let i = 0; i < moveCount; i++) {
176
196
  targetPanel = targetPanel.next()!;
177
197
  }
178
198
 
179
199
  targetPanel.focus().catch(this._onCatch);
180
- } else if (flicking.index < lastAnchor.panel.index) {
181
- flicking.moveTo(Math.min(flicking.index + moveCount, lastAnchor.panel.index))
200
+ } else if (index < lastAnchor.panel.index) {
201
+ flicking.moveTo(Math.min(index + moveCount, lastAnchor.panel.index))
182
202
  .catch(this._onCatch);
183
203
  } else if (camera.position > camera.range.min) {
184
- flicking.moveTo(flicking.index)
204
+ flicking.moveTo(index)
185
205
  .catch(this._onCatch);
186
206
  }
187
207
  }
@@ -199,6 +219,10 @@ class Arrow implements Plugin {
199
219
  }
200
220
  };
201
221
 
222
+ private _onWillChange = (e: WillChangeEvent) => {
223
+ this._nextIndex = e.index;
224
+ };
225
+
202
226
  private _updateClass(pos: number) {
203
227
  const flicking = this._flicking!;
204
228
  const disabledClass = this._disabledClass;