-
Notifications
You must be signed in to change notification settings - Fork 107
/
chrome.js
1846 lines (1581 loc) · 47.5 KB
/
chrome.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
/**
* @fileoverview The chrome class.
*/
goog.provide('treesaver.ui.Chrome');
goog.require('treesaver.array');
goog.require('treesaver.capabilities');
goog.require('treesaver.debug');
goog.require('treesaver.dimensions');
goog.require('treesaver.dom');
goog.require('treesaver.events');
goog.require('treesaver.network');
goog.require('treesaver.scheduler');
goog.require('treesaver.template');
goog.require('treesaver.ui.ArticleManager');
goog.require('treesaver.ui.Document');
goog.require('treesaver.ui.Index');
goog.require('treesaver.ui.Scrollable');
goog.scope(function() {
var debug = treesaver.debug,
dimensions = treesaver.dimensions,
dom = treesaver.dom,
Scrollable = treesaver.ui.Scrollable;
/**
* Chrome
* @param {!Element} node HTML node.
* @constructor
*/
treesaver.ui.Chrome = function(node) {
// DEBUG-only validation checks
if (goog.DEBUG) {
if (!dom.querySelectorAll('.viewer', node).length) {
debug.error('Chrome does not have a viewer');
}
if (node.parentNode.childNodes.length !== 1) {
debug.error('Chrome is not only child in container');
}
}
// TODO: Only store mutable capabilities
this.requirements = dom.hasAttr(node, 'data-requires') ?
node.getAttribute('data-requires').split(' ') : null;
// Create DOM infrastructure for scrolling elements
Scrollable.initDomTree(node);
// Save out the HTML now that the DOM is prepped
this.html = node.parentNode.innerHTML;
// Measure the chrome
this.size = new dimensions.Metrics(node);
// Clean up metrics object
delete this.size.w;
delete this.size.h;
};
});
goog.scope(function() {
var Chrome = treesaver.ui.Chrome,
Document = treesaver.ui.Document,
array = treesaver.array,
capabilities = treesaver.capabilities,
debug = treesaver.debug,
dimensions = treesaver.dimensions,
dom = treesaver.dom,
events = treesaver.events,
network = treesaver.network,
scheduler = treesaver.scheduler,
ArticleManager = treesaver.ui.ArticleManager,
Index = treesaver.ui.Index,
Scrollable = treesaver.ui.Scrollable;
/**
* List of required capabilities for this Chrome
*
* @type {?Array.<string>}
*/
Chrome.prototype.requirements;
/**
* @type {?Element}
*/
Chrome.prototype.node;
/**
* @type {string}
*/
Chrome.prototype.html;
/**
* The measurements of the chrome
* @type {!treesaver.dimensions.Metrics}
*/
Chrome.prototype.size;
/**
* The area available to pages (i.e. the size of the viewer)
* @type {?treesaver.dimensions.Size}
*/
Chrome.prototype.pageArea;
/**
* @type {number}
*/
Chrome.prototype.pageOffset;
/**
* @type {number|undefined}
*/
Chrome.prototype.pageShift_;
/**
* @type {boolean}
*/
Chrome.prototype.active;
/**
* Cached reference to viewer DOM
* @type {?Element}
*/
Chrome.prototype.viewer;
/**
* Cached reference to page width DOM
* @type {?Array.<Element>}
*/
Chrome.prototype.pageWidth;
/**
* @type {?Array.<treesaver.layout.Page>}
*/
Chrome.prototype.pages;
/**
* Whether the UI is current in active state
* @type {boolean}
*/
Chrome.prototype.uiActive;
/**
* Cached references to the menu TOC
* @type {?Array.<Element>}
*/
Chrome.prototype.menus;
/**
* @type {boolean}
*/
Chrome.prototype.lightBoxActive;
/**
* @type {?treesaver.ui.LightBox}
*/
Chrome.prototype.lightBox;
/**
* @type {?Array.<Element>}
*/
Chrome.prototype.sidebars;
/**
* Cached reference to the next page DOM
* @type {?Array.<Element>}
*/
Chrome.prototype.nextPage;
/**
* Cached reference to the next article DOM
* @type {?Array.<Element>}
*/
Chrome.prototype.nextArticle;
/**
* Cached reference to the previous page DOM
* @type {?Array.<Element>}
*/
Chrome.prototype.prevPage;
/**
* Cached reference to the previous article DOM
* @type {?Array.<Element>}
*/
Chrome.prototype.prevArticle;
/**
* Cached references to the position templates elements
* @type {?Array.<Element>}
*/
Chrome.prototype.positionElements;
/**
* Cached reference to the original position templates
* @type {?Array.<string>}
*/
Chrome.prototype.positionTemplates;
/**
* Cached references to the index templates elements
* @type {?Array.<Element>}
*/
Chrome.prototype.indexElements;
/**
* Cached reference to the original index templates
* @type {?Array.<string>}
*/
Chrome.prototype.indexTemplates;
/**
* Cached references to the current-document template elements
* @type {?Array.<Element>}
*/
Chrome.prototype.currentDocumentElements;
/**
* Cached reference to the original current-document templates
* @type {?Array.<string>}
*/
Chrome.prototype.currentDocumentTemplates;
/**
* Cached references to the publication template elements
* @type {?Array.<Element>}
*/
Chrome.prototype.publicationElements;
/**
* Cached reference to the original publication templates
* @type {?Array.<string>}
*/
Chrome.prototype.publicationTemplates;
/**
* @return {!Element} The activated node.
*/
Chrome.prototype.activate = function() {
if (!this.active) {
this.active = true;
this.node = dom.createElementFromHTML(this.html);
// Store references to the portions of the UI we must update
this.viewer = dom.querySelectorAll('.viewer', this.node)[0];
this.pageWidth = dom.querySelectorAll('.pagewidth', this.node);
this.nextPage = dom.querySelectorAll('.next', this.node);
this.nextArticle = dom.querySelectorAll('.nextArticle', this.node);
this.prevPage = dom.querySelectorAll('.prev', this.node);
this.prevArticle = dom.querySelectorAll('.prevArticle', this.node);
this.positionElements = [];
this.positionTemplates = [];
this.indexElements = [];
this.indexTemplates = [];
this.currentDocumentElements = [];
this.currentDocumentTemplates = [];
this.publicationElements = [];
this.publicationTemplates = [];
dom.querySelectorAll('[' + dom.customAttributePrefix + 'template]', this.node).forEach(function(el) {
var template_name = dom.getCustomAttr(el, 'template'),
elementArray, templateArray, newEl;
switch (template_name) {
case 'position':
elementArray = this.positionElements;
templateArray = this.positionTemplates;
break;
case 'index':
elementArray = this.indexElements;
templateArray = this.indexTemplates;
break;
case 'currentdocument':
elementArray = this.currentDocumentElements;
templateArray = this.currentDocumentTemplates;
break;
case 'publication':
elementArray = this.publicationElements;
templateArray = this.publicationTemplates;
break;
default: // Unknown template
return;
}
// Add
templateArray.push(el.innerHTML);
if (el.nodeName.toLowerCase() === 'script') {
newEl = document.createElement('div');
el.parentNode.replaceChild(newEl, el);
el = newEl;
}
elementArray.push(el);
}, this);
this.menus = dom.querySelectorAll('.menu', this.node);
this.sidebars = dom.querySelectorAll('.sidebar', this.node);
this.pages = [];
// Setup event handlers
Chrome.watchedEvents.forEach(function(evt) {
events.addListener(document, evt, this);
}, this);
// Always start off active
this.uiActive = false; // Set to false to force event firing
this.setUiActive_();
}
return /** @type {!Element} */ (this.node);
};
/**
* Deactivate the chrome
*/
Chrome.prototype.deactivate = function() {
if (!this.active) {
return;
}
this.stopDelayedFunctions();
this.active = false;
// Remove event handlers
Chrome.watchedEvents.forEach(function(evt) {
events.removeListener(document, evt, this);
}, this);
// Make sure to drop references
this.node = null;
this.viewer = null;
this.pageWidth = null;
this.menus = null;
this.sidebars = null;
this.nextPage = null;
this.nextArticle = null;
this.prevPage = null;
this.prevArticle = null;
this.positionElements = null;
this.positionTemplates = null;
this.indexElements = null;
this.indexTemplates = null;
this.currentDocumentElements = null;
this.currentDocumentTemplates = null;
this.publicationElements = null;
this.publicationTemplates = null;
// Deactivate pages
this.pages.forEach(function(page) {
if (page) {
page.deactivate();
}
});
this.pages = null;
this.pageArea = null;
// Clear out tasks
scheduler.clear('idletimer');
scheduler.clear('updateTOC');
this.stopDelayedFunctions();
};
/**
* Stop any delayed functions
* @private
*/
Chrome.prototype.stopDelayedFunctions = function() {
scheduler.clear('selectPages');
scheduler.clear('animatePages');
};
/**
* Events fired by Chrome objects
*
* @const
* @type {!Object.<string, string>}
*/
Chrome.events = {
ACTIVE: 'treesaver.active',
IDLE: 'treesaver.idle',
SIDEBARACTIVE: 'treesaver.sidebaractive',
SIDEBARINACTIVE: 'treesaver.sidebarinactive'
};
/**
* @type {Array.<string>}
*/
Chrome.watchedEvents = [
Index.events.UPDATED,
ArticleManager.events.PAGESCHANGED,
ArticleManager.events.DOCUMENTCHANGED,
'keydown',
'click',
'mousewheel',
'DOMMouseScroll'
];
// Add touch events only if the browser supports touch
if (capabilities.SUPPORTS_TOUCH) {
// Note that we hook up all the event handlers immediately,
// instead of waiting to do so during touchstart. This is
// because removing the touch handlers causes Android 2.1
// to stop sending all touch events
Chrome.watchedEvents.push('touchstart', 'touchmove', 'touchend', 'touchcancel');
}
else {
// Used for activity detection
Chrome.watchedEvents.push('mouseover');
// TODO: Move mousewheel in here as well?
}
/**
* Event dispatcher for all events
* @param {Event} e
*/
Chrome.prototype['handleEvent'] = function(e) {
switch (e.type) {
// Both these events mean that the pages we are displaying
// (or trying to display) may have changed. Make sure to
// fetch them again
// Article changed and TOC changed will affect nav indicators
case ArticleManager.events.PAGESCHANGED:
return this.selectPagesDelayed();
case Index.events.UPDATED:
this.updateTOCDelayed();
return this.selectPagesDelayed();
case ArticleManager.events.DOCUMENTCHANGED:
this.updateTOCActive();
this.updatePosition();
return;
case 'mouseover':
return this.mouseOver(e);
case 'touchstart':
return this.touchStart(e);
case 'touchmove':
return this.touchMove(e);
case 'touchend':
return this.touchEnd(e);
case 'touchcancel':
return this.touchCancel(e);
case 'keydown':
return this.keyDown(e);
case 'click':
return this.click(e);
case 'mousewheel':
case 'DOMMouseScroll':
return this.mouseWheel(e);
}
};
/**
* Whether one of the control/shift/alt/etc keys were pressed at the time
* of the event
*
* @private
* @param {!Event} e
* @return {boolean} True if at least one of those keys was pressed.
*/
Chrome.specialKeyPressed_ = function(e) {
return e.ctrlKey || e.shiftKey || e.altKey || e.metaKey;
};
/**
* Handle keyboard events
* @param {!Event} e
*/
Chrome.prototype.keyDown = function(e) {
// Lightbox active? Hide it
if (this.lightBoxActive) {
this.hideLightBox();
// Stop default actions and return early
e.preventDefault();
return;
}
// Ignore within forms
if (/input|select|textarea/i.test(e.target.tagName)) {
return;
}
// Don't override keyboard commands
if (!Chrome.specialKeyPressed_(e)) {
switch (e.keyCode) {
case 34: // PageUp
case 39: // Right && down
case 40:
case 74: // j
case 32: // Space
this.nextPage_();
break;
case 33: // PageDown
case 37: // Left & up
case 38:
case 75: // k
this.previousPage_();
break;
case 72: // h
ArticleManager.previousArticle();
break;
case 76: // l
ArticleManager.nextArticle();
break;
default: // Let the event through if not handled
return;
}
// Handled key always causes UI idle
this.setUiIdle_();
// Key handled, don't want any default actions
e.preventDefault();
}
};
/**
* Handle click event
* @param {!Event} e
*/
Chrome.prototype.click = function(e) {
// Ignore if done with a modifier key (could be opening in new tab, etc)
if (Chrome.specialKeyPressed_(e)) {
return true;
}
// Ignore if it's not a left-click
if ('which' in e && e.which !== 1 || e.button) {
debug.info('Click ignored due to non-left click');
return;
}
var el = Chrome.findTarget_(e.target),
ancestor,
url,
withinCurrentPage = false,
handled = false,
withinSidebar = false,
withinMenu = false,
target = null,
nearestSidebar = null;
// Lightbox active? Hide it
if (this.lightBoxActive) {
// Check if click was within lighbox
// TODO: FIXME
if (this.lightBox.node.contains(el)) {
if (el.nodeName === 'A') {
ancestor = el;
}
else {
ancestor = dom.getAncestor(el, 'A');
}
// Was it a link?
if (ancestor && ancestor.href) {
url = network.absoluteURL(ancestor.href);
// Try to go to the article
if (!ArticleManager.goToDocumentByURL(url)) {
// The URL is not an article, let the navigation happen normally
return;
}
}
}
// Close the lightbox no matter what
this.hideLightBox();
e.stopPropagation();
e.preventDefault();
return;
}
// If there are any menus active and the event target
// is contained within one, we deactive it and set
// withinMenu to true.
this.menus.forEach(function(menu) {
if (this.isMenuActive(menu)) {
withinMenu = menu.contains(el);
this.menuInactive(menu);
}
}, this);
withinSidebar = this.sidebars.some(function(sidebar) {
return sidebar.contains(el);
});
if (!withinSidebar) {
this.sidebars.forEach(function(sidebar) {
this.sidebarInactive(sidebar);
}, this);
} else {
if ((nearestSidebar = this.getNearestSidebar(el))) {
if (dom.hasClass(nearestSidebar, 'sidebar') && dom.hasClass(nearestSidebar, 'close-on-click')) {
this.sidebars.forEach(function(sidebar) {
this.sidebarInactive(sidebar);
}, this);
}
}
}
// Compiler cast
el = /** @type {!Element} */ (el);
// Check if the target is within one of the visible pages
// TODO: Once we have variable numbers of pages, this code will
// need to change
if (this.pages[0] && this.pages[0].node.contains(el)) {
this.previousPage_();
handled = true;
}
else if (this.pages[2] && this.pages[2].node.contains(el)) {
this.nextPage_();
handled = true;
}
else {
withinCurrentPage = this.pages[1] && this.pages[1].node.contains(el);
// Go up the tree and see if there's anything we want to process
while (!handled && el && el !== treesaver.tsContainer) {
if (!withinCurrentPage) {
if (dom.hasClass(el, 'prev')) {
this.previousPage_();
handled = true;
}
else if (dom.hasClass(el, 'next')) {
this.nextPage_();
handled = true;
}
else if (dom.hasClass(el, 'prevArticle')) {
ArticleManager.previousArticle();
handled = true;
}
else if (dom.hasClass(el, 'nextArticle')) {
ArticleManager.nextArticle();
handled = true;
}
else if (dom.hasClass(el, 'menu')) {
if (!withinMenu) {
this.menuActive(el);
}
handled = true;
}
else if (dom.hasClass(el, 'sidebar') ||
dom.hasClass(el, 'open-sidebar') ||
dom.hasClass(el, 'toggle-sidebar') ||
dom.hasClass(el, 'close-sidebar')) {
if ((nearestSidebar = this.getNearestSidebar(el))) {
if (dom.hasClass(el, 'sidebar') || dom.hasClass(el, 'open-sidebar')) {
this.sidebarActive(nearestSidebar);
}
else if (dom.hasClass(el, 'toggle-sidebar')) {
this.sidebarToggle(nearestSidebar);
}
else {
this.sidebarInactive(nearestSidebar);
}
handled = true;
}
}
}
else if (dom.hasClass(el, 'zoomable')) {
// Counts as handling the event only if showing is successful
handled = this.showLightBox(el);
}
// Check links last since they may be used as UI commands as well
// Links can occur in-page or in the chrome
if (!handled && el.href) {
target = el.getAttribute('target');
url = network.absoluteURL(el.href);
if (target === '_blank') {
return;
}
// Lightbox-flagged elements are skipped as processing goes up the chain
// if a zoomable is found on the way up the tree, it will be handled. If
// not, the link is navigated as-is
else if (target === 'ts-lightbox') {
// Skip this element and process the parent zoomable
el = /** @type {!Element} */ (el.parentNode);
continue;
}
else if (ArticleManager.goToDocumentByURL(url)) {
handled = true;
}
// Target is not blank a lightbox and it is not in the index.
else if (target === 'ts-treesaver') {
// Force processing the document as a Treesaver document by
// adding it to the index.
ArticleManager.index.appendChild(new Document(url));
ArticleManager.index.update();
if (ArticleManager.goToDocumentByURL(url)) {
handled = true;
}
}
}
el = /** @type {!Element} */ (el.parentNode);
}
}
if (handled) {
e.stopPropagation();
e.preventDefault();
}
};
/**
* The last time a mousewheel event was received
*
* @private
* @type {number}
*/
Chrome.prototype.lastMouseWheel_;
/**
* Handle the mousewheel event
* @param {!Event} e
*/
Chrome.prototype.mouseWheel = function(e) {
if (Chrome.specialKeyPressed_(e)) {
// Ignore if special key is down (user could be zooming)
return true;
}
var target = Chrome.findTarget_(e.target);
// Is the mousewheel within a scrolling element? If so, ignore
if (this.isWithinScroller_(target)) {
return true;
}
// Lightbox active? Hide it
if (this.lightBoxActive) {
this.hideLightBox();
e.preventDefault();
return;
}
var now = goog.now();
if (this.lastMouseWheel_ &&
(now - this.lastMouseWheel_ < MOUSE_WHEEL_INTERVAL)) {
// Ignore if too frequent (magic mouse)
return;
}
this.lastMouseWheel_ = now;
// Firefox handles this differently than others
// http://adomas.org/javascript-mouse-wheel/
var delta = e.wheelDelta ? e.wheelDelta : e.detail ? -e.detail : 0,
withinViewer = this.viewer.contains(Chrome.findTarget_(e.target));
if (!delta || !withinViewer) {
return;
}
// Handle the event
e.preventDefault();
e.stopPropagation();
if (delta > 0) {
this.previousPage_();
}
else {
this.nextPage_();
}
// Mousewheel always deactivates UI
this.setUiIdle_();
};
/**
* Sanitize the event target, which can be a textNode in Safari
*
* @private
* @param {?EventTarget} node
* @return {!Element}
*/
Chrome.findTarget_ = function(node) {
if (!node) {
node = treesaver.tsContainer;
}
else if (node.nodeType !== 1 && node.parentNode) {
// Safari Bug that gives you textNode on events
node = node.parentNode || treesaver.tsContainer;
}
// Cast for compiler
return /** @type {!Element} */ (node);
};
/**
* @private
* @type {Object}
*/
Chrome.prototype.touchData_;
/**
* Handle the touchstart event
* @param {!Event} e
*/
Chrome.prototype.touchStart = function(e) {
var target = Chrome.findTarget_(e.target),
scroller = this.isWithinScroller_(target),
withinViewer, node, id,
x, y, now;
if (!treesaver.tsContainer.contains(Chrome.findTarget_(e.target))) {
return;
}
// Do all the handling ourselves
e.stopPropagation();
e.preventDefault();
// Lightbox active? Hide it only if it can't scroll
if (this.lightBoxActive && !scroller) {
this.hideLightBox();
return;
}
// Ignore scrollers in prev/next page
if (scroller) {
node = scroller;
while (node) {
id = node.id;
if (id === 'prevPage' || id === 'nextPage') {
scroller = node = null;
break;
}
node = node.parentNode;
}
}
withinViewer = this.viewer.contains(target);
x = e.touches[0].pageX;
y = e.touches[0].pageY;
now = goog.now();
this.touchData_ = {
startX: x,
startY: y,
startTime: now,
lastX: x,
lastY: y,
lastTime: now,
deltaX: 0,
deltaY: 0,
deltaTime: 0,
totalX: 0,
totalY: 0,
totalTime: 0,
touchCount: e.touches.length,
withinViewer: withinViewer,
originalOffset: this.pageOffset,
scroller: scroller,
canScrollHorizontally: scroller && Scrollable.canScrollHorizontally(scroller)
};
if (this.touchData_.touchCount === 2) {
this.touchData_.startX2 = e.touches[1].pageX;
}
// Pause other work for better swipe performance
scheduler.pause(['resumeTasks']);
};
/**
* Handle the touchmove event
* @param {!Event} e
*/
Chrome.prototype.touchMove = function(e) {
var touchData = this.touchData_,
now, x, y;
if (!touchData) {
// No touch info, nothing to do
return;
}
// Do all the handling ourselves
e.stopPropagation();
e.preventDefault();
now = goog.now();
x = e.touches[0].pageX;
y = e.touches[0].pageY;
touchData.deltaTime = touchData.lastMove - now;
touchData.deltaX = x - touchData.lastX;
touchData.deltaY = y - touchData.lastY;
touchData.lastMove = now;
touchData.lastX = e.touches[0].pageX;
touchData.lastY = e.touches[0].pageY;
touchData.totalTime += touchData.deltaTime;
touchData.totalX += touchData.deltaX;
touchData.totalY += touchData.deltaY;
touchData.touchCount = Math.min(e.touches.length, touchData.touchCount);
touchData.swipe =
// One-finger only
touchData.touchCount === 1 &&
// Finger has to move far enough
Math.abs(touchData.totalX) >= SWIPE_THRESHOLD &&
// But not too vertical
Math.abs(touchData.totalX) * 2 > Math.abs(touchData.totalY);
if (touchData.scroller && (touchData.didScroll ||
(touchData.canScrollHorizontally || !touchData.swipe))) {
touchData.didScroll = touchData.didScroll || touchData.canScrollHorizontally ||
Math.abs(touchData.totalY) >= SWIPE_THRESHOLD;
Scrollable.setOffset(touchData.scroller, -touchData.deltaX, -touchData.deltaY);
if (!touchData.withinViewer) {
// Scrolling outside viewer means active, in case of long scroll
this.setUiActive_();
}
}
else if (touchData.touchCount === 2) {
// Track second finger changes
touchData.totalX2 = e.touches[1].pageX - touchData.startX2;
}
else {
this.pageOffset = touchData.originalOffset + touchData.totalX;
this._updatePagePositions(true);
}
};
/**
* Handle the touchend event
* @param {!Event} e
*/
Chrome.prototype.touchEnd = function(e) {
// Hold onto a reference before clearing
var touchData = this.touchData_,
// Flag to track whether we need to reset positons, etc
actionTaken = false,
target = Chrome.findTarget_(e.target),
// Lightbox is an honorary viewer
withinViewer = this.lightBoxActive || this.viewer.contains(target);
// Clear out touch data
this.touchCancel(e);
if (!touchData) {
// No touch info, nothing to do
return;
}
// Do all the handling ourselves
e.stopPropagation();
e.preventDefault();
if (touchData.didScroll) {
if (withinViewer) {
// Scrolling in viewer means idle
this.setUiIdle_();
}
else {
// Scrolling within the chrome should keep UI active
this.setUiActive_();
}
}
else if (touchData.touchCount === 1) {
// No move means we create a click
if (!touchData.lastMove) {
// TODO: Currently this code is OK since the IE browsers don't support
// touch. However, perhaps Windows Phone 7 will and needs a fix with
// IE7? Need to integrate this into treesaver.events
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, e.view, 1,