Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #575 from yast/cwm_table
implement cwm table
- Loading branch information
Showing
7 changed files
with
209 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Simple example to demonstrate object API Table for CWM | ||
|
||
require_relative "example_helper" | ||
|
||
require "cwm/table" | ||
|
||
Yast.import "CWM" | ||
Yast.import "Directory" | ||
Yast.import "Wizard" | ||
|
||
class NiceTable < CWM::Table | ||
def header | ||
[ | ||
Center("name"), | ||
Right("surname"), | ||
"icon" | ||
] | ||
end | ||
|
||
def items | ||
[ | ||
[1, "Joe", "Doe", | ||
cell(icon(Yast::Directory.icondir + "/22x22/apps/yast-partitioning"))], | ||
[2, "Billy", "Kid", nil], | ||
[3, "Benny", nil, nil] | ||
] | ||
end | ||
|
||
def init | ||
self.value = 3 | ||
end | ||
end | ||
|
||
module Yast | ||
class ExampleDialog | ||
include Yast::I18n | ||
include Yast::UIShortcuts | ||
def run | ||
textdomain "example" | ||
|
||
table_widget = NiceTable.new | ||
|
||
contents = HBox( | ||
table_widget | ||
) | ||
|
||
Yast::Wizard.CreateDialog | ||
CWM.show(contents, caption: _("Table Example")) | ||
Yast.y2milestone("Selected item: #{table_widget.value.inspect}") | ||
Yast::Wizard.CloseDialog | ||
end | ||
end | ||
end | ||
|
||
Yast::ExampleDialog.new.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
require "abstract_method" | ||
require "cwm/custom_widget" | ||
|
||
module CWM | ||
# Represents Table widget | ||
class Table < CustomWidget | ||
# @method header | ||
# return array of String or Yast::Term which is used as headers for table | ||
# it can use e.g. align Left/Center/Right | ||
# @note It has to be overwritten | ||
# @return [Array<String|Yast::Term>] | ||
# | ||
# @example of header | ||
# def header | ||
# [ | ||
# _("First Name"), | ||
# Right(_("Surname") | ||
# ] | ||
# 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. | ||
# @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 | ||
# then in #init call #change_items method, so it will be filled when all widgets are at place | ||
# and just filling its content. | ||
# | ||
# @example for table with two columns | ||
# def items | ||
# [ | ||
# [:first_user, "Joe", "Doe"], | ||
# [:best_user, "Chuck", "Norris"] | ||
# ] | ||
# end | ||
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} | ||
def change_items(items_list) | ||
Yast::UI.ChangeWidget(Id(widget_id), :Items, format_items(items_list)) | ||
end | ||
|
||
# gets id of selected item in table or array of ids if multiselection option is used | ||
# @return [Array<Object>, Object] array if multiselection? return true | ||
def value | ||
val = Yast::UI.QueryWidget(Id(widget_id), :SelectedItems) | ||
multiselection? ? val : val.first | ||
end | ||
|
||
# sets id of selected item(-s) in table | ||
# @param id [Object, Array<Object>] selected id, if multiselection? is true | ||
# it require array of ids to select | ||
def value=(id) | ||
Yast::UI.ChangeWidget(Id(widget_id), :SelectedItems, Array[id]) | ||
end | ||
|
||
# Replaces content of single cell | ||
# @param id [Object] id of row ( the first element in #items ) | ||
# @param column_number [Integer] index of cell in row. Index start with 0 for first cell. | ||
# @param cell_content [String, Yast::Term, Object] Content of cell. Support same stuff as #items | ||
# @note more efficient for bigger tables then changing everything with #change_items | ||
def change_cell(id, column_number, cell_content) | ||
Yast::UI.ChangeWidget(Id(widget_id), Cell(id, column_number), cell_content) | ||
end | ||
|
||
# Resulting table as YUI term. | ||
# @note used mainly to pass it CWM. | ||
def contents | ||
opt_args = respond_to?(:opt, true) ? opt : [] | ||
Table( | ||
Id(widget_id), | ||
Opt(*opt_args), | ||
Header(*header), | ||
format_items(items) | ||
) | ||
end | ||
|
||
protected | ||
|
||
# helper to create icon term | ||
# @param path [String] path to icon | ||
def icon(path) | ||
Yast::Term.new(:icon, path) | ||
end | ||
|
||
# helper to create icon term | ||
# @param args content of cell, often used to combine icon and string | ||
# @note Please see difference between `Cell` and `cell`. The first one is | ||
# used in queries to pick exact cell, but later is used for content of cell. | ||
# For reasons ask libyui authors. | ||
# | ||
# @example | ||
# cell(icon("/tmp/cool_icon.png"), "Really cool!!!") | ||
def cell(*args) | ||
Yast::Term.new(:cell, *args) | ||
end | ||
|
||
# helper to say if table have multiselection | ||
def multiselection? | ||
return false unless respond_to?(:opt, true) | ||
|
||
opt.include?(:multiSelection) | ||
end | ||
|
||
private | ||
|
||
def format_items(items) | ||
items.map do |item| | ||
Item(Id(item.first), *item[1..-1]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#! /usr/bin/env rspec --format doc | ||
|
||
require_relative "test_helper" | ||
|
||
require "cwm/rspec" | ||
require "cwm/table" | ||
|
||
describe CWM::Table do | ||
class MyTable < CWM::Table | ||
def header | ||
["English", "Deutsch"] | ||
end | ||
|
||
def items | ||
[ | ||
[:one, "one", "eins"], | ||
[:two, "two", "zwei"] | ||
] | ||
end | ||
end | ||
subject { MyTable.new } | ||
|
||
include_examples "CWM::CustomWidget" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters