tour.es6.js 7.3 KB

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