-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGloveBox.js
707 lines (583 loc) · 20 KB
/
GloveBox.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
The MIT License
Copyright (c) 2010-2011 Jesse MacFadyen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var GloveBox = (function(){
var Gbx = function(elemOrId,opts)
{
if ( this instanceof Gbx )
{
if (typeof elemOrId == "string" || elemOrId instanceof String)
{
this.element = document.getElementById(elemOrId);
}
else
{
this.element = elemOrId;
}
this.par.addEventListener(Gbx.StartEvent, this, false);
this.par.addEventListener('click', this, true);
this.element.addEventListener("webkitTransitionEnd", this, true );
this.element.style.webkitTransformOrigin = "0% 0%";
for(var v in opts)
{
this[v] = opts[v];
}
}
else
{
return new Gbx(elemOrId);
}
}
// set defaults for mouse
Gbx.StartEvent = "mousedown";
Gbx.EndEvent = "mouseup";
Gbx.MoveEvent = "mousemove";
Gbx.CancelEvent = "touchcancel";
// This used to be mouseout, but we still want to track once the mouse leave, so we leave it to touchcancel
// even though it will not be fired
Gbx.DraggingTransition = "-webkit-transform none";
Gbx.AfterTouchTransition = "-webkit-transform 400ms ease-out";
Gbx.BounceBackTransition = "-webkit-transform 200ms ease-out";
Gbx.Friction = 0.002;
try
{
document.createEvent("TouchEvent");
Gbx.CanTouch = true;
// update events to use touch events, they are available
Gbx.StartEvent = "touchstart";
Gbx.EndEvent = "touchend";
Gbx.MoveEvent = "touchmove";
Gbx.CancelEvent = "touchcancel";
}
catch (e) //look for exception to feature-detection touch events.
{
Gbx.CanTouch = false;
}
Gbx.prototype =
{
formElements:null,
state:"ready",
//Scaling stuff
_scalable:false,
isScaling:false,
startScale:1.0,
_scale:1.0,
maxScale:4.0,
minScale:0.2,
_scaleCenter:{x:0,y:0},
// Rotation stuff ( buggy )
_rotation:0,
canRotate:false,
startAngle:0,
_angle:0,
isDragging:false,
_x:0,_y:0,
startTime:0,
// snapping to coords ex. {x:20,y:20}
snap:null,
clampX:false,clampY:false,
lastX:0,lastY:0,
scrollX:false,scrollY:true,
_dragT:null,
_afterT:null,
_bounceT:null,
_dragThreshold:20,
par:function(){ return this.element.parentNode;},
get_x:function(){ return this._x; },
set_x:function(inX){ this.setPos(inX,this._y);},
get_y:function(){ return this._y;},
set_y:function(inY){ this.setPos(this._x,inY);},
get_scale:function(){return this._scale;},
set_scale:function(inS)
{
this._scale = Math.max(Math.min(inS,this.maxScale),this.minScale);
this._updateTransform();
},
// Transition to display while the user is dragging
get_dragTrans:function(){
return this._dragT || Gbx.DraggingTransition;
},
set_dragTrans:function(t){
this._dragT = t;
},
// Transition to display when the user releases
get_afterTrans:function(){
return this._afterT || Gbx.AfterTouchTransition;
},
set_afterTrans:function(t){
this._afterT = t;
},
// Transition to display when we enforce the boundaries
get_bounceTrans:function(){
return this._bounceT || Gbx.BounceBackTransition;
},
set_bounceTrans:function(t){
this._bounceT = t;
},
get_canScale:function(){return this._scalable;},
set_canScale:function(b)
{
this._scalable = (b === true);
if(this._scalable)
{
this.par.addEventListener("gesturestart", this,true);
}
else
{
this.par.removeEventListener("gesturestart", this,true);
}
},
getContentSize:function()
{
return {"y":this.element.scrollHeight * this.scale,
"x":this.element.scrollWidth * this.scale};
},
handleEvent:function(evt)
{
if(typeof this[evt.type] === "function")
{
return this[evt.type](evt);
}
else
{
console.log("event handler is not a function :: " + evt.type);
}
},
setPos:function(x,y)
{
this._x = (this.scrollX ? x : this._x);
this._y = (this.scrollY ? y : this._y);
this._updateTransform();
},
scrollToBottom:function()
{
var minY = this.par.offsetHeight - this.par.scrollHeight;
this.setPos(this._x,minY);
},
_updateTransform:function()
{
this.element.style.webkitTransform = "translate3d("+this._x+"px,"+this._y+"px,0px) scale3D("+this.scale+","+this.scale+",1.0)";
},
addTouchListeners:function()
{
window.addEventListener(Gbx.EndEvent, this, false);
window.addEventListener(Gbx.MoveEvent, this, false);
window.addEventListener(Gbx.CancelEvent, this, false);
},
removeTouchListeners:function()
{
window.removeEventListener(Gbx.EndEvent, this, false);
window.removeEventListener(Gbx.MoveEvent, this, false);
window.removeEventListener(Gbx.CancelEvent, this, false);
},
disableInputs:function()
{
var src = document;
if("getElementsByTagName" in this.par)
{
src = this.par;
}
this.formElements = [];
var tags = ["select","input","textarea"];
for(var s = 0; s < 3; s++)
{
var elems = src.getElementsByTagName(tags[s]);
if (elems)
{
for(var n = 0, len = elems.length; n < len; n++)
{
var elem = elems[n];
if(!elem.disabled)
{
elem.disabled = true;
this.formElements.push(elem);
}
}
}
}
},
reenableInputs:function()
{
if(this.formElements)
{
for(var n = 0, len = this.formElements.length; n < len; n++)
{
this.formElements[n].disabled = false;
}
this.formElements = null;
}
},
click:function(e)
{
if(this.isDragging)
{
e.preventDefault();
e.stopPropagation();
}
},
getClampedX:function(x)
{
if(!this.clampX)
return x;
var parentNode = this.par;
// get updated x position
var pWidth = parentNode.clientWidth;
var selfWidth = this.element.scrollWidth * this.scale;
var maxX = 0;
var minX = ( ( parentNode.offsetWidth - this.element.scrollWidth ) * this.scale);
var newLeft = x;
if(newLeft > maxX)
{
newLeft = maxX;
this.xVel = 0;
}
else if(newLeft < minX)
{
newLeft = minX;
this.xVel = 0;
}
return newLeft;
},
getClampedY:function(y)
{
if(!this.clampY)
return y;
var parentNode = this.par;
// get updated y position
var selfHeight = this.element.scrollHeight * this.scale;
var maxY = 0;
var minY = ((parentNode.offsetHeight - this.element.scrollHeight ) * this.scale);
var newTop = y;
if(newTop > maxY)
{
newTop = maxY;
this.yVel = 0;
}
else if(newTop < minY)
{
newTop = minY;
this.yVel = 0;
}
return newTop;
},
// Transition Ended
webkitTransitionEnd:function(e)
{
console.log("webkitTransitionEnd::" + this.state);
// this is only really here in case we decide to support event broadcasting later
switch(this.state)
{
case "ready" : // fallthrough is intentional
case "starting" :
case "moving" :
case "scaling" :
case "done":
break;
case "ending" :
this.finishScrolling();
break;
}
},
/* :: Mouse Events :: */
mousedown:function(e)
{
e.preventDefault(); // this prevents Chrome's drag action on images!
this.isDragging = true;
var touchEvent = {timeStamp:e.timeStamp};
touchEvent.preventDefault = function(){e.preventDefault();};
touchEvent.stopPropagation = function(){e.stopPropagation();};
touchEvent.targetTouches = [{pageX:e.x,pageY:e.y}];
return this.touchstart(touchEvent);
},
mousemove:function(e)
{
if(this.isDragging)
{
var touchEvent = {timeStamp:e.timeStamp};
touchEvent.preventDefault = function(){e.preventDefault();};
touchEvent.stopPropagation = function(){e.stopPropagation();};
touchEvent.targetTouches = [{pageX:e.x,pageY:e.y}];
return this.touchmove(touchEvent);
}
return false;
},
mouseup:function(e)
{
var touchEvent = {timeStamp:e.timeStamp};
touchEvent.preventDefault = function(){e.preventDefault();};
touchEvent.stopPropagation = function(){e.stopPropagation();};
return this.touchend(touchEvent);
},
/* :: Touch Events :: */
touchstart:function(e)
{
// Start tracking
if (e.targetTouches.length != 1 || this.isScaling)
{
return false;
}
this.stopMomentum();
this.state = "starting";
e.stopPropagation();
this.startX = e.targetTouches[0].pageX;
this.startY = e.targetTouches[0].pageY;
this.lastX = this.startX;
this.lastY = this.startY;
this.xVel = 0;
this.yVel = 0;
this.startTime = e.timeStamp;
this.addTouchListeners();
return false;
},
touchmove:function(e)
{
// Don't track motion when multiple touches are down in this element (that's a gesture)
if(!e.targetTouches || e.targetTouches.length != 1 || this.isScaling)
{
return false;
}
var currentX = e.targetTouches[0].pageX;
var currentY = e.targetTouches[0].pageY;
// TODO: this should allow velocity to be added
// instead of always moving it to where the finger is now,
// we could move it to where it was + ( velocity * elapsed )
// currently speed is NOT used while we are scrolling, and recalculated
// once we let go
if(this.formElements == null)
{
this.disableInputs();
}
this.state = "moving";
// Prevent the browser from doing its default thing (scroll, zoom)
e.preventDefault();
var dX = (currentX - this.lastX);
var dY = (currentY - this.lastY);
if(!this.isDragging)
{
if( Math.abs(dX) > this._dragThreshold || Math.abs(dY) > this._dragThreshold )
{
this.isDragging = true;
// use a tighter animation during dragging [ or NONE! ]
this.element.style.webkitTransition = this.dragTrans;
}
}
// it may have been changed by the statement above
if(this.isDragging)
{
var newX = this.x;
var newY = this.y;
if(this.scrollX)
{
newX = this.getClampedX(this.x + dX);
}
if(this.scrollY)
{
newY = this.getClampedY(this.y + dY);
}
this.setPos(newX,newY);
this.lastX = currentX;
this.lastY = currentY;
}
},
touchend:function(e)
{
if (this.isDragging)
{
var self = this;
// reenable inputs after a short wait
setTimeout(function(){ self.reenableInputs(); }, 1500);
// Prevent the browser from doing its default thing (scroll, zoom)
this.removeTouchListeners();
this.state = "ending";
var elapsed = e.timeStamp - this.startTime;
if(this.scrollY)
{
var dY = this.lastY - this.startY;
this.yVel = dY / elapsed;
if( Math.abs(this.yVel) < 0.5 )
{
this.yVel = 0;
}
}
if(this.scrollX)
{
var dX = this.lastX - this.startX;
this.xVel = dX / elapsed;
if(Math.abs(this.xVel) < 0.5)
{
this.xVel = 0;
}
}
// if we are not really moving, we can skip the extra logic of momentum
if (Math.abs(this.xVel) + Math.abs(this.yVel) < 0.5)
{
// can we get out early?
this.finishScrolling();
}
else
{
this.doMomentum();
}
}
return false;
},
touchcancel:function(e)
{
this.touchend(e);
},
/* :: Momentum :: aka Fauxmentum :: */
isDecelerating:function()
{
return this.state == "ending";
},
doMomentum:function()
{
// calculate the vector velocity, using both xVelocity and yVelocity
// this velocity value is a normalized vector, it is always positive
var velocity = Math.sqrt( this.xVel * this.xVel + this.yVel * this.yVel);
// how long until we are stopped completely
var time = Math.floor( velocity / Gbx.Friction );
// delta Distance = velocity * time OR px * ms / ms
var dY = this.yVel * time;
var dX = this.xVel * time;
// Now do bounds checking
var pHeight = this.par.clientHeight;
var selfHeight = this.element.scrollHeight * this.scale;
var maxY = 0;
var minY = pHeight - selfHeight;
var newTop = Math.min(Math.max(this.y + dY,minY),maxY);
var pWidth = this.par.clientWidth;
var selfWidth = this.element.scrollWidth * this.scale;
var maxX = 0;
var minX = pWidth - selfWidth;
var newLeft = Math.min(Math.max(this.x + dX,minX),maxX);
this.element.style.webkitTransition = '-webkit-transform ' + time + 'ms cubic-bezier(0, 0, 0.44, 1)';
this.setPos(newLeft,newTop);
},
stopMomentum:function()
{
if (this.isDecelerating())
{
// Get the computed style object.
var style = document.defaultView.getComputedStyle(this.element, null);
// Computed the transform in a matrix object given the style.
var transform = new WebKitCSSMatrix(style.webkitTransform);
// Clear the active transition so it doesn’t apply to our next transform.
this.element.style.webkitTransition = "none";
// Set the element transform to where it is right now.
this.setPos(transform.m41,transform.m42);
}
},
/* :: Gestures - still a work in progress aka Jestures -jm :: */
addGestureListeners:function()
{
var f = this.par.addEventListener;
f('gesturechange', this, true);
f('gestureend', this, true);
},
removeGestureListeners:function()
{
var f = this.par.removeEventListener;
f('gesturechange', this, true);
f('gestureend', this, true);
},
gesturestart:function(e)
{
this.addGestureListeners();
this.isDragging = false;
this.isScaling = true;
this.startScale = this._scale;
this.startAngle = this._angle;
this.element.style.webkitTransition = this.dragTrans;
return false;
},
gesturechange:function(e)
{
this.isScaling = true;
this.scale = this.startScale * e.scale;
return false;
},
gestureend:function(e)
{
this.removeGestureListeners();
this.isScaling = false;
this.finishScrolling();
return false;
},
finishScrolling:function()
{
console.log("finishScrolling");
this.state = "done";
this.element.style.webkitTransition = this.bounceTrans;
this.isDragging = false;
var parentNode = this.par;
// get updated x position
var pWidth = parentNode.clientWidth;
var selfWidth = this.element.scrollWidth * this.scale;
var maxX = 0;
var minX = 0;
var newLeft = 0;
if (pWidth > selfWidth)
{
maxX = pWidth - selfWidth;
minX = 0;
newLeft = (pWidth - selfWidth)/2;
}
else
{
maxX = 0;
minX = pWidth - selfWidth;
newLeft = Math.min(Math.max(this.x,minX),maxX);
}
// get updated y
var pHeight = parentNode.clientHeight;
var selfHeight = this.element.scrollHeight * this.scale;
var minY = 0;
var maxY = 0;
var newTop = 0;
if(pHeight > selfHeight)
{
minY = 0;
maxY = pHeight - selfHeight;
newTop = (pHeight - selfHeight)/2;
}
else
{
maxY = 0;
minY = pHeight - selfHeight;
newTop = Math.min(Math.max(this.y,minY),maxY);
}
if(this.snap != null)
{
newLeft = this.snap.x ? Math.round(newLeft / this.snap.x ) * this.snap.x : newLeft;
newTop = this.snap.y ? Math.round(newTop / this.snap.y ) * this.snap.y : newTop;
}
this.setPos(newLeft,newTop);
}
};
// Utility function for allowing obj.x to call an accessor function pointing to obj.get_x()
function makeAccessors(obj,props)
{
function makeAccessor(prop)
{
Object.defineProperty(obj, prop, { get : obj["get_" + prop], set : obj["set_" + prop] } );
}
props.forEach(makeAccessor);
}
// turn gb.x into a call to gb.get_x() which in turn uses gb._x
makeAccessors(Gbx.prototype,[ "x", "y", "scale", "dragTrans", "afterTrans", "bounceTrans", "canScale" ]);
Object.defineProperty(Gbx.prototype, "par", { get :function(){return this.element.parentNode;}});
return Gbx;
})();