dnet45/modules/uoa-repository-dashboard-gui/trunk/src/public/js/core/scrollspy-nav.js @ 46812
1 |
import { $, offsetTop, toJQuery, isInView } from '../util/index'; |
---|---|
2 |
|
3 |
export default function (UIkit) { |
4 |
|
5 |
UIkit.component('scrollspy-nav', { |
6 |
|
7 |
props: { |
8 |
cls: String, |
9 |
closest: String, |
10 |
scroll: Boolean, |
11 |
overflow: Boolean, |
12 |
offset: Number |
13 |
},
|
14 |
|
15 |
defaults: { |
16 |
cls: 'uk-active', |
17 |
closest: false, |
18 |
scroll: false, |
19 |
overflow: true, |
20 |
offset: 0 |
21 |
},
|
22 |
|
23 |
computed: { |
24 |
|
25 |
links() { |
26 |
return this.$el.find('a[href^="#"]').filter((i, el) => el.hash); |
27 |
},
|
28 |
|
29 |
elements() { |
30 |
return this.closest ? this.links.closest(this.closest) : this.links; |
31 |
},
|
32 |
|
33 |
targets() { |
34 |
return $(this.links.toArray().map(el => el.hash).join(',')); |
35 |
}
|
36 |
|
37 |
},
|
38 |
|
39 |
update: [ |
40 |
|
41 |
{
|
42 |
|
43 |
read() { |
44 |
if (this.scroll) { |
45 |
UIkit.scroll(this.links, {offset: this.offset || 0}); |
46 |
}
|
47 |
}
|
48 |
|
49 |
},
|
50 |
|
51 |
{
|
52 |
|
53 |
read() { |
54 |
|
55 |
var scroll = window.pageYOffset + this.offset, max = document.documentElement.scrollHeight - window.innerHeight + this.offset; |
56 |
|
57 |
this.active = false; |
58 |
|
59 |
this.targets.each((i, el) => { |
60 |
|
61 |
el = $(el); |
62 |
|
63 |
var top = offsetTop(el), last = i + 1 === this.targets.length; |
64 |
if (!this.overflow && (i === 0 && top > scroll || last && top + el[0].offsetTop < scroll)) { |
65 |
return false; |
66 |
}
|
67 |
|
68 |
if (!last && offsetTop(this.targets.eq(i + 1)) <= scroll) { |
69 |
return; |
70 |
}
|
71 |
|
72 |
if (scroll >= max) { |
73 |
for (var j = this.targets.length - 1; j > i; j--) { |
74 |
if (isInView(this.targets.eq(j))) { |
75 |
el = this.targets.eq(j); |
76 |
break; |
77 |
}
|
78 |
}
|
79 |
}
|
80 |
|
81 |
return !(this.active = toJQuery(this.links.filter(`[href="#${el.attr('id')}"]`))); |
82 |
|
83 |
});
|
84 |
|
85 |
},
|
86 |
|
87 |
write() { |
88 |
|
89 |
this.links.blur(); |
90 |
this.elements.removeClass(this.cls); |
91 |
|
92 |
if (this.active) { |
93 |
this.$el.trigger('active', [ |
94 |
this.active, |
95 |
(this.closest ? this.active.closest(this.closest) : this.active).addClass(this.cls) |
96 |
]);
|
97 |
}
|
98 |
|
99 |
},
|
100 |
|
101 |
events: ['scroll', 'load', 'resize'] |
102 |
|
103 |
}
|
104 |
|
105 |
]
|
106 |
|
107 |
});
|
108 |
|
109 |
}
|