1 | import createElementIfNotDefined from '../../shared/create-element-if-not-defined.js';
|
2 | import $ from '../../shared/dom.js';
|
3 | export default function Navigation({
|
4 | swiper,
|
5 | extendParams,
|
6 | on,
|
7 | emit
|
8 | }) {
|
9 | extendParams({
|
10 | navigation: {
|
11 | nextEl: null,
|
12 | prevEl: null,
|
13 | hideOnClick: false,
|
14 | disabledClass: 'swiper-button-disabled',
|
15 | hiddenClass: 'swiper-button-hidden',
|
16 | lockClass: 'swiper-button-lock',
|
17 | navigationDisabledClass: 'swiper-navigation-disabled'
|
18 | }
|
19 | });
|
20 | swiper.navigation = {
|
21 | nextEl: null,
|
22 | $nextEl: null,
|
23 | prevEl: null,
|
24 | $prevEl: null
|
25 | };
|
26 |
|
27 | function getEl(el) {
|
28 | let $el;
|
29 |
|
30 | if (el) {
|
31 | $el = $(el);
|
32 |
|
33 | if (swiper.params.uniqueNavElements && typeof el === 'string' && $el.length > 1 && swiper.$el.find(el).length === 1) {
|
34 | $el = swiper.$el.find(el);
|
35 | }
|
36 | }
|
37 |
|
38 | return $el;
|
39 | }
|
40 |
|
41 | function toggleEl($el, disabled) {
|
42 | const params = swiper.params.navigation;
|
43 |
|
44 | if ($el && $el.length > 0) {
|
45 | $el[disabled ? 'addClass' : 'removeClass'](params.disabledClass);
|
46 | if ($el[0] && $el[0].tagName === 'BUTTON') $el[0].disabled = disabled;
|
47 |
|
48 | if (swiper.params.watchOverflow && swiper.enabled) {
|
49 | $el[swiper.isLocked ? 'addClass' : 'removeClass'](params.lockClass);
|
50 | }
|
51 | }
|
52 | }
|
53 |
|
54 | function update() {
|
55 |
|
56 | if (swiper.params.loop) return;
|
57 | const {
|
58 | $nextEl,
|
59 | $prevEl
|
60 | } = swiper.navigation;
|
61 | toggleEl($prevEl, swiper.isBeginning && !swiper.params.rewind);
|
62 | toggleEl($nextEl, swiper.isEnd && !swiper.params.rewind);
|
63 | }
|
64 |
|
65 | function onPrevClick(e) {
|
66 | e.preventDefault();
|
67 | if (swiper.isBeginning && !swiper.params.loop && !swiper.params.rewind) return;
|
68 | swiper.slidePrev();
|
69 | emit('navigationPrev');
|
70 | }
|
71 |
|
72 | function onNextClick(e) {
|
73 | e.preventDefault();
|
74 | if (swiper.isEnd && !swiper.params.loop && !swiper.params.rewind) return;
|
75 | swiper.slideNext();
|
76 | emit('navigationNext');
|
77 | }
|
78 |
|
79 | function init() {
|
80 | const params = swiper.params.navigation;
|
81 | swiper.params.navigation = createElementIfNotDefined(swiper, swiper.originalParams.navigation, swiper.params.navigation, {
|
82 | nextEl: 'swiper-button-next',
|
83 | prevEl: 'swiper-button-prev'
|
84 | });
|
85 | if (!(params.nextEl || params.prevEl)) return;
|
86 | const $nextEl = getEl(params.nextEl);
|
87 | const $prevEl = getEl(params.prevEl);
|
88 |
|
89 | if ($nextEl && $nextEl.length > 0) {
|
90 | $nextEl.on('click', onNextClick);
|
91 | }
|
92 |
|
93 | if ($prevEl && $prevEl.length > 0) {
|
94 | $prevEl.on('click', onPrevClick);
|
95 | }
|
96 |
|
97 | Object.assign(swiper.navigation, {
|
98 | $nextEl,
|
99 | nextEl: $nextEl && $nextEl[0],
|
100 | $prevEl,
|
101 | prevEl: $prevEl && $prevEl[0]
|
102 | });
|
103 |
|
104 | if (!swiper.enabled) {
|
105 | if ($nextEl) $nextEl.addClass(params.lockClass);
|
106 | if ($prevEl) $prevEl.addClass(params.lockClass);
|
107 | }
|
108 | }
|
109 |
|
110 | function destroy() {
|
111 | const {
|
112 | $nextEl,
|
113 | $prevEl
|
114 | } = swiper.navigation;
|
115 |
|
116 | if ($nextEl && $nextEl.length) {
|
117 | $nextEl.off('click', onNextClick);
|
118 | $nextEl.removeClass(swiper.params.navigation.disabledClass);
|
119 | }
|
120 |
|
121 | if ($prevEl && $prevEl.length) {
|
122 | $prevEl.off('click', onPrevClick);
|
123 | $prevEl.removeClass(swiper.params.navigation.disabledClass);
|
124 | }
|
125 | }
|
126 |
|
127 | on('init', () => {
|
128 | if (swiper.params.navigation.enabled === false) {
|
129 |
|
130 | disable();
|
131 | } else {
|
132 | init();
|
133 | update();
|
134 | }
|
135 | });
|
136 | on('toEdge fromEdge lock unlock', () => {
|
137 | update();
|
138 | });
|
139 | on('destroy', () => {
|
140 | destroy();
|
141 | });
|
142 | on('enable disable', () => {
|
143 | const {
|
144 | $nextEl,
|
145 | $prevEl
|
146 | } = swiper.navigation;
|
147 |
|
148 | if ($nextEl) {
|
149 | $nextEl[swiper.enabled ? 'removeClass' : 'addClass'](swiper.params.navigation.lockClass);
|
150 | }
|
151 |
|
152 | if ($prevEl) {
|
153 | $prevEl[swiper.enabled ? 'removeClass' : 'addClass'](swiper.params.navigation.lockClass);
|
154 | }
|
155 | });
|
156 | on('click', (_s, e) => {
|
157 | const {
|
158 | $nextEl,
|
159 | $prevEl
|
160 | } = swiper.navigation;
|
161 | const targetEl = e.target;
|
162 |
|
163 | if (swiper.params.navigation.hideOnClick && !$(targetEl).is($prevEl) && !$(targetEl).is($nextEl)) {
|
164 | if (swiper.pagination && swiper.params.pagination && swiper.params.pagination.clickable && (swiper.pagination.el === targetEl || swiper.pagination.el.contains(targetEl))) return;
|
165 | let isHidden;
|
166 |
|
167 | if ($nextEl) {
|
168 | isHidden = $nextEl.hasClass(swiper.params.navigation.hiddenClass);
|
169 | } else if ($prevEl) {
|
170 | isHidden = $prevEl.hasClass(swiper.params.navigation.hiddenClass);
|
171 | }
|
172 |
|
173 | if (isHidden === true) {
|
174 | emit('navigationShow');
|
175 | } else {
|
176 | emit('navigationHide');
|
177 | }
|
178 |
|
179 | if ($nextEl) {
|
180 | $nextEl.toggleClass(swiper.params.navigation.hiddenClass);
|
181 | }
|
182 |
|
183 | if ($prevEl) {
|
184 | $prevEl.toggleClass(swiper.params.navigation.hiddenClass);
|
185 | }
|
186 | }
|
187 | });
|
188 |
|
189 | const enable = () => {
|
190 | swiper.$el.removeClass(swiper.params.navigation.navigationDisabledClass);
|
191 | init();
|
192 | update();
|
193 | };
|
194 |
|
195 | const disable = () => {
|
196 | swiper.$el.addClass(swiper.params.navigation.navigationDisabledClass);
|
197 | destroy();
|
198 | };
|
199 |
|
200 | Object.assign(swiper.navigation, {
|
201 | enable,
|
202 | disable,
|
203 | update,
|
204 | init,
|
205 | destroy
|
206 | });
|
207 | } |
\ | No newline at end of file |