-
Notifications
You must be signed in to change notification settings - Fork 3
/
sortable-list.html
106 lines (81 loc) · 2.75 KB
/
sortable-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
<link rel="import" href="../polymer/polymer.html">
<!--
# sortable-list
`<sortable-list>`
Polymer 1.X and 2.X custom element for drag-and-drop reorder of items.
For typical use just wrap your dom-repeat template item with a `<sortable-list>`
element to get drag-and-drop reordering.
Relies on
[HTML 5 drag and drop](https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API)
support, so if you need to support older browsers
which do not support HTML 5 drag and drop events then we suggest using a polyfill:
https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills
@demo demo/index.html
-->
<dom-module id="sortable-list">
<template>
<style>
:host {
display: block;
}
</style>
<slot></slot>
</template>
<script>
function getIndex(node) {
var children = node.parentNode.children;
return [].indexOf.call(children, node);
}
function dragstart(evt) {
var index = getIndex(this);
evt.dataTransfer.effectAllowed = "move";
evt.dataTransfer.setData("index", index);
}
function dragover(evt) {
evt.preventDefault();
evt.dataTransfer.dropEffect = "move";
}
function handledrop(evt) {
evt.preventDefault();
evt.dataTransfer.dropEffect = "move";
var current = Number(getIndex(evt.target));
var move = Number(evt.dataTransfer.getData("index"));
// set the underlying Array model new order
if (move !== current) {
var userTemplate = this.querySelector("template[is='dom-repeat']");
var data = userTemplate.items;
var item = data.splice(move, 1);
data.splice(current, null, item[0]);
userTemplate.notifySplices('items', [
{ index: move, removed: [item], addedCount: 0, object: data, type: "splice" },
{ index: current, removed: [], addedCount: 1, object: data, type: "splice" }
]);
}
}
Polymer({
is: 'sortable-list',
_addlisteners: function() {
var self = this;
var nodes = this.getEffectiveChildren();
nodes.forEach(function(item) {
if (!item.getAttribute('draggable')) {
item.setAttribute('draggable', true);
item.addEventListener('dragstart', dragstart, false);
item.addEventListener('dragover', dragover, false);
}
});
},
attached: function() {
var self = this;
this.async(function() {
self.addEventListener('drop', handledrop, false);
var template = this.querySelector("template[is='dom-repeat']");
template.addEventListener('dom-change', function() {
self._addlisteners();
});
self._addlisteners();
});
}
});
</script>
</dom-module>