-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
178 lines (159 loc) · 5.49 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const yo = require('yo-yo')
const onload = require('on-load')
const nanoraf = require('nanoraf')
const insertCss = require('insert-css')
const debounce = require('lodash.debounce')
insertCss(`
.dom-minimap-section {
position: absolute;
background-color: lightgrey;
overflow: hidden;
color: grey;
font-size: 11px;
padding-left: 2px;
border-radius: 2px;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: pointer;
left: 5px; right: 5px;
}
.dom-minimap-section:hover {
background-color: #e6e6e6;
}
.dom-minimap-scroll {
pointer-events: none;
position: absolute;
background-color: rgba(0,0,0,0.15);
top: 0; left: 0; right: 0; bottom: 0;
}
`)
module.exports = minimap
function minimap (opts) {
opts = opts || {}
opts.sections = opts.sections || 'minimap-section'
if (typeof opts.sections !== 'function') {
var sectionName = opts.sections
opts.sections = (container) => Array.prototype.slice.call(container.getElementsByClassName(sectionName))
}
opts.title = opts.title || 'data-section-title'
if (typeof opts.title !== 'function') {
var titleName = opts.title
opts.title = (section) => section.getAttribute(titleName)
}
opts.tooltip = opts.tooltip || 'data-section-tooltip'
if (typeof opts.hover !== 'function') {
var tooltipName = opts.tooltip
opts.tooltip = (section) => section.getAttribute(tooltipName)
}
opts.content = opts.content || 'minimap-content'
opts.mapStyle = typeof opts.mapStyle !== 'undefined' ? opts.mapStyle : 'height: 100%;'
opts.sectionStyle = opts.sectionStyle || ''
opts.clickOffset = opts.clickOffset || 0
var lastContainerHeight
var container
var element = document.createElement('div')
element.style.flex = '1'
const render = nanoraf(renderMap)
var state = { opts: opts }
onload(element, function load () {
container = typeof opts.content === 'string' ? document.getElementById(opts.content) : opts.content
lastContainerHeight = container.scrollHeight
// update on scroll event
container.addEventListener('scroll', scrollUpdate)
// update on window resize event
window.addEventListener('resize', debounce(update), 100)
// update on element loaded
update()
}, null, minimap)
element.addEventListener('wheel', function (event) {
if (container) container.scrollTop = container.scrollTop + event.deltaY
})
function scrollUpdate () {
state.scroll = getScroll(container)
var top = element.querySelector('.dom-minimap-scroll-top')
var bottom = element.querySelector('.dom-minimap-scroll-bottom')
if (!top || !bottom) return
top.style.bottom = state.scroll.topFromBottom
bottom.style.top = state.scroll.bottomFromTop
}
function update () {
var sections = getSections(container, element, opts)
if (!sections) return
var newState = Object.assign({}, state, {
sections: sections,
scroll: getScroll(container)
})
render(newState, state)
state = newState
}
return function () {
if (container) {
setTimeout(function () {
if (lastContainerHeight !== container.scrollHeight) {
update()
}
}, 1)
}
return element
}
function scrollTo () {
var top = this.style.top.slice(0, -1)
if (top) container.scrollTop = Math.round((container.scrollHeight * top) / 100) + opts.clickOffset
}
function renderMap (state) {
var content = yo`<div style="margin-top:20px;text-align:center">loading</div>`
if (state.sections) {
content = state.sections.map((section) => {
return yo`
<div class="dom-minimap-section unselectable"
title=${section.tooltip} onclick=${scrollTo}
style="top:${section.top};bottom:${section.bottom};${(
typeof opts.sectionStyle === 'function' ? opts.sectionStyle(section) : opts.sectionStyle
)}">
${section.title}
</div>
`
}).concat([
yo`<div class="dom-minimap-scroll dom-minimap-scroll-top" style="bottom:${state.scroll.topFromBottom}"></div>`,
yo`<div class="dom-minimap-scroll dom-minimap-scroll-bottom" style="top:${state.scroll.bottomFromTop}"></div>`
])
}
yo.update(element, yo`<div style='position:relative;${opts.mapStyle}'>${content}</div>`)
}
}
function getScroll (container) {
var top = container.scrollTop
var cHeight = container.clientHeight
var height = container.scrollHeight
return {
topFromBottom: ((height - top) / height * 100) + '%',
bottomFromTop: ((1 - ((height - top - cHeight) / height)) * 100) + '%'
}
}
function getSections (content, map, opts) {
if (!map.parentElement) return false
var cHeight = content.scrollHeight
var mHeight = map.parentElement.clientHeight
var cBounds = content.getBoundingClientRect()
var scrollTop = content.scrollTop
return opts.sections(content).map((section) => {
var bounds = section.getBoundingClientRect()
var top = (bounds.top - cBounds.top + scrollTop) / cHeight
var bottom = (bounds.bottom - cBounds.top + scrollTop) / cHeight
return {
top: top * 100 + '%',
bottom: applyPadding((1 - bottom) * 100 + '%', opts.paddingBottom),
title: opts.title(section, (mHeight * bottom) - mHeight * top),
tooltip: opts.tooltip(section),
element: section
}
})
}
function applyPadding (value, padding) {
if (!padding) return value
return `calc(${value} + ${padding})`
}