-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
391 lines (320 loc) · 10.5 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import * as d3drag from 'd3-drag'
import * as d3 from 'd3-selection'
import { EventEmitter } from 'events'
import util from 'util'
const clamp = function (value, min, max) {
return Math.min(Math.max(value, min), max)
}
const buildState = nodes => {
return nodes.map((node, idx) => {
let locked = node.classList.contains('draggable-list-lock')
, style = window.getComputedStyle(node)
, bb = node.getBoundingClientRect()
return {
node,
idx,
locked,
bb: Object.assign(bb, {
// Include margins of the node
outerHeight: bb.height + parseInt(style['margin-top']) + parseInt(style['margin-bottom']),
})
}
})
}
const shouldSwap = (state, travelerCenter) => {
// Build a virtual list so we don't thrash the DOM
let top = 0
, list = state.map(item => {
let result = {
top,
height: item.bb.outerHeight,
center: top + item.bb.height / 2,
placeholder: item.placeholder,
target: item.target,
}
top += result.height
return result
})
let placeholder = list.find(item => item.placeholder)
, target = list.find(item => item.target)
const isCloserToTarget = () => {
return Math.abs(travelerCenter - placeholder.center) > Math.abs(travelerCenter - target.center)
}
let swap = false
// Based on current location, determine if the traveler node is now closer
// to the target
if (isCloserToTarget()) {
// It's closer to the target, set the swap flag
swap = true
// Now re-arrange the list items and figure out new dimensions
list.splice(list.indexOf(target), 0, list.splice(list.indexOf(placeholder), 1)[0])
// Reassign dimensions based on the virtual location
let top = 0
list.forEach(item => {
item.top = top
item.center = top + item.height / 2
top += item.height
})
// Check another time to see if it's closer to the target again. If so
// We don't want to swap b/c it will "jump" between the nodes
if (isCloserToTarget()) {
// Don't do anything, the original location was a better match
swap = false
}
}
return swap
}
const isInvalid = (target, targetIndex, currentIndex, lockedIndexes) => {
if (!target) {
// We don't have a place to drop this node that's in the ul
return true
}
if (currentIndex == targetIndex) {
// Same node
return true
}
if (target.animated) {
// already working
return true
}
if (targetIndex == -1) {
// Not on a real node, carry on
return true
}
if (lockedIndexes.includes(targetIndex)) {
// Not a valid location
return true
}
return false
}
const animateItem = (startingBB, node) => {
let endingBB = node.getBoundingClientRect()
, ms = 250
if (startingBB.top == endingBB.top) {
// Same location, exit early
return
}
d3.select(node)
.style('transition', 'none')
.style('transform', 'translate(0px,'+ (startingBB.top - endingBB.top) + 'px)')
node.offsetWidth // trigger reflow
d3.select(node)
.style('transition', 'transform ' + ms + 'ms')
.style('transform', 'translate(0,0)')
clearTimeout(node.animated)
node.animated = setTimeout(function () {
d3.select(node)
.style('transition', '')
.style('transform', '')
node.animated = false
}, ms)
}
function dnd (container, options = {}) {
let ul = d3.select(container)
.style('position', 'relative') // needed for dnd to work
.classed('draggable-list', true)
, self = this
, drag = d3drag.drag()
.filter(function (e) {
let nodrag = d3.select(e.target).classed('draggable-list-nodrag') || // check target node
d3.select(this).classed('draggable-list-nodrag') || // check `li` element can be dragged
d3.select(this).classed('draggable-list-lock') // Make sure item isn't locked
, p = e.target.parentNode
// walk tree between target and list element seeing if there is a nodrag along the way
while (e.target !== this && p && p !== this && !nodrag) {
if (d3.select(p).classed('draggable-list-nodrag')) {
nodrag = true
}
p = p.parentNode
}
return !e.button && // prevent right clicks
!nodrag
})
, travelerTimeout = null
, dragging = false
, scrollEl = options.scrollEl
, _autoscrollTimeout
, getNodes = () => {
return Array.prototype.slice.call(container.children).filter(node => {
return !node.classList.contains('traveler')
})
}
drag.on('start', function (e) {
let nodes = getNodes()
, state = buildState(nodes)
, placeholderIndex = nodes.indexOf(this)
, node = this
d3.select(this).property('__startIndex__', placeholderIndex)
travelerTimeout = setTimeout(dndstart.bind(null, this), 300)
d3.select(window)
.on('keydown.dnd-escape', function (e) {
// If it's the escape, then cancel
if (e.keyCode === 27) {
self.emit('dndcancel')
cleanup(node)
rearrange(state, nodes.indexOf(node), placeholderIndex)
}
})
})
drag.on('drag', function (e) {
if (!dragging) {
dndstart(this)
clearTimeout(travelerTimeout)
dragging = true
}
_autoscroll(this, e.y)
_move(this, e.y)
})
drag.on('end', function () {
if (travelerTimeout) {
window.clearTimeout(travelerTimeout)
}
let startIndex = d3.select(this).property('__startIndex__')
, newIndex = getNodes().indexOf(this)
if (newIndex !== startIndex) {
self.emit('move', this, newIndex, startIndex)
}
cleanup(this)
self.emit('dndstop')
})
function _autoscroll (node, y) {
if (_autoscrollTimeout) {
// Prevent race conditions
window.clearTimeout(_autoscrollTimeout)
_autoscrollTimeout = null
}
if (!dragging || !scrollEl) {
return
}
let threshold = 30
, pixels = 10
if (y - threshold <= scrollEl.scrollTop) {
scroll(-pixels) // Scroll up
}
if (y + threshold >= scrollEl.scrollTop + scrollEl.offsetHeight) {
scroll(pixels) // Scroll down
}
function scroll (pixels) {
scrollEl.scrollTop += pixels
y += pixels
_move(node, y)
_autoscrollTimeout = setTimeout(function () {
_autoscroll(node, y)
}, 10)
}
}
function _move (node, y) {
let bb = node.getBoundingClientRect()
, containerBottom = container.offsetHeight + container.scrollTop
, halfHeight = bb.height / 2
, travelerTop = clamp(y - halfHeight, -halfHeight, containerBottom - halfHeight) // Top of the moving node
, travelerCenter = travelerTop + halfHeight
, traveler = ul.selectChildren('.traveler')
, x = Math.ceil(bb.left) // Round up to ensure we're inside of the `li` node in case a browser rounds down
// the `elementFromPoint` call.
, nodes = getNodes()
, placeholderIndex = nodes.indexOf(node)
, state = buildState(nodes)
, lockedIndexes = state.filter(obj => obj.locked).map(obj => obj.idx)
traveler.style('top', `${travelerTop}px`)
.style('display', 'none') // Hide it so we can get the node under the traveler
let target = document.elementFromPoint(x, travelerCenter + container.getBoundingClientRect().top)
, targetIndex = nodes.indexOf(target)
traveler.style('display', '') // Show it again
if (isInvalid(target, targetIndex, placeholderIndex, lockedIndexes)) {
// Can't move in this location
return
}
// Store placeholder and target fields on the state objects so
// we can use them when determining if to do swaps.
state.forEach(item => {
if (item.idx == placeholderIndex) {
item.placeholder = true
}
if (item.idx == targetIndex) {
item.target = true
}
})
if (shouldSwap(state, travelerCenter)) {
rearrange(state, targetIndex, placeholderIndex)
}
}
function lockedSort (objects) {
const locked = objects.filter(obj => obj.locked)
const all = objects.filter(obj => !obj.locked) // Start w/ the sorted list of unlocked
locked.forEach(obj => {
all.splice(obj.idx, 0, obj)
})
return all
}
function rearrange (state, targetIndex, currentIndex) {
// Move this node to its new location
state.splice(targetIndex, 0, state.splice(currentIndex, 1)[0])
// Sort the entire state to make sure locked nodes maintain their order
let items = lockedSort(state)
items.forEach(obj => {
// Append them to the container in the correct order
container.appendChild(obj.node)
})
// Run animations on the nodes
items.forEach(obj => {
animateItem(obj.bb, obj.node)
})
}
function dndstart (source) {
if (ul.selectChildren('.traveler').size()) {
// Already created
return
}
const traveler = source.cloneNode(true)
d3.select(source)
.classed('placeholder', true)
d3.select(traveler)
.classed('traveler', true)
.style('top', source.offsetTop + 'px')
.style('width', d3.select(source).style('width'))
.style('height', d3.select(source).style('height'))
.style('position', 'absolute')
.style('z-index', 1000)
.style('pointerEvents', 'none')
container.appendChild(traveler)
self.emit('dndstart')
ul.classed('is-dragging', true)
return traveler
}
function cleanup (node) {
dragging = false
ul.classed('is-dragging', false)
d3.select(window).on('keydown.dnd-escape', null)
d3.select(node).classed('placeholder', false)
.property('__startIndex__', null)
ul.selectChildren('.traveler').remove()
}
ul.selectChildren('li:not(.draggable-list-lock)')
.call(drag)
.filter(function (d, i, all) {
return all.length === 1
})
.classed('draggable-list-nodrag', true)
}
/**
* @constructs
*
* @param {Object} options
*
* @param {Object} [options.scrollEl] Element containing the list that allows
* the list to scroll while dragging.
*
*/
const List = function (selection, options) {
if (this instanceof List) {
dnd.call(this, selection, options)
return this
} else if (selection instanceof d3.selection) {
selection.each(function () {
dnd.call(new EventEmitter, this, options)
})
}
}
util.inherits(List, EventEmitter)
export default List