Pages

SketchUp WebDialog & Javascript Library

I was wondering how to create a library of common functions that SketchUp-Ruby coders could use in their WebDialogs.

The example I have attempted to create is a width method for WebDialog - it returns the current width of the WebDialog window.

This is what I came up with. I am hoping someone with a higher understanding of Ruby/SketchUp/JavaScript can help me clean it up, and point me in the right direction.

w.rb



class UI::WebDialog
@_width = 1
def width
puts "executing getWidth()"
execute_script("getWidth();")
@_width
end
end

# Extend the Dialog s
def ex(s)
def s.setWidth(v)
@_width = v
end
s.add_action_callback("width") {|d, a| puts "width callback called."; s.setWidth a }
end


# Create a new dialog
w = UI::WebDialog.new

# Extend the dialog (w) to include a .width method.
ex(w)


html = %[
<html>
<head>
<script>
function getWidth() {
window.location = "skp:width@" + document.body.offsetWidth;
}
</script>
</head>
<body>
<button onClick="window.location='skp:test'">Click</button>
</body>
</html>
]


##########################################
# Everything above here would be in
# separate files and required as needed.
# So your main script would be small, and
# all the definitions forthe extended
# functionality would be hidden.
#

# Set the html file for the dialog
# f = File.dirname(__FILE__) + "/w.html"
#w.set_file( f )
w.set_html( html )

# add a simple test callback
w.add_action_callback("test") { |d, a|
print "The width of this dialog is: "
p w.width
}

# show the dialog
w.show

No comments:

Post a Comment