class MetaRuby::GUI::HTML::Button
Representation of a button in {Page}
Attributes
The button ID
It is used to generate the button's {#base_url}
@return [String]
The text when the button is OFF
@return [String]
The text when the button is ON
@return [String]
The current button state
Public Class Methods
Create a button
@param [String] id the button {#id} @param [String] text the button text for a non-toggling button @param [String] on_text
the button text for a toggling button
when it is ON
@param [String] off_text
the button text for a toggling button
when it is OFF
@param [Boolean] state the initial button state
# File lib/metaruby/gui/html/button.rb, line 35 def initialize(id, text: nil, on_text: "#{id} (on)", off_text: "#{id} (off)", state: false) if id[0, 1] != '/' id = "/#{id}" elsif id[-1, 1] == '/' id = id[0..-2] end @id = id if text @on_text = text @off_text = text @state = true else @on_text = on_text @off_text = off_text @state = state end end
Public Instance Methods
The button base URL
@return [String]
# File lib/metaruby/gui/html/button.rb, line 63 def base_url; "btn://metaruby#{id}" end
@api private
The ID, quoted for HTML
@return [String]
# File lib/metaruby/gui/html/button.rb, line 58 def html_id; id.gsub(/[^\w]/, '_') end
Render the button as HTML
@return [String]
# File lib/metaruby/gui/html/button.rb, line 90 def render "<a id=\"#{html_id}\" href=\"#{toggle_url}\">#{text}</a>" end
The button text
# File lib/metaruby/gui/html/button.rb, line 81 def text if state then off_text else on_text end end
The URL that would toggle the button (i.e. turn it off if it is ON)
# File lib/metaruby/gui/html/button.rb, line 67 def toggle_url if state then "#{base_url}#off" else "#{base_url}#on" end end
The URL that represents this button and its current state
# File lib/metaruby/gui/html/button.rb, line 74 def url if state then "#{base_url}#on" else "#{base_url}#off" end end