1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,130 +0,0 @@ |
1 |
-import { window } from 'ssr-window'; |
|
2 |
-import Utils from '../../utils/utils'; |
|
3 |
- |
|
4 |
-const History = { |
|
5 |
- init() { |
|
6 |
- const swiper = this; |
|
7 |
- if (!swiper.params.history) return; |
|
8 |
- if (!window.history || !window.history.pushState) { |
|
9 |
- swiper.params.history.enabled = false; |
|
10 |
- swiper.params.hashNavigation.enabled = true; |
|
11 |
- return; |
|
12 |
- } |
|
13 |
- const history = swiper.history; |
|
14 |
- history.initialized = true; |
|
15 |
- history.paths = History.getPathValues(); |
|
16 |
- if (!history.paths.key && !history.paths.value) return; |
|
17 |
- history.scrollToSlide(0, history.paths.value, swiper.params.runCallbacksOnInit); |
|
18 |
- if (!swiper.params.history.replaceState) { |
|
19 |
- window.addEventListener('popstate', swiper.history.setHistoryPopState); |
|
20 |
- } |
|
21 |
- }, |
|
22 |
- destroy() { |
|
23 |
- const swiper = this; |
|
24 |
- if (!swiper.params.history.replaceState) { |
|
25 |
- window.removeEventListener('popstate', swiper.history.setHistoryPopState); |
|
26 |
- } |
|
27 |
- }, |
|
28 |
- setHistoryPopState() { |
|
29 |
- const swiper = this; |
|
30 |
- swiper.history.paths = History.getPathValues(); |
|
31 |
- swiper.history.scrollToSlide(swiper.params.speed, swiper.history.paths.value, false); |
|
32 |
- }, |
|
33 |
- getPathValues() { |
|
34 |
- const pathArray = window.location.pathname.slice(1).split('/').filter((part) => part !== ''); |
|
35 |
- const total = pathArray.length; |
|
36 |
- const key = pathArray[total - 2]; |
|
37 |
- const value = pathArray[total - 1]; |
|
38 |
- return { key, value }; |
|
39 |
- }, |
|
40 |
- setHistory(key, index) { |
|
41 |
- const swiper = this; |
|
42 |
- if (!swiper.history.initialized || !swiper.params.history.enabled) return; |
|
43 |
- const slide = swiper.slides.eq(index); |
|
44 |
- let value = History.slugify(slide.attr('data-history')); |
|
45 |
- if (!window.location.pathname.includes(key)) { |
|
46 |
- value = `${key}/${value}`; |
|
47 |
- } |
|
48 |
- const currentState = window.history.state; |
|
49 |
- if (currentState && currentState.value === value) { |
|
50 |
- return; |
|
51 |
- } |
|
52 |
- if (swiper.params.history.replaceState) { |
|
53 |
- window.history.replaceState({ value }, null, value); |
|
54 |
- } else { |
|
55 |
- window.history.pushState({ value }, null, value); |
|
56 |
- } |
|
57 |
- }, |
|
58 |
- slugify(text) { |
|
59 |
- return text.toString() |
|
60 |
- .replace(/\s+/g, '-') |
|
61 |
- .replace(/[^\w-]+/g, '') |
|
62 |
- .replace(/--+/g, '-') |
|
63 |
- .replace(/^-+/, '') |
|
64 |
- .replace(/-+$/, ''); |
|
65 |
- }, |
|
66 |
- scrollToSlide(speed, value, runCallbacks) { |
|
67 |
- const swiper = this; |
|
68 |
- if (value) { |
|
69 |
- for (let i = 0, length = swiper.slides.length; i < length; i += 1) { |
|
70 |
- const slide = swiper.slides.eq(i); |
|
71 |
- const slideHistory = History.slugify(slide.attr('data-history')); |
|
72 |
- if (slideHistory === value && !slide.hasClass(swiper.params.slideDuplicateClass)) { |
|
73 |
- const index = slide.index(); |
|
74 |
- swiper.slideTo(index, speed, runCallbacks); |
|
75 |
- } |
|
76 |
- } |
|
77 |
- } else { |
|
78 |
- swiper.slideTo(0, speed, runCallbacks); |
|
79 |
- } |
|
80 |
- }, |
|
81 |
-}; |
|
82 |
- |
|
83 |
-export default { |
|
84 |
- name: 'history', |
|
85 |
- params: { |
|
86 |
- history: { |
|
87 |
- enabled: false, |
|
88 |
- replaceState: false, |
|
89 |
- key: 'slides', |
|
90 |
- }, |
|
91 |
- }, |
|
92 |
- create() { |
|
93 |
- const swiper = this; |
|
94 |
- Utils.extend(swiper, { |
|
95 |
- history: { |
|
96 |
- init: History.init.bind(swiper), |
|
97 |
- setHistory: History.setHistory.bind(swiper), |
|
98 |
- setHistoryPopState: History.setHistoryPopState.bind(swiper), |
|
99 |
- scrollToSlide: History.scrollToSlide.bind(swiper), |
|
100 |
- destroy: History.destroy.bind(swiper), |
|
101 |
- }, |
|
102 |
- }); |
|
103 |
- }, |
|
104 |
- on: { |
|
105 |
- init() { |
|
106 |
- const swiper = this; |
|
107 |
- if (swiper.params.history.enabled) { |
|
108 |
- swiper.history.init(); |
|
109 |
- } |
|
110 |
- }, |
|
111 |
- destroy() { |
|
112 |
- const swiper = this; |
|
113 |
- if (swiper.params.history.enabled) { |
|
114 |
- swiper.history.destroy(); |
|
115 |
- } |
|
116 |
- }, |
|
117 |
- transitionEnd() { |
|
118 |
- const swiper = this; |
|
119 |
- if (swiper.history.initialized) { |
|
120 |
- swiper.history.setHistory(swiper.params.history.key, swiper.activeIndex); |
|
121 |
- } |
|
122 |
- }, |
|
123 |
- slideChange() { |
|
124 |
- const swiper = this; |
|
125 |
- if (swiper.history.initialized && swiper.params.cssMode) { |
|
126 |
- swiper.history.setHistory(swiper.params.history.key, swiper.activeIndex); |
|
127 |
- } |
|
128 |
- }, |
|
129 |
- }, |
|
130 |
-}; |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,130 @@ |
1 |
+import { window } from 'ssr-window'; |
|
2 |
+import Utils from '../../utils/utils'; |
|
3 |
+ |
|
4 |
+const History = { |
|
5 |
+ init() { |
|
6 |
+ const swiper = this; |
|
7 |
+ if (!swiper.params.history) return; |
|
8 |
+ if (!window.history || !window.history.pushState) { |
|
9 |
+ swiper.params.history.enabled = false; |
|
10 |
+ swiper.params.hashNavigation.enabled = true; |
|
11 |
+ return; |
|
12 |
+ } |
|
13 |
+ const history = swiper.history; |
|
14 |
+ history.initialized = true; |
|
15 |
+ history.paths = History.getPathValues(); |
|
16 |
+ if (!history.paths.key && !history.paths.value) return; |
|
17 |
+ history.scrollToSlide(0, history.paths.value, swiper.params.runCallbacksOnInit); |
|
18 |
+ if (!swiper.params.history.replaceState) { |
|
19 |
+ window.addEventListener('popstate', swiper.history.setHistoryPopState); |
|
20 |
+ } |
|
21 |
+ }, |
|
22 |
+ destroy() { |
|
23 |
+ const swiper = this; |
|
24 |
+ if (!swiper.params.history.replaceState) { |
|
25 |
+ window.removeEventListener('popstate', swiper.history.setHistoryPopState); |
|
26 |
+ } |
|
27 |
+ }, |
|
28 |
+ setHistoryPopState() { |
|
29 |
+ const swiper = this; |
|
30 |
+ swiper.history.paths = History.getPathValues(); |
|
31 |
+ swiper.history.scrollToSlide(swiper.params.speed, swiper.history.paths.value, false); |
|
32 |
+ }, |
|
33 |
+ getPathValues() { |
|
34 |
+ const pathArray = window.location.pathname.slice(1).split('/').filter((part) => part !== ''); |
|
35 |
+ const total = pathArray.length; |
|
36 |
+ const key = pathArray[total - 2]; |
|
37 |
+ const value = pathArray[total - 1]; |
|
38 |
+ return { key, value }; |
|
39 |
+ }, |
|
40 |
+ setHistory(key, index) { |
|
41 |
+ const swiper = this; |
|
42 |
+ if (!swiper.history.initialized || !swiper.params.history.enabled) return; |
|
43 |
+ const slide = swiper.slides.eq(index); |
|
44 |
+ let value = History.slugify(slide.attr('data-history')); |
|
45 |
+ if (!window.location.pathname.includes(key)) { |
|
46 |
+ value = `${key}/${value}`; |
|
47 |
+ } |
|
48 |
+ const currentState = window.history.state; |
|
49 |
+ if (currentState && currentState.value === value) { |
|
50 |
+ return; |
|
51 |
+ } |
|
52 |
+ if (swiper.params.history.replaceState) { |
|
53 |
+ window.history.replaceState({ value }, null, value); |
|
54 |
+ } else { |
|
55 |
+ window.history.pushState({ value }, null, value); |
|
56 |
+ } |
|
57 |
+ }, |
|
58 |
+ slugify(text) { |
|
59 |
+ return text.toString() |
|
60 |
+ .replace(/\s+/g, '-') |
|
61 |
+ .replace(/[^\w-]+/g, '') |
|
62 |
+ .replace(/--+/g, '-') |
|
63 |
+ .replace(/^-+/, '') |
|
64 |
+ .replace(/-+$/, ''); |
|
65 |
+ }, |
|
66 |
+ scrollToSlide(speed, value, runCallbacks) { |
|
67 |
+ const swiper = this; |
|
68 |
+ if (value) { |
|
69 |
+ for (let i = 0, length = swiper.slides.length; i < length; i += 1) { |
|
70 |
+ const slide = swiper.slides.eq(i); |
|
71 |
+ const slideHistory = History.slugify(slide.attr('data-history')); |
|
72 |
+ if (slideHistory === value && !slide.hasClass(swiper.params.slideDuplicateClass)) { |
|
73 |
+ const index = slide.index(); |
|
74 |
+ swiper.slideTo(index, speed, runCallbacks); |
|
75 |
+ } |
|
76 |
+ } |
|
77 |
+ } else { |
|
78 |
+ swiper.slideTo(0, speed, runCallbacks); |
|
79 |
+ } |
|
80 |
+ }, |
|
81 |
+}; |
|
82 |
+ |
|
83 |
+export default { |
|
84 |
+ name: 'history', |
|
85 |
+ params: { |
|
86 |
+ history: { |
|
87 |
+ enabled: false, |
|
88 |
+ replaceState: false, |
|
89 |
+ key: 'slides', |
|
90 |
+ }, |
|
91 |
+ }, |
|
92 |
+ create() { |
|
93 |
+ const swiper = this; |
|
94 |
+ Utils.extend(swiper, { |
|
95 |
+ history: { |
|
96 |
+ init: History.init.bind(swiper), |
|
97 |
+ setHistory: History.setHistory.bind(swiper), |
|
98 |
+ setHistoryPopState: History.setHistoryPopState.bind(swiper), |
|
99 |
+ scrollToSlide: History.scrollToSlide.bind(swiper), |
|
100 |
+ destroy: History.destroy.bind(swiper), |
|
101 |
+ }, |
|
102 |
+ }); |
|
103 |
+ }, |
|
104 |
+ on: { |
|
105 |
+ init() { |
|
106 |
+ const swiper = this; |
|
107 |
+ if (swiper.params.history.enabled) { |
|
108 |
+ swiper.history.init(); |
|
109 |
+ } |
|
110 |
+ }, |
|
111 |
+ destroy() { |
|
112 |
+ const swiper = this; |
|
113 |
+ if (swiper.params.history.enabled) { |
|
114 |
+ swiper.history.destroy(); |
|
115 |
+ } |
|
116 |
+ }, |
|
117 |
+ transitionEnd() { |
|
118 |
+ const swiper = this; |
|
119 |
+ if (swiper.history.initialized) { |
|
120 |
+ swiper.history.setHistory(swiper.params.history.key, swiper.activeIndex); |
|
121 |
+ } |
|
122 |
+ }, |
|
123 |
+ slideChange() { |
|
124 |
+ const swiper = this; |
|
125 |
+ if (swiper.history.initialized && swiper.params.cssMode) { |
|
126 |
+ swiper.history.setHistory(swiper.params.history.key, swiper.activeIndex); |
|
127 |
+ } |
|
128 |
+ }, |
|
129 |
+ }, |
|
130 |
+}; |