walter/app/views/application/_abraham.html.erb
Jonathan Abbett 4ad630c6ae
Feature: Manual tours (#52)
Resolves #5
2021-04-21 15:01:29 -04:00

76 lines
2.9 KiB
Plaintext

<script>
Abraham.tours["<%= tour_name %>"] = new Shepherd.Tour(<%= Rails.configuration.abraham.tour_options.html_safe unless Rails.configuration.abraham.tour_options.nil? %>);
<% if trigger != 'manual' %>
Abraham.tours["<%= tour_name %>"].on("complete", function() {
// Make AJAX call to save history of tour completion
return fetch("/abraham_histories/", {
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
authenticity_token: '<%= form_authenticity_token %>',
controller_name: '<%= controller_name %>',
action_name: '<%= action_name %>',
tour_name: '<%= tour_name %>'
})
});
});
Abraham.tours["<%= tour_name %>"].on("cancel", function() {
Cookies.set('<%= abraham_cookie_prefix %>-<%= tour_name %>', 'later', { domain: '<%= abraham_domain %>' });
});
<% end %>
<% steps.each_with_index do |(key, step), index| %>
Abraham.tours["<%= tour_name %>"].addStep({
id: 'step-<%= key %>',
<% if step.key?('title') %>
title: "<%= step['title'] %>",
<% end %>
text: "<%= step['text'] %>",
<% if step.key?('attachTo') %>
attachTo: { element: "<%= step['attachTo']['element'] %>", on: "<%= step['attachTo']['placement'] %>" },
showOn: function() {
// Only display this step if its selector is present
return document.querySelector("<%= step['attachTo']['element'] %>") ? true : false
},
<% end %>
buttons: [
<% if index == steps.size - 1 %>
{ text: '<%= t('abraham.done') %>', action: Abraham.tours["<%= tour_name %>"].complete }
<% else %>
<% if index == 0 %>
{ text: '<%= t('abraham.later') %>', action: Abraham.tours["<%= tour_name %>"].cancel, classes: 'shepherd-button-secondary' },
{ text: '<%= t('abraham.continue') %>', action: Abraham.tours["<%= tour_name %>"].next }
<% else %>
{ text: '<%= t('abraham.exit') %>', action: Abraham.tours["<%= tour_name %>"].cancel, classes: 'shepherd-button-secondary' },
{ text: '<%= t('abraham.next') %>', action: Abraham.tours["<%= tour_name %>"].next }
<% end %>
<% end %>
]
});
<% end %>
<% if trigger != "manual" %>
Abraham.tours["<%= tour_name %>"].checkAndStart = function (start) {
return function () {
// Don't start the tour if the user dismissed it once this session
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();
}
}
}(Abraham.tours["<%= tour_name %>"].start)
<% if !tour_completed %>
Abraham.incompleteTours.push("<%= tour_name %>");
<% end %>
<% end %>
</script>