-
Notifications
You must be signed in to change notification settings - Fork 19
/
master_detail_selector.rb
176 lines (144 loc) · 4.95 KB
/
master_detail_selector.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) [2020] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "yast"
require "cwm/custom_widget"
require "registration/widgets/checkbox_list"
require "registration/widgets/checkbox_item"
require "registration/widgets/item_details"
module Registration
module Widgets
# A custom widget displaying a multi selection list in a master-detail interface
#
# It relies on two child CWM::RichText widgets to show the list of selectable items (master) on
# top and the description (detail) for one of them at bottom.
#
# A controller is needed to manage the communication between all the pieces and keeps the
# selection logic and some configuration details such as the aspect relation or the availability
# of a filter. See {Registration::Controllers::AddonsSelection}
class MasterDetailSelector < CWM::CustomWidget
include Yast::Logger
include Yast::UIShortcuts
DEFAULT_MASTER_VWEIGHT = 60
private_constant :DEFAULT_MASTER_VWEIGHT
DEFAULT_DETAIL_VWEIGHT = 40
private_constant :DEFAULT_DETAIL_VWEIGHT
# Constructor
#
# @param controller [Registration::Controller::AddonsSelection]
def initialize(controller)
self.handle_all_events = true
@controller = controller
super()
end
# @macro seeAbstractWidget
def contents
VBox(
Left(filter),
master_content,
detail_content
)
end
# @macro seeAbstractWidget
def handle(event)
case event["ID"]
when :filter
controller.filter = Yast::UI.QueryWidget(Id(:filter), :Value)
# Reset the details box
item_details_widget.reset
# Refresh the list
checkbox_list_widget.update(checkbox_list_items)
when /#checkbox#/
id, click_on = event["ID"].split("#checkbox#")
item = controller.find_item(id)
# FIXME: this is not expected, but...
return unless item
# Show details
item_details_widget.enable
item_details_widget.update(item.description)
# All done if the user clicked on label
return if click_on == "label"
# Toggle selection
controller.toggle_item_selection(item)
# Refresh the list
checkbox_list_widget.update(checkbox_list_items)
end
nil
end
# @macro seeAbstractWidget
def help
CheckboxItem.help
end
private
attr_reader :controller
# Widget representing the filter when needed
#
# @return [Yast::Term] CheckBox if the filter should be included; Empty otherwise
def filter
return Empty() unless controller.include_filter?
CheckBox(Id(:filter), Opt(:notify), controller.filter_label, controller.filter)
end
# Returns the Yast::Term to fill the master area
#
# @return [Yast::Term]
def master_content
vweight = controller.master_options.fetch(:vweight, DEFAULT_MASTER_VWEIGHT)
VWeight(
vweight,
checkbox_list_widget
)
end
# Returns the Yast::Term to fill the detail area
#
# @return [Yast::Term]
def detail_content
vweight = controller.detail_options.fetch(:vweight, DEFAULT_DETAIL_VWEIGHT)
VWeight(
vweight,
item_details_widget
)
end
# Returns the widget to hold the selection list
#
# @return [Widgets::CheckboxList]
def checkbox_list_widget
@checkbox_list_widget ||=
CheckboxList.new(initial_content: checkbox_list_items)
end
# Returns the widget to display an item details
#
# @return [Widgets::ItemDetails]
def item_details_widget
@item_details_widget ||=
ItemDetails.new(placeholder: controller.detail_options[:placeholder])
end
# Builds the checkbox list
#
# @return [String]
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.enabled)
item = "<p>#{item}</p>" unless Yast::UI.TextMode
item
end
checkboxes.join(separator)
end
end
end
end