HTML via RUBY

hello,
i am trying to learn, how to create a html-menue called from skp-ruby
with a callback

and simple output of the input (selection) …..for the beginning.

i tried this code,
callback happens, but no data comes in (second puts).

is this a corract approach ?

thanx

stan

code:

```ruby

menu.add_item(‘XXXXXXXXXXXXXXXXXXX TEST DIALOG HTML)’) {

dialog = UI::HtmlDialog.new
dialog.set_html <<-HTML

Waehle eine Option

UEbertragen
  <script>
    function senden() {
      var auswahl = document.getElementById('meineAuswahl').value;
      window.location = 'skp:send=' + auswahl;
    }
  </script>
</body>
HTML

dialog.show

puts “Callback aufgerufen”

dialog.add_action_callback(“send”) do |dialog, params|
auswahl = params[“send”]

puts “Auswahl vom HTML-Menu: #{auswahl}”

end

}

```

A similar topic was started a few days ago:

__
Would you please so kind post your code properly, eg according to this post:

[How to] Post correctly formatted and colorized code on the forum


I put the topic in the Ruby API category, because that’s where it belongs.

1 Like

NO. You are convoluting the deprecated UI::WebDialog and the new UI::HtmlDialog classes.

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.

hi dan, thanx for your advice.

which part of the code refers to the deprecated ui::webdialogue ?

i moved the code from the menu to a new definition.

stan

  window.location = 'skp:send=' + auswahl;

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.

thanx so much, but it is so tricky. i fire the function “senden” with the value ‘meine auswahl’ in html,

but the callback for “senden” just do not realize this data….

i get the info by “puts callback aufgerufen”, but no data from the returned value.

such a beginner….

stan

(1) Your original post must be corrected.

(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