From 60dcd0dd310ab3dfa259f1adb3f74bd666c83fc4 Mon Sep 17 00:00:00 2001 From: Jonathan Abbett Date: Thu, 9 Jan 2020 07:39:29 -0500 Subject: [PATCH] Don't start a tour if the first step's attachTo isn't present Resolves #28, includes documentation note about step-skipping behavior. --- README.md | 5 +++++ abraham.gemspec | 1 + app/views/application/_abraham.html.erb | 8 +++++++- test/dummy/app/controllers/dashboard_controller.rb | 2 +- test/dummy/app/views/dashboard/missing.html.erb | 5 +++++ test/dummy/config/routes.rb | 2 +- test/dummy/config/tours/dashboard/missing.en.yml | 10 ++++++++++ test/dummy/config/tours/dashboard/other.en.yml | 3 +++ test/dummy/test/system/tours_test.rb | 6 ++++++ 9 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 test/dummy/app/views/dashboard/missing.html.erb create mode 100644 test/dummy/config/tours/dashboard/missing.en.yml diff --git a/README.md b/README.md index fb8b6e5..7d09c7d 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,11 @@ Abraham takes care of which buttons should appear with each step: * "Exit" and "Next" buttons on intermediate steps * "Done" button on the last step +Abraham tries to be helpful when your tour steps attach to page elements that are missing: + +* If your first step is attached to a particular element, and that element is not present on the page, the tour won't start. ([#28](https://github.com/actmd/abraham/issues/28)) +* If your tour has an intermediate step attached to a missing element, Abraham will skip that step and automatically show the next. ([#6](https://github.com/actmd/abraham/issues/6)) + ### Testing your tours Abraham loads tour definitions once when you start your server. Restart your server to see tour changes. diff --git a/abraham.gemspec b/abraham.gemspec index 0d0da07..65c6fb8 100644 --- a/abraham.gemspec +++ b/abraham.gemspec @@ -22,4 +22,5 @@ Gem::Specification.new do |s| s.add_development_dependency 'sqlite3' s.add_development_dependency 'rubocop' s.add_development_dependency 'listen' + s.add_development_dependency 'web-console' end diff --git a/app/views/application/_abraham.html.erb b/app/views/application/_abraham.html.erb index a743d5d..084124c 100644 --- a/app/views/application/_abraham.html.erb +++ b/app/views/application/_abraham.html.erb @@ -54,7 +54,13 @@ tour.start = function (start) { return function () { // Don't start the tour if the user dismissed it once this session - if (!Cookies.get('<%= abraham_cookie_prefix %>-<%= tour_name %>', {domain: '<%= abraham_domain %>'})) { + var tourMayStart = !Cookies.get('<%= abraham_cookie_prefix %>-<%= tour_name %>', {domain: '<%= abraham_domain %>'}); + <% if steps.first[1]['attachTo'] %> + // Don't start the tour if the first step's element is missing + tourMayStart = tourMayStart && document.querySelector("<%= steps.first[1]['attachTo']['element'] %>"); + <% end %> + + if (tourMayStart) { start(); } } diff --git a/test/dummy/app/controllers/dashboard_controller.rb b/test/dummy/app/controllers/dashboard_controller.rb index aa4389d..1452282 100644 --- a/test/dummy/app/controllers/dashboard_controller.rb +++ b/test/dummy/app/controllers/dashboard_controller.rb @@ -2,6 +2,6 @@ class DashboardController < ApplicationController def home; end - def other; end + def missing; end end diff --git a/test/dummy/app/views/dashboard/missing.html.erb b/test/dummy/app/views/dashboard/missing.html.erb new file mode 100644 index 0000000..b2ce2e1 --- /dev/null +++ b/test/dummy/app/views/dashboard/missing.html.erb @@ -0,0 +1,5 @@ +

missing

+ +This page does not have the first step attachTo element, +so we would expect the tour not to start, +even though the second step could be rendered. \ No newline at end of file diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index c88d17c..fb1f82e 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -2,8 +2,8 @@ Rails.application.routes.draw do get "dashboard/home" - get "dashboard/other" + get "dashboard/missing" # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/test/dummy/config/tours/dashboard/missing.en.yml b/test/dummy/config/tours/dashboard/missing.en.yml new file mode 100644 index 0000000..58f4f09 --- /dev/null +++ b/test/dummy/config/tours/dashboard/missing.en.yml @@ -0,0 +1,10 @@ +tour: + steps: + 1: + title: "This step points to a missing element" + text: "This should not skip to the second step" + attachTo: + element: "#i-dont-exist" + placement: "right" + 2: + text: "You should not see me!" \ No newline at end of file diff --git a/test/dummy/config/tours/dashboard/other.en.yml b/test/dummy/config/tours/dashboard/other.en.yml index 3f4e290..8b0d7ac 100644 --- a/test/dummy/config/tours/dashboard/other.en.yml +++ b/test/dummy/config/tours/dashboard/other.en.yml @@ -3,6 +3,9 @@ tour_one: 1: title: "TOUR ONE step one ENGLISH" text: "we show this on your first visit" + attachTo: + element: "p" + placement: "top" tour_two: steps: diff --git a/test/dummy/test/system/tours_test.rb b/test/dummy/test/system/tours_test.rb index f96c476..8d8de07 100644 --- a/test/dummy/test/system/tours_test.rb +++ b/test/dummy/test/system/tours_test.rb @@ -71,4 +71,10 @@ class ToursTest < ApplicationSystemTestCase find("a").click assert_selector ".shepherd-element", visible: true end + + test "tour with missing first step attachTo does not appear" do + visit dashboard_missing_url + # No tour should be visible, since the first step is invalid + refute_selector ".shepherd-element" + end end