class MetaRuby::GUI::HTML::Button

Representation of a button in {Page}

Attributes

id[R]

The button ID

It is used to generate the button's {#base_url}

@return [String]

off_text[R]

The text when the button is OFF

@return [String]

on_text[R]

The text when the button is ON

@return [String]

state[RW]

The current button state

Public Class Methods

new(id, text: nil, on_text: " click to toggle source

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

base_url() click to toggle source

The button base URL

@return [String]

# File lib/metaruby/gui/html/button.rb, line 63
def base_url; "btn://metaruby#{id}" end
html_id() click to toggle source

@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() click to toggle source

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
text() click to toggle source

The button text

# File lib/metaruby/gui/html/button.rb, line 81
def text
    if state then off_text
    else on_text
    end
end
toggle_url() click to toggle source

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
url() click to toggle source

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