Skip to content

Commit

Permalink
Add support for disabled items
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Feb 27, 2020
1 parent 96b4bc7 commit 3fde0e8
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 33 deletions.
19 changes: 16 additions & 3 deletions src/lib/registration/controllers/addons_selection.rb
Expand Up @@ -35,7 +35,7 @@ class << self
end

# A checkbox item representation, see {Widgets::CheckboxItem}
Item = Struct.new(:id, :label, :status, :description)
Item = Struct.new(:id, :label, :status, :description, :enabled)

# Constructor
#
Expand Down Expand Up @@ -199,9 +199,11 @@ def supported_addon_count?
def item_for(addon)
Item.new(
item_id_for(addon),
addon.label,
item_label_for(addon),
addon.status,
"<h3>#{addon.friendly_name}</h3>#{addon.description}"
"<h3>#{addon.friendly_name}</h3>#{addon.description}",
# the item will be enabled if the addon is selected, auto-selected or available
addon.selected? || addon.auto_selected? || addon.available?
)
end

Expand Down Expand Up @@ -244,6 +246,17 @@ def item_id_for(addon)
"#{addon.identifier}-#{addon.version}-#{addon.arch}"
end

# Build the Item label based on the addon availability
#
# @return [String] the addon label with the "not available" suffix when proceed
def item_label_for(addon)
if addon.available?
addon.label
else
_("%s (not available)") % addon.label
end
end

# Pre-selects recommended addons if there is none selected/registered yet
def preselect_recommended
if Addon.selected.empty? && Addon.registered.empty?
Expand Down
54 changes: 37 additions & 17 deletions src/lib/registration/widgets/checkbox_item.rb
Expand Up @@ -23,8 +23,6 @@ module Registration
module Widgets
# A plain Ruby object in charge to build an item "checkbox" representation to be used by
# {MasterDetailSelector} widget in a RichText widget.
#
# FIXME: give support for disabled items; see #icon
class CheckboxItem
extend Yast::I18n

