Class: SKUI::Bridge
- Inherits:
-
Object
- Object
- SKUI::Bridge
- Defined in:
- src/SKUI/bridge.rb
Overview
Handles the communication between Ruby and the WebDialog.
Defined Under Namespace
Classes: CommunicationError
Instance Attribute Summary collapse
Instance Method Summary collapse
- #add_container(container) ⇒ Nil
- #add_control(control) ⇒ Nil
-
#call(function, *args) ⇒ Mixed
Wrapper to build a script string and return the return value of the called Javascript function.
-
#get_checkbox_state(ui_id) ⇒ String
Returns the checked state for the given Control.ui_id.
-
#get_checked_state(selector) ⇒ String
Returns the checked state for the given jQuery selector.
-
#get_control_rect(ui_id) ⇒ Hash
Returns the Rect for the given Control.
-
#get_control_value(ui_id) ⇒ String
(also: #get_element_value)
Returns the value for the given Control.
-
#get_html(selector) ⇒ String
Returns the HTML code for the given jQuery selector.
-
#get_text(selector) ⇒ String
Returns the text content for the given jQuery selector.
-
#get_value(selector) ⇒ String
It appear that under OSX UI::WebDialog.get_element_value doesn’t work for <TEXTAREA> and <SELECT> elements.
-
#initialize(window, webdialog) ⇒ Bridge
constructor
A new instance of Bridge.
- #release ⇒ Nil
Constructor Details
#initialize(window, webdialog) ⇒ Bridge
Returns a new instance of Bridge.
23 24 25 26 |
# File 'src/SKUI/bridge.rb', line 23 def initialize( window, webdialog ) @window = window @webdialog = webdialog end |
Instance Attribute Details
#webdialog ⇒ Object
17 18 19 |
# File 'src/SKUI/bridge.rb', line 17 def webdialog @webdialog end |
#window ⇒ Object
17 18 19 |
# File 'src/SKUI/bridge.rb', line 17 def window @window end |
Instance Method Details
#add_container(container) ⇒ Nil
92 93 94 95 96 97 98 99 100 101 |
# File 'src/SKUI/bridge.rb', line 92 def add_container( container ) # (?) Compile into one large function call, might it be faster to execute? for control in container.controls add_control( control ) if control.is_a?( ControlManager ) add_container( control ) end end nil end |
#add_control(control) ⇒ Nil
83 84 85 86 |
# File 'src/SKUI/bridge.rb', line 83 def add_control( control ) call( 'UI.add_control', control.properties ) nil end |
#call(function, *args) ⇒ Mixed
Wrapper to build a script string and return the return value of the called Javascript function.
This method also ensures a that the <SCRIPT> elements which UI::WebDialog.execute_script
leaves behind is cleaned up.
return_value = window.bridge.call('alert', 'Hello World')
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 72 73 74 75 76 77 |
# File 'src/SKUI/bridge.rb', line 41 def call( function, *args ) # A JavaScript command is prepared and sent to the JS bridge which then # evaluates it and puts the return value into a hidden <input> element # which is then pulled back from Ruby and evaluated as Ruby objects. # Ensure that we don't pull old data back from the WebDialog in case there # is stale data due to some previous error. @webdialog.execute_script( 'Bridge.reset()' ) # (!) SU-0415 # Reports of .execute_script might have a hard limit - possibly under OSX # only. Windows does seem unaffected. # Test case: # w.execute_script("alert('#{'x'*10000000}'.length);") arguments = args.map { |arg| JSON.object_to_js( arg ) }.join(',') javascript = "#{function}(#{arguments});".inspect # If WebDialog is not visible, or no HTML is populated (lacking DOM) then # .execute_script returns false. # # (i) OSX - SU6 # http://forums.sketchucation.com/viewtopic.php?f=180&t=8316#p49259 # Indicates that ; might cause the call to fail. Seems to work without, # so keeping it like that to be on the safe size. # puts "Bridge.execute(#{javascript})" #DEBUG unless @webdialog.execute_script( "Bridge.execute(#{javascript})" ) if @webdialog.visible? raise( CommunicationError, 'Unknown error. Ensure DOM is ready.' ) else raise( CommunicationError, 'Window not visible.' ) end end # (?) Catch JavaScript errors? Or just let the WebDialog display the error? raw_data = @webdialog.get_element_value( 'SKUI_RUBY_BRIDGE' ) @webdialog.execute_script( 'Bridge.reset()' ) # The JS Bridge converts the JS values into Ruby code strings. # (?) Catch exceptions? Re-raise with custom exception? eval( raw_data ) # (?) Bind to top level scope? end |
#get_checkbox_state(ui_id) ⇒ String
Returns the checked state for the given Control.ui_id.
115 116 117 |
# File 'src/SKUI/bridge.rb', line 115 def get_checkbox_state( ui_id ) call( 'Bridge.get_checkbox_state', ui_id ) end |
#get_checked_state(selector) ⇒ String
Returns the checked state for the given jQuery selector.
123 124 125 |
# File 'src/SKUI/bridge.rb', line 123 def get_checked_state( selector ) call( 'Bridge.get_checked_state', selector ) end |
#get_control_rect(ui_id) ⇒ Hash
Returns the Rect for the given Control.
150 151 152 |
# File 'src/SKUI/bridge.rb', line 150 def get_control_rect( ui_id ) call( 'Bridge.get_control_rect', ui_id ) end |
#get_control_value(ui_id) ⇒ String Also known as: get_element_value
Returns the value for the given Control.
158 159 160 |
# File 'src/SKUI/bridge.rb', line 158 def get_control_value( ui_id ) get_value( "##{ui_id}" ) end |
#get_html(selector) ⇒ String
Returns the HTML code for the given jQuery selector.
107 108 109 |
# File 'src/SKUI/bridge.rb', line 107 def get_html( selector ) call( 'Bridge.get_html', selector ) end |
#get_text(selector) ⇒ String
Returns the text content for the given jQuery selector.
131 132 133 |
# File 'src/SKUI/bridge.rb', line 131 def get_text( selector ) call( 'Bridge.get_text', selector ) end |
#get_value(selector) ⇒ String
It appear that under OSX UI::WebDialog.get_element_value doesn’t work for <TEXTAREA> and <SELECT> elements. Using this instead solves the issue.
142 143 144 |
# File 'src/SKUI/bridge.rb', line 142 def get_value( selector ) call( 'Bridge.get_value', selector ) end |
#release ⇒ Nil
165 166 167 168 169 |
# File 'src/SKUI/bridge.rb', line 165 def release @window = nil @webdialog = nil nil end |