@egjs/flicking-plugins 4.5.0 → 4.7.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.
@@ -1,28 +1,48 @@
1
- import { FlickingError } from "@egjs/flicking";
2
-
3
1
  import { PAGINATION } from "../../const";
4
- import { BROWSER } from "../../event";
5
2
  import { addClass, removeClass } from "../../utils";
6
3
 
7
4
  import Renderer from "./Renderer";
8
5
 
9
6
  class BulletRenderer extends Renderer {
10
- private _childs: HTMLElement[] = [];
7
+ private _bullets: HTMLElement[] = [];
8
+ private _prevIndex: number = -1;
9
+
10
+ private get _bulletClass() {
11
+ const pagination = this._pagination;
12
+ return `${pagination.classPrefix}-${PAGINATION.BULLET_SUFFIX}`;
13
+ }
14
+
15
+ private get _activeClass() {
16
+ const pagination = this._pagination;
17
+ return `${pagination.classPrefix}-${PAGINATION.BULLET_ACTIVE_SUFFIX}`;
18
+ }
19
+
20
+ public destroy() {
21
+ this._bullets = [];
22
+ this._prevIndex = -1;
23
+ }
11
24
 
12
25
  public render() {
13
26
  const flicking = this._flicking;
14
27
  const pagination = this._pagination;
28
+ const wrapper = this._wrapper;
29
+ const bulletClass = this._bulletClass;
30
+ const activeClass = this._activeClass;
15
31
  const renderBullet = pagination.renderBullet;
32
+ const renderActiveBullet = pagination.renderActiveBullet;
16
33
  const bulletWrapperClass = `${pagination.classPrefix}-${PAGINATION.BULLET_WRAPPER_SUFFIX}`;
17
- const bulletClass = `${pagination.classPrefix}-${PAGINATION.BULLET_SUFFIX}`;
18
- const bulletActiveClass = `${pagination.classPrefix}-${PAGINATION.BULLET_ACTIVE_SUFFIX}`;
19
34
  const anchorPoints = flicking.camera.anchorPoints;
20
- const wrapper = this._wrapper;
21
35
 
22
36
  addClass(wrapper, bulletWrapperClass);
23
37
 
24
38
  wrapper.innerHTML = anchorPoints
25
- .map((_, index) => renderBullet(bulletClass, index))
39
+ .map((anchorPoint, index) => {
40
+ if (renderActiveBullet && anchorPoint.panel.index === flicking.index) {
41
+ return renderActiveBullet(bulletClass, index);
42
+ } else {
43
+ return renderBullet(bulletClass, index);
44
+ }
45
+ })
26
46
  .join("\n");
27
47
 
28
48
  const bullets = [].slice.call(wrapper.children) as HTMLElement[];
@@ -31,46 +51,66 @@ class BulletRenderer extends Renderer {
31
51
  const anchorPoint = anchorPoints[index];
32
52
 
33
53
  if (anchorPoint.panel.index === flicking.index) {
34
- addClass(bullet, bulletActiveClass);
54
+ addClass(bullet, activeClass);
55
+ this._prevIndex = index;
35
56
  }
36
57
 
37
- bullet.addEventListener(BROWSER.MOUSE_DOWN, e => {
38
- e.stopPropagation();
39
- });
40
-
41
- bullet.addEventListener(BROWSER.TOUCH_START, e => {
42
- e.stopPropagation();
43
- }, { passive: true });
44
-
45
- bullet.addEventListener(BROWSER.CLICK, () => {
46
- flicking.moveTo(anchorPoint.panel.index)
47
- .catch(err => {
48
- if (err instanceof FlickingError) return;
49
- throw err;
50
- });
51
- });
58
+ this._addBulletEvents(bullet, index);
52
59
  });
53
60
 
54
- this._childs = bullets;
61
+ this._bullets = bullets;
55
62
  }
56
63
 
