tour.es6.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if ($tour.find('li').length) {
  133. $tour.joyride({
  134. autoStart: true,
  135. postRideCallback() {
  136. that.model.set('isActive', false);
  137. },
  138. // HTML segments for tip layout.
  139. template: {
  140. link: '<a href=\"#close\" class=\"joyride-close-tip\">&times;</a>',
  141. button: '<a href=\"#\" class=\"button button--primary joyride-next-tip\"></a>',
  142. },
  143. });
  144. this.model.set({ isActive: true, activeTour: $tour });
  145. }
  146. }
  147. else {
  148. this.model.get('activeTour').joyride('destroy');
  149. this.model.set({ isActive: false, activeTour: [] });
  150. }
  151. },
  152. /**
  153. * Toolbar tab click event handler; toggles isActive.
  154. *
  155. * @param {jQuery.Event} event
  156. * The click event.
  157. */
  158. onClick(event) {
  159. this.model.set('isActive', !this.model.get('isActive'));
  160. event.preventDefault();
  161. event.stopPropagation();
  162. },
  163. /**
  164. * Gets the tour.
  165. *
  166. * @return {jQuery}
  167. * A jQuery element pointing to a `<ol>` containing tour items.
  168. */
  169. _getTour() {
  170. return this.model.get('tour');
  171. },
  172. /**
  173. * Gets the relevant document as a jQuery element.
  174. *
  175. * @return {jQuery}
  176. * A jQuery element pointing to the document within which a tour would be
  177. * started given the current state.
  178. */
  179. _getDocument() {
  180. return $(document);
  181. },
  182. /**
  183. * Removes tour items for elements that don't have matching page elements.
  184. *
  185. * Or that are explicitly filtered out via the 'tips' query string.
  186. *
  187. * @example
  188. * <caption>This will filter out tips that do not have a matching
  189. * page element or don't have the "bar" class.</caption>
  190. * http://example.com/foo?tips=bar
  191. *
  192. * @param {jQuery} $tour
  193. * A jQuery element pointing to a `<ol>` containing tour items.
  194. * @param {jQuery} $document
  195. * A jQuery element pointing to the document within which the elements
  196. * should be sought.
  197. *
  198. * @see Drupal.tour.views.ToggleTourView#_getDocument
  199. */
  200. _removeIrrelevantTourItems($tour, $document) {
  201. let removals = false;
  202. const tips = /tips=([^&]+)/.exec(queryString);
  203. $tour
  204. .find('li')
  205. .each(function () {
  206. const $this = $(this);
  207. const itemId = $this.attr('data-id');
  208. const itemClass = $this.attr('data-class');
  209. // If the query parameter 'tips' is set, remove all tips that don't
  210. // have the matching class.
  211. if (tips && !$(this).hasClass(tips[1])) {
  212. removals = true;
  213. $this.remove();
  214. return;
  215. }
  216. // Remove tip from the DOM if there is no corresponding page element.
  217. if ((!itemId && !itemClass) ||
  218. (itemId && $document.find(`#${itemId}`).length) ||
  219. (itemClass && $document.find(`.${itemClass}`).length)) {
  220. return;
  221. }
  222. removals = true;
  223. $this.remove();
  224. });
  225. // If there were removals, we'll have to do some clean-up.
  226. if (removals) {
  227. const total = $tour.find('li').length;
  228. if (!total) {
  229. this.model.set({ tour: [] });
  230. }
  231. $tour
  232. .find('li')
  233. // Rebuild the progress data.
  234. .each(function (index) {
  235. const progress = Drupal.t('!tour_item of !total', { '!tour_item': index + 1, '!total': total });
  236. $(this).find('.tour-progress').text(progress);
  237. })
  238. // Update the last item to have "End tour" as the button.
  239. .eq(-1)
  240. .attr('data-text', Drupal.t('End tour'));
  241. }
  242. },
  243. });
  244. }(jQuery, Backbone, Drupal, document));