-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
130 lines (91 loc) · 2.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Alphabet Jump Nav
@author Kevin Jantzer, Blackstone Publishing
*/
module.exports = class AlphabetJumpNav {
constructor(el){
this.el = el || document.createElement('div')
this.el.classList.add('alphabet-jump-nav')
let letters = this.letters.map(letter=>`<div class="letter">${letter}</div>`)
this.el.innerHTML = '<div class="bar">'+letters.join("\n")+'</div>'
+'<div class="preview"></div>'
this.el.querySelectorAll('.letter').forEach(el=>{
el.addEventListener('click', this.onClick.bind(this))
})
this.preview = this.el.querySelector('.preview')
this.onTouchMove = this.onTouchMove.bind(this)
this.el.addEventListener('touchstart', this.onTouchMove)
this.el.addEventListener('touchmove', _.throttle(this.onTouchMove, 50))
this.el.addEventListener('touchend', this.onTouchEnd.bind(this))
}
linkTo(el){
this.listEl = el
this.starterRows = {}
let starterRows = {}
el.childNodes.forEach(el=>{
let char = el.dataset.title&&el.dataset.title[0]
if( !char ) return
char = char.toLowerCase()
let charCode = char.charCodeAt(0) - 97
if( charCode < 0 || charCode >= 26 )
char = '#';
if( !starterRows[char] )
starterRows[char] = el
})
let last = null
// fill in missing rows
this.letters.reverse().forEach(letter=>{
if( !starterRows[letter] )
starterRows[letter] = last
else
last = starterRows[letter]
})
this.starterRows = starterRows
return this
}
appendTo(el){
el.appendChild(this.el)
return this
}
onClick(e){
this.scrollTo(e.currentTarget)
}
onTouchEnd(e){
this.isTouching = false
setTimeout(()=>{
this.preview.style.visibility = 'hidden'
}, 100)
}
onTouchMove(e){
e.stopPropagation()
e.preventDefault()
this.isTouching = true
let xPos = this.el.offsetLeft + (this.el.offsetWidth/2)
let yPos = e.touches[0].pageY;
let el = document.elementFromPoint(xPos, yPos)
this.preview.style.visibility = 'visible'
this.preview.style.top = (yPos - this.el.getBoundingClientRect().top - (this.preview.offsetHeight/2)) +'px'
this.scrollTo(el)
return false;
}
scrollTo(el){
if( !el || el == this.el || !this.el.contains(el) ) return;
let val = el.innerText.toLowerCase()
this.preview.innerHTML = val;
if( this.lastTouched == val ) return;
this.lastTouched = val
if( !this.starterRows || !this.listEl ) return
if( this.starterRows[this.lastTouched] )
this.starterRows[this.lastTouched].scrollIntoView()
else
this.listEl.lastElementChild.scrollIntoView()
if( !this.isTouching )
this.lastTouched = null
}
get letters(){
return ['#'].concat(Array
.apply(null, {length: 26})
.map((x, i) => String.fromCharCode(97 + i))
)
}
}