Skip to content

Instantly share code, notes, and snippets.

@joseivanlopez
Last active March 6, 2020 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseivanlopez/f520017cb0effe70887a4447562b0339 to your computer and use it in GitHub Desktop.
Save joseivanlopez/f520017cb0effe70887a4447562b0339 to your computer and use it in GitHub Desktop.
# * [ok] Allow to select more than one item.
# * [ok] Multi state: no selected, selected, auto selected.
# * [ok] Enable/disable items.
# * [ok] Event over item title.
# * [ok] Automatic text wrapping.
# * [No] Auto-focus to the selected item.
# * [No] Event when moving with arrow up/down.
class MultiStateSelectionBox < CWM::RichText
def item(id)
items.find { |i| i.id == id }
end
def handle(event)
if event == over_checkbox_link
item = item(even.item_id)
toggle(item)
end
refresh
event
end
def refresh
# update html content based on items
end
def toggle(item)
item.toggle
end
def value
self.items
end
# @param v [Array<Item>]
def value=(v)
self.items = v
end
def content
# html based on items
end
class Item
abstract_method :id, :label, :status
module Status
SELECTED = :selected
UNSELECTED = :unselected
AUTO_SELECTED = :auto_selected
end
def toggle
selected? ? Status::UNSELECTED : Status::SELECTED
end
def selected?
status == Status::SELECTED
end
# rest of logic to generate the html for the item, etc
end
private
# @return [Array<Item>]
abstract_method :items
end
# In the dialog from yast-registration:
class AddonsSelector < CWM::MultiStateSelectionBox
def content
VBox(filter_widget, super, description_widget)
end
def filter_widget
@filter_widget ||= ...
end
def description_widget
@description_widget ||= DescriptionWidget.new
end
def handle(event)
case event
when over_name_link
item = item(event.id)
description_widget.value = item.description
when filter
self.value = calculate_items
end
super
end
private
def items
@items ||= Addons.find_all.each { |a| Item.new(a) }
end
def toggle(item)
item.toggle
calculate_dependencies
end
class Item < CWM::MultiStateSelectionBox::Item
def initialize(addon)
@addon = addon
end
def status
return Status::AUTO_SELECTED if @addon.auto_selected?
@addon.selected? ? Status::SELECTED : Status::UNSELECTED
end
end
end
# In the dialog from yast-packager
class ProductSelector < CWM::MultiStateSelectionBox
def content
VBox(super, description_widget)
end
def description_widget
@description_widget ||= DescriptionWidget.new
end
def handle(event)
if event == over_name_link
item = item(event.id)
description_widget.value = item.description
end
super
end
private
def items
@items ||= Finder.find ...
end
def toggle(item)
item.toggle
calculate_dependencies
end
class Item < CWM::MultiStateSelectionBox::Item
def initialize(product)
@product = product
end
def status
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment