annotate core/modules/tour/js/tour.es6.js @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * Attaches behaviors for the Tour module's toolbar tab.
Chris@0 4 */
Chris@0 5
Chris@0 6 (function ($, Backbone, Drupal, document) {
Chris@0 7 const queryString = decodeURI(window.location.search);
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Attaches the tour's toolbar tab behavior.
Chris@0 11 *
Chris@0 12 * It uses the query string for:
Chris@0 13 * - tour: When ?tour=1 is present, the tour will start automatically after
Chris@0 14 * the page has loaded.
Chris@0 15 * - tips: Pass ?tips=class in the url to filter the available tips to the
Chris@0 16 * subset which match the given class.
Chris@0 17 *
Chris@0 18 * @example
Chris@0 19 * http://example.com/foo?tour=1&tips=bar
Chris@0 20 *
Chris@0 21 * @type {Drupal~behavior}
Chris@0 22 *
Chris@0 23 * @prop {Drupal~behaviorAttach} attach
Chris@0 24 * Attach tour functionality on `tour` events.
Chris@0 25 */
Chris@0 26 Drupal.behaviors.tour = {
Chris@0 27 attach(context) {
Chris@0 28 $('body').once('tour').each(() => {
Chris@0 29 const model = new Drupal.tour.models.StateModel();
Chris@0 30 new Drupal.tour.views.ToggleTourView({
Chris@0 31 el: $(context).find('#toolbar-tab-tour'),
Chris@0 32 model,
Chris@0 33 });
Chris@0 34
Chris@0 35 model
Chris@0 36 // Allow other scripts to respond to tour events.
Chris@0 37 .on('change:isActive', (model, isActive) => {
Chris@0 38 $(document).trigger((isActive) ? 'drupalTourStarted' : 'drupalTourStopped');
Chris@0 39 })
Chris@0 40 // Initialization: check whether a tour is available on the current
Chris@0 41 // page.
Chris@0 42 .set('tour', $(context).find('ol#tour'));
Chris@0 43
Chris@0 44 // Start the tour immediately if toggled via query string.
Chris@0 45 if (/tour=?/i.test(queryString)) {
Chris@0 46 model.set('isActive', true);
Chris@0 47 }
Chris@0 48 });
Chris@0 49 },
Chris@0 50 };
Chris@0 51
Chris@0 52 /**
Chris@0 53 * @namespace
Chris@0 54 */
Chris@0 55 Drupal.tour = Drupal.tour || {
Chris@0 56
Chris@0 57 /**
Chris@0 58 * @namespace Drupal.tour.models
Chris@0 59 */
Chris@0 60 models: {},
Chris@0 61
Chris@0 62 /**
Chris@0 63 * @namespace Drupal.tour.views
Chris@0 64 */
Chris@0 65 views: {},
Chris@0 66 };
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Backbone Model for tours.
Chris@0 70 *
Chris@0 71 * @constructor
Chris@0 72 *
Chris@0 73 * @augments Backbone.Model
Chris@0 74 */
Chris@0 75 Drupal.tour.models.StateModel = Backbone.Model.extend(/** @lends Drupal.tour.models.StateModel# */{
Chris@0 76
Chris@0 77 /**
Chris@0 78 * @type {object}
Chris@0 79 */
Chris@0 80 defaults: /** @lends Drupal.tour.models.StateModel# */{
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Indicates whether the Drupal root window has a tour.
Chris@0 84 *
Chris@0 85 * @type {Array}
Chris@0 86 */
Chris@0 87 tour: [],
Chris@0 88
Chris@0 89 /**
Chris@0 90 * Indicates whether the tour is currently running.
Chris@0 91 *
Chris@0 92 * @type {bool}
Chris@0 93 */
Chris@0 94 isActive: false,
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Indicates which tour is the active one (necessary to cleanly stop).
Chris@0 98 *
Chris@0 99 * @type {Array}
Chris@0 100 */
Chris@0 101 activeTour: [],
Chris@0 102 },
Chris@0 103 });
Chris@0 104
Chris@0 105 Drupal.tour.views.ToggleTourView = Backbone.View.extend(/** @lends Drupal.tour.views.ToggleTourView# */{
Chris@0 106
Chris@0 107 /**
Chris@0 108 * @type {object}
Chris@0 109 */
Chris@0 110 events: { click: 'onClick' },
Chris@0 111
Chris@0 112 /**
Chris@0 113 * Handles edit mode toggle interactions.
Chris@0 114 *
Chris@0 115 * @constructs
Chris@0 116 *
Chris@0 117 * @augments Backbone.View
Chris@0 118 */
Chris@0 119 initialize() {
Chris@0 120 this.listenTo(this.model, 'change:tour change:isActive', this.render);
Chris@0 121 this.listenTo(this.model, 'change:isActive', this.toggleTour);
Chris@0 122 },
Chris@0 123
Chris@0 124 /**
Chris@0 125 * @inheritdoc
Chris@0 126 *
Chris@0 127 * @return {Drupal.tour.views.ToggleTourView}
Chris@0 128 * The `ToggleTourView` view.
Chris@0 129 */
Chris@0 130 render() {
Chris@0 131 // Render the visibility.
Chris@0 132 this.$el.toggleClass('hidden', this._getTour().length === 0);
Chris@0 133 // Render the state.
Chris@0 134 const isActive = this.model.get('isActive');
Chris@0 135 this.$el.find('button')
Chris@0 136 .toggleClass('is-active', isActive)
Chris@0 137 .prop('aria-pressed', isActive);
Chris@0 138 return this;
Chris@0 139 },
Chris@0 140
Chris@0 141 /**
Chris@0 142 * Model change handler; starts or stops the tour.
Chris@0 143 */
Chris@0 144 toggleTour() {
Chris@0 145 if (this.model.get('isActive')) {
Chris@0 146 const $tour = this._getTour();
Chris@0 147 this._removeIrrelevantTourItems($tour, this._getDocument());
Chris@0 148 const that = this;
Chris@0 149 if ($tour.find('li').length) {
Chris@0 150 $tour.joyride({
Chris@0 151 autoStart: true,
Chris@0 152 postRideCallback() {
Chris@0 153 that.model.set('isActive', false);
Chris@0 154 },
Chris@0 155 // HTML segments for tip layout.
Chris@0 156 template: {
Chris@0 157 link: '<a href=\"#close\" class=\"joyride-close-tip\">&times;</a>',
Chris@0 158 button: '<a href=\"#\" class=\"button button--primary joyride-next-tip\"></a>',
Chris@0 159 },
Chris@0 160 });
Chris@0 161 this.model.set({ isActive: true, activeTour: $tour });
Chris@0 162 }
Chris@0 163 }
Chris@0 164 else {
Chris@0 165 this.model.get('activeTour').joyride('destroy');
Chris@0 166 this.model.set({ isActive: false, activeTour: [] });
Chris@0 167 }
Chris@0 168 },
Chris@0 169
Chris@0 170 /**
Chris@0 171 * Toolbar tab click event handler; toggles isActive.
Chris@0 172 *
Chris@0 173 * @param {jQuery.Event} event
Chris@0 174 * The click event.
Chris@0 175 */
Chris@0 176 onClick(event) {
Chris@0 177 this.model.set('isActive', !this.model.get('isActive'));
Chris@0 178 event.preventDefault();
Chris@0 179 event.stopPropagation();
Chris@0 180 },
Chris@0 181
Chris@0 182 /**
Chris@0 183 * Gets the tour.
Chris@0 184 *
Chris@0 185 * @return {jQuery}
Chris@0 186 * A jQuery element pointing to a `<ol>` containing tour items.
Chris@0 187 */
Chris@0 188 _getTour() {
Chris@0 189 return this.model.get('tour');
Chris@0 190 },
Chris@0 191
Chris@0 192 /**
Chris@0 193 * Gets the relevant document as a jQuery element.
Chris@0 194 *
Chris@0 195 * @return {jQuery}
Chris@0 196 * A jQuery element pointing to the document within which a tour would be
Chris@0 197 * started given the current state.
Chris@0 198 */
Chris@0 199 _getDocument() {
Chris@0 200 return $(document);
Chris@0 201 },
Chris@0 202
Chris@0 203 /**
Chris@0 204 * Removes tour items for elements that don't have matching page elements.
Chris@0 205 *
Chris@0 206 * Or that are explicitly filtered out via the 'tips' query string.
Chris@0 207 *
Chris@0 208 * @example
Chris@0 209 * <caption>This will filter out tips that do not have a matching
Chris@0 210 * page element or don't have the "bar" class.</caption>
Chris@0 211 * http://example.com/foo?tips=bar
Chris@0 212 *
Chris@0 213 * @param {jQuery} $tour
Chris@0 214 * A jQuery element pointing to a `<ol>` containing tour items.
Chris@0 215 * @param {jQuery} $document
Chris@0 216 * A jQuery element pointing to the document within which the elements
Chris@0 217 * should be sought.
Chris@0 218 *
Chris@0 219 * @see Drupal.tour.views.ToggleTourView#_getDocument
Chris@0 220 */
Chris@0 221 _removeIrrelevantTourItems($tour, $document) {
Chris@0 222 let removals = false;
Chris@0 223 const tips = /tips=([^&]+)/.exec(queryString);
Chris@0 224 $tour
Chris@0 225 .find('li')
Chris@0 226 .each(function () {
Chris@0 227 const $this = $(this);
Chris@0 228 const itemId = $this.attr('data-id');
Chris@0 229 const itemClass = $this.attr('data-class');
Chris@0 230 // If the query parameter 'tips' is set, remove all tips that don't
Chris@0 231 // have the matching class.
Chris@0 232 if (tips && !$(this).hasClass(tips[1])) {
Chris@0 233 removals = true;
Chris@0 234 $this.remove();
Chris@0 235 return;
Chris@0 236 }
Chris@0 237 // Remove tip from the DOM if there is no corresponding page element.
Chris@0 238 if ((!itemId && !itemClass) ||
Chris@0 239 (itemId && $document.find(`#${itemId}`).length) ||
Chris@0 240 (itemClass && $document.find(`.${itemClass}`).length)) {
Chris@0 241 return;
Chris@0 242 }
Chris@0 243 removals = true;
Chris@0 244 $this.remove();
Chris@0 245 });
Chris@0 246
Chris@0 247 // If there were removals, we'll have to do some clean-up.
Chris@0 248 if (removals) {
Chris@0 249 const total = $tour.find('li').length;
Chris@0 250 if (!total) {
Chris@0 251 this.model.set({ tour: [] });
Chris@0 252 }
Chris@0 253
Chris@0 254 $tour
Chris@0 255 .find('li')
Chris@0 256 // Rebuild the progress data.
Chris@0 257 .each(function (index) {
Chris@0 258 const progress = Drupal.t('!tour_item of !total', { '!tour_item': index + 1, '!total': total });
Chris@0 259 $(this).find('.tour-progress').text(progress);
Chris@0 260 })
Chris@0 261 // Update the last item to have "End tour" as the button.
Chris@0 262 .eq(-1)
Chris@0 263 .attr('data-text', Drupal.t('End tour'));
Chris@0 264 }
Chris@0 265 },
Chris@0 266
Chris@0 267 });
Chris@0 268 }(jQuery, Backbone, Drupal, document));