-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdrag-sort-list.js
More file actions
242 lines (178 loc) · 6.89 KB
/
drag-sort-list.js
File metadata and controls
242 lines (178 loc) · 6.89 KB
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
// ----- Ember modules -----
import Component from '@ember/component'
import {inject as service} from '@ember/service'
import {not, reads} from '@ember/object/computed'
import {computed, get, observer} from '@ember/object'
import {next} from '@ember/runloop'
import { A } from '@ember/array'
// ----- Ember addons -----
// ----- Own modules -----
import layout from '../templates/components/drag-sort-list'
export default Component.extend({
// ----- Arguments -----
additionalArgs : undefined,
items : undefined,
group : undefined,
draggingEnabled : true,
childClass : '',
childTagName : 'div',
handle : null,
isHorizontal : false,
isRtl : false,
dragEndAction : undefined,
dragStartAction : undefined,
determineForeignPositionAction : undefined,
// ----- Services -----
dragSort : service(),
// ----- Overridden properties -----
layout,
classNameBindings : [
':dragSortList',
'draggingEnabled:-draggingEnabled',
'isHorizontal:-horizontal',
'isVertical:-vertical',
'isRtl:-rtl',
'isDragging:-isDragging',
'isDraggingOver:-isDraggingOver',
'isExpanded2:-isExpanded',
'isEmpty:-isEmpty',
'sourceOnly:-sourceOnlyList',
],
// ----- Static properties -----
// ----- Aliases -----
sourceList : reads('dragSort.sourceList'),
targetList : reads('dragSort.targetList'),
sourceIndex : reads('dragSort.sourceIndex'),
draggedItem : reads('dragSort.draggedItem'),
lastDragEnteredList : reads('dragSort.lastDragEnteredList'),
isVertical : not('isHorizontal'),
// ----- Computed properties -----
isDragging : computed('dragSort.{isDragging,group}', 'group', function () {
const isDragging = this.get('dragSort.isDragging')
const group = this.get('group')
const groupFromService = this.get('dragSort.group')
return isDragging && group === groupFromService
}),
isDraggingOver : computed('isDragging', 'items', 'targetList', function () {
const isDragging = this.get('isDragging')
const items = this.get('items')
const targetList = this.get('targetList')
return isDragging && items === targetList
}),
isExpanded : computed('isDragging', 'isEmpty', 'isOnlyElementDragged', function () {
const isDragging = this.get('isDragging')
const isEmpty = this.get('isEmpty')
const isOnlyElementDragged = this.get('isOnlyElementDragged')
return isDragging && (isEmpty || isOnlyElementDragged)
}),
isExpanded2 : reads('isExpanded'),
isEmpty : computed('items.[]', function () {
const count = this.get('items.length')
return !count
}),
isOnlyElementDragged : computed('items.length', 'items', 'sourceList', 'sourceIndex', function () {
const count = this.get('items.length')
const items = this.get('items')
const sourceList = this.get('sourceList')
const sourceIndex = this.get('sourceIndex')
return (
count === 1
&& items === sourceList
&& !sourceIndex
)
}),
// ----- Overridden methods -----
dragEnter (event) {
// Ignore irrelevant drags
if (!this.get('dragSort.isDragging')) return
// Ignore irrelevant groups
const group = this.get('group')
const activeGroup = this.get('dragSort.group')
if (group !== activeGroup) return
event.stopPropagation()
// Ignore duplicate events (explanation: https://github.com/lolmaus/jquery.dragbetter#what-this-is-all-about )
const items = this.get('items')
const lastDragEnteredList = this.get('lastDragEnteredList')
if (items === lastDragEnteredList) return
this.dragEntering(event)
if (this.get('determineForeignPositionAction')) {
this.forceDraggingOver()
}
},
dragOver (event) {
// This event is only used for placing the dragged element into the end of a horizontal list
if (this.get('isVertical')) {
return
}
// Ignore irrelevant drags
if (
!this.get('dragSort.isDragging')
|| this.get('determineForeignPositionAction')
) return
const group = this.get('group')
const activeGroup = this.get('dragSort.group')
if (group !== activeGroup) return
event.stopPropagation()
this.isDraggingOverHorizontal(event)
},
// ----- Custom methods -----
dragEntering (event) {
const group = this.get('group')
const items = this.get('items')
const dragSort = this.get('dragSort')
const isHorizontal = this.get('isHorizontal')
const targetArgs = this.get('additionalArgs')
let targetIndex = 0
if (isHorizontal) {
targetIndex = this.getClosestHorizontalIndex(event)
dragSort.set('isDraggingUp', false)
}
dragSort.dragEntering({group, items, isHorizontal, targetArgs, targetIndex})
},
getClosestHorizontalIndex (event) {
// Calculate which item is closest and make that the target
const itemsNodeList = this.get('element').querySelectorAll('.dragSortItem')
const draggableItems = A(Array.prototype.slice.call(itemsNodeList))
const positions = A(draggableItems.map(draggableItem => draggableItem.getBoundingClientRect()))
const rows = positions.uniqBy('top').mapBy('top').sort()
const currentRowPosition = rows.filter(row => row < event.clientY).pop()
const closestItem = positions.filterBy('top', currentRowPosition).pop()
return closestItem ? positions.indexOf(closestItem) : 0
},
forceDraggingOver () {
const determineForeignPositionAction = this.get('determineForeignPositionAction')
const group = this.get('group')
const items = this.get('items')
const itemsLength = get(items, 'length')
const draggedItem = this.get('draggedItem')
const sourceList = this.get('sourceList')
const dragSort = this.get('dragSort')
let isDraggingUp = true
let index =
items === sourceList
? items.indexOf(draggedItem) + 1
: determineForeignPositionAction({draggedItem, items})
if (index >= itemsLength) {
index = itemsLength - 1
isDraggingUp = false
}
dragSort.draggingOver({group, index, items, isDraggingUp})
},
isDraggingOverHorizontal (event) {
const dragSort = this.get('dragSort')
const group = this.get('group')
const items = this.get('items')
const index = this.getClosestHorizontalIndex(event)
const isDraggingUp = false
dragSort.draggingOver({group, index, items, isDraggingUp})
},
// ----- Observers -----
setIsExpanded2 : observer('isExpanded', function () {
// The delay is necessary for HTML class to update with a delay.
// Otherwise, dragging is finished immediately.
next(() => {
if (this.get('isDestroying') || this.get('isDestroyed')) return
this.set('isExpanded2', this.get('isExpanded'))
})
}),
})