-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathpolymer-sortablejs.html
More file actions
211 lines (182 loc) · 9.07 KB
/
polymer-sortablejs.html
File metadata and controls
211 lines (182 loc) · 9.07 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
<link rel="import" href="../polymer/polymer.html">
<script src="../Sortable/Sortable.js"></script>
<dom-module id="sortable-js">
<template>
<content></content>
</template>
</dom-module>
<script>
'use strict';
Polymer({
is: "sortable-js",
properties: {
group : { type: Object, value: () => {return {name: Math.random()};}, observer: "groupChanged" },
disableSort : { type: Boolean, value: false, observer: "disableSortChanged" },
//sort has been deprecated in favour of disableSort see https://github.com/SortableJS/polymer-sortablejs/issues/22
sort : { type: Boolean, value: true, observer: "sortChanged" },
disabled : { type: Boolean, value: false, observer: "disabledChanged" },
store : { type: Object, value: null, observer: "storeChanged" },
handle : { type: String, value: null, observer: "handleChanged" },
scrollSensitivity : { type: Number, value: 30, observer: "scrollSensitivityChanged" },
scrollSpeed : { type: Number, value: 10, observer: "scrollSpeedChanged" },
ghostClass : { type: String, value: "sortable-ghost", observer: "ghostClassChanged" },
chosenClass : { type: String, value: "sortable-chosen", observer: "chosenClassChanged" },
ignore : { type: String, value: "a, img", observer: "ignoreChanged" },
filter : { type: Object, value: null, observer: "filterChanged" },
animation : { type: Number, value: 0, observer: "animationChanged" },
dropBubble : { type: Boolean, value: false, observer: "dropBubbleChanged" },
dragoverBubble : { type: Boolean, value: false, observer: "dragoverBubbleChanged" },
dataIdAttr : { type: String, value: "data-id", observer: "dataIdAttrChanged" },
delay : { type: Number, value: 0, observer: "delayChanged" },
forceFallback : { type: Boolean, value: false, observer: "forceFallbackChanged" },
fallbackClass : { type: String, value: "sortable-fallback", observer: "fallbackClassChanged" },
fallbackOnBody : { type: Boolean, value: false, observer: "fallbackOnBodyChanged" },
draggable : {},
scroll : {}
},
created: function() {
// override default DOM property behavior
Object.defineProperties(this, {
draggable: { get: function() { return this._draggable || this.getAttribute("draggable") || ">*";}, set: function(value) { this._draggable = value; this.draggableChanged(value);} },
scroll: { get: function() { return this._scroll || JSON.parse(this.getAttribute("scroll") || "true"); }, set: function(value) { this._scroll = value; this.scrollChanged(value);} }
});
},
attached: function() {
// Given
// <sortable-js>
// <template is="dom-repeat" items={{data}}>
// <div>
// <template is="dom-if" if="true">
// <span>hello</span></template></div>
// After render, it becomes
// <sortable-js>
// <div>
// <span>hello</span>
// <template is="dom-if">
// <tempalte is="dom-repeat">
this.initialize();
},
detached: function() {
this.destroy();
},
initialize: function() {
var templates = this.querySelectorAll("template[is='dom-repeat']");
var template = templates[templates.length-1];
var options = {};
Object.keys(this.properties).forEach(function(key) {
options[key] = this[key];
}.bind(this));
//override sort with disableSort as sort doesn't work as expected anyway during config via markup. See https://github.com/SortableJS/polymer-sortablejs/issues/22
options.sort = !this.disableSort;
var _this = this;
var eventCallbacks = {
onUpdate: function (e) {
if (template) {
template.splice("items", e.newIndex, 0, template.splice("items", e.oldIndex, 1)[0]);
/*
if (manuallyHandleUpdateEvents) {
template.items.splice(e.newIndex, 0, template.items.splice(e.oldIndex, 1)[0]);
} else {
template.splice("items", e.newIndex, 0, template.splice("items", e.oldIndex, 1)[0]);
}
*/
}
_this.fire("update", e);
},
onAdd: function(e) {
if (template) {
var froms = e.from.querySelectorAll("template[is='dom-repeat']");
var from = froms[froms.length-1];
var model = from.modelForElement(e.item);
template.splice("items", e.newIndex, 0, model.item);
e.model = model;
}
_this.fire("add", e);
},
onRemove: function(e) {
// Donot remove if group.pull is clone
if (e.target.group.pull === 'clone') {
return false;
}
if (template) {
var item = template.splice("items", e.oldIndex, 1)[0];
e.model = {item: item};
}
_this.fire("remove", e);
},
onChoose: function(e) {
_this.fire("choose", e);
},
onStart: function(e) {
_this.fire("start", e);
},
onEnd: function(e) {
_this.fire("end", e);
},
onSort: function(e) {
_this.fire("sort", e);
},
onFilter: function(e) {
_this.fire("filter", e);
},
onMove: function(e) {
_this.fire("move", e);
},
onClone: function(e) {
_this.fire("clone", e);
}
};
Object.keys(eventCallbacks).forEach(function(name){
options[name] = eventCallbacks[name];
});
this.sortable = Sortable.create(this, options);
},
destroy: function() {
if(this.sortable) {
this.sortable.destroy();
}
},
groupChanged : function(value) {
if(typeof(value) === 'string') {
return this.set('group', {name: value});
}
this.sortable && this.sortable.option("group", value );
},
disableSortChanged : function(value) {
this.sortable && this.sortable.option("sort", !value);
this.sort = !value;
},
sortChanged : function(value) {
if(this.sortable) {
//Whilst the sort property is deprecated, keep disablesort and sort in sync for backwards compatibility.
var alreadyChanged = this.sortable.option("sort") == value;
if(!alreadyChanged) {
this.sortable.option("sort", value);
if(this.sortable && typeof window.console !== "undefined" && typeof window.console.log !== "undefined") {
console.log("The sort property has been deprecated as it cannot be used with with polymer via markup due to boolean's not supporting the default true, please use the disableSort property instead. See https://github.com/SortableJS/polymer-sortablejs/issues/22 for more information.");
}
this.disableSort = !value;
}
}
},
disabledChanged : function(value) { this.sortable && this.sortable.option("disabled", value); },
storeChanged : function(value) { this.sortable && this.sortable.option("store", value); },
handleChanged : function(value) { this.sortable && this.sortable.option("handle", value); },
scrollChanged : function(value) { this.sortable && this.sortable.option("scroll", value); },
scrollSensitivityChanged : function(value) { this.sortable && this.sortable.option("scrollSensitivity", value); },
scrollSpeedChanged : function(value) { this.sortable && this.sortable.option("scrollSpeed", value); },
draggableChanged : function(value) { this.sortable && this.sortable.option("draggable", value); },
ghostClassChanged : function(value) { this.sortable && this.sortable.option("ghostClass", value); },
chosenClassChanged : function(value) { this.sortable && this.sortable.option("chosenClass", value); },
ignoreChanged : function(value) { this.sortable && this.sortable.option("ignore", value); },
filterChanged : function(value) { this.sortable && this.sortable.option("filter", value); },
animationChanged : function(value) { this.sortable && this.sortable.option("animation", value); },
dropBubbleChanged : function(value) { this.sortable && this.sortable.option("dropBubble", value); },
dragoverBubbleChanged : function(value) { this.sortable && this.sortable.option("dragoverBubble", value); },
dataIdAttrChanged : function(value) { this.sortable && this.sortable.option("dataIdAttr", value); },
delayChanged : function(value) { this.sortable && this.sortable.option("delay", value); },
forceFallbackChanged : function(value) { this.sortable && this.sortable.option("forceFallback", value); },
fallbackClassChanged : function(value) { this.sortable && this.sortable.option("fallbackClass", value); },
fallbackOnBodyChanged : function(value) { this.sortable && this.sortable.option("fallbackOnBody", value); }
});
</script>