/
browser-tour.js
1033 lines (871 loc) · 34.7 KB
/
browser-tour.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// create namespace
if (typeof Mozilla == 'undefined') {
var Mozilla = {};
}
;(function($, Mozilla) {
'use strict';
function BrowserTour (options) {
this.options = {
id: null,
onTourComplete: null
};
if (typeof options === 'object') {
for (var i in options) {
if (options.hasOwnProperty(i)) {
this.options[i] = options[i];
}
}
}
// use all the flags!
this.tourHasStarted = false;
this.tourHasFinished = false;
this.tourIsVisible = false;
this.tourIsPostponed = false;
this.tourIsAnimating = false;
this.onTourCompleteHasFired = false;
// set timeout for highlights
this.highlightTimer = null;
this.isLargeViewport = matchMedia('(min-width: 800px)');
this.$body = $('body');
this.$doc = $(document);
this.$tour = $('#ui-tour').detach().show();
this.$mask = $('#ui-tour-mask').detach().show();
this.$maskInner = this.$mask.find('.mask-inner');
this.$body.append(this.$mask).append(this.$tour);
if (!this.options.allowScroll) {
this.$body.addClass('noscroll');
}
this.$mask.focus();
this.$tourList = $('.ui-tour-list');
this.$prevButton = $('button.prev');
this.$nextButton = $('button.next');
this.$closeButton = $('button.close');
this.$progress = $('.progress-step');
this.$progressStep = this.$progress.find('.step');
this.$progressMeter = this.$progress.find('.progress');
this.$compactTitle = $('.compact-title');
this.$tourTip = $('.tour-tip');
this.$tourControls = $('.ui-tour-controls');
this.$cta = $('.cta');
this.$inTourLinks = this.$tourList.find('a.more');
// bind UITour event listeners
this.bindEvents();
// bind resize handler to hide highlights at smaller viewports
// as positioning can sometimes be incorrect (Bug 1091785)
this.bindResize();
}
/*
* Show the initial door-hanger menu that begins the tour
* and display the 'splash screen' animation.
*/
BrowserTour.prototype.init = function () {
var that = this;
var $p = this.$maskInner.find('p');
var $main = this.$maskInner.find('.main');
var words = $p.text().split(' ');
var delay = $('body').hasClass('html-ltr') ? 100 : 0;
var $tempEl = $('<div>');
// wrap each word in a <span> with an incremental
// transition-delay value.
$p.empty().show();
$.each(words, function(i, word) {
var $span = $('<span>').text(word + ' ');
$span.css({
'transition-delay': (i * delay) + 'ms'
});
$tempEl.append($span);
});
$p.html($tempEl.html());
setTimeout(function () {
// animate the mask welcome message
that.$maskInner.addClass('in');
setTimeout(function () {
$p.addClass('in');
}, 1000);
// show the door hanger if the tab is visible
if (!document.hidden) {
// trigger the door hanger for whatsnew
$('.tour-init').trigger('tour-step');
}
}, 1000);
// Register page id for Telemetry
Mozilla.UITour.registerPageID(this.options.id);
};
/*
* Bind custom events to handle calls to Mozilla.UITour
* as well as regular event listeners for UI interaction
*/
BrowserTour.prototype.bindEvents = function () {
// door-hanger menu buttons
var buttons = [
{
label: window.trans('later'),
style: 'link',
callback: this.postponeTour.bind(this)
},
{
label: window.trans('start'),
style: 'primary',
callback: this.startTour.bind(this)
}
];
// callback to postpone tour if user clicks the (x) button
var options = {
closeButtonCallback: this.postponeTour.bind(this)
};
// show the door-hanger info panel
$('.tour-init').on('tour-step', function () {
var icon = Mozilla.ImageHelper.isHighDpi() ? this.dataset.iconHighRes : this.dataset.icon;
Mozilla.UITour.showInfo(
this.dataset.target,
window.trans('title'),
window.trans('text'),
icon,
buttons,
options
);
});
// show a hightlighted target feature in the browser UI
$('.tour-highlight').on('tour-step', function () {
Mozilla.UITour.showHighlight(this.dataset.target, this.dataset.effect);
});
// show a targeted menu panel in the browser UI
$('.tour-menu').on('tour-step', function () {
Mozilla.UITour.showMenu(this.dataset.target);
});
// 33.1 privacy tour 'forget' button doorhanger
$('.tour-forget-widget').on('tour-step', this.highlightForgetButton.bind(this));
// 33.1 privacy tour search engine highlight
$('.tour-search-engine').on('tour-step', this.highlightSearchEngine);
// handle page visibility changes to show the appropriate tour step
this.$doc.on('visibilitychange', this.handleVisibilityChange.bind(this));
// carousel event handlers
this.$tour.on('transitionend', '.ui-tour-list li.current', this.onTourStep.bind(this));
this.$closeButton.on('click', this.closeTour.bind(this));
this.$mask.on('click', this.closeTour.bind(this));
$('.cta button').on('click', this.startBrowsing.bind(this));
$('button.step').on('click', this.onStepClick.bind(this));
// show tooltips on prev/next buttons
this.$tourControls.on('mouseenter focus', 'button.step', this.onStepHover.bind(this));
this.$tourControls.on('mouseleave blur', 'button.step', this.offStepHover.bind(this));
// alternate tour has in-step links to advance section
this.$inTourLinks.on('click', this.onTourLinkClick.bind(this));
// prevent focusing out of mask while initially visible
this.$doc.on('focus.ui-tour', 'body', $.proxy(function(e) {
if (!this.tourHasStarted && !this.$mask[0].contains(e.target)) {
e.stopPropagation();
this.$mask.focus();
}
}, this));
// toggle floating signpost visibility when tabzilla opens / closes
$('#tabzilla').on('click', this.toggleSignPost.bind(this));
$(document).on('keydown', '#tabzilla-panel', $.proxy(function (event) {
if (event.which === 27 && this.tourIsPostponed) {
$('.floating-cta').fadeIn();
}
}, this));
};
/*
* Hide UITour highlights if browser is resized < 900px as
* a temp workaround for Bug 1091785
*/
BrowserTour.prototype.bindResize = function () {
var that = this;
this.isLargeViewport.addListener(function (mq) {
if (!mq.matches && that.tourIsVisible) {
Mozilla.UITour.hideHighlight();
Mozilla.UITour.hideInfo();
} else if (mq.matches && that.tourIsVisible) {
clearInterval(that.highlightTimer);
that.highlightTimer = setTimeout(function () {
that.showHighlight();
}, 900);
}
});
};
/*
*Alternate version of the tour has in-step links to advance sections
*/
BrowserTour.prototype.onTourLinkClick = function (e) {
var $current = this.$tourList.find('li.current');
var step = $current.data('step');
e.preventDefault();
if (this.tourIsAnimating || !this.tourIsVisible) {
return;
}
this.$tour.focus();
if ($current.is(':last-child')) {
this.doCloseTour();
} else {
this.goToTourStep('next');
step += 1;
gaTrack(['_trackEvent', 'Tour Interaction', 'link click to', 'Step ' + step]);
}
};
/*
* Shows tooltips for next/prev steps when mouseenter or focus is applied to button
*/
BrowserTour.prototype.onStepHover = function (e) {
e.preventDefault();
var $button = $(e.target);
var $current = this.$tourList.find('li.current');
var tipText;
// if the tour is compact do nothing
if ($button.hasClass('up')) {
return;
}
tipText = $button.hasClass('prev') ? $current.data('tipPrev') : $current.data('tipNext');
if (tipText) {
this.$tourTip.html(tipText).addClass('show-tip');
}
};
/*
* Hide tooltips on mouseleave or blur
*/
BrowserTour.prototype.offStepHover = function (e) {
e.preventDefault();
this.$tourTip.removeClass('show-tip');
};
/*
* Postpone the tour until later.
* Close the tour and display sign-post cta top right corner.
*/
BrowserTour.prototype.postponeTour = function () {
this.tourIsPostponed = true;
var $cta = $('<button class="floating-cta"></button>');
$cta.html(window.trans('laterCta'));
this.$body.append($cta);
$('.floating-cta').one('click', $.proxy(function (e) {
e.preventDefault();
this.restartTour();
this.setCustomGAVariable();
gaTrack(['_trackEvent', 'Tour Interaction', 'click', 'Signpost CTA']);
}, this));
this.doCloseTour();
gaTrack(['_trackEvent', 'Tour Interaction', 'click', 'Not now']);
};
/*
* Restarts the tour once postponed.
* This can be called by an in-page cta or floating signpost cta
*/
BrowserTour.prototype.restartTour = function () {
var that = this;
$('.floating-cta').fadeOut(function () {
$(this).remove();
});
if (!this.tourHasStarted) {
if (!this.options.allowScroll) {
this.$body.addClass('noscroll');
}
this.$mask.show();
setTimeout(function () {
that.$mask.removeClass('out');
that.startTour();
}, 50);
} else {
this.expandTour();
}
if (typeof this.options.onRestartTour === 'function') {
this.options.onRestartTour();
}
};
/*
* Toggles floating signpost cta visibility
* Needed when Tabzilla opens/closes
*/
BrowserTour.prototype.toggleSignPost = function () {
var $cta = $('.floating-cta');
if (this.tourIsPostponed) {
if ($cta.is(':visible')) {
$cta.fadeOut();
} else if (!$cta.is(':visible')) {
$cta.fadeIn();
}
}
};
/*
* Updates the tour UI controls buttons to reflect the current step
*/
BrowserTour.prototype.updateControls = function () {
var $current = this.$tourList.find('li.current');
this.$closeButton.removeAttr('disabled', 'disabled');
// update prev/next button states
if ($current.is(':first-child')) {
this.$prevButton.attr('disabled', 'disabled').addClass('faded');
this.$nextButton.removeAttr('disabled').removeClass('faded');
} else if ($current.is(':last-child')) {
this.$nextButton.attr('disabled', 'disabled').addClass('faded');
this.$prevButton.removeAttr('disabled').removeClass('faded');
} else {
$('.ui-tour-controls button').removeAttr('disabled').removeClass('faded');
}
};
/*
* Show a doorhanger prompting to add 'forget' button to the toolbar
*/
BrowserTour.prototype.highlightForgetButton = function () {
var that = this;
var targetIsAvailable = false;
var $sequence = $('.tour-forget-widget');
var $step;
var buttons = [];
var options;
var icon = '';
Mozilla.UITour.getConfiguration('availableTargets', function (config) {
if (config.targets) {
icon = Mozilla.ImageHelper.isHighDpi() ? $sequence.data('iconHighRes') : $sequence.data('icon');
// find if user already has 'forget' button available
$.each(config.targets, function(index, value) {
if (value === 'forget') {
targetIsAvailable = true;
return false;
}
});
// if not, show door-hanger prompting to add it
if (!targetIsAvailable) {
$step = $sequence.find('.forget-hanger-1');
buttons = [
{
label: $step.data('buttonLater'),
style: 'link',
callback: that.laterForgetButton.bind(that)
},
{
label: $step.data('buttonAdd'),
style: 'primary',
callback: that.addForgetButton.bind(that)
}
];
options = {
closeButtonCallback: that.closeForgetDoorhanger.bind(that),
targetCallback: that.closeForgetDoorhanger.bind(that)
};
// temp fix for Bug 1049130
Mozilla.UITour.showHighlight('appMenu', 'none');
Mozilla.UITour.showHighlight('appMenu', 'none');
Mozilla.UITour.showInfo(
'appMenu',
$step.data('title'),
$step.data('text'),
icon,
buttons,
options
);
} else {
// else highlight the forget button with info panel
// $step = $sequence.find('.forget-hanger-2');
// options = {
// closeButtonCallback: that.closeForgetDoorhanger.bind(that)
// };
// temp fix for Bug 1049130
Mozilla.UITour.showHighlight('forget', 'wobble');
Mozilla.UITour.showHighlight('forget', 'wobble');
// Mozilla.UITour.showInfo(
// 'forget',
// $step.data('title'),
// $step.data('text'),
// icon,
// buttons,
// options
// );
}
}
});
};
/*
* Add the forget button to the user toolbar
*/
BrowserTour.prototype.addForgetButton = function () {
Mozilla.UITour.hideHighlight();
Mozilla.UITour.addNavBarWidget('forget', this.highlightForgetButton.bind(this));
gaTrack(['_trackEvent', 'Tour Interaction', 'Add it now', 'The new Forget Button']);
};
/*
* Show doorhanger with instructions on how to add forget button at later date
*/
BrowserTour.prototype.laterForgetButton = function () {
var $sequence = $('.tour-forget-widget');
var $step = $sequence.find('.forget-hanger-3');
var icon = Mozilla.ImageHelper.isHighDpi() ? $sequence.data('iconHighRes') : $sequence.data('icon');
var buttons = [];
var options = {
closeButtonCallback: this.closeForgetDoorhanger.bind(this),
targetCallback: this.closeForgetDoorhanger.bind(this)
};
Mozilla.UITour.hideHighlight();
Mozilla.UITour.showHighlight('appMenu', 'wobble');
Mozilla.UITour.showInfo(
'appMenu',
$step.data('title'),
$step.data('text'),
icon,
buttons,
options
);
gaTrack(['_trackEvent', 'Tour Interaction', 'Later', 'The new Forget Button']);
};
/*
* Close forget button doorhanger and remove highlight
*/
BrowserTour.prototype.closeForgetDoorhanger = function () {
Mozilla.UITour.hideHighlight();
Mozilla.UITour.hideInfo();
};
/*
* Open search engines menu if available and highlight a specified target
*/
BrowserTour.prototype.highlightSearchEngine = function () {
var target = this.dataset.target;
Mozilla.UITour.getConfiguration('availableTargets', function (config) {
var hasSearchBar;
if (config.targets) {
hasSearchBar = $.inArray('searchProvider', config.targets) !== -1;
// if the searchEngine is available in the list, highlight it
if (hasSearchBar && $.inArray(target, config.targets) !== -1) {
Mozilla.UITour.showMenu('searchEngines', function () {
Mozilla.UITour.showHighlight(target);
});
// else just highlight the searchProvider drop-down
} else if (hasSearchBar) {
Mozilla.UITour.showHighlight('searchProvider', 'wobble');
}
}
});
};
/*
* Shows current tour step highlight item
*/
BrowserTour.prototype.showHighlight = function () {
var $current = this.$tourList.find('li.current');
var $stepTarget = $current.find('.step-target');
var that = this;
var target;
// if the tab is not visible when event fires or viewport is too small
// don't show the highlight step
if (document.hidden || !this.isLargeViewport.matches) {
return;
}
// if we're simply highlighting a target,
// check it's availability in the UI first
if ($stepTarget.hasClass('tour-highlight')) {
Mozilla.UITour.getConfiguration('availableTargets', function (config) {
target = $stepTarget.data('target');
if (target && config.targets && $.inArray(target, config.targets) !== -1) {
// tour can force sticky menu between steps
// using conditional 'app-menu' class
if ($current.hasClass('app-menu')) {
Mozilla.UITour.showMenu('appMenu');
} else {
Mozilla.UITour.hideMenu('appMenu');
}
clearInterval(that.highlightTimer);
that.highlightTimer = setTimeout(function () {
$stepTarget.trigger('tour-step');
}, 200);
}
});
// other UITour actions handle target availability checking
// in their own 'tour-step' event handlers.
// So just trigger the event.
} else {
clearInterval(this.highlightTimer);
this.highlightTimer = setTimeout(function () {
$stepTarget.trigger('tour-step');
}, 200);
}
};
/*
* Triggers the current step tour highlight / interaction
* Called on `transitionend` event after carousel item animates
*/
BrowserTour.prototype.onTourStep = function (e) {
if (e.originalEvent.propertyName === 'transform') {
var $current = this.$tourList.find('li.current');
var step = $current.data('step');
var tipText;
this.tourIsAnimating = false;
this.showHighlight();
this.$progressStep.html(window.trans('step' + step));
this.$progressMeter.attr('aria-valuenow', step);
this.$tourList.find('.tour-step').not('.current').removeClass('visible');
// update the button states
this.updateControls();
// set focus on the header of current slide
$current.find('h2').focus();
if ($current.is(':last-child')) {
// fire callback when reaching the last tour step.
this.onTourComplete();
// show green cta for the last step
this.$cta.removeAttr('disabled').fadeIn();
}
// if user still has hover/focus over a button show the tooltip
if ($('button.next:hover').length) {
tipText = $current.data('tipNext');
if (tipText) {
this.$tourTip.html(tipText);
this.$tourTip.addClass('show-tip');
}
} else if ($('button.prev:hover').length) {
tipText = $current.data('tipPrev');
if (tipText) {
this.$tourTip.html(tipText);
this.$tourTip.addClass('show-tip');
}
}
}
};
/*
* Tour navigation click handler
*/
BrowserTour.prototype.onStepClick = function (e) {
e.preventDefault();
var $button = $(e.target);
var $current = this.$tourList.find('li.current');
var step = $current.data('step');
// if the tour is compact do nothing
// as uses it's own handler
if ($button.hasClass('up')) {
return;
}
var trans = $button.hasClass('prev') ? 'prev' : 'next';
this.goToTourStep(trans);
if (trans === 'prev') {
step -= 1;
} else {
step += 1;
}
gaTrack(['_trackEvent', 'Tour Interaction', 'arrow click to', 'Step ' + step]);
};
/*
* Transitions carousel animation to the next/prev step of the tour
*/
BrowserTour.prototype.goToTourStep = function (trans) {
var $current = this.$tourList.find('li.current');
var $prev;
var $next;
if (!trans) {
return;
}
this.tourIsAnimating = true;
// hide any current highlights
// or info panels
clearInterval(this.highlightTimer);
Mozilla.UITour.hideMenu('appMenu');
Mozilla.UITour.hideInfo();
Mozilla.UITour.hideHighlight();
this.$tourTip.removeClass('show-tip');
// disable tour control buttons while animating
$('.ui-tour-controls button').attr('disabled', 'disabled');
// if we're moving back from the last step, hide green cta
if ($current.is(':last-child')) {
this.$cta.attr('disabled', 'disabled').fadeOut();
}
// animate in/out the correct tour panel
if (trans === 'prev') {
$current.removeClass('current next-out').addClass('prev-out');
$prev = $current.prev().addClass('visible');
// slight delay is needed when animating an element
// after applying display: block;
setTimeout(function () {
$prev.addClass('current');
}, 50);
} else if (trans === 'next') {
$current.removeClass('current prev-out').addClass('next-out');
$next = $current.next().addClass('visible');
setTimeout(function () {
$next.addClass('current');
}, 50);
}
};
/*
* Go directly to a specific step in the tour. This can be called from
* within the web page to go directly to a specific tour step.
*/
BrowserTour.prototype.goToStep = function (step) {
var $current = $('.ui-tour-list .tour-step[data-step="' + step + '"]');
$('.ui-tour-list .tour-step.current').removeClass('current visible');
$('.ui-tour-list .tour-step').removeClass('prev-out next-out');
$current.addClass('current visible');
$('.ui-tour-list .tour-step:gt(' + step + ')').addClass('prev-out');
$('.ui-tour-list .tour-step:lt(' + step + ')').addClass('next-out');
this.$progressStep.html(window.trans('step' + step));
this.$progressMeter.attr('aria-valuenow', step);
this.updateControls();
};
/*
* Closes the browser tour when user clicks green cta button
*/
BrowserTour.prototype.startBrowsing = function () {
if (this.tourIsAnimating || !this.tourIsVisible) {
return;
}
this.doCloseTour();
gaTrack(['_trackEvent', 'Tour Interaction', 'button click', 'Start Browsing']);
};
/*
* Determines whether tour should be minimized or closed completely
*/
BrowserTour.prototype.closeTour = function () {
var $current = this.$tourList.find('li.current');
if (this.tourIsAnimating || !this.tourIsVisible) {
return;
}
if ($current.is(':last-child')) {
this.doCloseTour();
} else {
this.doCompactTour();
}
};
/*
* Closes the tour completely
* Triggered on last step or if user presses esc key
*/
BrowserTour.prototype.doCloseTour = function () {
this.tourIsVisible = false;
this.tourHasStarted = false;
this.tourHasFinished = true;
this.hideAnnotations();
if (this.tourHasStarted) {
gaTrack(['_trackEvent', 'Tour Interaction', 'click', 'Close tour']);
}
this.$cta.fadeOut('fast', $.proxy(function () {
this.$tour.removeClass('in');
this.$mask.addClass('out');
setTimeout(this.onCloseTour.bind(this), 600);
}, this));
};
BrowserTour.prototype.onCloseTour = function () {
this.$mask.find('.mask-inner').addClass('out');
this.$mask.hide();
this.$body.removeClass('noscroll');
// unbind ui-tour focus and keyboard event listeners
this.$doc.off('.ui-tour').focus();
this.$tour.off('.ui-tour');
if (typeof this.options.onCloseTour === 'function') {
this.options.onCloseTour();
}
};
/*
* Minimize the tour to compact state
* Called when pressing the close button mid-way through the tour
*/
BrowserTour.prototype.doCompactTour = function () {
this.tourIsVisible = false;
this.tourIsAnimating = true;
this.hideAnnotations();
this.$tour.removeClass('in').addClass('compact');
this.$tour.attr('aria-expanded', false);
// fade out the main modal content
this.$tourList.fadeOut('fast');
this.$progress.fadeOut('fast');
this.$prevButton.fadeOut('fast');
this.$closeButton.fadeOut('fast');
// apply focus to the 'open' button once tour is compact.
this.$nextButton.addClass('up').text(window.trans('open')).focus();
this.$nextButton.off().on('click', this.expandTour.bind(this));
// fade out the mask so user can interact with the page
this.$mask.addClass('out');
setTimeout(this.onCompactTour.bind(this), 600);
gaTrack(['_trackEvent', 'Tour Interaction', 'click', 'Compact tour']);
};
BrowserTour.prototype.onCompactTour = function () {
var title;
title = this.$tourList.find('li.current h2').text();
this.$mask.hide();
this.$body.removeClass('noscroll');
// fade in the compact modal content
this.$compactTitle.html('<h2>' + title + '</h2>').fadeIn();
this.$progress.addClass('compact').fadeIn($.proxy(function () {
this.tourIsAnimating = false;
}, this));
if (typeof this.options.onCompactTour === 'function') {
this.options.onCompactTour();
}
};
/*
* Expand tour from compact state and go back to the step
* user was on on prior to minimizing the tour.
*/
BrowserTour.prototype.expandTour = function () {
var that = this;
if (this.tourIsAnimating) {
return;
}
this.tourIsVisible = true;
this.tourIsAnimating = true;
this.$tour.removeClass('compact').addClass('in').focus();
this.$tour.attr('aria-expanded', true);
this.$compactTitle.fadeOut('fast');
this.$progress.fadeOut('fast');
this.$prevButton.fadeIn('fast');
this.$nextButton.off().on('click', this.onStepClick.bind(this));
this.$nextButton.removeClass('up').text(window.trans('next'));
this.$closeButton.fadeIn('fast');
this.$mask.show();
setTimeout(this.onTourExpand.bind(this), 600);
setTimeout(function () {
that.$mask.removeClass('out');
}, 50);
gaTrack(['_trackEvent', 'Tour Interaction', 'click', 'Expand tour']);
};
BrowserTour.prototype.onTourExpand = function () {
if (!this.options.allowScroll) {
this.$body.addClass('noscroll');
}
this.showHighlight();
this.$progress.removeClass('compact').fadeIn('slow');
this.$tourList.find('li.current').find('h2').focus();
this.$tourList.fadeIn('slow', $.proxy(function () {
this.tourIsAnimating = false;
}, this));
if (typeof this.options.onExpandTour === 'function') {
this.options.onExpandTour();
}
};
/*
* Minimizes / closes the tour based on current step
* Triggered when user presses the esc key
*/
BrowserTour.prototype.onKeyUp = function (e) {
var $current = this.$tourList.find('li.current');
if (this.tourIsVisible && !this.tourIsAnimating) {
switch (e.which) {
// esc minimizes the tour
case 27:
this.closeTour();
break;
// left arrow key to previous step
case 37:
if (!$current.is(':first-child')) {
this.goToTourStep('prev');
}
break;
// right arrow key to previous step
case 39:
if (!$current.is(':last-child')) {
this.goToTourStep('next');
}
break;
}
}
};
/*
* Set custom GA variable so we know if the tour is taken for the first time
* The custom var should only be set if cookies are enabled.
*/
BrowserTour.prototype.setCustomGAVariable = function () {
var firstTime = 'True';
try {
if (localStorage.getItem(this.options.id) === 'taken') {
firstTime = 'False';
} else {
localStorage.setItem(this.options.id, 'taken');
}
gaTrack(['_setCustomVar', 5, 'First Time Taking Firefox Tour', firstTime, 2]);
} catch (e) {}
};
/*
* Starts the tour and animates the carousel up from bottom of viewport
*/
BrowserTour.prototype.startTour = function () {
this.updateControls();
var that = this;
var $current = this.$tourList.find('li.current');
var step = $current.data('step');
this.$progressStep.html(window.trans('step' + step));
// fade out the inner mask messaging that's shown the the page loads
this.$maskInner.addClass('out');
this.$tour.addClass('in').focus();
this.$tour.attr('aria-expanded', true);
this.tourIsVisible = true;
this.tourHasStarted = true;
this.tourIsAnimating = true;
Mozilla.UITour.hideInfo();
// toggle/close with escape key
this.$body.on('keyup.ui-tour', this.onKeyUp.bind(this));
// prevent focusing out of modal while open
this.$doc.off('focus.ui-tour', 'body').on('focus.ui-tour', 'body', function(e) {
if (that.tourIsVisible && !that.$tour[0].contains(e.target)) {
e.stopPropagation();
that.$tour.focus();
}
});
setTimeout(this.onStartTour.bind(this), 600);
if (!this.tourIsPostponed) {
this.setCustomGAVariable();
gaTrack(['_trackEvent', 'Tour Interaction', 'click', 'Lets go']);
} else {
this.tourIsPostponed = false;
}
};
/*
* When the tour finishes animating in from bottom, trigger the tour step
*/
BrowserTour.prototype.onStartTour = function () {
var $current = this.$tourList.find('li.current');
var that = this;
$current.find('h2').focus();
this.tourIsAnimating = false;
this.highlightTimer = setTimeout(function () {
that.showHighlight();
}, 100);
};
/*
* Fire an optional callback when the user reaches last step in the tour
*/
BrowserTour.prototype.onTourComplete = function () {
if (typeof this.options.onTourComplete === 'function' && !this.onTourCompleteHasFired) {
this.options.onTourComplete();
this.onTourCompleteHasFired = true;
}
};
/*
* Helper method to hide currently visible highlight annotations
*/
BrowserTour.prototype.hideAnnotations = function () {
clearInterval(this.highlightTimer);
Mozilla.UITour.hideMenu('appMenu');
Mozilla.UITour.hideHighlight();
Mozilla.UITour.hideInfo();
};
/*
* Handles page visibility changes if user leaves/returns to current tab.
* Tour step UI highlights should hide when user leaves the tab, and appear
* again when the user returns to the tab.
*/
BrowserTour.prototype.handleVisibilityChange = function () {
var $current = this.$tourList.find('li.current');
var step = $current.data('step');
var that = this;