Sha256: de9d7cf6c92e8b8eab890b7c4eb76ee6812295b6a64758a1562f429d964368fe

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

//= require gretel.trails.jsuri

<%
unless defined?(Gretel::Trails::HiddenStrategy)
  raise "Gretel::Trails::HiddenStrategy` was not found. Please add `Gretel::Trails.strategy = :hidden` to an initializer."
end
%>

# Remove trail from querystring
removeTrailFromUrl = ->
  return if location.href.indexOf("<%= Gretel::Trails.trail_param %>=") is -1
  if history.replaceState?
    uri = new Gretel.Trails.Uri(location.href)
    history.replaceState history.state, document.title, uri.deleteQueryParam("<%= Gretel::Trails.trail_param %>")

# Remove trail on load
removeTrailFromUrl()

if Turbolinks?
  # Remove trail after page change
  $(document).on "page:change", -> removeTrailFromUrl()

# Add trails to querystring
$ ->
  $("body").on "click", "[data-<%= Gretel::Trails::HiddenStrategy.data_attribute %>], <%= Gretel::Trails::HiddenStrategy.js_selector %>", ->
    if trail = $(this).data("<%= Gretel::Trails::HiddenStrategy.data_attribute %>") || $("body").data("<%= Gretel::Trails::HiddenStrategy.data_attribute %>")
      href = $(this).attr("href")
      uri = new Gretel.Trails.Uri(href)
      href = uri.deleteQueryParam("<%= Gretel::Trails.trail_param %>").addQueryParam("<%= Gretel::Trails.trail_param %>", trail)
      $(this).attr("href", href)
    else
      console?.log "[Gretel] No `data-<%= Gretel::Trails::HiddenStrategy.data_attribute %>` was found on the <body> tag or the link you just clicked. Please set it using the `breadcrumb_trail` helper or see the Gretel::Trails readme for more info."

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gretel-trails-0.0.3 lib/assets/javascripts/gretel.trails.hidden.js.coffee.erb
gretel-trails-0.0.2 lib/assets/javascripts/gretel.trails.hidden.js.coffee.erb