scratch – Blame information for rev 117
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
117 | office | 1 | /*========================= |
2 | History Api with fallback to Hashnav |
||
3 | ===========================*/ |
||
4 | s.history = { |
||
5 | init: function () { |
||
6 | if (!s.params.history) return; |
||
7 | if (!window.history || !window.history.pushState) { |
||
8 | s.params.history = false; |
||
9 | s.params.hashnav = true; |
||
10 | return; |
||
11 | } |
||
12 | s.history.initialized = true; |
||
13 | this.paths = this.getPathValues(); |
||
14 | if (!this.paths.key && !this.paths.value) return; |
||
15 | this.scrollToSlide(0, this.paths.value, s.params.runCallbacksOnInit); |
||
16 | if (!s.params.replaceState) { |
||
17 | window.addEventListener('popstate', this.setHistoryPopState); |
||
18 | } |
||
19 | }, |
||
20 | setHistoryPopState: function() { |
||
21 | s.history.paths = s.history.getPathValues(); |
||
22 | s.history.scrollToSlide(s.params.speed, s.history.paths.value, false); |
||
23 | }, |
||
24 | getPathValues: function() { |
||
25 | var pathArray = window.location.pathname.slice(1).split('/'); |
||
26 | var total = pathArray.length; |
||
27 | var key = pathArray[total - 2]; |
||
28 | var value = pathArray[total - 1]; |
||
29 | return { key: key, value: value }; |
||
30 | }, |
||
31 | setHistory: function (key, index) { |
||
32 | if (!s.history.initialized || !s.params.history) return; |
||
33 | var slide = s.slides.eq(index); |
||
34 | var value = this.slugify(slide.attr('data-history')); |
||
35 | if (!window.location.pathname.includes(key)) { |
||
36 | value = key + '/' + value; |
||
37 | } |
||
38 | if (s.params.replaceState) { |
||
39 | window.history.replaceState(null, null, value); |
||
40 | } else { |
||
41 | window.history.pushState(null, null, value); |
||
42 | } |
||
43 | }, |
||
44 | slugify: function(text) { |
||
45 | return text.toString().toLowerCase() |
||
46 | .replace(/\s+/g, '-') |
||
47 | .replace(/[^\w\-]+/g, '') |
||
48 | .replace(/\-\-+/g, '-') |
||
49 | .replace(/^-+/, '') |
||
50 | .replace(/-+$/, ''); |
||
51 | }, |
||
52 | scrollToSlide: function(speed, value, runCallbacks) { |
||
53 | if (value) { |
||
54 | for (var i = 0, length = s.slides.length; i < length; i++) { |
||
55 | var slide = s.slides.eq(i); |
||
56 | var slideHistory = this.slugify(slide.attr('data-history')); |
||
57 | if (slideHistory === value && !slide.hasClass(s.params.slideDuplicateClass)) { |
||
58 | var index = slide.index(); |
||
59 | s.slideTo(index, speed, runCallbacks); |
||
60 | } |
||
61 | } |
||
62 | } else { |
||
63 | s.slideTo(0, speed, runCallbacks); |
||
64 | } |
||
65 | } |
||
66 | }; |