fiedl/your_platform

View on GitHub
app/assets/javascripts/your_platform/tree_ul.coffee

Summary

Maintainability
Test Coverage
# https://bootsnipp.com/snippets/ypNAe

$.fn.extend treed: (o) ->
  openedClass = 'glyphicon-minus-sign'
  closedClass = 'glyphicon-plus-sign'
  if typeof o != 'undefined'
    if typeof o.openedClass != 'undefined'
      openedClass = o.openedClass
    if typeof o.closedClass != 'undefined'
      closedClass = o.closedClass
  #initialize each of the top levels
  tree = $(this)
  tree.addClass 'tree'
  tree.find('li').has('ul').each ->
    branch = $(this)
    #li with children ul
    branch.prepend '<i class=\'indicator glyphicon ' + closedClass + '\'></i>'
    branch.addClass 'branch'
    branch.children().children().toggle()
    return

$(document).on 'click', 'ul.tree .branch .indicator', ->
  openedClass = 'fa fa-chevron-down'
  closedClass = 'fa fa-chevron-right'
  icon = $(this)
  icon.toggleClass openedClass + ' ' + closedClass
  branch = icon.closest('.branch')
  branch.children().children().toggle()

# ---
# generated by js2coffee 2.2.0

$(document).ready ->
  $("ul.tree").treed({openedClass:'fa fa-chevron-down', closedClass:'fa fa-chevron-right'})

  # open the first tree item to indicate that this is nested.
  $("ul.tree > .branch:first > .indicator").click()