Class: Yast::DialogTreeClass

Inherits:
Module
  • Object
show all
Defined in:
library/cwm/src/modules/DialogTree.rb

Instance Method Summary collapse

Instance Method Details

#AdjustButtons(buttons) ⇒ Object

Adjust buttons at the bottom of the dialog

Parameters:

  • buttons (Hash{String => String})

    a map with keys "abort_button", "back_button" and "next_button" adn values labels of appropriate buttons



180
181
182
183
184
185
186
187
188
189
190
# File 'library/cwm/src/modules/DialogTree.rb', line 180

def AdjustButtons(buttons)
  buttons = deep_copy(buttons)
  CWM.AdjustButtons(
    Ops.get(buttons, "next_button") { Label.NextButton },
    Ops.get(buttons, "back_button") { Label.BackButton },
    Ops.get(buttons, "abort_button") { Label.AbortButton },
    Label.HelpButton
  )

  nil
end

#AdjustButtonsAny(buttons) ⇒ Object

Adjust buttons at the bottom of the dialog

Parameters:

  • buttons (Hash{String => Object})

    a map with keys "abort_button", "back_button" and "next_button" adn values labels of appropriate buttons, other keys with values of other types are possible



196
197
198
199
200
201
202
203
204
205
206
# File 'library/cwm/src/modules/DialogTree.rb', line 196

def AdjustButtonsAny(buttons)
  buttons = deep_copy(buttons)
  buttons2 = Convert.convert(
    Builtins.filter(buttons) { |k, _v| Builtins.issubstring(k, "_button") },
    from: "map <string, any>",
    to:   "map <string, string>"
  )
  AdjustButtons(buttons2)

  nil
end

#DialogTreeHandle(_key, event) ⇒ Symbol

Handle function of virtual DialogTree widget

Parameters:

  • key (String)

    string widget key

  • event (Hash)

    map event that caused handler call

Returns:

  • (Symbol)

    for wizard sequencer or nil



82
83
84
85
86
87
88
89
90
# File 'library/cwm/src/modules/DialogTree.rb', line 82

def DialogTreeHandle(_key, event)
  event = deep_copy(event)
  ret = Ops.get(event, "ID")

  ret = Convert.to_string(UI.QueryWidget(Id(:wizardTree), :CurrentItem)) if ret == :wizardTree
  @previous_screen = @selected_screen
  @selected_screen = Convert.to_string(ret)
  :_cwm_internal_tree_handle
end

#DialogTreeInit(_key) ⇒ Object

Init function of virtual DialogTree widget

Parameters:

  • key (String)

    string widget key



67
68
69
70
71
72
73
74
75
76
# File 'library/cwm/src/modules/DialogTree.rb', line 67

def DialogTreeInit(_key)
  if UI.WidgetExists(Id(:wizardTree))
    UI.ChangeWidget(Id(:wizardTree), :CurrentItem, @selected_screen)
    UI.SetFocus(Id(:wizardTree))
  else
    UI.WizardCommand(term(:SelectTreeItem, @selected_screen))
  end

  nil
end

#DrawScreen(current_screen, widget_descr, extra_widget, set_focus) ⇒ Object

Draw the screen related to the particular tree item extra_widget a map of the additional widget to be added at the end of the list of widgets

Parameters:

  • current_screen (Hash{String => Object})

    a map describing the current screen

  • widget_descr (Hash <String, Hash{String => Object>})

    a map describing all widgets that may be present in the screen

Returns:

  • a list of preprocessed widgets that appear in this dialog



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'library/cwm/src/modules/DialogTree.rb', line 118

def DrawScreen(current_screen, widget_descr, extra_widget, set_focus)
  current_screen = deep_copy(current_screen)
  widget_descr = deep_copy(widget_descr)
  extra_widget = deep_copy(extra_widget)
  widget_names = Ops.get_list(current_screen, "widget_names", [])
  contents = Ops.get_term(current_screen, "contents", VBox())
  caption = Ops.get_string(current_screen, "caption", "")

  w = CWM.CreateWidgets(widget_names, widget_descr)
  help = CWM.MergeHelps(w)
  contents = CWM.PrepareDialog(contents, w)
  Wizard.SetContentsFocus(caption, contents, help, true, true, set_focus)

  # add virtual widget
  w = Builtins.add(w, extra_widget)

  # return widgets of the dialog for further usage
  deep_copy(w)
