Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/tour/js/tour.js @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 /** | |
2 * DO NOT EDIT THIS FILE. | |
3 * See the following change record for more information, | |
4 * https://www.drupal.org/node/2815083 | |
5 * @preserve | |
6 **/ | |
7 | |
8 (function ($, Backbone, Drupal, document) { | |
9 var queryString = decodeURI(window.location.search); | |
10 | |
11 Drupal.behaviors.tour = { | |
12 attach: function attach(context) { | |
13 $('body').once('tour').each(function () { | |
14 var model = new Drupal.tour.models.StateModel(); | |
15 new Drupal.tour.views.ToggleTourView({ | |
16 el: $(context).find('#toolbar-tab-tour'), | |
17 model: model | |
18 }); | |
19 | |
20 model.on('change:isActive', function (model, isActive) { | |
21 $(document).trigger(isActive ? 'drupalTourStarted' : 'drupalTourStopped'); | |
22 }).set('tour', $(context).find('ol#tour')); | |
23 | |
24 if (/tour=?/i.test(queryString)) { | |
25 model.set('isActive', true); | |
26 } | |
27 }); | |
28 } | |
29 }; | |
30 | |
31 Drupal.tour = Drupal.tour || { | |
32 models: {}, | |
33 | |
34 views: {} | |
35 }; | |
36 | |
37 Drupal.tour.models.StateModel = Backbone.Model.extend({ | |
38 defaults: { | |
39 tour: [], | |
40 | |
41 isActive: false, | |
42 | |
43 activeTour: [] | |
44 } | |
45 }); | |
46 | |
47 Drupal.tour.views.ToggleTourView = Backbone.View.extend({ | |
48 events: { click: 'onClick' }, | |
49 | |
50 initialize: function initialize() { | |
51 this.listenTo(this.model, 'change:tour change:isActive', this.render); | |
52 this.listenTo(this.model, 'change:isActive', this.toggleTour); | |
53 }, | |
54 render: function render() { | |
55 this.$el.toggleClass('hidden', this._getTour().length === 0); | |
56 | |
57 var isActive = this.model.get('isActive'); | |
58 this.$el.find('button').toggleClass('is-active', isActive).prop('aria-pressed', isActive); | |
59 return this; | |
60 }, | |
61 toggleTour: function toggleTour() { | |
62 if (this.model.get('isActive')) { | |
63 var $tour = this._getTour(); | |
64 this._removeIrrelevantTourItems($tour, this._getDocument()); | |
65 var that = this; | |
66 var close = Drupal.t('Close'); | |
67 if ($tour.find('li').length) { | |
68 $tour.joyride({ | |
69 autoStart: true, | |
70 postRideCallback: function postRideCallback() { | |
71 that.model.set('isActive', false); | |
72 }, | |
73 | |
74 template: { | |
75 link: '<a href="#close" class="joyride-close-tip" aria-label="' + close + '">×</a>', | |
76 button: '<a href="#" class="button button--primary joyride-next-tip"></a>' | |
77 } | |
78 }); | |
79 this.model.set({ isActive: true, activeTour: $tour }); | |
80 } | |
81 } else { | |
82 this.model.get('activeTour').joyride('destroy'); | |
83 this.model.set({ isActive: false, activeTour: [] }); | |
84 } | |
85 }, | |
86 onClick: function onClick(event) { | |
87 this.model.set('isActive', !this.model.get('isActive')); | |
88 event.preventDefault(); | |
89 event.stopPropagation(); | |
90 }, | |
91 _getTour: function _getTour() { | |
92 return this.model.get('tour'); | |
93 }, | |
94 _getDocument: function _getDocument() { | |
95 return $(document); | |
96 }, | |
97 _removeIrrelevantTourItems: function _removeIrrelevantTourItems($tour, $document) { | |
98 var removals = false; | |
99 var tips = /tips=([^&]+)/.exec(queryString); | |
100 $tour.find('li').each(function () { | |
101 var $this = $(this); | |
102 var itemId = $this.attr('data-id'); | |
103 var itemClass = $this.attr('data-class'); | |
104 | |
105 if (tips && !$(this).hasClass(tips[1])) { | |
106 removals = true; | |
107 $this.remove(); | |
108 return; | |
109 } | |
110 | |
111 if (!itemId && !itemClass || itemId && $document.find('#' + itemId).length || itemClass && $document.find('.' + itemClass).length) { | |
112 return; | |
113 } | |
114 removals = true; | |
115 $this.remove(); | |
116 }); | |
117 | |
118 if (removals) { | |
119 var total = $tour.find('li').length; | |
120 if (!total) { | |
121 this.model.set({ tour: [] }); | |
122 } | |
123 | |
124 $tour.find('li').each(function (index) { | |
125 var progress = Drupal.t('!tour_item of !total', { '!tour_item': index + 1, '!total': total }); | |
126 $(this).find('.tour-progress').text(progress); | |
127 }).eq(-1).attr('data-text', Drupal.t('End tour')); | |
128 } | |
129 } | |
130 }); | |
131 })(jQuery, Backbone, Drupal, document); |