NavigationTree.Agent

An agent represing a navigation tree. The agent holds transformed configuration state.

Provides convenience wrappers to generate Twitter/bootstrap-freindly userrole-aware HTML out of this state through NavigationTree.Helper and NavigationTree.Bootstrap.

Navigation tree nodes are to be configured as NavigationTree.Node structs.

Examples

Configure and start agent

iex> alias NavigationTree.Node, as: Node
nil

iex> NavigationTree.Agent.start_link %Node{
  name: "Home",
  url:  "/",
  children: [
    %Node{ name: "Login", url: "/auth" },
    %Node{
      name:  "Admin",
      roles: ["admin"],
      children: [
        %Node{ name: "Users", roles: ["user_admin"] },
        %Node{ name: "Roles" }
      ]
    }
  ]
}
{:ok, #PID<0.120.0>}

get tree node by path hopping through tree

iex> NavigationTree.Agent.node_of ["Home", "Admin", "Users"]
%{children: [], controller: nil, name: "Users", roles: ["admin", "user_admin"], url: "/admin/users"}

generate bootstrap-style HTML for a user that has userroles ["admin"]

iex> NavigationTree.Agent.as_html ["admin"], :bootstrap
  """
    <ul class="nav navbar-nav">
      <li><a href="/auth">Login</a></li>
      <li class="dropdown">
        <a href="#">Admin</a>
        <ul class="dropdown-menu">
          <li><a href="/admin/roles">Roles</a></li>
        </ul>
      </li>
    </ul>
  """

Summary

as_html(roles, atom2)

Return an HTML string suitable to fit in a navbar in a Twitter/Bootstrap environment

current_node(conn)

Convenience method to retrieve nearest_node_of current connection’s path

get()
init_opts(root_node)
nearest_node_of(path_info)

Same as node_of( url )m but tries to successively cut away parts from the end to find a node

next_sibling(path)

Returns the next sibling (next child of parent) or nil

next_sibling(list1, num)
next_sibling(path, num, op \\ :erlang.make_fun(:erlang, :+, 2))
node_of(path)

Returns node at given path. Path must be a either

parent_of(node)

Returns node’s parent or nil

path_of(node)

Returns node path for given node or url

previous_sibling(path)

Returns the previous sibling (previous child of parent) or nil

previous_sibling(path, num)
start_link(root_node)
stop()

Functions

as_html(roles, atom2)

Return an HTML string suitable to fit in a navbar in a Twitter/Bootstrap environment.

  • roles must be a list of user roles (strings)
  • atom2 must be either
  • :bootstrap (currently the only supported framework)
  • a module implementing tree_to_html( tree, roles )
current_node(conn)

Convenience method to retrieve nearest_node_of current connection’s path

get()
init_opts(root_node)
nearest_node_of(path_info)

Same as node_of( url )m but tries to successively cut away parts from the end to find a node.

Example: nearest_node_of(“/foo/bar/baz”) would return node_of(“/foo”) if there was a “/foo” node, but neither “/foo/bar” nor “/foo/bar/baz”.

next_sibling(path)

Returns the next sibling (next child of parent) or nil

next_sibling(list1, num)
next_sibling(path, num, op \\ :erlang.make_fun(:erlang, :+, 2))
node_of(path)

Returns node at given path. Path must be a either

  • A list of node names traversing the stree starting with root node’s name
  • An URL string. URL must be absolute e.g. “/admin/users”
parent_of(node)

Returns node’s parent or nil

path_of(node)

Returns node path for given node or url

previous_sibling(path)

Returns the previous sibling (previous child of parent) or nil

previous_sibling(path, num)
start_link(root_node)
stop()