Files
nairobi/app/assets/javascripts/tree_navigator.js.coffee
Javi Martín cc7e0d586b Reduce local variables usage in CoffeeScript
Local variables are one of the things CoffeeScript doesn't compile to
modern JavaScript automatically: it uses `var` instead of `const` or
`let`.

Besides, using `$this = $(this)` is usually done to reference the
current object in another function where the current object is a
different one. Here we were using it with no clear purpose.
2019-09-11 03:14:17 +02:00

34 lines
988 B
CoffeeScript

"use strict"
App.TreeNavigator =
setNodes: (nodes) ->
nodes.children("ul").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: ->
if($(this).hasClass("open"))
$(this).removeClass("open").addClass("closed")
$(this).siblings("ul").hide()
else if($(this).hasClass("closed"))
$(this).removeClass("closed").addClass("open")
$(this).siblings("ul").show()
anchor = $(location).attr("hash")
if anchor
elem.find("a[href='#{anchor}']").parents("ul").each ->
$(this).show()
$(this).siblings("span").removeClass("closed").addClass("open")