Class: MetaRuby::GUI::HTML::Button

Inherits:
Object
  • Object
show all
Defined in:
lib/metaruby/gui/html/button.rb

Overview

Representation of a button in Page

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, text: nil, on_text: "#{id} (on)", off_text: "#{id} (off)", state: false) ⇒ Button

Create a button

Parameters:

  • id (String)

    the button #id

  • text (String) (defaults to: nil)

    the button text for a non-toggling button

  • on_text (String) (defaults to: "#{id} (on)")

    the button text for a toggling button when it is ON

  • off_text (String) (defaults to: "#{id} (off)")

    the button text for a toggling button when it is OFF

  • state (Boolean) (defaults to: false)

    the initial button state



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

Instance Attribute Details

#idString (readonly)

The button ID

It is used to generate the button’s #base_url

Returns:

  • (String)


11
12
13
# File 'lib/metaruby/gui/html/button.rb', line 11

def id
  @id
end

#off_textString (readonly)

The text when the button is OFF

Returns:

  • (String)


21
22
23
# File 'lib/metaruby/gui/html/button.rb', line 21

def off_text
  @off_text
end

#on_textString (readonly)

The text when the button is ON

Returns:

  • (String)


16
17
18
# File 'lib/metaruby/gui/html/button.rb', line 16

def on_text
  @on_text
end

#stateObject

The current button state



24
25
26
# File 'lib/metaruby/gui/html/button.rb', line 24

def state
  @state
end

Instance Method Details

#base_urlString

The button base URL

Returns:

  • (String)


63
# File 'lib/metaruby/gui/html/button.rb', line 63

def base_url; "btn://metaruby#{id}" end

#html_idString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The ID, quoted for HTML

Returns:

  • (String)


58
# File 'lib/metaruby/gui/html/button.rb', line 58

def html_id; id.gsub(/[^\w]/, '_') end

#renderString

Render the button as HTML

Returns:

  • (String)


90
91
92
# File 'lib/metaruby/gui/html/button.rb', line 90

def render
    "<a id=\"#{html_id}\" href=\"#{toggle_url}\">#{text}</a>"
end

#textObject

The button text



81
82
83
84
85
# File 'lib/metaruby/gui/html/button.rb', line 81

def text
    if state then off_text
    else on_text
    end
end

#toggle_urlObject

The URL that would toggle the button (i.e. turn it off if it is ON)



67
68
69
70
71
# File 'lib/metaruby/gui/html/button.rb', line 67

def toggle_url
    if state then "#{base_url}#off"
    else "#{base_url}#on"
    end
end

#urlObject

The URL that represents this button and its current state



74
75
76
77
78
# File 'lib/metaruby/gui/html/button.rb', line 74

def url
    if state then "#{base_url}#on"
    else "#{base_url}#off"
    end
end