For the newer class, you should attach the callback before you show the dialog.
Also, code should not be within the Menu#add_item block. This is because you cannot change it without closing and restarting SketchUp. It is better to put the code within a command method, and then have the block just call the method. You can then make changes to the method code and reload your Ruby code to redefine the method without restarting SketchUp.
The new dialog class should not use the the “skp:” protocol.
(By the way, for the old WebDialog class, the “=” should have been a “@” character.)
It should use the sketchupJavascript object instead. Ex:
sketchup.send(auswahl);
You can also pass JS Objects over to Ruby and they will automatically be converted into a Ruby Hash object. Or you can pass a JS Array and it will be converted to a Ruby Array object.
(2) The HTML is not showing correctly. We do not know what element had id “meineAuswahl”.
(3) Your HTML is not proper, well-formed HTML code.
(4) Also the statement in the callback: auswahl = params["send"] implies you want to receive a hash. Therefore, your Javascript must create a JS object for passing back to Ruby. If you simply pass a string value back them params will be the value by itself.
module StanAMS
module Test
extend self
@dialog = nil
def attach_callbacks(dialog)
dialog.add_action_callback("send") do |dialog, params|
# params is now a Ruby hash:
puts "Callback 'send' aufgerufen"
auswahl = params["auswahl"]
puts "Auswahl vom HTML-Menu: #{auswahl}"
end
end
def dialog
dialog = UI::HtmlDialog.new
dialog.set_html <<-HTML
<!DOCTYPE html>
<html>
<head>
<script>
function senden() {
// Create a JS Object to send to a Ruby Hash:
var params = {
auswahl: document.getElementById('meineAuswahl').value
};
sketchup.send(params);
};
</script>
</head>
<body>
Waehle eine Option
<!-- show correct HTML here -->
</body>
</html>
HTML
attach_callbacks(dialog)
dialog
end
def show_dialog
if @dialog
if @dialog.visible?
@dialog.bring_to_front
else
attach_callbacks(@dialog)
@dialog.show
end
else
@dialog = dialog()
@dialog.show
end
end
unless defined?(@loaded)
menu = UI.menu('Plugins')
menu.add_item('StanAMS: TEST DIALOG HTML)') {
show_dialog()
}
end
end
end