Class: Wee::Brush::GenericTagBrush

Inherits:
Wee::Brush show all
Defined in:
lib/wee/html_brushes.rb,
lib/wee/html_brushes.rb

Constant Summary collapse

EVENTS =

generic support for onXXX events

{:click => 'onclick'.freeze,
:dblclick => 'ondblclick'.freeze,
:mouseover => 'onmouseover'.freeze,
:mouseout => 'onmouseout'.freeze,
:change => 'onchange'.freeze}.freeze

Instance Attribute Summary

Attributes inherited from Wee::Brush

#canvas, #document

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Wee::Brush

#close, nesting?, #setup

Constructor Details

#initialize(tag) ⇒ GenericTagBrush

Returns a new instance of GenericTagBrush.



93
94
95
96
97
# File 'lib/wee/html_brushes.rb', line 93

def initialize(tag)
  super()
  @tag = tag
  @attributes = Hash.new
end

Class Method Details

.html_attr(attr, hash = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wee/html_brushes.rb', line 49

def self.html_attr(attr, hash={})
  name = hash[:html_name] || attr
  if hash[:type] == :bool
    class_eval %{
      def #{ attr }(bool=true)
        if bool
          @attributes[:"#{ name }"] = nil
        else
          @attributes.delete(:"#{ name }")
        end
        self
      end
    }
  else
    class_eval %{ 
      def #{ attr }(value)
        if value == nil
          @attributes.delete(:"#{ name }")
        else
          @attributes[:"#{ name }"] = value
        end
        self
      end
    }
  end

  (hash[:aliases] || []).each do |a|
    class_eval "alias #{ a } #{ attr }"
  end

  (hash[:shortcuts] || {}).each_pair do |k, v|
    class_eval "def #{ k }() #{ attr }(#{ v.inspect }) end"
  end
end

Instance Method Details

#callback_on(event, &block) ⇒ Object

Raises:

  • (ArgumentError)


130
131
132
133
134
135
# File 'lib/wee/html_brushes.rb', line 130

def callback_on(event, &block)
  raise ArgumentError unless block
  url = @canvas.url_for_callback(block)
  javascript_on(event, "document.location.href='#{ url }'")
  self
end

#get_oidObject

Returns a unique DOM id for the underlying component



109
110
111
# File 'lib/wee/html_brushes.rb', line 109

def get_oid
  "wee_#{@canvas.current_component.object_id}"
end

#javascript_on(event, javascript) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
# File 'lib/wee/html_brushes.rb', line 123

def javascript_on(event, javascript)
  ev = EVENTS[event] 
  raise ArgumentError unless ev
  @attributes[ev] = "javascript: #{javascript};"
  self
end

#oidObject

Assigns a unique DOM id



102
103
104
# File 'lib/wee/html_brushes.rb', line 102

def oid
  id(get_oid())
end

#onclick_callback(&block) ⇒ Object



161
162
163
# File 'lib/wee/html_brushes.rb', line 161

def onclick_callback(&block)
  callback_on(:click, &block)
end

#onclick_javascript(v) ⇒ Object



157
158
159
# File 'lib/wee/html_brushes.rb', line 157

def onclick_javascript(v)
  javascript_on(:click, v)
end

#ondblclick_callback(&block) ⇒ Object



165
166
167
# File 'lib/wee/html_brushes.rb', line 165

def ondblclick_callback(&block)
  callback_on(:dblclick, &block)
end

#update_component_on(event, component = nil, &callback_block) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/wee/html_brushes.rb', line 144

def update_component_on(event, component=nil, &callback_block)
  component ||= @canvas.current_component

  render_block = proc {|r|
    callback_block.call if callback_block
    r.render(component)
  }

  url = @canvas.url_for_callback(@canvas.session.render_ajax_proc(render_block, component))
  javascript_on(event, "wee.update('#{ url }')")
  self
end

#update_on(event, &render_block) ⇒ Object

Raises:

  • (ArgumentError)


137
138
139
140
141
142
# File 'lib/wee/html_brushes.rb', line 137

def update_on(event, &render_block)
  raise ArgumentError unless render_block
  url = @canvas.url_for_callback(@canvas.session.render_ajax_proc(render_block, @canvas.current_component))
  javascript_on(event, "wee.update('#{ url }')")
  self
end

#with(text = nil, &block) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/wee/html_brushes.rb', line 169

def with(text=nil, &block)
  @document.start_tag(@tag, @attributes)
  @document.text(text) if text
  @canvas.nest(&block) if block
  @document.end_tag(@tag)
  @document = @canvas = nil
end