Sha256: ebefbdcc3a512a42b5068671cb8d42883d2e2f805c847134d848871b9c0a7524

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

#= require partystreusel/base

class Tab extends Partystreusel.Base
  @className = 'Tab'

  constructor: (el) ->
    super
    @$el.find('.tab__nav-item').on 'click', @openItem
    @$navItems = @$el.find('.tab__nav-item')
    @$panels = @$el.find('.tab__panel')

    if @findItemAndPanel(window.location.hash)
      @openPanel(window.location.hash)

    unless @currentPanelName()?
      @openPanel(0)

  openItem: (e) =>
    item = $(e.target).closest('.tab__nav-item')
    name = item.find('[href]').attr('href')
    @openPanel(name)

  currentPanelName: ->
    @$panels.filter('.tab__panel--active').attr('id')

  openPanel: (name_or_number) ->
    if typeof name_or_number == 'number'
      panelName = @$panels[name_or_number].id
    else
      panelName = name_or_number

    itemAndPanel = @findItemAndPanel(panelName)
    unless itemAndPanel
      console.log("Nav Item or panel with name #{panelName} not found")
      return

    itemAndPanelToClose = @findItemAndPanel(@currentPanelName())
    if itemAndPanelToClose
      closeName = @currentPanelName()
      itemAndPanelToClose[0].removeClass('tab__nav-item--active')
      itemAndPanelToClose[1].removeClass('tab__panel--active')
      @trigger('close', closeName, itemAndPanelToClose[0], itemAndPanelToClose[1])

    itemAndPanel[0].addClass('tab__nav-item--active')
    itemAndPanel[1].addClass('tab__panel--active')

    @trigger('open', @currentPanelName(), itemAndPanel[0], itemAndPanel[1])

  findItemAndPanel: (name) ->
    return unless name?
    return unless name.length > 0

    if name.indexOf('#') == 0
      name = name.slice(1)

    panel = @$panels.filter("[id=#{name}]")
    if panel.length == 0
      return

    navItem = @$navItems.filter (index, item) ->
      $(item).find("[href='\##{name}']").length > 0
    if navItem.length == 0
      return

    [navItem, panel]

Partystreusel.Tab = Tab

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
partystreusel-2.0.0 app/assets/javascripts/partystreusel/tab.js.coffee
partystreusel-1.2.0 app/assets/javascripts/partystreusel/tab.js.coffee