tour.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. (function ($, Backbone, Drupal, document) {
  8. var queryString = decodeURI(window.location.search);
  9. Drupal.behaviors.tour = {
  10. attach: function attach(context) {
  11. $('body').once('tour').each(function () {
  12. var model = new Drupal.tour.models.StateModel();
  13. new Drupal.tour.views.ToggleTourView({
  14. el: $(context).find('#toolbar-tab-tour'),
  15. model: model
  16. });
  17. model.on('change:isActive', function (model, isActive) {
  18. $(document).trigger(isActive ? 'drupalTourStarted' : 'drupalTourStopped');
  19. }).set('tour', $(context).find('ol#tour'));
  20. if (/tour=?/i.test(queryString)) {
  21. model.set('isActive', true);
  22. }
  23. });
  24. }
  25. };
  26. Drupal.tour = Drupal.tour || {
  27. models: {},
  28. views: {}
  29. };
  30. Drupal.tour.models.StateModel = Backbone.Model.extend({
  31. defaults: {
  32. tour: [],
  33. isActive: false,
  34. activeTour: []
  35. }
  36. });
  37. Drupal.tour.views.ToggleTourView = Backbone.View.extend({
  38. events: { click: 'onClick' },
  39. initialize: function initialize() {
  40. this.listenTo(this.model, 'change:tour change:isActive', this.render);
  41. this.listenTo(this.model, 'change:isActive', this.toggleTour);
  42. },
  43. render: function render() {
  44. this.$el.toggleClass('hidden', this._getTour().length === 0);
  45. var isActive = this.model.get('isActive');
  46. this.$el.find('button').toggleClass('is-active', isActive).prop('aria-pressed', isActive);
  47. return this;
  48. },
  49. toggleTour: function toggleTour() {
  50. if (this.model.get('isActive')) {
  51. var $tour = this._getTour();
  52. this._removeIrrelevantTourItems($tour, this._getDocument());
  53. var that = this;
  54. if ($tour.find('li').length) {
  55. $tour.joyride({
  56. autoStart: true,
  57. postRideCallback: function postRideCallback() {
  58. that.model.set('isActive', false);
  59. },
  60. template: {
  61. link: '<a href=\"#close\" class=\"joyride-close-tip\">&times;</a>',
  62. button: '<a href=\"#\" class=\"button button--primary joyride-next-tip\"></a>'
  63. }
  64. });
  65. this.model.set({ isActive: true, activeTour: $tour });
  66. }
  67. } else {
  68. this.model.get('activeTour').joyride('destroy');
  69. this.model.set({ isActive: false, activeTour: [] });
  70. }
  71. },
  72. onClick: function onClick(event) {
  73. this.model.set('isActive', !this.model.get('isActive'));
  74. event.preventDefault();
  75. event.stopPropagation();
  76. },
  77. _getTour: function _getTour() {
  78. return this.model.get('tour');
  79. },
  80. _getDocument: function _getDocument() {
  81. return $(document);
  82. },
  83. _removeIrrelevantTourItems: function _removeIrrelevantTourItems($tour, $document) {
  84. var removals = false;
  85. var tips = /tips=([^&]+)/.exec(queryString);
  86. $tour.find('li').each(function () {
  87. var $this = $(this);
  88. var itemId = $this.attr('data-id');
  89. var itemClass = $this.attr('data-class');
  90. if (tips && !$(this).hasClass(tips[1])) {
  91. removals = true;
  92. $this.remove();
  93. return;
  94. }
  95. if (!itemId && !itemClass || itemId && $document.find('#' + itemId).length || itemClass && $document.find('.' + itemClass).length) {
  96. return;
  97. }
  98. removals = true;
  99. $this.remove();
  100. });
  101. if (removals) {
  102. var total = $tour.find('li').length;
  103. if (!total) {
  104. this.model.set({ tour: [] });
  105. }
  106. $tour.find('li').each(function (index) {
  107. var progress = Drupal.t('!tour_item of !total', { '!tour_item': index + 1, '!total': total });
  108. $(this).find('.tour-progress').text(progress);
  109. }).eq(-1).attr('data-text', Drupal.t('End tour'));
  110. }
  111. }
  112. });
  113. })(jQuery, Backbone, Drupal, document);