Skip to content

Commit

Permalink
Merge pull request #1111 from ancorgs/cwm_nested_table
Browse files Browse the repository at this point in the history
Support for nested items in CWM::Table
  • Loading branch information
ancorgs committed Oct 15, 2020
2 parents b1bd537 + 59fd7b2 commit 82f2cc7
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 14 deletions.
47 changes: 45 additions & 2 deletions library/cwm/examples/object_api_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,47 @@ def init
end
end

class NestedTable < CWM::Table
def header
[
"Device",
"Size",
"Type"
]
end

def items
[
CWM::TableItem.new(:sda, ["/dev/sda", "931.5G", "Disk"], children: sda_items),
CWM::TableItem.new(:sdb, ["/dev/sdb", "900.0G", "Disk"]),
[:sdc, "/dev/sdc", "521.5G", "Disk"],
item(:sdd, ["/dev/sdd", "0.89T", "Disk"], children: sdd_items, open: false)
]
end

private

def sda_items
[
[:sda1, "sda1", "97.7G", "Ntfs Partition"],
[:sda2, "sda2", "833.9G", "Ext4 Partition"]
]
end

def sdd_items
[
CWM::TableItem.new(:sdd1, ["sdd1", "0.89T", "BtrFS Partition"], children: sdd1_items)
]
end

def sdd1_items
[
item(:home, ["@/home", "", "BtrFS Subvolume"]),
item(:opt, ["@/opt", "", "BtrFS Subvolume"])
]
end
end

module Yast
class ExampleDialog
include Yast::I18n
Expand All @@ -39,9 +80,11 @@ def run
textdomain "example"

table_widget = NiceTable.new
nested_table_widget = NestedTable.new

contents = HBox(
table_widget
contents = VBox(
table_widget,
nested_table_widget
)

Yast::Wizard.CreateDialog
Expand Down
4 changes: 2 additions & 2 deletions library/cwm/src/lib/cwm/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@
end

describe "#items" do
it "produces an array of arrays" do
it "produces a collection with arrays and/or TableItem objects" do
expect(subject.items).to be_an Enumerable
subject.items.each do |item|
expect(item).to be_a Array
expect(item).to be_a(Array).or be_a(CWM::TableItem)
end
end
end
Expand Down
72 changes: 63 additions & 9 deletions library/cwm/src/lib/cwm/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@
require "cwm/custom_widget"

module CWM
# An entry of a {Table}
class TableItem
# @return [String, Symbol]
attr_reader :id

# @return [Array]
attr_reader :values

# @see Table#items
# @return [Array<TableItem, Array>]
attr_reader :children

# @return [Boolean] whether children are initially expanded
attr_reader :open

def initialize(id, values, open: true, children: [])
@id = id
@values = values
@open = open
@children = children
end
end

# Represents Table widget
class Table < CustomWidget
# @method header
Expand All @@ -19,10 +42,15 @@ class Table < CustomWidget
# end
abstract_method :header

# gets initial two dimensional array of Table content
# one element in the first dimension contain as first element id and then
# rest is data in table, which can be e.g. terms. Then it have to be enclosed in
# `cell` term.
# Array with the table content
#
# Each element can be a {TableItem} or an Array. The helper {#item} can be used to easily
# create {TableItem} objects.
#
# If it's an array, the row contains no nested elements. The first element represents
# the id of the row and the rest is used as data for its cells. Those can be e.g. terms.
# Then it has to be enclosed in `cell` term.
#
# @see for more complex example see examples directory
# @note default value is empty array. It is useful when computation expensive content
# need to be set. In such case, it is better to keep empty items to quickly show table and
Expand All @@ -32,18 +60,27 @@ class Table < CustomWidget
# @example for table with two columns
# def items
# [
# [:first_user, "Joe", "Doe"],
# item(:first_user, ["Joe", "Doe"], children: first_children),
# [:best_user, "Chuck", "Norris"]
# ]
# end
#
# def first_children
# [
# [:first_sub1, "SubJoe1", "Doe"],
# [:first_sub2, "SubJoe2", "Doe"]
# ]
# end
#
# @return [Array<TableItem, Array>]
def items
[]
end

# change list on fly with argument. Useful when content of widget is changed.
# @note items and change_items is consistent with ItemsSelection mixin, just format of
# items is different due to nature of Table content.
# @param items_list [Array<Array<Object>>] same format as {#items}
# @param items_list [Array] same format as {#items}
def change_items(items_list)
Yast::UI.ChangeWidget(Id(widget_id), :Items, format_items(items_list))
end
Expand Down Expand Up @@ -109,6 +146,16 @@ def cell(*args)
Yast::Term.new(:cell, *args)
end

# helper to create a {TableItem}
# @param args content of item, see {TableItem#initialize}
# @note Not to be confused with the UI shortcut `Item`
#
# @example
# item(:joe, ["Joe", "Doe"], children: [[:joeson, "Joeson", "Doe"]], open: false)
def item(*args)
TableItem.new(*args)
end

# helper to say if table have multiselection
def multiselection?
return false unless respond_to?(:opt, true)
Expand All @@ -119,9 +166,16 @@ def multiselection?
private

def format_items(items)
items.map do |item|
Item(Id(item.first), *item[1..-1])
end
items.map { |i| format_item(i) }
end

# @see #format_items
def format_item(item)
return Item(Id(item.first), *item[1..-1]) if item.is_a?(Array)

children = format_items(item.children)
open_tag = item.open ? :open : :closed
Item(Id(item.id), *item.values, children, open_tag)
end
end
end
2 changes: 1 addition & 1 deletion library/cwm/test/table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def header
def items
[
[:one, "one", "eins"],
[:two, "two", "zwei"]
item(:two, ["two", "zwei"])
]
end
end
Expand Down

0 comments on commit 82f2cc7

Please sign in to comment.