-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfin-hypergrid-dnd-list.html
396 lines (346 loc) · 15.1 KB
/
fin-hypergrid-dnd-list.html
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
392
393
394
395
396
<link rel="import" href="../polymer/polymer.html">
<!-- <link rel="import" href="../../polymer-gestures.html"> -->
<polymer-element name="fin-hypergrid-dnd-list" attributes="label list">
<template>
<!-- <link href='http://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'> -->
<link rel="stylesheet" type="text/css" href="fin-hypergrid-dnd-list.css">
<div class="font-special toolbar dnd-header">{{title}}</div>
<ul id="ulist" class="font-special list" touch-action="pan-y" size="3">
<template repeat="{{item, i in list}}">
<li value="{{idAdapter(i)}}_spacer" class="spacer beSmall"></li>
<li value="{{idAdapter(i)}}" class="item" style="-moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;user-select:none;white-space:nowrap;line-height: 46px;list-style-type: none;font-family: 'Roboto', sans-serif;border-bottom: 1px solid #e0e0e0;text-transform: capitalize;cursor: move;background-color: white;color: black;">
<span style="font-size: 30px;color: #b6b6b6;margin: 8px;position: relative;top: 2px;cursor: move;">
⬤
</span>
{{labelAdapter(item)}}
</li>
</template>
<li value="last_spacer" class="spacer beSmall"></li>
</ul>
</div>
</template>
<script>
/* global PolymerGestures */
'use strict';
(function() {
var noop = function() {
};
Polymer('fin-hypergrid-dnd-list', { /* jshint ignore:line */
label: 'label',
id: 'id',
attached: function() {
// populate the element’s data model
// (the salutations array)
this.title = this.title || 'title';
this.list = this.list || [{
id: 0,
label: 'list item zero',
}, {
id: 1,
label: 'list item one',
}, {
id: 2,
label: 'list item two',
}, {
id: 3,
label: 'list item three',
}, {
id: 4,
label: 'list item four',
}, {
id: 5,
label: 'list item five',
}, {
id: 6,
label: 'list item six',
}, {
id: 7,
label: 'list item seven',
}, {
id: 8,
label: 'list item eight',
}, {
id: 9,
label: 'list item nine',
}, ];
var parent = this.parentElement;
var self = this;
parent.oncontextmenu = function() {
return false;
};
PolymerGestures.addEventListener(this.$.ulist, 'trackstart', function(e) {
var li = e.path[0];
if (li.nodeName !== 'LI') {
return; // not a list item; ignore
}
self.initiateItemDrag(li, e);
});
},
getRowHeights: function() {
var bounds = this.$.ulist.getBoundingClientRect();
var items = this.$.ulist.querySelectorAll('li.item').array();
if (items.length === 0) {
return [0];
}
var boundries = items.map(function(e) {
return e.getBoundingClientRect().top - bounds.top;
});
var last = items[items.length - 1];
boundries.push(boundries[boundries.length - 1] + last.getBoundingClientRect().height);
boundries[0] = 0;
return boundries;
},
//these can be overriden, or the label and id values can be override
labelAdapter: function(item) {
if (typeof item === 'object') {
return item[this.label];
}
return item;
},
idAdapter: function(item) {
if (typeof item === 'object') {
return item[this.id];
}
return item;
},
//I've just been dragged over, this is the notification
handleDragHoverEnter: function(dragged, x, y) {
noop(dragged, x, y);
},
//I've just had a dragging operation leave me and
//begin hovering over another drag target
handleDragHoverExit: function(dragged, x, y) {
noop(dragged, x, y);
this.correctItemState();
},
//I'm being dragged over
handleDragOver: function(dragged, x, y) {
var self = this;
var bounds = this.$.ulist.getBoundingClientRect();
//var items = this.$.ulist.querySelectorAll('li.item').array();
var boundries = this.getRowHeights();
var localY = y - bounds.top;
var minValue = 1000000;
var minIndex = 0;
for (var i = 0; i < boundries.length; i++) {
var distance = Math.abs(localY - boundries[i]);
if (distance < minValue) {
minIndex = i;
minValue = distance;
}
}
var overRow = minIndex;
if (this.overRow !== overRow) {
if (this.isTransition) {
return;
}
this.isTransition = true;
var spacers = this.$.ulist.querySelectorAll('li.spacer');
//shrink previous if it exists
if (this.overRow || this.overRow === 0) {
var previous = this.$.ulist.querySelector('li.spacer.beBig');
if (previous) {
previous.classList.add('transition');
requestAnimationFrame(function() {
previous.classList.remove('beBig');
previous.classList.add('beSmall');
});
}
}
//expand current
this.overRow = overRow;
var spacer = spacers[overRow];
spacer.classList.add('transition');
requestAnimationFrame(function() {
spacer.classList.remove('beSmall');
spacer.classList.add('beBig');
setTimeout(function() {
spacer.classList.remove('transition');
self.isTransition = false;
}, 210);
});
}
},
handleDrag: function(e) {
if (!this.dragFodder) {
return;
}
var dragFodderRect = this.dragFodder.getBoundingClientRect();
var cxo = dragFodderRect.width / 2;
var cyo = dragFodderRect.height / 2;
var globalX = (e.x || e.clientX) - this.dragEventStart[0];
var globalY = (e.y || e.clientY) - this.dragEventStart[1];
//var sx = this.dragItemStart[0];
//var sy = this.dragItemStart[1];
this.setCssLocation(this.dragFodder.style, globalX, globalY);
//lets check for a drag over....
//we need to make invisible briefly so as not to
//obscure what it's over
this.dragFodder.style.display = 'none';
var dropTarget = document.elementFromPoint(globalX + cxo, globalY + cyo);
this.dragFodder.style.display = '';
if (dropTarget && dropTarget.handleDragOver) {
if (this.currentDropTarget !== dropTarget) {
if (!dropTarget.canDropItem(this.list, dropTarget.list, this.dragFodder.sourceIndex, this.dragFodder.dragItem, e)) {
return;
}
if (this.currentDropTarget) {
this.currentDropTarget.handleDragHoverExit(this.dragFodder, globalX + cxo, globalY + cyo);
}
this.currentDropTarget = dropTarget;
this.currentDropTarget.handleDragHoverEnter(this.dragFodder, globalX + cxo, globalY + cyo);
}
dropTarget.handleDragOver(this.dragFodder, globalX + cxo, globalY + cyo);
}
},
//lets notify the drop target of a drop
//the dropItem contains it's source, dragSource
handleDrop: function(e) {
noop(e);
var dropTarget = this.currentDropTarget;
var dragFodder = this.dragFodder;
dropTarget.listItemDropped(dragFodder);
},
//ive had a list item dropped on me do the proper thing
listItemDropped: function(listItem) {
var self = this;
var dropSpacer = this.$.ulist.querySelector('li.spacer.beBig');
var items = this.$.ulist.querySelectorAll('li').array();
var sourceItem = listItem.dragItem;
var insertIndex = items.indexOf(dropSpacer) / 2;
var targetRect = dropSpacer.getBoundingClientRect();
var targetTop = targetRect.top;
var targetLeft = targetRect.left;
listItem.style.webkitTransition = '-webkit-transform 150ms ease-in';
listItem.style.MozTransition = '-moz-transform 150ms ease-in';
listItem.style.msTransition = '-ms-transform 150ms ease-in';
listItem.style.oTransition = '-o-transform 150ms ease-in';
listItem.style.transition = 'transform 150ms ease-in';
requestAnimationFrame(function() {
self.setCssLocation(listItem.style, targetLeft, targetTop);
setTimeout(function() {
listItem.parentElement.removeChild(listItem);
dropSpacer.classList.remove('beBig');
dropSpacer.classList.add('beSmall');
self.list.splice(insertIndex, 0, sourceItem);
self.overRow = undefined;
}, 170);
});
//this.correctItemState();
//remove the item from body
},
//this function can be replaced to
//control what can be dragged out and what cannot
canDragItem: function(list, item, index, e) {
noop(list, item, index, e);
//some examples....
//return list.length > 1; // 1 item must be left
//return item !== 'sector'; // can't remove sector
//default allow anything to be dragged out
return true;
},
//this function can be replaced to
//control what can be dragged out and what cannot
//the api could be done better given more time,
//but this will suffice for current needs
canDropItem: function(sourceList, myList, sourceIndex, item, e) {
noop(sourceList, myList, sourceIndex, item, e);
//some examples....
//return ['sector', 'gics', 'strategy'].indexOf(item) > -1; // 1 item must be in this list
//return item !== 'sector'; // can't drop sector here
//default allow anything to be dragged out
return true;
},
initiateItemDrag: function(li, e) {
//let' attach the this as the drag source to
//the item were dragging around
li.dragSource = this;
li.dragIndex = parseInt(li.getAttribute('value'));
li.dragItem = this.list[li.dragIndex];
if (!this.canDragItem(this.list, li.dragItem, li.dragIndex, e)) {
return;
}
this.overRow = undefined;
var transitions = this.$.ulist.querySelectorAll('.transition').array();
transitions.forEach(function(e) {
e.classList.remove('transition');
});
e.preventDefault();
var goAwayer = li.nextElementSibling;
var bounds = li.getBoundingClientRect();
var self = this;
var parent = this.$.ulist;
var parentBounds = parent.getBoundingClientRect();
//li.classList.add('level3');
li.style.width = parentBounds.width + 'px';
// if (this.dragFodder) {
// document.body.removeChild(this.dragFodder);
// }
this.list.splice(li.dragIndex, 1);
this.dragFodder = li;
this.dragEventStart = [(e.x || e.clientX) - bounds.left, (e.y || e.clientY) - bounds.top];
//this.dragItemStart = [bounds.left - parentBounds.left, bounds.top - parentBounds.top];
//lets insert this guy and do a transition to
//shrink his height
//goAwayer.classList.remove('transition');
goAwayer.classList.add('beBig');
goAwayer.classList.remove('beSmall');
document.body.appendChild(li);
PolymerGestures.addEventListener(li, 'track', function(e) {
self.handleDrag(e);
});
PolymerGestures.addEventListener(li, 'trackend', function(e) {
self.handleDrop(e);
});
// PolymerGestures.addEventListener(li, 'up', function(e) {
// console.log('up', e);
// });
requestAnimationFrame(function() {
//goAwayer.classList.remove('beBig');
//goAwayer.classList.add('beSmall');
//goAwayer.classList.add('transition');
//wait a little longer than the transition
//and remove the spacer so as not to have
//duplicate spacers
setTimeout(function() {
goAwayer.classList.remove('transition');
}, 210);
});
//make the new guy generate touch events
},
setCssLocation: function(style, x, y) {
style.position = 'fixed';
style.zIndex = 10;
style.top = 0;
style.left = 0;
style.webkitTransform = 'translate(' + x + 'px, ' + y + 'px)';
style.MozTransform = 'translate(' + x + 'px, ' + y + 'px)';
style.mmsTransform = 'translate(' + x + 'px, ' + y + 'px)';
style.oTransform = 'translate(' + x + 'px, ' + y + 'px)';
style.transform = 'translate(' + x + 'px, ' + y + 'px)';
style.border = '1px solid #bbbbbb';
style.boxShadow = '0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23)';
},
//I no longer have a drop prospect, it has hovered over another drop prospect, correct my expansion state
correctItemState: function() {
var self = this;
var spacer = this.$.ulist.querySelector('li.spacer.beBig');
if (spacer) {
requestAnimationFrame(function() {
spacer.classList.remove('beBig');
spacer.classList.add('beSmall');
spacer.classList.add('transition');
//wait a little longer than the transition
//and remove the spacer so as not to have
//duplicate spacers
setTimeout(function() {
spacer.classList.remove('transition');
self.overRow = undefined;
}, 210);
});
}
},
});
})();
</script>
</polymer-element>