@tomasvn/glightbox 0.0.0-stage → 3.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +16 -0
- package/.eslintignore +3 -0
- package/.eslintrc.json +94 -0
- package/.github/FUNDING.yml +3 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +23 -0
- package/.github/stale.yml +21 -0
- package/.github/workflows/greetings.yml +13 -0
- package/.husky/pre-commit +3 -0
- package/.lando.yml +10 -0
- package/.prettierignore +3 -0
- package/.prettierrc.json +30 -0
- package/CHANGELOG.md +259 -0
- package/CONTRIBUTING.md +29 -0
- package/LICENSE.md +21 -0
- package/README.md +714 -2
- package/bower.json +29 -0
- package/development/build.js +44 -0
- package/development/jscompiler.js +90 -0
- package/development/notifications.js +15 -0
- package/development/package.js +116 -0
- package/development/postcss.js +63 -0
- package/development/watcher.js +105 -0
- package/dist/css/glightbox.css +939 -0
- package/dist/css/glightbox.min.css +1 -0
- package/dist/js/glightbox.js +3109 -0
- package/dist/js/glightbox.min.js +1 -0
- package/index.d.ts +586 -0
- package/package.json +60 -4
- package/src/js/.jshintrc +6 -0
- package/src/js/core/drag.js +203 -0
- package/src/js/core/keyboard-navigation.js +101 -0
- package/src/js/core/slide-parser.js +236 -0
- package/src/js/core/slide.js +251 -0
- package/src/js/core/touch-events.js +378 -0
- package/src/js/core/touch-navigation.js +291 -0
- package/src/js/core/zoom.js +156 -0
- package/src/js/glightbox.js +1351 -0
- package/src/js/slides/iframe.js +24 -0
- package/src/js/slides/image.js +56 -0
- package/src/js/slides/inline.js +71 -0
- package/src/js/slides/video.js +108 -0
- package/src/js/utils/helpers.js +541 -0
- package/src/postcss/glightbox.css +725 -0
- package/tests/slide.test.mjs +130 -0
- package/tests/touch-navigation.test.mjs +13 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slide
|
|
3
|
+
* class to hablde slide creation
|
|
4
|
+
* and config parser
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import ZoomImages from './zoom.js';
|
|
8
|
+
import DragSlides from './drag.js';
|
|
9
|
+
import slideImage from '../slides/image.js';
|
|
10
|
+
import slideVideo from '../slides/video.js';
|
|
11
|
+
import slideInline from '../slides/inline.js';
|
|
12
|
+
import slideIframe from '../slides/iframe.js';
|
|
13
|
+
import SlideConfigParser from './slide-parser.js';
|
|
14
|
+
import { addEvent, addClass, removeClass, hasClass, closest, isMobile, isFunction, isNode, createHTML } from '../utils/helpers.js';
|
|
15
|
+
|
|
16
|
+
export default class Slide {
|
|
17
|
+
constructor(el, instance, index) {
|
|
18
|
+
this.element = el;
|
|
19
|
+
this.instance = instance;
|
|
20
|
+
this.index = index;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Set slide content
|
|
25
|
+
*
|
|
26
|
+
* @param {node} slide
|
|
27
|
+
* @param {object} data
|
|
28
|
+
* @param {function} callback
|
|
29
|
+
*/
|
|
30
|
+
setContent(slide = null, callback = false) {
|
|
31
|
+
if (hasClass(slide, 'loaded')) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const settings = this.instance.settings;
|
|
36
|
+
const slideConfig = this.slideConfig;
|
|
37
|
+
const isMobileDevice = isMobile();
|
|
38
|
+
|
|
39
|
+
if (isFunction(settings.beforeSlideLoad)) {
|
|
40
|
+
settings.beforeSlideLoad({
|
|
41
|
+
index: this.index,
|
|
42
|
+
slide: slide,
|
|
43
|
+
player: false
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let type = slideConfig.type;
|
|
48
|
+
let position = slideConfig.descPosition;
|
|
49
|
+
let slideMedia = slide.querySelector('.gslide-media');
|
|
50
|
+
let slideTitle = slide.querySelector('.gslide-title');
|
|
51
|
+
let slideText = slide.querySelector('.gslide-desc');
|
|
52
|
+
let slideDesc = slide.querySelector('.gdesc-inner');
|
|
53
|
+
let finalCallback = callback;
|
|
54
|
+
|
|
55
|
+
// used for image accessiblity
|
|
56
|
+
let titleID = 'gSlideTitle_' + this.index;
|
|
57
|
+
let textID = 'gSlideDesc_' + this.index;
|
|
58
|
+
|
|
59
|
+
if (isFunction(settings.afterSlideLoad)) {
|
|
60
|
+
finalCallback = () => {
|
|
61
|
+
if (isFunction(callback)) {
|
|
62
|
+
callback();
|
|
63
|
+
}
|
|
64
|
+
settings.afterSlideLoad({
|
|
65
|
+
index: this.index,
|
|
66
|
+
slide: slide,
|
|
67
|
+
player: this.instance.getSlidePlayerInstance(this.index)
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (slideConfig.title == '' && slideConfig.description == '') {
|
|
73
|
+
if (slideDesc) {
|
|
74
|
+
slideDesc.parentNode.parentNode.removeChild(slideDesc.parentNode);
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
if (slideTitle && slideConfig.title !== '') {
|
|
78
|
+
slideTitle.id = titleID;
|
|
79
|
+
slideTitle.innerHTML = slideConfig.title;
|
|
80
|
+
} else {
|
|
81
|
+
slideTitle.parentNode.removeChild(slideTitle);
|
|
82
|
+
}
|
|
83
|
+
if (slideText && slideConfig.description !== '') {
|
|
84
|
+
slideText.id = textID;
|
|
85
|
+
if (isMobileDevice && settings.moreLength > 0) {
|
|
86
|
+
slideConfig.smallDescription = this.slideShortDesc(slideConfig.description, settings.moreLength, settings.moreText);
|
|
87
|
+
slideText.innerHTML = slideConfig.smallDescription;
|
|
88
|
+
this.descriptionEvents(slideText, slideConfig);
|
|
89
|
+
} else {
|
|
90
|
+
slideText.innerHTML = slideConfig.description;
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
slideText.parentNode.removeChild(slideText);
|
|
94
|
+
}
|
|
95
|
+
addClass(slideMedia.parentNode, `desc-${position}`);
|
|
96
|
+
addClass(slideDesc.parentNode, `description-${position}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
addClass(slideMedia, `gslide-${type}`);
|
|
100
|
+
addClass(slide, 'loaded');
|
|
101
|
+
|
|
102
|
+
if (type === 'video') {
|
|
103
|
+
slideVideo.apply(this.instance, [slide, slideConfig, this.index, finalCallback]);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (type === 'external') {
|
|
108
|
+
slideIframe.apply(this, [slide, slideConfig, this.index, finalCallback]);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (type === 'inline') {
|
|
113
|
+
slideInline.apply(this.instance, [slide, slideConfig, this.index, finalCallback]);
|
|
114
|
+
if (slideConfig.draggable) {
|
|
115
|
+
new DragSlides({
|
|
116
|
+
dragEl: slide.querySelector('.gslide-inline'),
|
|
117
|
+
toleranceX: settings.dragToleranceX,
|
|
118
|
+
toleranceY: settings.dragToleranceY,
|
|
119
|
+
slide: slide,
|
|
120
|
+
instance: this.instance
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (type === 'image') {
|
|
127
|
+
slideImage(slide, slideConfig, this.index, () => {
|
|
128
|
+
const img = slide.querySelector('img');
|
|
129
|
+
|
|
130
|
+
if (slideConfig.draggable) {
|
|
131
|
+
new DragSlides({
|
|
132
|
+
dragEl: img,
|
|
133
|
+
toleranceX: settings.dragToleranceX,
|
|
134
|
+
toleranceY: settings.dragToleranceY,
|
|
135
|
+
slide: slide,
|
|
136
|
+
instance: this.instance
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (slideConfig.zoomable && img.naturalWidth > img.offsetWidth) {
|
|
140
|
+
addClass(img, 'zoomable');
|
|
141
|
+
new ZoomImages(img, slide, () => {
|
|
142
|
+
this.instance.resize();
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (isFunction(finalCallback)) {
|
|
147
|
+
finalCallback();
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (isFunction(finalCallback)) {
|
|
154
|
+
finalCallback();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
slideShortDesc(string, n = 50, wordBoundary = false) {
|
|
159
|
+
let div = document.createElement('div');
|
|
160
|
+
div.innerHTML = string;
|
|
161
|
+
let cleanedString = div.innerText;
|
|
162
|
+
|
|
163
|
+
let useWordBoundary = wordBoundary;
|
|
164
|
+
string = cleanedString.trim();
|
|
165
|
+
if (string.length <= n) {
|
|
166
|
+
return string;
|
|
167
|
+
}
|
|
168
|
+
let subString = string.substr(0, n - 1);
|
|
169
|
+
if (!useWordBoundary) {
|
|
170
|
+
return subString;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
div = null;
|
|
174
|
+
return subString + '... <a href="#" class="desc-more">' + wordBoundary + '</a>';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
descriptionEvents(desc, data) {
|
|
178
|
+
let moreLink = desc.querySelector('.desc-more');
|
|
179
|
+
if (!moreLink) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
addEvent('click', {
|
|
184
|
+
onElement: moreLink,
|
|
185
|
+
withCallback: (event, target) => {
|
|
186
|
+
event.preventDefault();
|
|
187
|
+
event.stopPropagation();
|
|
188
|
+
const body = document.body;
|
|
189
|
+
|
|
190
|
+
let desc = closest(target, '.gslide-desc');
|
|
191
|
+
if (!desc) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
desc.innerHTML = `${data.description} <a href="#" class="desc-less">${this.instance.settings.lessText}</a>`;
|
|
196
|
+
addClass(body, 'gdesc-open');
|
|
197
|
+
|
|
198
|
+
let shortEvent = addEvent('click', {
|
|
199
|
+
onElement: [body, closest(desc, '.gslide-description')],
|
|
200
|
+
withCallback: (event, target) => {
|
|
201
|
+
const isLessLink = hasClass(event.target, 'desc-less');
|
|
202
|
+
if (event.target.nodeName.toLowerCase() !== 'a' || isLessLink) {
|
|
203
|
+
event.stopPropagation();
|
|
204
|
+
if (isLessLink) {
|
|
205
|
+
event.preventDefault();
|
|
206
|
+
}
|
|
207
|
+
removeClass(body, 'gdesc-open');
|
|
208
|
+
addClass(body, 'gdesc-closed');
|
|
209
|
+
desc.innerHTML = data.smallDescription;
|
|
210
|
+
this.descriptionEvents(desc, data);
|
|
211
|
+
|
|
212
|
+
setTimeout(() => {
|
|
213
|
+
removeClass(body, 'gdesc-closed');
|
|
214
|
+
}, 400);
|
|
215
|
+
shortEvent.destroy();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Create Slide Node
|
|
225
|
+
*
|
|
226
|
+
* @return { node }
|
|
227
|
+
*/
|
|
228
|
+
create() {
|
|
229
|
+
return createHTML(this.instance.settings.slideHTML);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Get slide config
|
|
234
|
+
* returns each individual slide config
|
|
235
|
+
* it uses SlideConfigParser
|
|
236
|
+
* each slide can overwrite a global setting
|
|
237
|
+
* read more in the SlideConfigParser class
|
|
238
|
+
*
|
|
239
|
+
* @return { object }
|
|
240
|
+
*/
|
|
241
|
+
getConfig() {
|
|
242
|
+
if (!isNode(this.element) && !this.element.hasOwnProperty('draggable')) {
|
|
243
|
+
this.element.draggable = this.instance.settings.draggable;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const parser = new SlideConfigParser(this.instance.settings.slideExtraAttributes);
|
|
247
|
+
this.slideConfig = parser.parseConfig(this.element, this.instance.settings);
|
|
248
|
+
|
|
249
|
+
return this.slideConfig;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
function getLen(v) {
|
|
2
|
+
return Math.sqrt(v.x * v.x + v.y * v.y);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function dot(v1, v2) {
|
|
6
|
+
return v1.x * v2.x + v1.y * v2.y;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function getAngle(v1, v2) {
|
|
10
|
+
var mr = getLen(v1) * getLen(v2);
|
|
11
|
+
if (mr === 0) {
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
|
14
|
+
var r = dot(v1, v2) / mr;
|
|
15
|
+
if (r > 1) {
|
|
16
|
+
r = 1;
|
|
17
|
+
}
|
|
18
|
+
return Math.acos(r);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function cross(v1, v2) {
|
|
22
|
+
return v1.x * v2.y - v2.x * v1.y;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getRotateAngle(v1, v2) {
|
|
26
|
+
var angle = getAngle(v1, v2);
|
|
27
|
+
if (cross(v1, v2) > 0) {
|
|
28
|
+
angle *= -1;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return (angle * 180) / Math.PI;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
class EventsHandlerAdmin {
|
|
35
|
+
constructor(el) {
|
|
36
|
+
this.handlers = [];
|
|
37
|
+
this.el = el;
|
|
38
|
+
}
|
|
39
|
+
add(handler) {
|
|
40
|
+
this.handlers.push(handler);
|
|
41
|
+
}
|
|
42
|
+
del(handler) {
|
|
43
|
+
if (!handler) {
|
|
44
|
+
this.handlers = [];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for (var i = this.handlers.length; i >= 0; i--) {
|
|
48
|
+
if (this.handlers[i] === handler) {
|
|
49
|
+
this.handlers.splice(i, 1);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
dispatch() {
|
|
54
|
+
for (var i = 0, len = this.handlers.length; i < len; i++) {
|
|
55
|
+
var handler = this.handlers[i];
|
|
56
|
+
if (typeof handler === 'function') {
|
|
57
|
+
handler.apply(this.el, arguments);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function wrapFunc(el, handler) {
|
|
64
|
+
var EventshandlerAdmin = new EventsHandlerAdmin(el);
|
|
65
|
+
EventshandlerAdmin.add(handler);
|
|
66
|
+
|
|
67
|
+
return EventshandlerAdmin;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Modified version of AlloyFinger
|
|
71
|
+
export default class TouchEvents {
|
|
72
|
+
constructor(el, option) {
|
|
73
|
+
this.element = typeof el == 'string' ? document.querySelector(el) : el;
|
|
74
|
+
|
|
75
|
+
this.start = this.start.bind(this);
|
|
76
|
+
this.move = this.move.bind(this);
|
|
77
|
+
this.end = this.end.bind(this);
|
|
78
|
+
this.cancel = this.cancel.bind(this);
|
|
79
|
+
this.element.addEventListener('touchstart', this.start, false);
|
|
80
|
+
this.element.addEventListener('touchmove', this.move, false);
|
|
81
|
+
this.element.addEventListener('touchend', this.end, false);
|
|
82
|
+
this.element.addEventListener('touchcancel', this.cancel, false);
|
|
83
|
+
|
|
84
|
+
this.preV = { x: null, y: null };
|
|
85
|
+
this.pinchStartLen = null;
|
|
86
|
+
this.zoom = 1;
|
|
87
|
+
this.isDoubleTap = false;
|
|
88
|
+
|
|
89
|
+
var noop = function () {};
|
|
90
|
+
|
|
91
|
+
this.rotate = wrapFunc(this.element, option.rotate || noop);
|
|
92
|
+
this.touchStart = wrapFunc(this.element, option.touchStart || noop);
|
|
93
|
+
this.multipointStart = wrapFunc(this.element, option.multipointStart || noop);
|
|
94
|
+
this.multipointEnd = wrapFunc(this.element, option.multipointEnd || noop);
|
|
95
|
+
this.pinch = wrapFunc(this.element, option.pinch || noop);
|
|
96
|
+
this.swipe = wrapFunc(this.element, option.swipe || noop);
|
|
97
|
+
this.tap = wrapFunc(this.element, option.tap || noop);
|
|
98
|
+
this.doubleTap = wrapFunc(this.element, option.doubleTap || noop);
|
|
99
|
+
this.longTap = wrapFunc(this.element, option.longTap || noop);
|
|
100
|
+
this.singleTap = wrapFunc(this.element, option.singleTap || noop);
|
|
101
|
+
this.pressMove = wrapFunc(this.element, option.pressMove || noop);
|
|
102
|
+
this.twoFingerPressMove = wrapFunc(this.element, option.twoFingerPressMove || noop);
|
|
103
|
+
this.touchMove = wrapFunc(this.element, option.touchMove || noop);
|
|
104
|
+
this.touchEnd = wrapFunc(this.element, option.touchEnd || noop);
|
|
105
|
+
this.touchCancel = wrapFunc(this.element, option.touchCancel || noop);
|
|
106
|
+
this.translateContainer = this.element;
|
|
107
|
+
|
|
108
|
+
this._cancelAllHandler = this.cancelAll.bind(this);
|
|
109
|
+
|
|
110
|
+
window.addEventListener('scroll', this._cancelAllHandler);
|
|
111
|
+
|
|
112
|
+
this.delta = null;
|
|
113
|
+
this.last = null;
|
|
114
|
+
this.now = null;
|
|
115
|
+
this.tapTimeout = null;
|
|
116
|
+
this.singleTapTimeout = null;
|
|
117
|
+
this.longTapTimeout = null;
|
|
118
|
+
this.swipeTimeout = null;
|
|
119
|
+
this.x1 = this.x2 = this.y1 = this.y2 = null;
|
|
120
|
+
this.preTapPosition = { x: null, y: null };
|
|
121
|
+
}
|
|
122
|
+
start(evt) {
|
|
123
|
+
if (!evt.touches) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Fix Media Buttons Not responding on Android #233
|
|
128
|
+
const ignoreDragFor = ['a', 'button', 'input'];
|
|
129
|
+
if (evt.target && evt.target.nodeName && ignoreDragFor.indexOf(evt.target.nodeName.toLowerCase()) >= 0) {
|
|
130
|
+
console.log('ignore drag for this touched element', evt.target.nodeName.toLowerCase());
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
this.now = Date.now();
|
|
135
|
+
this.x1 = evt.touches[0].pageX;
|
|
136
|
+
this.y1 = evt.touches[0].pageY;
|
|
137
|
+
this.delta = this.now - (this.last || this.now);
|
|
138
|
+
this.touchStart.dispatch(evt, this.element);
|
|
139
|
+
if (this.preTapPosition.x !== null) {
|
|
140
|
+
this.isDoubleTap = this.delta > 0 && this.delta <= 250 && Math.abs(this.preTapPosition.x - this.x1) < 30 && Math.abs(this.preTapPosition.y - this.y1) < 30;
|
|
141
|
+
if (this.isDoubleTap) {
|
|
142
|
+
clearTimeout(this.singleTapTimeout);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
this.preTapPosition.x = this.x1;
|
|
146
|
+
this.preTapPosition.y = this.y1;
|
|
147
|
+
this.last = this.now;
|
|
148
|
+
var preV = this.preV,
|
|
149
|
+
len = evt.touches.length;
|
|
150
|
+
if (len > 1) {
|
|
151
|
+
this._cancelLongTap();
|
|
152
|
+
this._cancelSingleTap();
|
|
153
|
+
var v = { x: evt.touches[1].pageX - this.x1, y: evt.touches[1].pageY - this.y1 };
|
|
154
|
+
preV.x = v.x;
|
|
155
|
+
preV.y = v.y;
|
|
156
|
+
this.pinchStartLen = getLen(preV);
|
|
157
|
+
this.multipointStart.dispatch(evt, this.element);
|
|
158
|
+
}
|
|
159
|
+
this._preventTap = false;
|
|
160
|
+
this.longTapTimeout = setTimeout(
|
|
161
|
+
function () {
|
|
162
|
+
this.longTap.dispatch(evt, this.element);
|
|
163
|
+
this._preventTap = true;
|
|
164
|
+
}.bind(this),
|
|
165
|
+
750
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
move(evt) {
|
|
169
|
+
if (!evt.touches) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
var preV = this.preV,
|
|
173
|
+
len = evt.touches.length,
|
|
174
|
+
currentX = evt.touches[0].pageX,
|
|
175
|
+
currentY = evt.touches[0].pageY;
|
|
176
|
+
this.isDoubleTap = false;
|
|
177
|
+
if (len > 1) {
|
|
178
|
+
var sCurrentX = evt.touches[1].pageX,
|
|
179
|
+
sCurrentY = evt.touches[1].pageY;
|
|
180
|
+
var v = { x: evt.touches[1].pageX - currentX, y: evt.touches[1].pageY - currentY };
|
|
181
|
+
|
|
182
|
+
if (preV.x !== null) {
|
|
183
|
+
if (this.pinchStartLen > 0) {
|
|
184
|
+
evt.zoom = getLen(v) / this.pinchStartLen;
|
|
185
|
+
this.pinch.dispatch(evt, this.element);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
evt.angle = getRotateAngle(v, preV);
|
|
189
|
+
this.rotate.dispatch(evt, this.element);
|
|
190
|
+
}
|
|
191
|
+
preV.x = v.x;
|
|
192
|
+
preV.y = v.y;
|
|
193
|
+
|
|
194
|
+
if (this.x2 !== null && this.sx2 !== null) {
|
|
195
|
+
evt.deltaX = (currentX - this.x2 + sCurrentX - this.sx2) / 2;
|
|
196
|
+
evt.deltaY = (currentY - this.y2 + sCurrentY - this.sy2) / 2;
|
|
197
|
+
} else {
|
|
198
|
+
evt.deltaX = 0;
|
|
199
|
+
evt.deltaY = 0;
|
|
200
|
+
}
|
|
201
|
+
this.twoFingerPressMove.dispatch(evt, this.element);
|
|
202
|
+
|
|
203
|
+
this.sx2 = sCurrentX;
|
|
204
|
+
this.sy2 = sCurrentY;
|
|
205
|
+
} else {
|
|
206
|
+
if (this.x2 !== null) {
|
|
207
|
+
evt.deltaX = currentX - this.x2;
|
|
208
|
+
evt.deltaY = currentY - this.y2;
|
|
209
|
+
|
|
210
|
+
var movedX = Math.abs(this.x1 - this.x2),
|
|
211
|
+
movedY = Math.abs(this.y1 - this.y2);
|
|
212
|
+
|
|
213
|
+
if (movedX > 10 || movedY > 10) {
|
|
214
|
+
this._preventTap = true;
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
evt.deltaX = 0;
|
|
218
|
+
evt.deltaY = 0;
|
|
219
|
+
}
|
|
220
|
+
this.pressMove.dispatch(evt, this.element);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
this.touchMove.dispatch(evt, this.element);
|
|
224
|
+
|
|
225
|
+
this._cancelLongTap();
|
|
226
|
+
this.x2 = currentX;
|
|
227
|
+
this.y2 = currentY;
|
|
228
|
+
|
|
229
|
+
if (len > 1) {
|
|
230
|
+
evt.preventDefault();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
end(evt) {
|
|
234
|
+
if (!evt.changedTouches) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
this._cancelLongTap();
|
|
238
|
+
var self = this;
|
|
239
|
+
if (evt.touches.length < 2) {
|
|
240
|
+
this.multipointEnd.dispatch(evt, this.element);
|
|
241
|
+
this.sx2 = this.sy2 = null;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
//swipe
|
|
245
|
+
if ((this.x2 && Math.abs(this.x1 - this.x2) > 30) || (this.y2 && Math.abs(this.y1 - this.y2) > 30)) {
|
|
246
|
+
evt.direction = this._swipeDirection(this.x1, this.x2, this.y1, this.y2);
|
|
247
|
+
this.swipeTimeout = setTimeout(function () {
|
|
248
|
+
self.swipe.dispatch(evt, self.element);
|
|
249
|
+
}, 0);
|
|
250
|
+
} else {
|
|
251
|
+
this.tapTimeout = setTimeout(function () {
|
|
252
|
+
if (!self._preventTap) {
|
|
253
|
+
self.tap.dispatch(evt, self.element);
|
|
254
|
+
}
|
|
255
|
+
// trigger double tap immediately
|
|
256
|
+
if (self.isDoubleTap) {
|
|
257
|
+
self.doubleTap.dispatch(evt, self.element);
|
|
258
|
+
self.isDoubleTap = false;
|
|
259
|
+
}
|
|
260
|
+
}, 0);
|
|
261
|
+
|
|
262
|
+
if (!self.isDoubleTap) {
|
|
263
|
+
self.singleTapTimeout = setTimeout(function () {
|
|
264
|
+
self.singleTap.dispatch(evt, self.element);
|
|
265
|
+
}, 250);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
this.touchEnd.dispatch(evt, this.element);
|
|
270
|
+
|
|
271
|
+
this.preV.x = 0;
|
|
272
|
+
this.preV.y = 0;
|
|
273
|
+
this.zoom = 1;
|
|
274
|
+
this.pinchStartLen = null;
|
|
275
|
+
this.x1 = this.x2 = this.y1 = this.y2 = null;
|
|
276
|
+
}
|
|
277
|
+
cancelAll() {
|
|
278
|
+
this._preventTap = true;
|
|
279
|
+
clearTimeout(this.singleTapTimeout);
|
|
280
|
+
clearTimeout(this.tapTimeout);
|
|
281
|
+
clearTimeout(this.longTapTimeout);
|
|
282
|
+
clearTimeout(this.swipeTimeout);
|
|
283
|
+
}
|
|
284
|
+
cancel(evt) {
|
|
285
|
+
this.cancelAll();
|
|
286
|
+
this.touchCancel.dispatch(evt, this.element);
|
|
287
|
+
}
|
|
288
|
+
_cancelLongTap() {
|
|
289
|
+
clearTimeout(this.longTapTimeout);
|
|
290
|
+
}
|
|
291
|
+
_cancelSingleTap() {
|
|
292
|
+
clearTimeout(this.singleTapTimeout);
|
|
293
|
+
}
|
|
294
|
+
_swipeDirection(x1, x2, y1, y2) {
|
|
295
|
+
return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : y1 - y2 > 0 ? 'Up' : 'Down';
|
|
296
|
+
}
|
|
297
|
+
on(evt, handler) {
|
|
298
|
+
if (this[evt]) {
|
|
299
|
+
this[evt].add(handler);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
off(evt, handler) {
|
|
303
|
+
if (this[evt]) {
|
|
304
|
+
this[evt].del(handler);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
destroy() {
|
|
308
|
+
if (this.singleTapTimeout) {
|
|
309
|
+
clearTimeout(this.singleTapTimeout);
|
|
310
|
+
}
|
|
311
|
+
if (this.tapTimeout) {
|
|
312
|
+
clearTimeout(this.tapTimeout);
|
|
313
|
+
}
|
|
314
|
+
if (this.longTapTimeout) {
|
|
315
|
+
clearTimeout(this.longTapTimeout);
|
|
316
|
+
}
|
|
317
|
+
if (this.swipeTimeout) {
|
|
318
|
+
clearTimeout(this.swipeTimeout);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
this.element.removeEventListener('touchstart', this.start);
|
|
322
|
+
this.element.removeEventListener('touchmove', this.move);
|
|
323
|
+
this.element.removeEventListener('touchend', this.end);
|
|
324
|
+
this.element.removeEventListener('touchcancel', this.cancel);
|
|
325
|
+
|
|
326
|
+
this.rotate.del();
|
|
327
|
+
this.touchStart.del();
|
|
328
|
+
this.multipointStart.del();
|
|
329
|
+
this.multipointEnd.del();
|
|
330
|
+
this.pinch.del();
|
|
331
|
+
this.swipe.del();
|
|
332
|
+
this.tap.del();
|
|
333
|
+
this.doubleTap.del();
|
|
334
|
+
this.longTap.del();
|
|
335
|
+
this.singleTap.del();
|
|
336
|
+
this.pressMove.del();
|
|
337
|
+
this.twoFingerPressMove.del();
|
|
338
|
+
this.touchMove.del();
|
|
339
|
+
this.touchEnd.del();
|
|
340
|
+
this.touchCancel.del();
|
|
341
|
+
|
|
342
|
+
this.preV =
|
|
343
|
+
this.pinchStartLen =
|
|
344
|
+
this.zoom =
|
|
345
|
+
this.isDoubleTap =
|
|
346
|
+
this.delta =
|
|
347
|
+
this.last =
|
|
348
|
+
this.now =
|
|
349
|
+
this.tapTimeout =
|
|
350
|
+
this.singleTapTimeout =
|
|
351
|
+
this.longTapTimeout =
|
|
352
|
+
this.swipeTimeout =
|
|
353
|
+
this.x1 =
|
|
354
|
+
this.x2 =
|
|
355
|
+
this.y1 =
|
|
356
|
+
this.y2 =
|
|
357
|
+
this.preTapPosition =
|
|
358
|
+
this.rotate =
|
|
359
|
+
this.touchStart =
|
|
360
|
+
this.multipointStart =
|
|
361
|
+
this.multipointEnd =
|
|
362
|
+
this.pinch =
|
|
363
|
+
this.swipe =
|
|
364
|
+
this.tap =
|
|
365
|
+
this.doubleTap =
|
|
366
|
+
this.longTap =
|
|
367
|
+
this.singleTap =
|
|
368
|
+
this.pressMove =
|
|
369
|
+
this.touchMove =
|
|
370
|
+
this.touchEnd =
|
|
371
|
+
this.touchCancel =
|
|
372
|
+
this.twoFingerPressMove =
|
|
373
|
+
null;
|
|
374
|
+
|
|
375
|
+
window.removeEventListener('scroll', this._cancelAllHandler);
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
}
|