Class: Scarpe::Webview::WebWrangler::ElementWrangler
- Inherits:
-
Object
- Object
- Scarpe::Webview::WebWrangler::ElementWrangler
- 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
-
#html_id ⇒ Object
readonly
Returns the value of attribute html_id.
Instance Method Summary collapse
-
#initialize(html_id: nil, selector: nil, multi: false) ⇒ ElementWrangler
constructor
Create an ElementWrangler for the given HTML ID or selector.
-
#inner_html=(new_html) ⇒ Scarpe::Promise
Update the JS DOM element's inner_html.
-
#inner_text=(new_text) ⇒ Scarpe::Promise
Update the JS DOM element's inner_text.
-
#outer_html=(new_html) ⇒ Scarpe::Promise
Update the JS DOM element's outer_html.
-
#promise_update ⇒ Scarpe::Promise
Return a promise that will be fulfilled when all changes scheduled via this ElementWrangler are verified complete.
-
#remove ⇒ Scarpe::Promise
Remove the specified DOM element.
-
#set_attribute(attribute, value) ⇒ Scarpe::Promise
Update the JS DOM element's attribute.
-
#set_style(style_attr, value) ⇒ Scarpe::Promise
Update an attribute of the JS DOM element's style.
- #toggle_input_button(mark) ⇒ Object
-
#value=(new_value) ⇒ Scarpe::Promise
Update the JS DOM element's value.
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.
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_id ⇒ Object (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.
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.
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.
817 818 819 |
# File 'lib/scarpe/wv/web_wrangler.rb', line 817 def outer_html=(new_html) on_each(".outerHTML = `" + new_html + "`") end |
#promise_update ⇒ Scarpe::Promise
Return a promise that will be fulfilled when all changes scheduled via this ElementWrangler are verified complete.
785 786 787 |
# File 'lib/scarpe/wv/web_wrangler.rb', line 785 def promise_update @webwrangler.dom_promise_redraw end |
#remove ⇒ Scarpe::Promise
Remove the specified DOM element
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.
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.
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 (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.
793 794 795 |
# File 'lib/scarpe/wv/web_wrangler.rb', line 793 def value=(new_value) on_each(".value = `" + new_value + "`") end |