Expand All @@ -41,8 +39,8 @@ class CheckboxItem
IMAGES = {
"inst:[a]:enabled" => "auto-selected.svg",
"inst:[x]:enabled" => "inst_checkbox-on.svg",
"inst:[x]:disabled" => "inst_checkbox-on-disabled.svg",
"inst:[ ]:enabled" => "inst_checkbox-off.svg",
"inst:[x]disabled" => "inst_checkbox-on-disabled.svg",
"inst:[ ]:disabled" => "inst_checkbox-off-disabled.svg",
"normal:[a]:enabled" => "auto-selected.svg",
"normal:[x]:enabled" => "checkbox-on.svg",
Expand Down Expand Up @@ -93,10 +91,12 @@ def self.icon_for(status)
# @param id [String, Integer] the representing the item
# @param text [String] the text to be displayed
# @param status [String, Symbol] the item status
def initialize(id, text, status)
# @param enabled [Boolean] if the item should be enabled or not
def initialize(id, text, status, enabled = true)
@id = id
@text = text
@status = status
@enabled = enabled
end

# Returns the checkbox representation for the item
Expand All @@ -116,28 +116,36 @@ def icon
value
else
# an image key looks like "inst:[a]:enabled"
image_key = [mode, value, "enabled"].join(":")
image_key = [mode, value, state].join(":")

"<img src=\"#{IMAGES_DIR}/#{IMAGES[image_key]}\">"
end
end

private

attr_reader :id, :text, :status
attr_reader :id, :text, :status, :enabled

# Builds the checkbox input representation
#
# @return [String]
def checkbox
"<a href=\"#{id}#checkbox#input\" style=\"#{text_style}\">#{icon}</a>"
if enabled
"<a href=\"#{id}#checkbox#input\" style=\"#{text_style}\">#{icon}</a>"
else
"<span style\"#{text_style}\">#{icon}</a>"
end
end

# Builds the checkbox label representation
#
# @return [String]
def label
"<a href=\"#{id}#checkbox#label\" style=\"#{text_style}\">#{text}</a>"
if enabled
"<a href=\"#{id}#checkbox#label\" style=\"#{text_style}\">#{text}</a>"
else
"<span style\"#{text_style}\">#{text}</a>"
end
end

# Returns the status string representation
Expand All @@ -156,25 +164,37 @@ def value
end
end

# Returns style rules for the text
#
# @return [String] the status text representation
def text_style
"text-decoration: none; color: #{color}"
end

# Returns the current mode
#
# @return [String] "normal" in a running system; "inst" during the installation
def mode
installation? ? "inst" : "normal"
end

# Returns the current input state
#
# @return [String] "enabled" when item must be enabled; "disabled" otherwise
def state
enabled ? "enabled" : "disabled"
end

# Returns style rules for the text
#
# @return [String] the status text representation
def text_style
"text-decoration: none; color: #{color}"
end

# Determines the color for the text
#
# @return [String] "black" in a running system; "white" during the isntallation
# @return [String] "grey" for a disabled item;
# "white" when enabled and running in installation mode;
# "black" otherwise
def color
installation? ? "white" : "black"
return "grey" unless enabled
return "white" if installation?

"black"
end

# Determines whether running in installation mode
Expand Down
2 changes: 1 addition & 1 deletion src/lib/registration/widgets/master_detail_selector.rb
Expand Up @@ -164,7 +164,7 @@ def checkbox_list_items
separator = Yast::UI.TextMode ? "<br />" : ""

checkboxes = controller.items.map do |item|
item = CheckboxItem.new(item.id, item.label, item.status)
item = CheckboxItem.new(item.id, item.label, item.status, item.enabled)
item = "<p>#{item}</p>" unless Yast::UI.TextMode
item
end
Expand Down
49 changes: 49 additions & 0 deletions test/registration/controllers/addons_selection_test.rb
Expand Up @@ -128,6 +128,55 @@
end
end

describe "#item_for" do
let(:addon) { Registration::Addon.new(addon_generator) }
let(:item) { subject.send(:item_for, addon) }

before do
allow(addon).to receive(:available?).and_return(available)
allow(addon).to receive(:selected?).and_return(selected)
allow(addon).to receive(:auto_selected?).and_return(auto_selected)
end

let(:available) { false }
let(:selected) { false }
let(:auto_selected) { false }

context "when an available addon is given" do
let(:available) { true }

it "returns an enabled item" do
expect(item.enabled).to eq(true)
end
end

context "when a not available addon is given" do
it "returns a not enabled item" do
expect(item.enabled).to eq(false)
end

it "includes the 'not available' suffix in the label" do
expect(item.label).to match(/not available/)
end

context "but it is selected" do
let(:selected) { true }

it "returns an enabled item" do
expect(item.enabled).to eq(true)
end
end

context "but it is auto-selected" do
let(:auto_selected) { true }

it "returns an enabled item" do
expect(item.enabled).to eq(true)
end
end
end
end

describe "#find_item" do
let(:item) { subject.items.first }

Expand Down
46 changes: 35 additions & 11 deletions test/registration/widgets/checkbox_item_test.rb
Expand Up @@ -22,34 +22,58 @@
require "registration/widgets/checkbox_item"

describe Registration::Widgets::CheckboxItem do
subject { described_class.new(item.id, item.label, item.status) }
subject { described_class.new(item.id, item.label, item.status, item.enabled) }

let(:item) do
double(
"Item",
id: "whatever",
label: "Text uses as label",
status: status
id: "whatever",
label: "Text uses as label",
status: status,
enabled: enabled
)
end

let(:status) { :selected }
let(:enabled) { true }

describe "#to_s" do
it "returns a string" do
expect(subject.to_s).to be_a(String)
end

it "includes a link for the input" do
expect(subject.to_s).to match(/.*href="whatever#checkbox#input".*/)
end
context "when the item is enabled" do
it "includes a link for the input" do
expect(subject.to_s).to match(/.*href="whatever#checkbox#input".*/)
end

it "includes a link for the label" do
expect(subject.to_s).to match(/.*href="whatever#checkbox#label".*/)
end

it "includes a link for the label" do
expect(subject.to_s).to match(/.*href="whatever#checkbox#label".*/)
it "includes the item label" do
expect(subject.to_s).to include(item.label)
end
end

it "includes the item label" do
expect(subject.to_s).to include(item.label)
context "when the item is not enabled" do
let(:enabled) { false }

it "uses a grey color" do
expect(subject.to_s).to match(/.*color: grey.*/)
end

it "includes the item label" do
expect(subject.to_s).to include(item.label)
end

it "does not include a link for the input" do
expect(subject.to_s).to_not match(/.*href="whatever#checkbox#input".*/)
end

it "does not include a link for the label" do
expect(subject.to_s).to_not match(/.*href="whatever#checkbox#label".*/)
end
end

context "when running in text mode" do
Expand Down
8 changes: 7 additions & 1 deletion test/registration/widgets/master_detail_selector_test.rb
Expand Up @@ -40,7 +40,13 @@
end

let(:item) do
double(id: 1, label: "Fake item", status: :whatever, description: "Fake addon item")
double(
id: 1,
label: "Fake item",
status: :whatever,
description: "Fake addon item",
enabled: true
)
end

let(:include_filter) { true }
Expand Down

0 comments on commit 3fde0e8

Please sign in to comment.