-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdynamic_form.rb
71 lines (59 loc) · 1.98 KB
/
dynamic_form.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
require 'glimmer-dsl-libui'
class User
ALL_ATTRIBUTES = [:first_name, :last_name, :email, :street, :city, :state, :zip_code, :country]
attr_accessor :customizable_attributes, *ALL_ATTRIBUTES
def initialize
# allow customizing all attributes by default
self.customizable_attributes = ALL_ATTRIBUTES.dup
end
def select_customizable_attribute(attribute, selected)
if selected
customizable_attributes.push(attribute)
else
customizable_attributes.delete(attribute)
end
customizable_attributes.sort_by! {|attribute| User::ALL_ATTRIBUTES.index(attribute)}
end
end
class DynamicForm
include Glimmer::LibUI::Application
before_body do
@user = User.new
end
body {
window('Dynamic Form') {
margined true
vertical_box {
horizontal_box {
User::ALL_ATTRIBUTES.each do |attribute|
checkbox(attribute.to_s) {
checked <=> [@user, :customizable_attributes,
on_read: -> (attributes) { @user.customizable_attributes.include?(attribute) },
on_write: -> (checked_value) { @user.select_customizable_attribute(attribute, checked_value) }
]
}
end
}
form {
stretchy false
# Control content data-binding allows dynamically changing content based on changes in a model attribute
content(@user, :customizable_attributes) {
@user.customizable_attributes.each do |attribute|
entry {
label attribute.to_s.split('_').map(&:capitalize).join(' ')
text <=> [@user, attribute]
}
end
}
}
button('Summarize') {
on_clicked do
summary = @user.customizable_attributes.map { |attribute| @user.send(attribute) }.join(', ')
msg_box('Summary', summary)
end
}
}
}
}
end
DynamicForm.launch