end

#GetVirtualDialogTreeWidget(ids) ⇒ Hash

Get the map of the virtal left tree widget

Parameters:

  • ids (Array<String>)

    a list of widget ids of all tree items

Returns:

  • (Hash)

    tree of the widget



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'library/cwm/src/modules/DialogTree.rb', line 95

def GetVirtualDialogTreeWidget(ids)
  ids = deep_copy(ids)
  handle_events = deep_copy(ids)
  handle_events = Builtins.add(handle_events, :wizardTree)
  {
    "init"          => fun_ref(method(:DialogTreeInit), "void (string)"),
    "handle_events" => handle_events,
    "handle"        => fun_ref(
      method(:DialogTreeHandle),
      "symbol (string, map)"
    )
  }
end

#mainObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'library/cwm/src/modules/DialogTree.rb', line 33

def main
  Yast.import "UI"
  textdomain "base"

  Yast.import "CWM"
  Yast.import "Label"
  Yast.import "Wizard"

  # local data

  # Currently selected item in the tree
  @selected_screen = nil

  # Previously selected item in the tree
  @previous_screen = nil
end

#RestoreSelectedDialogObject

Restore the previously selected dialog after clicking another item not causing dialog change due to validation failed



52
53
54
55
56
57
58
59
60
61
# File 'library/cwm/src/modules/DialogTree.rb', line 52

def RestoreSelectedDialog
  @selected_screen = @previous_screen
  if UI.WidgetExists(Id(:wizardTree))
    UI.ChangeWidget(Id(:wizardTree), :CurrentItem, @selected_screen)
  else
    UI.WizardCommand(term(:SelectTreeItem, @selected_screen))
  end

  nil
end

#Run(settings) ⇒ Symbol

Generic function to create dialog and handle it's events. Run the event loop over the dialog with the left tree.


"screens" : map<string,map<string,any>> of all screens
            (key is screen ID, value is screen description map)
"widget_descr" : map<string,map<string,any>> description map of all widgets
"initial_screen" : string the id of the screen that should be displayed
                   as the first
"fallback" : map<any,any> initialize/save/handle fallbacks if not specified
             with the widgets, to be passed to CWM

Parameters:

  • setttings

    a map of settings of the dialog

Returns:

  • (Symbol)

    wizard sequencer symbol



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'library/cwm/src/modules/DialogTree.rb', line 221

def Run(settings)
  settings = deep_copy(settings)
  screens = Ops.get_map(settings, "screens", {})
  widget_descr = Ops.get_map(settings, "widget_descr", {})
  initial_screen = Ops.get_string(settings, "initial_screen", "")
  functions = Ops.get_map(settings, "functions", {})

  initial_screen = "" if initial_screen.nil?
  if initial_screen == ""
    Builtins.foreach(screens) do |k, _v|
      initial_screen = k if initial_screen == ""
    end
  end

  @selected_screen = initial_screen
  ids = Builtins.maplist(screens) { |k, _v| k }
  extra_widget = GetVirtualDialogTreeWidget(ids)

  w = DrawScreen(
    Ops.get(screens, @selected_screen, {}),
    widget_descr,
    extra_widget,
    true
  )

  ret = nil
  while ret.nil?
    CWM.SetValidationFailedHandler(
      fun_ref(method(:RestoreSelectedDialog), "void ()")
    )
    ret = CWM.Run(w, functions)
    CWM.SetValidationFailedHandler(nil)
    # switching scrren, dialog was validated and stored
    next if ret != :_cwm_internal_tree_handle

    toEval = Convert.convert(
      Ops.get(screens, [@selected_screen, "init"]),
      from: "any",
      to:   "symbol (string)"
    )
    tab_init = nil
    tab_init = toEval.call(@selected_screen) if !toEval.nil?
    if tab_init.nil? # everything OK
      w = DrawScreen(
        Ops.get(screens, @selected_screen, {}),
        widget_descr,
        extra_widget,
        false
      )
      ret = nil
    elsif tab_init == :refuse_display # do not display this screen
      @selected_screen = @previous_screen
      ret = nil # exit dialog
    else
      ret = tab_init
    end
  end
  ret
