JcropTouch.js
2.8 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
/**
* JcropTouch
* Detects and enables mobile touch support
*/
// var JcropTouch = function(core){{{
var JcropTouch = function(core){
this.core = core;
this.init();
};
// }}}
$.extend(JcropTouch,{
// support: function(){{{
support: function(){
if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch)
return true;
},
// }}}
prototype: {
// init: function(){{{
init: function(){
var t = this,
p = $.Jcrop.component.DragState.prototype;
// A bit of an ugly hack to make sure we modify prototype
// only once, store a key on the prototype
if (!p.touch) {
t.initEvents();
t.shimDragState();
t.shimStageDrag();
p.touch = true;
}
},
// }}}
// shimDragState: function(){{{
shimDragState: function(){
var t = this;
$.Jcrop.component.DragState.prototype.initEvents = function(e){
// Attach subsequent drag event handlers based on initial
// event type - avoids collecting "pseudo-mouse" events
// generated by some mobile browsers in some circumstances
if (e.type.substr(0,5) == 'touch') {
$(this.eventTarget)
.on('touchmove.jcrop.jcrop-touch',t.dragWrap(this.createDragHandler()))
.on('touchend.jcrop.jcrop-touch',this.createStopHandler());
}
// For other events, use the mouse handlers that
// the default DragState.initEvents() method sets...
else {
$(this.eventTarget)
.on('mousemove.jcrop',this.createDragHandler())
.on('mouseup.jcrop',this.createStopHandler());
}
};
},
// }}}
// shimStageDrag: function(){{{
shimStageDrag: function(){
this.core.container
.addClass('jcrop-touch')
.on('touchstart.jcrop.jcrop-stage',this.dragWrap(this.core.ui.manager.startDragHandler()));
},
// }}}
// dragWrap: function(cb){{{
dragWrap: function(cb){
return function(e){
e.preventDefault();
e.stopPropagation();
if (e.type.substr(0,5) == 'touch') {
e.pageX = e.originalEvent.changedTouches[0].pageX;
e.pageY = e.originalEvent.changedTouches[0].pageY;
return cb(e);
}
return false;
};
},
// }}}
// initEvents: function(){{{
initEvents: function(){
var t = this, c = t.core;
c.container.on(
'touchstart.jcrop.jcrop-touch',
'.'+c.opt.css_drag,
t.dragWrap(c.startDrag())
);
}
// }}}
}
});
Jcrop.registerComponent('Touch',JcropTouch);