Class: Scarpe::Webview::WebWrangler::ElementWrangler

Inherits:
Object
  • Object
show all
Defined in:
lib/scarpe/wv/web_wrangler.rb

Overview

An ElementWrangler provides a way for a Drawable to manipulate is DOM element(s) via their HTML IDs. The most straightforward Drawables can have a single HTML ID and use a single ElementWrangler to make any needed changes.

For now we don't need an ElementWrangler to add DOM elements, just to manipulate them after initial render. New DOM objects for Drawables are normally added via full redraws rather than incremental updates.

Any changes made via ElementWrangler may be cancelled if a full redraw occurs, since it is assumed that small DOM manipulations are no longer needed. If a change would need to be made even if a full redraw occurred, it should be scheduled via WebWrangler#eval_js_async, not via an ElementWrangler.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_id: nil, selector: nil, multi: false) ⇒ ElementWrangler

Create an ElementWrangler for the given HTML ID or selector. The caller should provide exactly one of the html_id or selector.

Parameters:

  • html_id (String) (defaults to: nil)

    the HTML ID for the DOM element

Raises:



754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/scarpe/wv/web_wrangler.rb', line 754

def initialize(html_id: nil, selector: nil, multi: false)
  @webwrangler = ::Scarpe::Webview::DisplayService.instance.wrangler
  raise Scarpe::MissingWranglerError, "Can't get WebWrangler!" unless @webwrangler

  if html_id && !selector
    @selector = "document.getElementById('" + html_id + "')"
  elsif selector && !html_id
    @selector = selector
  else
    raise ArgumentError, "Must provide exactly one of html_id or selector!"
  end

  @multi = multi
end

Instance Attribute Details

#html_idObject (readonly)

Returns the value of attribute html_id.



748
749
750
# File 'lib/scarpe/wv/web_wrangler.rb', line 748

def html_id
  @html_id
end

Instance Method Details

#inner_html=(new_html) ⇒ Scarpe::Promise

Update the JS DOM element's inner_html. The given Ruby value will be converted to string and assigned in backquotes.

Parameters:

  • new_html (String)

    the new inner_html

Returns:

  • (Scarpe::Promise)

    a promise that will be fulfilled when the change is complete



809
810
811
# File 'lib/scarpe/wv/web_wrangler.rb', line 809

def inner_html=(new_html)
  on_each(".innerHTML = `" + new_html + "`")
end

#inner_text=(new_text) ⇒ Scarpe::Promise

Update the JS DOM element's inner_text. The given Ruby value will be converted to string and assigned in single-quotes.

Parameters:

  • new_text (String)

    the new inner_text

Returns:

  • (Scarpe::Promise)

    a promise that will be fulfilled when the change is complete



801
802
803
# File 'lib/scarpe/wv/web_wrangler.rb', line 801

def inner_text=(new_text)
  on_each(".innerText = '" + new_text + "'")
end

#outer_html=(new_html) ⇒ Scarpe::Promise

Update the JS DOM element's outer_html. The given Ruby value will be converted to string and assigned in backquotes.

Parameters:

  • new_html (String)

    the new outer_html

Returns:

  • (Scarpe::Promise)

    a promise that will be fulfilled when the change is complete



817
818
819
# File 'lib/scarpe/wv/web_wrangler.rb', line 817

def outer_html=(new_html)
  on_each(".outerHTML = `" + new_html + "`")
end

#promise_updateScarpe::Promise

Return a promise that will be fulfilled when all changes scheduled via this ElementWrangler are verified complete.

Returns:

  • (Scarpe::Promise)

    a promise that will be fulfilled when scheduled changes are complete



785
786
787
# File 'lib/scarpe/wv/web_wrangler.rb', line 785

def promise_update
  @webwrangler.dom_promise_redraw
end

#removeScarpe::Promise

Remove the specified DOM element

Returns:

  • (Scarpe::Promise)

    a promise that wil be fulfilled when the element is removed



842
843
844
# File 'lib/scarpe/wv/web_wrangler.rb', line 842

def remove
  on_each(".remove()")
end

#set_attribute(attribute, value) ⇒ Scarpe::Promise

Update the JS DOM element's attribute. The given Ruby value will be inspected and assigned.

Parameters:

  • attribute (String)

    the attribute name

  • value (String)

    the new attribute value

Returns:

  • (Scarpe::Promise)

    a promise that will be fulfilled when the change is complete



826
827
828
# File 'lib/scarpe/wv/web_wrangler.rb', line 826

def set_attribute(attribute, value)
  on_each(".setAttribute(" + attribute.inspect + "," + value.inspect + ")")
end

#set_style(style_attr, value) ⇒ Scarpe::Promise

Update an attribute of the JS DOM element's style. The given Ruby value will be inspected and assigned.

Parameters:

  • style_attr (String)

    the style attribute name

  • value (String)

    the new style attribute value

Returns:

  • (Scarpe::Promise)

    a promise that will be fulfilled when the change is complete



835
836
837
# File 'lib/scarpe/wv/web_wrangler.rb', line 835

def set_style(style_attr, value)
  on_each(".style.#{style_attr} = " + value.inspect + ";")
end

#toggle_input_button(mark) ⇒ Object



846
847
848
849
# File 'lib/scarpe/wv/web_wrangler.rb', line 846

def toggle_input_button(mark)
  checked_value = mark ? "true" : "false"
  on_each(".checked = #{checked_value}")
end

#value=(new_value) ⇒ Scarpe::Promise

Update the JS DOM element's value. The given Ruby value will be converted to string and assigned in backquotes.

Parameters:

  • new_value (String)

    the new value

Returns:

  • (Scarpe::Promise)

    a promise that will be fulfilled when the change is complete



793
794
795
# File 'lib/scarpe/wv/web_wrangler.rb', line 793

def value=(new_value)
  on_each(".value = `" + new_value + "`")
end