end

#RunAndHide(settings) ⇒ Symbol

Run the event loop over the dialog with the left tree. After finished, run UI::CloseDialog

Parameters:

  • setttings

    a map of settings of the dialog. See @Run for possible keys

Returns:

  • (Symbol)

    wizard sequencer symbol



285
286
287
288
289
290
# File 'library/cwm/src/modules/DialogTree.rb', line 285

def RunAndHide(settings)
  settings = deep_copy(settings)
  Run(settings)
ensure
  UI.CloseDialog
end

#ShowAndRun(settings) ⇒ Symbol

Display the dialog and run its event loop


"ids_order" : list<string> of IDs in the same order as they are expected
              to be in the left menu. Not used if "tree_creator" is defined
"tree_creator" : list<map>() a callback to a function that creates
                 the tree using Wizard::AddTreeItem and returns the
                 resulting tree
"back_button" : string label of the back button (optional)
"next_button" : string label of the next button (optional)
"abort_button" : string label of the abort button (optional)
See @RunAndHide for other possible keys in the map

Parameters:

  • setttings

    a map of settings of the dialog

Returns:

  • (Symbol)

    wizard sequencer symbol



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'library/cwm/src/modules/DialogTree.rb', line 306

def ShowAndRun(settings)
  settings = deep_copy(settings)
  ids_order = Ops.get_list(settings, "ids_order", [])
  screens = Ops.get_map(settings, "screens", {})
  tree_handler = Convert.convert(
    Ops.get(settings, "tree_creator"),
    from: "any",
    to:   "list <map> ()"
  )

  if tree_handler.nil?
    ShowFlat(ids_order, screens)
    if Ops.get_string(settings, "initial_screen", "") == ""
      Builtins.find(ids_order) do |s|
        Ops.set(settings, "initial_screen", s)
        true
      end
    end
  else
    ShowTree(tree_handler)
  end
  AdjustButtonsAny(settings)
  RunAndHide(settings)
end

#ShowFlat(ids_order, screens) ⇒ Object

Draw the dialog with the flat tree (only single level of the tree entries)

Parameters:

  • ids_order (Array<String>)

    a list of IDs in the same order as they are expected to be in the left menu

  • screens (Hash <String, Hash{String => Object>})

    map of all screens (key is screen ID, value is screen description map



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'library/cwm/src/modules/DialogTree.rb', line 143

def ShowFlat(ids_order, screens)
  ids_order = deep_copy(ids_order)
  screens = deep_copy(screens)
  Wizard.OpenTreeNextBackDialog
  tree = []
  Builtins.foreach(ids_order) do |i|
    tree = Wizard.AddTreeItem(
      tree,
      "",
      Ops.get_string(
        screens,
        [i, "tree_item_label"],
        Ops.get_string(screens, [i, "caption"], "")
      ),
      i
    )
  end
  Wizard.CreateTree(tree, "")

  nil
end

#ShowTree(tree_handler) ⇒ Object

Draw the dialog with multi-level tree

Parameters:

  • tree_handler (list <map> ())

    a callback to a function that creates the tree using Wizard::AddTreeItem and returns the resulting tree



168
169
170
171
172
173
174
175
# File 'library/cwm/src/modules/DialogTree.rb', line 168

def ShowTree(tree_handler)
  tree_handler = deep_copy(tree_handler)
  Wizard.OpenTreeNextBackDialog
  tree = tree_handler.call
  Wizard.CreateTree(tree, "")

  nil
end