This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
swipeout.js
187 lines (154 loc) · 4.8 KB
/
swipeout.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
/*
* SwipeOut
* version 0.3.0
* https://github.com/ankane/swipeout
* Licensed under the MIT license.
*/
/*jslint browser: true, indent: 2 */
/*global Hammer*/
function SwipeOut(listEl, options) {
'use strict';
options = options || {};
var swiped = false,
preventSwipe = false,
hammer = null,
deleteBtn = document.createElement("div"),
btnText = options.btnText || "Delete",
touchable = "ontouchstart" in window;
// generic helpers
// https://stackoverflow.com/questions/195951/change-an-elements-css-class-with-javascript
function addClass(el, cssClass) {
el.className += (" " + cssClass);
}
function removeElement(el) {
if (el.parentNode) {
el.parentNode.removeChild(el);
}
}
function addCss(css) {
var head = document.getElementsByTagName("head")[0],
style = document.createElement("style");
style.type = "text/css";
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
}
// custom helpers
function findListItemNode(el) {
var currentEl = el;
while (currentEl && currentEl.parentNode !== listEl) {
currentEl = currentEl.parentNode;
}
return currentEl;
}
function transform(style) {
deleteBtn.style.transform = style;
deleteBtn.style.webkitTransform = style;
deleteBtn.style.mozTransform = style;
deleteBtn.style.oTransform = style;
}
function hideButton() {
swiped = false;
deleteBtn.style.opacity = 0;
transform("translate3d(20px,0,0)"); // use 3d for hardware acceleration
}
function centerButton() {
deleteBtn.style.top = ((findListItemNode(deleteBtn).offsetHeight - deleteBtn.offsetHeight) / 2) + "px";
}
function showButton() {
centerButton();
deleteBtn.style.opacity = 1;
transform("translate3d(0,0,0)");
}
// events
// trap click events on list when delete is shown
// https://stackoverflow.com/questions/6157486/jquery-trap-all-click-events-before-they-happen
function onClick(e) {
if (swiped || preventSwipe) {
e.preventDefault();
e.stopPropagation();
}
if (swiped && e.target === deleteBtn) {
var li = findListItemNode(e.target),
event = document.createEvent("Events");
// must send event before removal from the dom
event.initEvent("delete", true, true, null, null, null, null, null, null, null, null, null, null, null, null);
li.dispatchEvent(event);
removeElement(li);
hideButton();
}
}
function onTouchStart(e) {
preventSwipe = false;
if (swiped && e.target !== deleteBtn) {
e.preventDefault();
e.stopPropagation();
hideButton();
preventSwipe = true;
}
}
function onOrientationChange() {
centerButton();
}
function onDragStart(e) {
if (!preventSwipe) {
if (swiped) {
hideButton();
} else {
// add delete button
swiped = true;
var li = findListItemNode(e.target);
removeElement(deleteBtn);
li.appendChild(deleteBtn);
showButton(deleteBtn);
}
}
}
// attach / detach events
function attachEvents() {
listEl.addEventListener("click", onClick, true);
if (touchable) {
listEl.addEventListener("touchstart", onTouchStart, false);
} else {
listEl.addEventListener("mousedown", onTouchStart, false);
}
window.addEventListener("orientationchange", onOrientationChange, false);
hammer = Hammer(listEl).on("swipe", onDragStart);
}
function detachEvents() {
removeElement(deleteBtn);
hammer.off("swipe");
window.removeEventListener("orientationchange", onOrientationChange, false);
if (touchable) {
listEl.removeEventListener("touchstart", onTouchStart, false);
} else {
listEl.removeEventListener("mousedown", onTouchStart, false);
}
listEl.removeEventListener("click", onClick, true);
}
// add text
deleteBtn.appendChild(document.createTextNode(btnText));
// style button
deleteBtn.style.position = "absolute";
deleteBtn.style.right = "6px";
deleteBtn.style.transition = "transform 0.25s ease-in-out, opacity 0.25s ease-in-out";
deleteBtn.style.webkitTransition = "-webkit-transform 0.25s ease-in-out, opacity 0.25s ease-in-out";
deleteBtn.style.mozTransition = "-moz-transform 0.25s ease-in-out, opacity 0.25s ease-in-out";
deleteBtn.style.oTransition = "-o-transform 0.25s ease-in-out, opacity 0.25s ease-in-out";
hideButton();
addClass(deleteBtn, "delete-btn");
// style list items
// TODO insert only once per page and clean up
addCss(".swipe-out > li { position: relative; }");
// style list
listEl.style.overflow = "hidden";
addClass(listEl, "swipe-out");
attachEvents();
// public methods
this.destroy = function () {
detachEvents();
};
}