-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
103 lines (93 loc) · 3.4 KB
/
script.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
/*jslint browser: true*/
/*global console, document*/
var Swipe = (function (document, console) {
'use strict';
var activeSwipe = null,
startEvent = null,
swipeElements = [],
topElement = 0,
defaultSettings = {
selector: '.swipe',
dropCallback: function (direction) {console.log(direction); },
dropDistance: 100,
yEnabled: true
};
/**
* Starts the moving of the activeSwipe element
* @param {event} event The onmousedown event
*/
function beginTracking(event) {
startEvent = event;
activeSwipe = event.srcElement;
topElement = ++topElement % swipeElements.length;
activeSwipe.style.left = startEvent.pageX - startEvent.offsetX + 'px';
activeSwipe.style.top = startEvent.pageY - startEvent.offsetY + 'px';
activeSwipe.style.position = 'absolute';
addEventListeners(swipeElements[topElement]);
}
/**
* Track the activeSwipe element
* @param {event} event The mousemove event
*/
function track(event) {
if (activeSwipe !== null) {
activeSwipe.style.left = event.pageX - startEvent.offsetX + 'px';
if(defaultSettings.yEnabled) activeSwipe.style.top = event.pageY - startEvent.offsetY + 'px';
}
}
/**
* Ends the moving of the activeSwipe element
* @param {event} event The onmousedown event
*/
function endTracking(event) {
if (activeSwipe !== null) {
if (event.clientX < startEvent.clientX - defaultSettings.dropDistance) {
defaultSettings.dropCallback('left');
}
if (event.clientX > startEvent.clientX + defaultSettings.dropDistance) {
defaultSettings.dropCallback('right');
}
resetSwipe();
startEvent = null;
}
}
/**
* Resets all the values of the activeSwipe element
*/
function resetSwipe() {
activeSwipe.removeEventListener('mousedown', beginTracking);
activeSwipe.removeEventListener('mousemove', track);
activeSwipe.removeEventListener('mouseup', endTracking);
activeSwipe.style.display = '';
activeSwipe.style.position = '';
activeSwipe.style.left = '';
activeSwipe.style.top = '';
activeSwipe = null;
}
/**
* Adds the event listeners to the given swipe element
* @param {element} element The element that the event listeners should be put on
*/
function addEventListeners(currentElement) {
currentElement.style.display = 'block';
currentElement.addEventListener('mousedown', beginTracking);
document.addEventListener('mousemove', track);
document.addEventListener('mouseup', endTracking);
}
/**
* Starts the swipe library
* @param {string} selector The element containing the swipe elements
* @param {object} settings Object with the settings override
*/
function init(selector, settings) {
defaultSettings.selector = selector || defaultSettings.selector;
Object.assign(defaultSettings, settings || {});
swipeElements = document.querySelector(defaultSettings.selector).children;
if (swipeElements.length > 0) {
addEventListeners(swipeElements[topElement]);
}
}
return {
init: init
};
})(document, console);