-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss3d-simple-resize.js
205 lines (153 loc) · 6.58 KB
/
css3d-simple-resize.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
css3d.prototype.SimpleResize = function (options) {
_.defaults(options, { resizable3d: this });
if (this.resizer && this.resizer.resizable3d) this.resizer.destroy();
this.resizer = new SimpleResize(options);
this._registeredListeners.push('resizer');
};
var SimpleResize = function (options) {
_.extend(this, options);
this.min = this.min || 0;
this.rootOffset = this.min;
this.offsetX = [0.0, 0.0];
this.resizing = false;
this.tap = this.tap.bind(this);
this.drag = this.drag.bind(this);
this.pinch = this.pinch.bind(this);
this.release = this.release.bind(this);
// It's important to have both events:
// e.g. MS Surface supports both depending on touch or stylus click
if (css3d.touchIsSupported) {
this.resizable3d.el.addEventListener('touchstart', this.tap);
}
this.resizable3d.el.addEventListener('mousedown', this.tap);
};
_.extend(SimpleResize.prototype, {
tap: function (e) {
if (e.target !== this.resizable3d.el) {
return;
}
this.resizable3d.style[css3d.duration] = 0;
this.resizing = false;
this.reference0 = this.xpos(e);
if (e.targetTouches && e.targetTouches.length > 1) {
this.reference0 = getTouchPoint(e, 0);
// Set rootOffset to the left-most touch point:
this.rootOffset = this.reference0 - this.resizable3d.el.parentElement.getBoundingClientRect().left;
window.removeEventListener('touchmove', this.drag);
window.addEventListener('touchmove', this.pinch);
window.addEventListener('touchend', this.release);
return false; // cancel any other click handlers
}
// Store a reference for where each of the drag handles are:
this.offsetX = [0, parseInt(this.resizable3d.style.width)];
this.rootOffset = this.resizable3d.getX();
// Store which handle we're dragging from ...
if (this.xOffset(e) < 20) { // 20 is the number of pixels overlapping from the transparent resize areas
this.curDragHandle = 0; // the drag handle on the left
} else if (this.offsetX[1] - this.xOffset(e) < 20) { // within 20px of resizable el's width
this.curDragHandle = 1; // the drag handle on the right
} else {
return; // we're not resizing, don't set up any other listeners etc
}
this.resizable3d.el.classList.add('resizing');
if (css3d.touchIsSupported) {
window.addEventListener('touchmove', this.drag);
window.addEventListener('touchend', this.release);
}
window.addEventListener('mousemove', this.drag);
window.addEventListener('mouseup', this.release);
e.stopPropagation();
e.stopImmediatePropagation();
e.preventDefault();
return false;
},
drag: function (e) {
var x, delta;
// 'x' & 'reference' are in the window space (clientX)
x = this.xpos(e);
delta = this.reference0 - x;
if (delta > 2 || delta < -2) {
this.resizing = true;
this.reference0 = x;
this.offsetX[this.curDragHandle] -= delta;
this.resize(); // uses offsetX[n0, n1]
}
// e.stopPropagation();
e.preventDefault();
// return false;
},
pinch: function (e) {
// We get here when we're __touching__ with >= 2 fingers
this.offsetX[0] = getTouchPoint(e, 0);
this.offsetX[1] = getTouchPoint(e, 1);
this.resizable3d.setTranslate(this.rootOffset + this.offsetX[0] - this.reference0);
this.resizable3d.style.width = Math.max(90, this.offsetX[1] - this.offsetX[0]) + 'px';
e.preventDefault();
},
resize: function () {
// We get here when dragging one(!) of the loop handles
// (example) this.offsetX === [-5, 395];
var leftAnchor = Math.min(this.offsetX[0], this.offsetX[1]) + this.rootOffset;
leftAnchor = Math.max(0, Math.min(leftAnchor, this.max)); // bounds check
var width = Math.max(this.offsetX[0], this.offsetX[1]) + this.rootOffset - leftAnchor;
width = Math.max(90, Math.min(width, this.max - leftAnchor)); // bounds check
width += 'px'; // deliberately on a new line for memory efficiency
this.resizable3d.setTranslate(leftAnchor);
this.resizable3d.style.width = width;
},
release: function (e) {
this.resizable3d.style[css3d.duration] = null;
this.resizable3d.el.classList.remove('resizing');
if (css3d.touchIsSupported) {
if (e.touches && e.touches.length < 2) {
window.removeEventListener('touchmove', this.pinch);
}
window.removeEventListener('touchmove', this.drag);
window.removeEventListener('touchend', this.release);
}
window.removeEventListener('mousemove', this.drag);
window.removeEventListener('mouseup', this.release);
this.resizing = false;
e.preventDefault();
},
xOffset: function (e, finger) {
// touch event
if (e.touches && e.touches.length >= 1) {
return e.touches[finger || 0].pageX - this.resizable3d.el.getBoundingClientRect().left;
}
// mouse event
// offsetX doesn't exist in firefox, but getBoundingClientRect does
return e.offsetX || e.pageX - this.resizable3d.el.getBoundingClientRect().left;
},
xpos: function (e) {
// touch event
if (e.touches && e.touches.length >= 1) {
return e.touches[0].clientX;
}
// mouse event
return e.clientX;
},
killEventHandlers: function () {
if (css3d.touchIsSupported) {
window.removeEventListener('touchmove', this.drag);
window.removeEventListener('touchmove', this.pinch);
window.removeEventListener('touchend', this.release);
}
window.removeEventListener('mousemove', this.drag);
window.removeEventListener('mouseup', this.release);
},
destroy: function () {
this.killEventHandlers();
if (css3d.touchIsSupported) {
this.resizable3d.el.removeEventListener('touchstart', this.tap);
}
this.resizable3d.el.removeEventListener('mousedown', this.tap);
this.resizable3d = this.tap = this.drag = this.pinch = this.release = null;
},
});
// touch point 0 is the left-most touch point
// touch point 1 is the right-most touch point
function getTouchPoint(e, point) {
return Math[point === 1 ? 'max' : 'min'](e.touches[0].pageX, e.touches[1].pageX);
}