Class: CWM::UIState

Inherits:
Object
  • Object
show all
Includes:
Yast::I18n
Defined in:
library/cwm/src/lib/cwm/ui_state.rb

Overview

Singleton class to keep the position of the user in the UI and other similar information that needs to be rememberd across UI redraws to give the user a sense of continuity.

If you want to see it in action, have a look at yast2-storage-ng or yast2-firewall modules.

Examples:

Defining a UIState class to handle firewall zones (extracted from yast2-firewall)


require "cwm/ui_state"
class MyUIState < CWM::UIState
  def go_to_tree_node(page)
    super
    self.candidate_notes =
      if page.respond_to?(:zone)
        zone_page_candidates(page)
      else
        [page.label]
      end
    end
  end

private

  def zone_page_candidates(page)
    [page.zone.name]
  end
end

Using the UIState from an CWM::TreePager


class OverviewTreePager < CWM::TreePager
  # Overrides default behaviour to register the new state.
  def switch_page(page)
    MyUIState.instance.go_to_tree_node(page)
    super
  end

  # Ensures the tree is properly initialized according to the UI state after
  # a redraw
  def initial_page
    MyUIState.instance.find_tree_node(@pages) || super
  end
end

Registering the selected row from a CWM::Table


class MyTable < ::CWM::Table
  def opt
    [:notify, :immediate]
  end

  def handle(event)
    MyUIState.instance.select_row(value) if event["EventReason"] == "SelectionChanged"
    nil
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUIState

Constructor

Called through create_instance, starts with a blank situation (which means default for each widget will be honored).



88
89
90
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 88

def initialize
  @candidate_nodes = []
end

Instance Attribute Details

#candidate_nodesArray<Integer, String> (protected)

Where to place the user within the general tree in next redraw

Returns:

  • (Array<Integer, String>)


163
164
165
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 163

def candidate_nodes
  @candidate_nodes
end

#row_idObject

See Also:



155
156
157
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 155

def row_id
  @row_id
end

#tabString? (protected)

Concrete tab within the current node to show in the next redraw

Returns:

  • (String, nil)


167
168
169
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 167

def tab
  @tab
end

Class Method Details

.create_instanceObject

Enforce a new clean instance



194
195
196
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 194

def create_instance
  @instance = new
end

.instanceObject

Singleton instance



188
189
190
191
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 188

def instance
  create_instance unless @instance
  @instance
end

Instance Method Details

#find_tab(pages) ⇒ CWM::Page?

Select the tab to open within the node after a redraw

Parameters:

  • pages (Array<CWM::Page>)

    pages for all the possible tabs

Returns:



146
147
148
149
150
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 146

def find_tab(pages)
  return nil unless tab

  pages.find { |page| page.label == tab }
end

#find_tree_node(pages) ⇒ CWM::Page?

Select the page to open in the general tree after a redraw

Parameters:

  • pages (Array<CWM::Page>)

    all the pages in the tree

Returns:



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 128

def find_tree_node(pages)
  candidate_nodes.each.with_index do |candidate, idx|
    result = pages.find { |page| matches?(page, candidate) }
    if result
      # If we had to use one of the fallbacks, the tab name is not longer
      # trustworthy
      self.tab = nil unless idx.zero?
      return result
    end
  end
  self.tab = nil
  nil
end

#go_to_tree_node(page) ⇒ Object

Method to be called when the user decides to visit a given page by clicking in one node of the general tree.

It remembers the decision so the user is taken back to a sensible point of the tree (very often the last he decided to visit) after redrawing.

Parameters:

  • page (CWM::Page)

    associated to the tree node



99
100
101
102
103
104
105
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 99

def go_to_tree_node(page)
  self.candidate_nodes = [page.label]

  # Landing in a new node, so invalidate previous details about position
  # within a node, they no longer apply
  self.tab = nil
end

#matches?(page, candidate) ⇒ Boolean (protected)

Whether the given page matches with the candidate tree node

Parameters:

  • page (CWM::Page)
  • candidate (Integer, String)

Returns:

  • (Boolean)

    boolean



182
183
184
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 182

def matches?(page, candidate)
  page.label == candidate
end

#select_row(row_id) ⇒ Object

Method to be called when the user operates in a row of a table.

Parameters:

  • row_id (Object)

    row identifier



120
121
122
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 120

def select_row(row_id)
  self.row_id = row_id
end

#switch_to_tab(page) ⇒ Object

Method to be called when the user switches to a tab within a tree node.

It remembers the decision so the same tab is showed in case the user stays in the same node after redrawing.

Parameters:



113
114
115
# File 'library/cwm/src/lib/cwm/ui_state.rb', line 113

def switch_to_tab(page)
  self.tab = page.label
end