# The Click class handles clicked links, verifying if Turbolinks should # take control by inspecting both the event and the link. If it should, # the page change process is initiated. If not, control is passed back # to the browser for default functionality. class window.Click

@installHandlerLast: (event) ->
  unless event.defaultPrevented
    document.removeEventListener 'click', Click.handle, false
    document.addEventListener 'click', Click.handle, false

@handle: (event) ->
  new Click event

constructor: (@event) ->
  return if @event.defaultPrevented
  @_extractLink()
  if @_validForTurbolinks()
    Turbolinks.visit @link.href
    @event.preventDefault()

_extractLink: ->
  link = @event.target
  link = link.parentNode until !link.parentNode or link.nodeName is 'A'
  @link = new Link(link) if link.nodeName is 'A' and link.href.length isnt 0

_validForTurbolinks: ->
  @link? and not (@link.shouldIgnore() or @_nonStandardClick())

_nonStandardClick: ->
  @event.which > 1 or
    @event.metaKey or
    @event.ctrlKey or
    @event.shiftKey or
    @event.altKey