-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzwoosh.js
1380 lines (1380 loc) · 70.2 KB
/
zwoosh.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
/**
* Export function of the module
*
* @param {HTMLElement} container - The HTMLElement to swoooosh!
* @param {Options} options - the options object to configure Zwoosh
* @return {Zwoosh} - Zwoosh object instance
*/
function zwoosh(container, options) {
if (options === void 0) { options = {}; }
/**
* Polyfill bind function for older browsers
* The bind function is an addition to ECMA-262, 5th edition
* @see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
*/
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, FNOP = function () { }, fBound = function () {
return fToBind.apply(this instanceof FNOP ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
};
if (this.prototype) {
// Function.prototype doesn't have a prototype property
FNOP.prototype = this.prototype;
}
fBound.prototype = new FNOP();
return fBound;
};
}
/**
* Polyfill array.indexOf function for older browsers
* The indexOf() function was added to the ECMA-262 standard in the 5th edition
* as such it may not be present in all browsers.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
*/
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement, fromIndex) {
var k;
if (this === null) {
throw new TypeError("'this' is null or not defined");
}
var o = Object(this);
var len = o.length >>> 0;
if (len === 0) {
return -1;
}
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
if (n >= len) {
return -1;
}
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (k in o && o[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
/* list of real events */
var htmlEvents = {
/* <body> and <frameset> Events */
onload: 1,
onunload: 1,
/* Form Events */
onblur: 1,
onchange: 1,
onfocus: 1,
onreset: 1,
onselect: 1,
onsubmit: 1,
/* Image Events */
onabort: 1,
/* Keyboard Events */
onkeydown: 1,
onkeypress: 1,
onkeyup: 1,
/* Mouse Events */
onclick: 1,
ondblclick: 1,
onmousedown: 1,
onmousemove: 1,
onmouseout: 1,
onmouseover: 1,
onmouseup: 1
};
var mapEvents = {
onscroll: window
};
;
/**
* Zwoosh provides a set of functions to implement scroll by drag, zoom by mousewheel,
* hash links inside the document and other special scroll related requirements.
*
* @author Roman Gruber <p1020389@yahoo.com>
* @version 1.0.1
*/
var Zwoosh = (function () {
function Zwoosh(container, options) {
this.container = container;
this.options = options;
/* CSS style classes */
this.classInner = "zw-inner";
this.classOuter = "zw-outer";
this.classGrab = "zw-grab";
this.classNoGrab = "zw-nograb";
this.classGrabbing = "zw-grabbing";
this.classUnique = "zw-" + Math.random().toString(36).substring(7);
this.classScale = "zw-scale";
this.classIgnore = "zw-ignore";
this.classFakeBody = "zw-fakebody";
/* array holding the custom events mapping callbacks to bound callbacks */
this.customEvents = [];
this.triggered = {
collideLeft: false,
collideTop: false,
collideRight: false,
collideBottom: false
};
/* fadeOut */
this.timeouts = [];
this.vx = [];
this.vy = [];
this.container = container;
/* set default options */
var defaultOptions = {
/* 1 means do not align to a grid */
gridX: 1,
gridY: 1,
/* shows a grid as an overlay over the element. Works only if the browser supports
* CSS Generated content for pseudo-elements
* @see http://caniuse.com/#search=%3Abefore */
gridShow: false,
/* which edge should be elastic */
elasticEdges: {
left: false,
right: false,
top: false,
bottom: false
},
/* activates/deactivates scrolling by drag */
dragScroll: true,
dragOptions: {
exclude: ["input", "textarea", "a", "button", "." + this.classIgnore, "select"],
only: [],
/* activates a scroll fade when scrolling by drag */
fade: true,
/* fade: brake acceleration in pixels per second per second (p/s^2) */
brakeSpeed: 2500,
/* fade: frames per second of the zwoosh fadeout animation (>=25 looks like motion) */
fps: 30,
/* fade: this speed will never be exceeded */
maxSpeed: 3000,
/* fade: minimum speed which triggers the fade */
minSpeed: 300
},
/* activates/deactivates scrolling by wheel. If the dreiction is vertical and there are
* scrollbars present, zwoosh lets leaves scrolling to the browser. */
wheelScroll: true,
wheelOptions: {
/* direction to scroll when the mouse wheel is used */
direction: "vertical",
/* amount of pixels for one scroll step */
step: 114,
/* scroll smooth or instant */
smooth: true
},
/* activates/deactivates zooming by wheel. Works only with a CSS3 2D Transform capable browser.
* @see http://caniuse.com/#feat=transforms2d */
wheelZoom: false,
zoomOptions: {
/* the maximum scale, 0 means no maximum */
maxScale: 0,
/* the minimum scale, 0 means no minimum */
minScale: 0,
/* one step when using the wheel to zoom */
step: 0.1,
/* mouse wheel direction to zoom larger */
direction: "up"
},
/* let zwoosh handle anchor links targeting to an anchor inside of this zwoosh element.
* the element outside (maybe the body) handles anchors too. If you want to prevent this,
* add to body as zwoosh element too. */
handleAnchors: true
};
this.options = this.merge(options, defaultOptions);
this.init();
}
/**
* Merge the default option object with the provided one
*
* @param {Options} options - the given options
* @param {Options} defaultOptions - the default options
* @return {Options} - the merged options object
*/
Zwoosh.prototype.merge = function (options, defaultOptions) {
/* merge the default option object with the provided one */
for (var key in options) {
if (options.hasOwnProperty(key)) {
if (typeof options[key] === "object") {
for (var okey in options[key]) {
if (options[key].hasOwnProperty(okey))
defaultOptions[key][okey] = options[key][okey];
}
}
else {
defaultOptions[key] = options[key];
}
}
}
return defaultOptions;
};
/**
* Initialize DOM manipulations and event handlers
*
* @return {void}
*/
Zwoosh.prototype.init = function () {
var _this = this;
this.initBody();
this.addClass(this.container, this.classOuter);
this.scrollElement = this.container;
var x = this.getScrollLeft();
var y = this.getScrollTop();
/* create inner div element and append it to the container with its contents in it */
this.inner = document.createElement("div");
this.addClass(this.inner, this.classInner);
this.addClass(this.inner, this.classUnique);
this.scaleElement = document.createElement("div");
this.addClass(this.scaleElement, this.classScale);
this.scaleElement.appendChild(this.inner);
/* move all childNodes to the new inner element */
while (this.container.childNodes.length > 0) {
this.inner.appendChild(this.container.childNodes[0]);
}
this.container.appendChild(this.scaleElement);
var border = this.getBorder(this.container);
this.inner.style.minWidth = (this.container.scrollWidth - border[0]) + "px";
this.inner.style.minHeight = (this.container.scrollHeight - border[1]) + "px";
this.scaleElement.style.minWidth = this.inner.style.minWidth;
this.scaleElement.style.minHeight = this.inner.style.minHeight;
this.scaleElement.style.overflow = "hidden";
this.initGrid();
this.oldClientWidth = document.documentElement.clientWidth;
this.oldClientHeight = document.documentElement.clientHeight;
/* just call the function, to trigger possible events */
this.onScroll();
/* scroll to the initial position */
this.scrollTo(x, y, false);
/* Event handler registration start here */
this.initWheelScroll();
this.initWheelZoom();
/* scrollhandler */
this.scrollHandler = function (e) { return _this.onScroll(e); };
this.addEventListener(this.container, "scroll", this.scrollHandler);
/* if the scroll element is body, adjust the inner div when resizing */
if (this.isBody) {
this.resizeHandler = function (e) { return _this.onResize(e); }; // TODO: same as above in the wheel handler
window.onresize = this.resizeHandler;
}
this.initDragScroll();
this.initAnchors();
};
/**
* Reinitialize the zwoosh element
*
* @return {Zwoosh} - The Zwoosh object instance
* @TODO: preserve scroll position in init()
*/
Zwoosh.prototype.reinit = function () {
this.destroy();
this.classUnique = "zw-" + Math.random().toString(36).substring(7);
this.init();
return this;
};
Zwoosh.prototype.initBody = function () {
this.isBody = this.container.tagName === "BODY" ? true : false;
/* Chrome solution to scroll the body is not really viable, so we create a fake body
* div element to scroll on */
if (this.isBody === true) {
var pseudoBody = document.createElement("div");
this.addClass(pseudoBody, this.classFakeBody);
pseudoBody.style.cssText = document.body.style.cssText;
while (this.container.childNodes.length > 0) {
pseudoBody.appendChild(this.container.childNodes[0]);
}
this.container.appendChild(pseudoBody);
this.container = pseudoBody;
}
};
Zwoosh.prototype.initGrid = function () {
/* show the grid only if at least one of the grid values is not 1 */
if ((this.options.gridX !== 1 || this.options.gridY !== 1) && this.options.gridShow) {
var bgi = [];
if (this.options.gridX !== 1) {
bgi.push("linear-gradient(to right, grey 1px, transparent 1px)");
}
if (this.options.gridY !== 1) {
bgi.push("linear-gradient(to bottom, grey 1px, transparent 1px)");
}
this.addBeforeCSS(this.classUnique, "width", this.inner.style.minWidth);
this.addBeforeCSS(this.classUnique, "height", this.inner.style.minHeight);
this.addBeforeCSS(this.classUnique, "left", "-" + this.getStyle(this.container, "paddingLeft"));
this.addBeforeCSS(this.classUnique, "top", "-" + this.getStyle(this.container, "paddingTop"));
this.addBeforeCSS(this.classUnique, "background-size", (this.options.gridX !== 1 ? this.options.gridX + "px " : "auto ") + (this.options.gridY !== 1 ? this.options.gridY + "px" : "auto"));
this.addBeforeCSS(this.classUnique, "background-image", bgi.join(", "));
}
};
Zwoosh.prototype.initWheelScroll = function () {
var _this = this;
/* TODO: not 2 different event handlers registrations -> do it in this.addEventListener() */
if (this.options.wheelScroll === false) {
this.mouseScrollHandler = function (e) { return _this.disableMouseScroll(e); };
this.scrollElement.onmousewheel = this.mouseScrollHandler;
this.addEventListener(this.scrollElement, "wheel", this.mouseScrollHandler);
}
else if (this.options.wheelScroll === true) {
this.mouseScrollHandler = function (e) { return _this.activeMouseScroll(e); };
this.scrollElement.onmousewheel = this.mouseScrollHandler;
this.addEventListener(this.scrollElement, "wheel", this.mouseScrollHandler);
}
};
/* wheelzoom */
Zwoosh.prototype.initWheelZoom = function () {
var _this = this;
if (this.options.gridShow) {
this.scaleTo(1);
}
if (this.options.wheelZoom === true) {
this.mouseZoomHandler = function (e) { return _this.activeMouseZoom(e); };
this.addEventListener(this.scrollElement, "wheel", this.mouseZoomHandler);
}
};
Zwoosh.prototype.initDragScroll = function () {
var _this = this;
/* if dragscroll is activated, register mousedown event */
if (this.options.dragScroll === true) {
this.addClass(this.inner, this.classGrab);
this.mouseDownHandler = function (e) { return _this.mouseDown(e); };
this.addEventListener(this.inner, "mousedown", this.mouseDownHandler);
}
else {
this.addClass(this.container, this.classNoGrab);
}
};
Zwoosh.prototype.initAnchors = function () {
var _this = this;
if (this.options.handleAnchors === true) {
var links = this.container.querySelectorAll("a[href^='#']");
this.hashChangeClickHandler = function (e) {
var target = e ? e.target : window.event.srcElement;
if (typeof target !== "undefined") {
/* pushState changes the hash without triggering hashchange */
history.pushState({}, "", target.href);
/* we don't want to trigger hashchange, so prevent default behavior when clicking on anchor links */
if (e.preventDefault) {
e.preventDefault();
}
else {
e.returnValue = false;
}
}
/* trigger a custom hashchange event, because pushState prevents the real hashchange event */
_this.triggerEvent(window, "myhashchange");
};
/* loop trough all anchor links in the element and disable them to prevent the
* browser from scrolling because of the changing hash value. Instead the own
* event myhashchange should handle page and element scrolling */
for (var i = 0; i < links.length; i++) {
this.addEventListener(links[i], "click", this.hashChangeClickHandler, false);
}
this.hashChangeHandler = function (e) { return _this.onHashChange(e); };
this.addEventListener(window, "myhashchange", this.hashChangeHandler);
this.addEventListener(window, "hashchange", this.hashChangeHandler);
this.onHashChange();
}
};
/* @TODO: ScrollWidth and ClientWidth ScrollHeight ClientHeight */
Zwoosh.prototype.getScrollLeft = function () {
return this.scrollElement.scrollLeft;
};
Zwoosh.prototype.setScrollLeft = function (left) {
this.scrollElement.scrollLeft = left;
};
Zwoosh.prototype.getScrollTop = function () {
return this.scrollElement.scrollTop;
};
Zwoosh.prototype.setScrollTop = function (top) {
this.scrollElement.scrollTop = top;
};
/**
* Handle hashchanges with own scroll function
*
* @param {Event} e - the hashchange or myhashchange event, or nothing
* @return {void}
*/
Zwoosh.prototype.onHashChange = function (e) {
if (e === void 0) { e = null; }
var hash = window.location.hash.substr(1);
if (hash !== "") {
var anchors = this.container.querySelectorAll("#" + hash);
for (var i = 0; i < anchors.length; i++) {
var element = anchors[i];
var container_1 = anchors[i];
// find the next parent which is a container element
var nextContainer = element;
while (container_1 && container_1.parentElement && this.container !== container_1) {
if (this.hasClass(container_1, this.classOuter)) {
nextContainer = container_1;
}
container_1 = container_1.parentElement;
}
if (e !== null) {
if (e.type === "hashchange") {
/* scrolling instantly back to origin, before do the animated scroll */
this.scrollTo(this.originScrollLeft, this.originScrollTop, false);
}
}
this.scrollToElement(nextContainer);
return;
}
}
};
/**
* Scroll to an element in the DOM
*
* @param {HTMLElement} element - the HTMLElement to scroll to
*/
Zwoosh.prototype.scrollToElement = function (element) {
/* get relative coords from the anchor element */
var x = (element.offsetLeft - this.container.offsetLeft) * this.getScale();
var y = (element.offsetTop - this.container.offsetTop) * this.getScale();
this.scrollTo(x, y);
};
/**
* Workaround to manipulate ::before CSS styles with javascript
*
* @param {string} cssClass - the CSS class name to add ::before properties
* @param {string} cssProperty - the CSS property to set
* @param {string} cssValue - the CSS value to set
* @return {void}
*/
Zwoosh.prototype.addBeforeCSS = function (cssClass, cssProperty, cssValue) {
if (typeof document.styleSheets[0].insertRule === "function") {
document.styleSheets[0].insertRule("." + cssClass + "::before { " + cssProperty + ": " + cssValue + "}", 0);
}
else if (typeof document.styleSheets[0].addRule === "function") {
document.styleSheets[0].addRule("." + cssClass + "::before", cssProperty + ": " + cssValue);
}
};
/**
* Get compute pixel number of the whole width and height from a border of an element
*
* @param {HTMLElement} el - the HTML element
* @return {array} [width, height] - the amount of pixels
*/
Zwoosh.prototype.getBorder = function (el) {
var bl = this.convertBorder(this.getStyle(el, "borderLeftWidth"));
var br = this.convertBorder(this.getStyle(el, "borderRightWidth"));
var pl = this.convertSpan(this.getStyle(el, "paddingLeft"));
var pr = this.convertSpan(this.getStyle(el, "paddingRight"));
var ml = this.convertSpan(this.getStyle(el, "marginLeft"));
var mr = this.convertSpan(this.getStyle(el, "marginRight"));
var bt = this.convertBorder(this.getStyle(el, "borderTopWidth"));
var bb = this.convertBorder(this.getStyle(el, "borderBottomWidth"));
var pt = this.convertSpan(this.getStyle(el, "paddingTop"));
var pb = this.convertSpan(this.getStyle(el, "paddingBottom"));
var mt = this.convertSpan(this.getStyle(el, "marginTop"));
var mb = this.convertSpan(this.getStyle(el, "marginBottom"));
return [
(pl + pr + bl + br + ml + mr),
(pt + pb + bt + bb + mt + mb)
];
};
Zwoosh.prototype.convertBorder = function (b) {
return b === "thin" ? 1 : b === "medium" ? 3 : b === "thick" ? 5 : !isNaN(parseInt(b, 10)) ? parseInt(b, 10) : 0;
};
Zwoosh.prototype.convertSpan = function (v) {
return v === "auto" ? 0 : !isNaN(parseInt(v, 10)) ? parseInt(v, 10) : 0;
};
/**
* Disables the scroll wheel of the mouse
*
* @param {MouseWheelEvent} e - the mouse wheel event
* @return {void}
*/
Zwoosh.prototype.disableMouseScroll = function (e) {
if (this.elementBehindCursorIsMe(e.clientX, e.clientY)) {
this.clearTimeouts();
if (!e) {
e = window.event;
}
if (e.preventDefault) {
e.preventDefault();
}
else {
e.returnValue = false;
}
}
};
/**
* Determine whether an element has a scrollbar or not
*
* @param {HTMLElement} element - the HTMLElement
* @param {string} direction - determine the vertical or horizontal scrollbar?
* @return {boolean} - whether the element has scrollbars or not
*/
Zwoosh.prototype.hasScrollbar = function (element, direction) {
var has = false;
var overflow = "overflow";
if (direction === "vertical") {
overflow = "overflowY";
has = element.scrollHeight > element.clientHeight;
}
else if (direction === "horizontal") {
overflow = "overflowX";
has = element.scrollWidth > element.clientWidth;
}
// Check the overflow and overflowDirection properties for "auto" and "visible" values
has = this.getStyle(this.container, "overflow") === "visible" ||
this.getStyle(this.container, "overflowY") === "visible" ||
(has && this.getStyle(this.container, "overflow") === "auto") ||
(has && this.getStyle(this.container, "overflowY") === "auto");
return has;
};
/**
* Enables the scroll wheel of the mouse to scroll, specially for divs withour scrollbar
*
* @param {MouseWheelEvent} e - the mouse wheel event
* @return {void}
*/
Zwoosh.prototype.activeMouseScroll = function (e) {
if (!e) {
e = window.event;
}
if (this.elementBehindCursorIsMe(e.clientX, e.clientY)) {
var direction = void 0;
if ("deltaY" in e) {
direction = e.deltaY > 0 ? "down" : "up";
}
else if ("wheelDelta" in e) {
direction = e.wheelDelta > 0 ? "up" : "down";
}
else {
return;
}
/* use the normal scroll, when there are scrollbars and the direction is "vertical" */
if (this.options.wheelOptions.direction === "vertical" && this.hasScrollbar(this.scrollElement, this.options.wheelOptions.direction)) {
if (!((this.triggered.collideBottom && direction === "down") || (this.triggered.collideTop && direction === "up"))) {
this.clearTimeouts();
return;
}
}
this.disableMouseScroll(e);
var x = this.getScrollLeft();
var y = this.getScrollTop();
if (this.options.wheelOptions.direction === "horizontal") {
x = this.getScrollLeft() + (direction === "down" ? this.options.wheelOptions.step : this.options.wheelOptions.step * -1);
}
else if (this.options.wheelOptions.direction === "vertical") {
y = this.getScrollTop() + (direction === "down" ? this.options.wheelOptions.step : this.options.wheelOptions.step * -1);
}
this.scrollTo(x, y, false);
}
};
/**
* Enables the scroll wheel of the mouse to zoom
*
* @param {MouseWheelEvent} e - the mouse wheel event
* @return {void}
*/
Zwoosh.prototype.activeMouseZoom = function (e) {
if (!e) {
e = window.event;
}
if (this.elementBehindCursorIsMe(e.clientX, e.clientY)) {
var direction = void 0;
if ("deltaY" in e) {
direction = e.deltaY > 0 ? "down" : "up";
}
else if ("wheelDelta" in e) {
direction = e.wheelDelta > 0 ? "up" : "down";
}
else {
return;
}
var scale = void 0;
if (direction === this.options.zoomOptions.direction) {
scale = this.getScale() * (1 + this.options.zoomOptions.step);
}
else {
scale = this.getScale() / (1 + this.options.zoomOptions.step);
}
this.scaleTo(scale);
}
};
/**
* Calculates the size of the vertical scrollbar.
*
* @param {HTMLElement} el - The HTMLElememnt
* @return {number} - the amount of pixels used by the vertical scrollbar
*/
Zwoosh.prototype.scrollbarWidth = function (el) {
return el.offsetWidth - el.clientWidth - parseInt(this.getStyle(el, "borderLeftWidth")) - parseInt(this.getStyle(el, "borderRightWidth"));
};
/**
* Calculates the size of the horizontal scrollbar.
*
* @param {HTMLElement} el - The HTMLElememnt
* @return {number} - the amount of pixels used by the horizontal scrollbar
*/
Zwoosh.prototype.scrollbarHeight = function (el) {
return el.offsetHeight - el.clientHeight - parseInt(this.getStyle(el, "borderTopWidth")) - parseInt(this.getStyle(el, "borderBottomWidth"));
};
/**
* Retrieves the current scale value or 1 if it is not set.
*
* @return {number} - the current scale value
*/
Zwoosh.prototype.getScale = function () {
if (typeof this.inner.style.transform !== "undefined") {
var r = this.inner.style.transform.match(/scale\(([0-9,\.]+)\)/) || [""];
return parseFloat(r[1]) || 1;
}
return 1;
};
/**
* Scales the inner element by a relatice value based on the current scale value.
*
* @param {number} percent - percentage of the current scale value
* @param {boolean} honourLimits - whether to honour maxScale and the minimum width and height
* of the container element.
* @return {void}
*/
Zwoosh.prototype.scaleBy = function (percent, honourLimits) {
if (honourLimits === void 0) { honourLimits = true; }
var scale = this.getScale() * (percent / 100);
this.scaleTo(scale, honourLimits);
};
/**
* Scales the inner element to an absolute value.
*
* @param {number} scale - the scale
* @param {boolean} honourLimits - whether to honour maxScale and the minimum width and height
* of the container element.
* @return {void}
*/
Zwoosh.prototype.scaleTo = function (scale, honourLimits) {
if (honourLimits === void 0) { honourLimits = true; }
var width = (parseFloat(this.inner.style.minWidth) * scale);
var height = (parseFloat(this.inner.style.minHeight) * scale);
/* scrollbars have width and height too */
var minWidth = this.container.clientWidth + this.scrollbarWidth(this.container);
var minHeight = this.container.clientHeight + this.scrollbarHeight(this.container);
if (honourLimits) {
/* loop as long as all limits are honoured */
while ((scale > this.options.zoomOptions.maxScale && this.options.zoomOptions.maxScale !== 0) ||
(scale < this.options.zoomOptions.minScale && this.options.zoomOptions.minScale !== 0) ||
(width < this.container.clientWidth && !this.isBody) ||
(height < this.container.clientHeight && !this.isBody)) {
if (scale > this.options.zoomOptions.maxScale) {
scale = this.options.zoomOptions.maxScale;
}
else if (scale < this.options.zoomOptions.minScale) {
scale = this.options.zoomOptions.minScale;
}
if (this.options.zoomOptions.maxScale !== 0) {
width = Math.floor(parseInt(this.inner.style.minWidth) * scale);
height = Math.floor(parseInt(this.inner.style.minHeight) * scale);
}
if (width < minWidth && !this.isBody) {
scale = scale / width * minWidth;
height = Math.floor(parseInt(this.inner.style.minHeight) * scale);
width = minWidth;
}
if (height < minHeight && !this.isBody) {
scale = scale / height * minHeight;
width = Math.floor(parseInt(this.inner.style.minWidth) * scale);
height = minHeight;
}
}
}
this.inner.style.transform = "translate(0px, 0px) scale(" + scale + ")";
this.scaleElement.style.minWidth = this.scaleElement.style.width = width + "px";
this.scaleElement.style.minHeight = this.scaleElement.style.height = height + "px";
/* TODO: here scrollTo based on where the mouse cursor is */
};
/**
* Disables the scroll wheel of the mouse
*
* @param {number} x - the x-coordinates
* @param {number} y - the y-coordinates
* @return {boolean} - whether the nearest related parent inner element is the one of this object instance
*/
Zwoosh.prototype.elementBehindCursorIsMe = function (x, y) {
var elementBehindCursor = document.elementFromPoint(x, y);
/**
* If the element directly behind the cursor is an outer element throw out, because when clicking on a scrollbar
* from a div, a drag of the parent Zwoosh element is initiated.
*/
if (this.hasClass(elementBehindCursor, this.classOuter)) {
return false;
}
/* find the next parent which is an inner element */
while (elementBehindCursor && !this.hasClass(elementBehindCursor, this.classInner)) {
elementBehindCursor = elementBehindCursor.parentElement;
}
return this.inner === elementBehindCursor;
};
Zwoosh.prototype.getTimestamp = function () {
if (typeof window.performance === "object") {
if ("now" in window.performance) {
return window.performance.now();
}
else if ("webkitNow" in window.performance) {
return window.performance.webkitNow();
}
}
return new Date().getTime();
};
/**
* Scroll handler to trigger the custom events
*
* @param {Event} e - The scroll event object (TODO: needed?)
* @throws Event collideLeft
* @throws Event collideRight
* @throws Event collideTop
* @throws Event collideBottom
* @return {void}
*/
Zwoosh.prototype.onScroll = function (e) {
var x = this.getScrollLeft();
var y = this.getScrollTop();
this.scrollMaxLeft = (this.scrollElement.scrollWidth - this.scrollElement.clientWidth);
this.scrollMaxTop = (this.scrollElement.scrollHeight - this.scrollElement.clientHeight);
// the collideLeft event
if (x === 0 && !this.triggered.collideLeft) {
this.triggerEvent(this.inner, "collide.left");
}
this.triggered.collideLeft = x === 0;
// the collideTop event
if (y === 0 && !this.triggered.collideTop) {
this.triggerEvent(this.inner, "collide.top");
}
this.triggered.collideTop = y === 0;
// the collideRight event
if (x === this.scrollMaxLeft && !this.triggered.collideRight) {
this.triggerEvent(this.inner, "collide.right");
}
this.triggered.collideRight = x === this.scrollMaxLeft;
// the collideBottom event
if (y === this.scrollMaxTop && !this.triggered.collideBottom) {
this.triggerEvent(this.inner, "collide.bottom");
}
this.triggered.collideBottom = y === this.scrollMaxTop;
};
/**
* window resize handler to recalculate the inner div minWidth and minHeight
*
* @param {Event} e - The window resize event object (TODO: needed?)
* @return {void}
*/
Zwoosh.prototype.onResize = function (e) {
var _this = this;
var onResize = function () {
_this.inner.style.minWidth = null;
_this.inner.style.minHeight = null;
/* take away the margin values of the body element */
var xDelta = parseInt(_this.getStyle(document.body, "marginLeft"), 10) + parseInt(_this.getStyle(document.body, "marginRight"), 10);
var yDelta = parseInt(_this.getStyle(document.body, "marginTop"), 10) + parseInt(_this.getStyle(document.body, "marginBottom"), 10);
// TODO: with this.getBorder()
_this.inner.style.minWidth = (document.documentElement.scrollWidth - xDelta) + "px";
_this.inner.style.minHeight = (document.documentElement.scrollHeight - yDelta - 100) + "px"; // TODO: WTF? why -100 for IE8?
};
/**
* Trigger the function only when the clientWidth or clientHeight really have changed.
* IE8 resides in an infinity loop always triggering the resite event when altering css.
*/
if (this.oldClientWidth !== document.documentElement.clientWidth ||
this.oldClientHeight !== document.documentElement.clientHeight) {
window.clearTimeout(this.resizeTimeout);
this.resizeTimeout = window.setTimeout(onResize, 10);
}
/* write down the old clientWidth and clientHeight for the above comparsion */
this.oldClientWidth = document.documentElement.clientWidth;
this.oldClientHeight = document.documentElement.clientHeight;
};
Zwoosh.prototype.clearTextSelection = function () {
if (window.getSelection) {
window.getSelection().removeAllRanges();
}
if (document.selection) {
document.selection.empty();
}
};
/**
* Browser independent event registration
*
* @param {any} obj - The HTMLElement to attach the event to
* @param {string} event - The event name without the leading "on"
* @param {(e: Event) => void} callback - A callback function to attach to the event
* @param {boolean} bound - whether to bind the callback to the object instance or not
* @return {void}
*/
Zwoosh.prototype.addEventListener = function (obj, event, callback, bound) {
if (bound === void 0) { bound = true; }
var boundCallback = bound ? callback.bind(this) : callback;
if (typeof obj.addEventListener === "function") {
if (mapEvents["on" + event] && obj.tagName === "BODY") {
obj = mapEvents["on" + event];
}
obj.addEventListener(event, boundCallback);
}
else if (typeof obj.attachEvent === "object" && htmlEvents["on" + event]) {
obj.attachEvent("on" + event, boundCallback);
}
else if (typeof obj.attachEvent === "object" && mapEvents["on" + event]) {
if (obj.tagName === "BODY") {
var p = "on" + event;
/* example: window.onscroll = boundCallback */
mapEvents[p][p] = boundCallback;
}
else {
/* TODO: obj.onscroll ?? */
obj.onscroll = boundCallback;
}
}
else if (typeof obj.attachEvent === "object") {
obj[event] = 1;
boundCallback = function (e) {
/* TODO: e is the onpropertychange event not one of the custom event objects */
if (e.propertyName === event) {
callback(e);
}
};
obj.attachEvent("onpropertychange", boundCallback);
}
else {
obj["on" + event] = boundCallback;
}
if (!this.customEvents[event]) {
this.customEvents[event] = [];
}
this.customEvents[event].push([callback, boundCallback]);
};
/**
* Browser independent event deregistration
*
* @param {any} obj - The HTMLElement or window whose event should be detached
* @param {string} event - The event name without the leading "on"
* @param {(e: Event) => void} callback - The callback function when attached
* @return {void}
*
* @TODO: unregistering of mapEvents
*/
Zwoosh.prototype.removeEventListener = function (obj, event, callback) {
if (event in this.customEvents) {
for (var i in this.customEvents[event]) {
/* if the event was found in the array by its callback reference */
if (this.customEvents[event][i][0] === callback) {
/* remove the listener from the array by its bound callback reference */
callback = this.customEvents[event][i][1];
this.customEvents[event].splice(i, 1);
break;
}
}
}
if (typeof obj.removeEventListener === "function") {
obj.removeEventListener(event, callback);
}
else if (typeof obj.detachEvent === "object" && htmlEvents["on" + event]) {
obj.detachEvent("on" + event, callback);
}
else if (typeof obj.detachEvent === "object") {
obj.detachEvent("onpropertychange", callback);
}
else {
obj["on" + event] = null;
}
};
/**
* Browser independent event trigger function
*
* @param {HTMLElement} obj - The HTMLElement which triggers the event
* @param {string} eventName - The event name without the leading "on"
* @return {void}
*/
Zwoosh.prototype.triggerEvent = function (obj, eventName) {
var event;
if (typeof window.CustomEvent === "function") {
event = new CustomEvent(eventName);
}
else if (typeof document.createEvent === "function") {
event = document.createEvent("HTMLEvents");
event.initEvent(eventName, true, true);
}
else if (document.createEventObject) {
event = document.createEventObject();
event.eventType = eventName;
}
event.eventName = eventName;
if (obj.dispatchEvent) {
obj.dispatchEvent(event);
}
else if (obj[eventName]) {
obj[eventName]++;
}
else if (obj.fireEvent && htmlEvents["on" + eventName]) {
obj.fireEvent("on" + event.eventType, event);
}
else if (obj[eventName]) {
obj[eventName]();
}
else if (obj["on" + eventName]) {
obj["on" + eventName]();
}
};
Zwoosh.prototype.addClass = function (el, cssClass) {
if (!this.hasClass(el, cssClass)) {
el.className += " " + cssClass + " ";
}
};
Zwoosh.prototype.removeClass = function (el, cssClass) {
var re = new RegExp(" " + cssClass + " ", "g");
el.className = el.className.replace(re, "");
};
Zwoosh.prototype.hasClass = function (el, cssClass) {
var re = new RegExp(" " + cssClass + " ");
return el.className.match(re) !== null;
};
/**
* Get a css style property value browser independent
*
* @param {HTMLElement} el - The HTMLElement to lookup
* @param {string} jsProperty - The css property name in javascript in camelCase (e.g. "marginLeft", not "margin-left")
* @return {string} - the property value
*/
Zwoosh.prototype.getStyle = function (el, jsProperty) {
var cssProperty = jsProperty.replace(/([A-Z])/g, "-$1").toLowerCase();
if (typeof window.getComputedStyle === "function") {
return window.getComputedStyle(el).getPropertyValue(cssProperty);
}
else {
return el.currentStyle[jsProperty];
}
};
Zwoosh.prototype.clearTimeouts = function () {
if (this.timeouts) {
for (var i in this.timeouts) {
if (this.timeouts.hasOwnProperty(i)) {
clearTimeout(this.timeouts[i]);
}
}
if (this.timeouts.length > 0) {
this.timeouts = [];
this.removeEventListener(this.inner, "collide.left", this.clearListenerLeft);
this.removeEventListener(this.inner, "collide.right", this.clearListenerRight);
this.removeEventListener(this.inner, "collide.top", this.clearListenerTop);
this.removeEventListener(this.inner, "collide.bottom", this.clearListenerBottom);
}
}
};
/**
* Mouse down handler
* Registers the mousemove and mouseup handlers and finds the next inner element
*
* @param {MouseEvent} e - The mouse down event object
* @return {void}
*/
Zwoosh.prototype.mouseDown = function (e) {
var _this = this;
this.clearTimeouts();
/* drag only if the left mouse button was pressed */
if (("which" in e && e.which === 1) || (typeof e.which === "undefined" && "button" in e && e.button === 1)) {
if (this.elementBehindCursorIsMe(e.clientX, e.clientY)) {
/* prevent image dragging action */
var imgs = this.container.querySelectorAll("img");
for (var i = 0; i < imgs.length; i++) {
imgs[i].ondragstart = function () { return false; }; // MSIE
}
/* search the DOM for exclude elements */
if (this.options.dragOptions.exclude.length !== 0) {
/* drag only if the mouse clicked on an allowed element */
var el = document.elementFromPoint(e.clientX, e.clientY);