Files
nairobi/app/assets/javascripts/tree_navigator.js.coffee
Javi Martín b27855c1cf Use double quotes in CoffeeScript files
As we do in the rest of the application.

Note we cannot add a rule enforcing double quotes because CoffeeScript
Lint does not have such rule.
2019-03-06 11:41:21 +01:00

37 lines
1.0 KiB
CoffeeScript

App.TreeNavigator =
setNodes: (nodes) ->
children = nodes.children("ul")
if(children.length == 0)
return
children.each ->
link = $(this).prev("a")
$('<span class="open"></span>').insertBefore(link)
App.TreeNavigator.setNodes($(this).children())
initialize: ->
elem = $("[data-tree-navigator]")
if(elem.length == 0)
return
ul = elem.find("ul:eq(0)")
if(ul.length && ul.children().length)
App.TreeNavigator.setNodes(ul.children())
$("[data-tree-navigator] span").on
click: (e) ->
elem = $(this)
if(elem.hasClass("open"))
elem.removeClass("open").addClass("closed")
elem.siblings("ul").hide()
else if(elem.hasClass("closed"))
elem.removeClass("closed").addClass("open")
elem.siblings("ul").show()
if anchor = $(location).attr("hash")
if link = elem.find("a[href='#{anchor}']")
link.parents("ul").each ->
$(this).show()
$(this).siblings("span").removeClass("closed").addClass("open")