57
64
  public update(index: number) {
58
65
  const flicking = this._flicking;
59
66
  const pagination = this._pagination;
60
- const bullets = this._childs;
61
- const activeClass = `${pagination.classPrefix}-${PAGINATION.BULLET_ACTIVE_SUFFIX}`;
67
+ const wrapper = this._wrapper;
68
+ const bullets = this._bullets;
69
+ const bulletClass = this._bulletClass;
70
+ const activeClass = this._activeClass;
71
+ const prevIndex = this._prevIndex;
62
72
  const anchorPoints = flicking.camera.anchorPoints;
73
+ const renderBullet = pagination.renderBullet;
74
+ const renderActiveBullet = pagination.renderActiveBullet;
63
75
 
64
76
  if (anchorPoints.length <= 0) return;
65
77
 
66
- bullets.forEach(bullet => {
67
- removeClass(bullet, activeClass);
68
- });
69
-
70
78
  const anchorOffset = anchorPoints[0].panel.index;
71
- const activeBullet = bullets[index - anchorOffset];
79
+ const activeBulletIndex = index - anchorOffset;
80
+
81
+ if (prevIndex === activeBulletIndex) return;
82
+
83
+ if (renderActiveBullet) {
84
+ const prevBullet = bullets[prevIndex];
85
+ if (prevBullet) {
86
+ const newBullet = this._createBulletFromString(
87
+ renderBullet(bulletClass, prevIndex),
88
+ prevIndex
89
+ );
90
+ prevBullet.parentElement!.replaceChild(newBullet, prevBullet);
91
+ bullets[prevIndex] = newBullet;
92
+ }
93
+
94
+ const activeBullet = bullets[activeBulletIndex];
95
+ const newActiveBullet = this._createBulletFromString(
96
+ renderActiveBullet(`${bulletClass} ${activeClass}`, activeBulletIndex),
97
+ activeBulletIndex
98
+ );
99
+
100
+ wrapper.replaceChild(newActiveBullet, activeBullet);
101
+ bullets[activeBulletIndex] = newActiveBullet;
102
+ } else {
103
+ const activeBullet = bullets[activeBulletIndex];
104
+ const prevBullet = bullets[prevIndex];
105
+
106
+ if (prevBullet) {
107
+ removeClass(prevBullet, activeClass);
108
+ }
109
+
110
+ addClass(activeBullet, activeClass);
111
+ }
72
112
 
73
- addClass(activeBullet, activeClass);
113
+ this._prevIndex = activeBulletIndex;
74
114
  }
75
115
  }
76
116
 
@@ -4,6 +4,14 @@ import { addClass } from "../../utils";
4
4
  import Renderer from "./Renderer";
5
5
 
