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.

Sidebar/content focus is driven entirely by the controller’s Focus object. Controllers that want Tab-driven sidebar navigation declare ‘focus_ring :sidebar, :content` (generated apps do); without those slots in the ring, `focus_sidebar`/`focus_content` are no-ops.

Instance Method Summary collapse

Instance Method Details

#content_focused?Boolean

True when focus is on the content side: any current slot other than :sidebar.

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/charming/controller/sidebar_navigation.rb', line 34

def content_focused?
  current = focus.current
  !current.nil? && current != :sidebar
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)


52
53
54
55
56
57
58
# File 'lib/charming/controller/sidebar_navigation.rb', line 52

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 side (the inverse of ‘focus_sidebar`). “Content” is the :content slot when the ring declares one, otherwise the first non-sidebar slot — so `focus_ring :sidebar, :entries` works without a literal :content.



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

def focus_content
  slot = content_slot
  focus.focus(slot) if slot
  render_default_action
end

#focus_sidebarObject

Moves focus to the sidebar slot and remembers the highlighted route.



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

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

True when the sidebar slot is the current focus target.

Returns:

  • (Boolean)


29
30
31
# File 'lib/charming/controller/sidebar_navigation.rb', line 29

def sidebar_focused?
  focused?(:sidebar)
end

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



41
42
43
# File 'lib/charming/controller/sidebar_navigation.rb', line 41

def sidebar_index
  session[:sidebar_index] || current_route_index
end

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



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

def sidebar_routes
  application.routes.all
end