3 * Attaches behaviors for the Tour module's toolbar tab.
6 (function ($, Backbone, Drupal, document) {
7 const queryString = decodeURI(window.location.search);
10 * Attaches the tour's toolbar tab behavior.
12 * It uses the query string for:
13 * - tour: When ?tour=1 is present, the tour will start automatically after
14 * the page has loaded.
15 * - tips: Pass ?tips=class in the url to filter the available tips to the
16 * subset which match the given class.
19 * http://example.com/foo?tour=1&tips=bar
21 * @type {Drupal~behavior}
23 * @prop {Drupal~behaviorAttach} attach
24 * Attach tour functionality on `tour` events.
26 Drupal.behaviors.tour = {
28 $('body').once('tour').each(() => {
29 const model = new Drupal.tour.models.StateModel();
30 new Drupal.tour.views.ToggleTourView({
31 el: $(context).find('#toolbar-tab-tour'),
36 // Allow other scripts to respond to tour events.
37 .on('change:isActive', (model, isActive) => {
38 $(document).trigger((isActive) ? 'drupalTourStarted' : 'drupalTourStopped');
40 // Initialization: check whether a tour is available on the current
42 .set('tour', $(context).find('ol#tour'));
44 // Start the tour immediately if toggled via query string.
45 if (/tour=?/i.test(queryString)) {
46 model.set('isActive', true);
55 Drupal.tour = Drupal.tour || {
58 * @namespace Drupal.tour.models
63 * @namespace Drupal.tour.views
69 * Backbone Model for tours.
73 * @augments Backbone.Model
75 Drupal.tour.models.StateModel = Backbone.Model.extend(/** @lends Drupal.tour.models.StateModel# */{
80 defaults: /** @lends Drupal.tour.models.StateModel# */{
83 * Indicates whether the Drupal root window has a tour.
90 * Indicates whether the tour is currently running.
97 * Indicates which tour is the active one (necessary to cleanly stop).
105 Drupal.tour.views.ToggleTourView = Backbone.View.extend(/** @lends Drupal.tour.views.ToggleTourView# */{
110 events: { click: 'onClick' },
113 * Handles edit mode toggle interactions.
117 * @augments Backbone.View
120 this.listenTo(this.model, 'change:tour change:isActive', this.render);
121 this.listenTo(this.model, 'change:isActive', this.toggleTour);
127 * @return {Drupal.tour.views.ToggleTourView}
128 * The `ToggleTourView` view.
131 // Render the visibility.
132 this.$el.toggleClass('hidden', this._getTour().length === 0);
134 const isActive = this.model.get('isActive');
135 this.$el.find('button')
136 .toggleClass('is-active', isActive)
137 .prop('aria-pressed', isActive);
142 * Model change handler; starts or stops the tour.
145 if (this.model.get('isActive')) {
146 const $tour = this._getTour();
147 this._removeIrrelevantTourItems($tour, this._getDocument());
149 if ($tour.find('li').length) {
153 that.model.set('isActive', false);
155 // HTML segments for tip layout.
157 link: '<a href=\"#close\" class=\"joyride-close-tip\">×</a>',
158 button: '<a href=\"#\" class=\"button button--primary joyride-next-tip\"></a>',
161 this.model.set({ isActive: true, activeTour: $tour });
165 this.model.get('activeTour').joyride('destroy');
166 this.model.set({ isActive: false, activeTour: [] });
171 * Toolbar tab click event handler; toggles isActive.
173 * @param {jQuery.Event} event
177 this.model.set('isActive', !this.model.get('isActive'));
178 event.preventDefault();
179 event.stopPropagation();
186 * A jQuery element pointing to a `<ol>` containing tour items.
189 return this.model.get('tour');
193 * Gets the relevant document as a jQuery element.
196 * A jQuery element pointing to the document within which a tour would be
197 * started given the current state.
204 * Removes tour items for elements that don't have matching page elements.
206 * Or that are explicitly filtered out via the 'tips' query string.
209 * <caption>This will filter out tips that do not have a matching
210 * page element or don't have the "bar" class.</caption>
211 * http://example.com/foo?tips=bar
213 * @param {jQuery} $tour
214 * A jQuery element pointing to a `<ol>` containing tour items.
215 * @param {jQuery} $document
216 * A jQuery element pointing to the document within which the elements
219 * @see Drupal.tour.views.ToggleTourView#_getDocument
221 _removeIrrelevantTourItems($tour, $document) {
222 let removals = false;
223 const tips = /tips=([^&]+)/.exec(queryString);
227 const $this = $(this);
228 const itemId = $this.attr('data-id');
229 const itemClass = $this.attr('data-class');
230 // If the query parameter 'tips' is set, remove all tips that don't
231 // have the matching class.
232 if (tips && !$(this).hasClass(tips[1])) {
237 // Remove tip from the DOM if there is no corresponding page element.
238 if ((!itemId && !itemClass) ||
239 (itemId && $document.find(`#${itemId}`).length) ||
240 (itemClass && $document.find(`.${itemClass}`).length)) {
247 // If there were removals, we'll have to do some clean-up.
249 const total = $tour.find('li').length;
251 this.model.set({ tour: [] });
256 // Rebuild the progress data.
257 .each(function (index) {
258 const progress = Drupal.t('!tour_item of !total', { '!tour_item': index + 1, '!total': total });
259 $(this).find('.tour-progress').text(progress);
261 // Update the last item to have "End tour" as the button.
263 .attr('data-text', Drupal.t('End tour'));
268 }(jQuery, Backbone, Drupal, document));