-
Notifications
You must be signed in to change notification settings - Fork 4
Salt form data #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Salt form data #22
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bedb781
Add a class to handle form data
imobachgs b32a8e6
Add missing form ids
imobachgs 251628b
Add a widget to group all form widgets
imobachgs 899a5f6
Improve collections listing
imobachgs 9f2e0b3
Implement Group#value=
imobachgs 902819f
The Select widget does not use an artificial ID anymore
imobachgs 2dbc628
Implement adding/removing collection items
imobachgs 7d2c4ae
Displays the data after closing the form's dialog
imobachgs 5265834
Rubocop and test fixes
imobachgs a293d35
Improve test coverage
imobachgs f96f523
Changes based on code review suggestions
teclator 284763b
Add Widgets::Boolean#id to match the other widgets
mvidner d58f93b
Fixed event ID in the collection widget test
teclator 68e29df
Changes based on code review suggestions
teclator 81d1c51
Fixed getting data for the main form
mvidner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -18,6 +18,7 @@ | |||||||
# find current contact information at www.suse.com. | ||||||||
|
||||||||
require "y2configuration_management/salt/form_builder" | ||||||||
require "y2configuration_management/salt/form_data" | ||||||||
require "y2configuration_management/widgets/form_popup" | ||||||||
|
||||||||
Yast.import "CWM" | ||||||||
|
@@ -38,29 +39,55 @@ class FormController | |||||||
# Constructor | ||||||||
# | ||||||||
# @param form [Y2ConfigurationManagement::Salt::Form] Form | ||||||||
# @param state [Hash] Current state (TODO) | ||||||||
def initialize(form, state = {}) | ||||||||
textdomain "configuration_management" | ||||||||
|
||||||||
@state = state # TODO | ||||||||
def initialize(form) | ||||||||
@data = FormData.new(form) | ||||||||
@form = form | ||||||||
end | ||||||||
|
||||||||
# Renders the main form's dialog | ||||||||
def show_main_dialog | ||||||||
show_dialog(form.root.name, form_builder.build(form.root.elements)) | ||||||||
Yast::Wizard.CreateDialog | ||||||||
Yast::CWM.show( | ||||||||
HBox(replace_point), | ||||||||
caption: form.root.name, next_handler: method(:next_handler) | ||||||||
) | ||||||||
ensure | ||||||||
Yast::Wizard.CloseDialog | ||||||||
end | ||||||||
|
||||||||
# Convenience method for returning the value of a given element | ||||||||
# | ||||||||
# @param path [String] Path to the element | ||||||||
def get(path) | ||||||||
@data.get(path) | ||||||||
end | ||||||||
|
||||||||
# Opens a new dialog in order to add a new element to a collection | ||||||||
# @todo | ||||||||
# | ||||||||
# @param path [String] Collection's path | ||||||||
def add(path) | ||||||||
element = form.find_element_by(path: path).prototype | ||||||||
show_popup(element.name, form_builder.build(element)) | ||||||||
result = show_popup(element.name, form_builder.build(element)) | ||||||||
return if result.nil? | ||||||||
@data.add(path, result.values.first) | ||||||||
refresh_main_form | ||||||||
end | ||||||||
|
||||||||
# Removes an element from a collection | ||||||||
# | ||||||||
# @param path [String] Collection's path | ||||||||
# @param index [Integer] Element's index | ||||||||
def remove(path, index) | ||||||||
@data.remove(path, index) | ||||||||
refresh_main_form | ||||||||
end | ||||||||
|
||||||||
private | ||||||||
|
||||||||
attr_reader :form, :state | ||||||||
# @return [Form] | ||||||||
attr_reader :form | ||||||||
# @return [FormData] | ||||||||
attr_reader :data | ||||||||
|
||||||||
# Returns the form builder | ||||||||
# | ||||||||
|
@@ -69,26 +96,45 @@ def form_builder | |||||||
@form_builder ||= Y2ConfigurationManagement::Salt::FormBuilder.new(self) | ||||||||
end | ||||||||
|
||||||||
# Displays a form dialog | ||||||||
# Renders the main form's dialog | ||||||||
def main_form | ||||||||
widget_form = form_builder.build(form.root.elements) | ||||||||
widget_form.value = get(form.root.path) | ||||||||
widget_form | ||||||||
end | ||||||||
|
||||||||
# Refreshes the main form content | ||||||||
def refresh_main_form | ||||||||
replace_point.replace(main_form) | ||||||||
end | ||||||||
|
||||||||
# Replace point to place the main dialog | ||||||||
# | ||||||||
# @param title [String] Dialog title | ||||||||
# @param contents [Array<CWM::AbstractWidget>] Popup content (as an array of CWM widgets) | ||||||||
def show_dialog(title, contents) | ||||||||
next_handler = proc { Yast::Popup.YesNo("Exit?") } | ||||||||
Yast::Wizard.CreateDialog | ||||||||
Yast::CWM.show( | ||||||||
VBox(*contents), caption: title, next_handler: next_handler | ||||||||
) | ||||||||
ensure | ||||||||
Yast::Wizard.CloseDialog | ||||||||
# @return [CWM::ReplacePoint] | ||||||||
def replace_point | ||||||||
@replace_point ||= ::CWM::ReplacePoint.new(widget: main_form) | ||||||||
end | ||||||||
|
||||||||
# Displays a popup | ||||||||
# | ||||||||
# @param title [String] Popup title | ||||||||
# @param contents [Array<CWM::AbstractWidget>] Popup content (as an array of CWM widgets) | ||||||||
def show_popup(title, contents) | ||||||||
Widgets::FormPopup.new(title, contents).run | ||||||||
# @param widget [Array<CWM::AbstractWidget>] Popup content (as an array of CWM widgets) | ||||||||
# @return [Hash,nil] Dialog's result | ||||||||
def show_popup(title, widget) | ||||||||
Widgets::FormPopup.new(title, widget).run | ||||||||
widget.result | ||||||||
end | ||||||||
|
||||||||
# @todo This version is just for debugging purposes. It should be replaced with a meaningful | ||||||||
# version. | ||||||||
def next_handler | ||||||||
return false unless Yast::Popup.YesNo("Do you want to exit?") | ||||||||
# This does not work. should main_form be memoized? | ||||||||
# main_form.store | ||||||||
# data.update(form.root.path, main_form.result) | ||||||||
data.update(form.root.path, main_form.store) | ||||||||
puts data.to_h.inspect | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
true | ||||||||
end | ||||||||
end | ||||||||
end | ||||||||
|
This file contains hidden or 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,122 @@ | ||
# Copyright (c) [2018] 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. | ||
|
||
module Y2ConfigurationManagement | ||
module Salt | ||
# This class holds data for a given Salt Formula Form | ||
# | ||
# @todo The support for collections is rather simple and nesting collections is not supported. | ||
# We might consider using JSON Patch to modify the data. | ||
class FormData | ||
PATH_DELIMITER = ".".freeze | ||
# @return [Y2ConfigurationManagement::Salt::Form] Form | ||
attr_reader :form | ||
|
||
# Constructor | ||
# | ||
# @param form [Y2ConfigurationManagement::Salt::Form] Form | ||
def initialize(form) | ||
@data = data_for_form(form) | ||
@form = form | ||
end | ||
|
||
# Returns the value of a given element | ||
# | ||
# @param path [String] Path to the element | ||
def get(path) | ||
@data.dig(*path_to_parts(path)) || default_for(path) | ||
end | ||
|
||
# Adds an element to a collection | ||
# | ||
# @param path [String] Path to the collection | ||
# @param value [Hash] Value to add | ||
def add(path, value) | ||
collection = get(path) | ||
collection.push(value) | ||
end | ||
|
||
# Updates an element's value | ||
# | ||
# @param path [String] Path to the collection | ||
# @param value [Object] New value | ||
def update(path, value) | ||
parts = path_to_parts(path) | ||
parent_parts = parts[0..-2] | ||
parent = @data | ||
parent = parent.dig(* parent_parts) unless parent_parts.empty? | ||
parent[parts.last] = value | ||
end | ||
|
||
# Removes an element from a collection | ||
# | ||
# @param path [String] Path to the collection | ||
# @param index [Integer] Position of the element to remove | ||
def remove(path, index) | ||
collection = get(path) | ||
collection.delete_at(index) | ||
end | ||
|
||
# Returns a hash containing the form data | ||
# | ||
# @return [Hash] | ||
def to_h | ||
@data | ||
end | ||
|
||
private | ||
|
||
# Default value for a given element | ||
# | ||
# @param path [String] Element path | ||
def default_for(path) | ||
element = form.find_element_by(path: path) | ||
element ? element.default : nil | ||
end | ||
|
||
# Split the path into different parts | ||
# | ||
# @param path [String] Element path | ||
def path_to_parts(path) | ||
path[1..-1].split(PATH_DELIMITER) | ||
end | ||
|
||
# Builds a hash to keep the form data | ||
# | ||
# @param form [Y2ConfigurationManagement::Salt::Form] | ||
# @return [Hash] | ||
def data_for_form(form) | ||
data_for_element(form.root) | ||
end | ||
|
||
# Builds a hash to keep the form element data | ||
# | ||
# @param element [Y2ConfigurationManagement::Salt::FormElement] | ||
# @return [Hash] | ||
def data_for_element(element) | ||
if element.is_a?(Container) | ||
defaults = element.elements.reduce({}) { |a, e| a.merge(data_for_element(e)) } | ||
{ element.id => defaults } | ||
else | ||
{ element.id => element.default } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.