Module: Charming::Controller::SidebarNavigation

Included in:
Charming::Controller
Defined in:
lib/charming/controller/sidebar_navigation.rb

Overview

Sidebar-navigation helpers mixed into Controller. Tracks the sidebar’s current route index, routes j/k/enter/tab keys when the sidebar is focused, and exposes ‘sidebar_focused?` for views.

Instance Method Summary collapse

Instance Method Details

#content_focused?Boolean

True when the content pane is the current focus target. Uses the focus ring when defined.

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/charming/controller/sidebar_navigation.rb', line 38

def content_focused?
  return focused?(:content) if focus_ring_slot?(:content)

  session[:focus] == :content
end

#current_route?(candidate) ⇒ Boolean

True when candidate route matches the controller’s currently active route (used to highlight the current row in the sidebar).

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/charming/controller/sidebar_navigation.rb', line 57

def current_route?(candidate)
  return candidate.controller_class == self.class && candidate.action == :show unless route

  candidate.path == route.path &&
    candidate.controller_class == route.controller_class &&
    candidate.action == route.action
end

#focus_contentObject

Moves focus to the content pane (the inverse of ‘focus_sidebar`).



21
22
23
24
25
26
27
28
# File 'lib/charming/controller/sidebar_navigation.rb', line 21

def focus_content
  if focus_ring_slot?(:content)
    focus.focus(:content)
  else
    session[:focus] = :content
  end
  render_default_action
end

#focus_sidebarObject

Moves focus to the sidebar. When the controller declared a focus ring, the focus object is updated; otherwise a fallback session key tracks focus.



10
11
12
13
14
15
16
17
18
# File 'lib/charming/controller/sidebar_navigation.rb', line 10

def focus_sidebar
  if focus_ring_slot?(:sidebar)
    focus.focus(:sidebar)
  else
    session[:focus] = :sidebar
  end
  session[:sidebar_index] ||= current_route_index
  render_default_action
end

True when the sidebar is the current focus target. Uses the focus ring when defined.

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/charming/controller/sidebar_navigation.rb', line 31

def sidebar_focused?
  return focused?(:sidebar) if focus_ring_slot?(:sidebar)

  session[:focus] == :sidebar
end

Returns the index of the currently selected route in ‘sidebar_routes`, defaulting to the active route when the session index is unset.



46
47
48
# File 'lib/charming/controller/sidebar_navigation.rb', line 46

def sidebar_index
  session[:sidebar_index] || current_route_index
end

Returns all routes from the application’s router, in registration order.



51
52
53
# File 'lib/charming/controller/sidebar_navigation.rb', line 51

def sidebar_routes
  application.routes.all
end