6
6
  class FractionRenderer extends Renderer {
7
+ private _prevIndex: number = -1;
8
+ private _prevTotal: number = -1;
9
+
10
+ public destroy(): void {
11
+ this._prevIndex = -1;
12
+ this._prevTotal = -1;
13
+ }
14
+
7
15
  public render() {
8
16
  const flicking = this._flicking;
9
17
  const wrapper = this._wrapper;
@@ -23,19 +31,26 @@ class FractionRenderer extends Renderer {
23
31
  const flicking = this._flicking;
24
32
  const wrapper = this._wrapper;
25
33
  const pagination = this._pagination;
26
- const fractionCurrentClass = `${pagination.classPrefix}-${PAGINATION.FRACTION_CURRENT_SUFFIX}`;
27
- const fractionTotalClass = `${pagination.classPrefix}-${PAGINATION.FRACTION_TOTAL_SUFFIX}`;
28
34
 
29
- const currentWrapper = wrapper.querySelector(`.${fractionCurrentClass}`) as HTMLElement;
30
- const totalWrapper = wrapper.querySelector(`.${fractionTotalClass}`) as HTMLElement;
31
35
  const anchorPoints = flicking.camera.anchorPoints;
32
-
33
36
  const currentIndex = anchorPoints.length > 0
34
37
  ? index - anchorPoints[0].panel.index + 1
35
38
  : 0;
39
+ const anchorCount = anchorPoints.length;
40
+
41
+ if (currentIndex === this._prevIndex && anchorCount === this._prevTotal) return;
42
+
43
+ const fractionCurrentSelector = `.${pagination.classPrefix}-${PAGINATION.FRACTION_CURRENT_SUFFIX}`;
44
+ const fractionTotalSelector = `.${pagination.classPrefix}-${PAGINATION.FRACTION_TOTAL_SUFFIX}`;
45
+
46
+ const currentWrapper = wrapper.querySelector(fractionCurrentSelector) as HTMLElement;
47
+ const totalWrapper = wrapper.querySelector(fractionTotalSelector) as HTMLElement;
36
48
 
37
49
  currentWrapper.innerHTML = pagination.fractionCurrentFormat(currentIndex);
38
- totalWrapper.innerHTML = pagination.fractionTotalFormat(anchorPoints.length);
50
+ totalWrapper.innerHTML = pagination.fractionTotalFormat(anchorCount);
51
+
52
+ this._prevIndex = currentIndex;
53
+ this._prevTotal = anchorCount;
39
54
  }
40
55
  }
41
56
 
@@ -1,5 +1,6 @@
1
- import Flicking from "@egjs/flicking";
1
+ import Flicking, { FlickingError } from "@egjs/flicking";
2
2
 
3
+ import { BROWSER } from "../../event";
3
4
  import Pagination from "../Pagination";
4
5
 
5
6
  export interface RendererOptions {
@@ -23,8 +24,40 @@ abstract class Renderer {
23
24
  this._wrapper = wrapper;
24
25
  }
25
26
 
27
+ public abstract destroy(): void;
26
28
  public abstract render(): void;
27
29
  public abstract update(index: number): void;
30
+
31
+ protected _createBulletFromString(html: string, index: number) {
32
+ const range = document.createRange();
33
+ const frag = range.createContextualFragment(html);
34
+ const bullet = frag.firstChild as HTMLElement;
35
+
36
+ this._addBulletEvents(bullet, index);
37
+
38
+ return bullet;
39
+ }
40
+
41
+ protected _addBulletEvents(bullet: HTMLElement, index: number) {
42
+ const anchorPoints = this._flicking.camera.anchorPoints;
43
+ const panelIndex = anchorPoints[index].panel.index;
44
+
45
+ bullet.addEventListener(BROWSER.MOUSE_DOWN, e => {
46
+ e.stopPropagation();
47
+ });
48
+
49
+ bullet.addEventListener(BROWSER.TOUCH_START, e => {
50
+ e.stopPropagation();
51
+ }, { passive: true });
52
+
53
+ bullet.addEventListener(BROWSER.CLICK, () => {
54
+ this._flicking.moveTo(panelIndex)
55
+ .catch(err => {
56
+ if (err instanceof FlickingError) return;
57
+ throw err;
58
+ });
59
+ });
60
+ }
28
61
  }
29
62
 
30
63
  export default Renderer;
@@ -1,7 +1,6 @@
1
- import { DIRECTION, FlickingError } from "@egjs/flicking";
1
+ import { DIRECTION } from "@egjs/flicking";
2
2
 
3
3
  import { PAGINATION } from "../../const";
4
- import { BROWSER } from "../../event";
5
4
  import { addClass, removeClass } from "../../utils";
6
5
 
7
6
  import Renderer from "./Renderer";
@@ -12,6 +11,13 @@ class ScrollBulletRenderer extends Renderer {
12
11
  private _previousIndex: number = -1;
13
12
  private _sliderIndex: number = -1;
14
13
 
14
+ public destroy(): void {
15
+ this._bullets = [];
16
+ this._bulletSize = 0;
17
+ this._previousIndex = -1;
18
+ this._sliderIndex = -1;
19
+ }
20
+
15
21
  public render() {
16
22
  const wrapper = this._wrapper;
17
23
  const flicking = this._flicking;
@@ -38,23 +44,7 @@ class ScrollBulletRenderer extends Renderer {
38
44
  const bullets = [].slice.call(sliderEl.children) as HTMLElement[];
39
45
 
40
46
  bullets.forEach((bullet, index) => {
41
- const anchorPoint = anchorPoints[index];
42
-
43
- bullet.addEventListener(BROWSER.MOUSE_DOWN, e => {
44
- e.stopPropagation();
45
- });
46
-
47
- bullet.addEventListener(BROWSER.TOUCH_START, e => {
48
- e.stopPropagation();
49
- }, { passive: true });
50
-
51
- bullet.addEventListener(BROWSER.CLICK, () => {
52
- flicking.moveTo(anchorPoint.panel.index)
53
- .catch(err => {
54
- if (err instanceof FlickingError) return;
55
- throw err;
56
- });
57
- });
47
+ this._addBulletEvents(bullet, index);
58
48
  });
59
49
 
60
50
  if (bullets.length <= 0) return;
@@ -66,6 +56,7 @@ class ScrollBulletRenderer extends Renderer {
66
56
 
67
57
  this._bullets = bullets;
68
58
  this._bulletSize = bulletSize;
59
+ this._previousIndex = -1;
69
60
 
70
61
  this.update(this._flicking.index);
71
62
 
@@ -80,12 +71,16 @@ class ScrollBulletRenderer extends Renderer {
80
71
  const bullets = this._bullets;
81
72
  const prevIndex = this._previousIndex;
82
73
 
74
+ const renderBullet = pagination.renderBullet;
75
+ const renderActiveBullet = pagination.renderActiveBullet;
76
+
83
77
  const anchorPoints = flicking.camera.anchorPoints;
84
78
  const anchorOffset = anchorPoints[0].panel.index;
85
79
  const activeIndex = index - anchorOffset;
86
80
 
87
81
  if (anchorPoints.length <= 0) return;
88
82
 
83
+ const bulletClass = `${pagination.classPrefix}-${PAGINATION.BULLET_SUFFIX}`;
89
84
  const bulletActiveClass = `${pagination.classPrefix}-${PAGINATION.BULLET_ACTIVE_SUFFIX}`;
90
85
  const prevClassPrefix = `${pagination.classPrefix}-${PAGINATION.SCROLL_PREV_SUFFIX}`;
91
86
  const nextClassPrefix = `${pagination.classPrefix}-${PAGINATION.SCROLL_NEXT_SUFFIX}`;
@@ -95,6 +90,29 @@ class ScrollBulletRenderer extends Renderer {
95
90
  const prevClassRegex = new RegExp(`^${prevClassPrefix}`);
96
91
  const nextClassRegex = new RegExp(`^${nextClassPrefix}`);
97
92
 
93
+ if (renderActiveBullet) {
94
+ const prevBullet = bullets[prevIndex];
95
+ if (prevBullet) {
96
+ const newBullet = this._createBulletFromString(
97
+ renderBullet(bulletClass, prevIndex),
98
+ prevIndex
99
+ );
100
+ prevBullet.parentElement!.replaceChild(newBullet, prevBullet);
101
+ bullets[prevIndex] = newBullet;
102
+ }
103
+
104
+ const activeBullet = bullets[activeIndex];
105
+ if (activeBullet) {
106
+ const newActiveBullet = this._createBulletFromString(
107
+ renderActiveBullet(bulletClass, activeIndex),
108
+ activeIndex
109
+ );
110
+
111
+ activeBullet.parentElement!.replaceChild(newActiveBullet, activeBullet);
112
+ bullets[activeIndex] = newActiveBullet;
113
+ }
114
+ }
115
+
98
116
  bullets.forEach((bullet, idx) => {
99
117
  const indexOffset = idx - activeIndex;
100
118
  const classList = bullet.className.split(" ");
@@ -116,7 +134,7 @@ class ScrollBulletRenderer extends Renderer {
116
134
 
117
135
  pagination.scrollOnChange(activeIndex, {
118
136
  total: bullets.length,
119
- prevIndex: prevIndex,
137
+ prevIndex,
120
138
  sliderIndex: this._sliderIndex,
121
139
  direction: activeIndex > prevIndex ? DIRECTION.NEXT : DIRECTION.PREV,
122
140
  bullets: [...bullets],
@@ -1,2 +0,0 @@
1
- import Renderer from "./Renderer";
2
- export type { Renderer };
@@ -1,5 +0,0 @@
1
- import Renderer from "./Renderer";
2
-
3
- export type {
4
- Renderer
5
- };