Class: Wee::HtmlCanvas

Inherits:
Renderer show all
Defined in:
lib/wee/html_canvas.rb

Constant Summary collapse

HTML_NBSP =
" ".freeze
HTML_TYPE_CSS =
'text/css'.freeze
HTML_REL_STYLESHEET =
'stylesheet'.freeze

Instance Attribute Summary

Attributes inherited from Renderer

#callbacks, #current_component, #document, #request, #response, #session

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Renderer

#render, #render_decoration, #with

Constructor Details

#initialize(*args) ⇒ HtmlCanvas

Returns a new instance of HtmlCanvas.



7
8
9
10
# File 'lib/wee/html_canvas.rb', line 7

def initialize(*args)
  super
  @current_brush = nil
end

Class Method Details

.brush_tag(attr, klass, *args_to_new) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wee/html_canvas.rb', line 27

def self.brush_tag(attr, klass, *args_to_new)
  args_to_new = args_to_new.map {|a| a.inspect}.join(", ")
  if klass.instance_method(:with).arity != 0
    class_eval %{ 
      def #{attr}(*args, &block)
        handle(#{klass}.new(#{args_to_new}), *args, &block)
      end
    }
  elsif klass.nesting?
    class_eval %{ 
      def #{attr}(&block)
        handle2(#{klass}.new(#{args_to_new}), &block)
      end
    }
  else
    class_eval %{ 
      def #{attr}
        handle3(#{klass}.new(#{args_to_new}))
      end
    }
  end
end

.generic_single_tag(*attrs) ⇒ Object



54
55
56
# File 'lib/wee/html_canvas.rb', line 54

def self.generic_single_tag(*attrs)
  attrs.each {|attr| brush_tag attr, Brush::GenericSingleTagBrush, attr }
end

.generic_tag(*attrs) ⇒ Object



50
51
52
# File 'lib/wee/html_canvas.rb', line 50

def self.generic_tag(*attrs)
  attrs.each {|attr| brush_tag attr, Brush::GenericTagBrush, attr }
end

Instance Method Details

#build_url(*args) ⇒ Object



208
209
210
# File 'lib/wee/html_canvas.rb', line 208

def build_url(*args)
  @request.build_url(*args)
end

#closeObject



12
13
14
15
# File 'lib/wee/html_canvas.rb', line 12

def close
  @current_brush.close if @current_brush
  @current_brush = nil
end

#css(str) ⇒ Object



116
117
118
# File 'lib/wee/html_canvas.rb', line 116

def css(str)
  style.type('text/css').with(str)
end

#define_divert(tag) ⇒ Object

Define a divert location



150
151
152
# File 'lib/wee/html_canvas.rb', line 150

def define_divert(tag)
  @document.define_divert(tag)
end

#divert(tag, txt = nil, &block) ⇒ Object

Change into an existing divert location and append txt or the contents of block.



158
159
160
# File 'lib/wee/html_canvas.rb', line 158

def divert(tag, txt=nil, &block)
  @document.divert(tag, txt, &block)
end

#encode_text(str) ⇒ Object



109
110
111
112
113
114
# File 'lib/wee/html_canvas.rb', line 109

def encode_text(str)
  @current_brush.close if @current_brush
  @current_brush = nil
  @document.encode_text(str)
  nil
end


188
189
190
# File 'lib/wee/html_canvas.rb', line 188

def link_css(url)
  link.type(HTML_TYPE_CSS).rel(HTML_REL_STYLESHEET).href(url)
end

#multiline_text(str, encode = true) ⇒ Object

converts n into <br/>



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wee/html_canvas.rb', line 130

def multiline_text(str, encode=true)
  @current_brush.close if @current_brush
  @current_brush = nil

  first = true
  str.each_line do |line|
    @document.single_tag(:br) unless first
    first = false

    if encode
      @document.encode_text(line)
    else
      @document.text(line)
    end
  end 
end

#nestObject



17
18
19
20
21
22
23
24
25
# File 'lib/wee/html_canvas.rb', line 17

def nest
  old_brush = @current_brush
  # we don't want that Brush#close is calledas #nest
  # is called from #with -> this avoids an infinite loop
  @current_brush = nil 
  yield
  @current_brush.close if @current_brush
  @current_brush = old_brush
end

#new_radio_groupObject



192
193
194
# File 'lib/wee/html_canvas.rb', line 192

def new_radio_group
  Wee::Brush::RadioButtonTag::RadioGroup.new(self)
end

#once(tag) ⇒ Object

Render specific markup only once. For example style and/or javascript of a component which has many instances.



179
180
181
182
183
# File 'lib/wee/html_canvas.rb', line 179

def once(tag)
  return if  @document.set.has_key?(tag)
  @document.set[tag] = true
  yield if block_given? 
end

#register_callback(type, callback) ⇒ Object



212
213
214
215
216
217
218
219
# File 'lib/wee/html_canvas.rb', line 212

def register_callback(type, callback)
  cbs = @callbacks
  if cbs.respond_to?("#{type}_callbacks")
    cbs.send("#{type}_callbacks").register(@current_component, callback)
  else
    raise
  end
end

#render_style(component) ⇒ Object

Depends on an existing divert location :styles.



123
124
125
# File 'lib/wee/html_canvas.rb', line 123

def render_style(component)
  once(component.class) { try_divert(:styles, component.style) }
end

#select_list(items, &block) ⇒ Object



90
91
92
# File 'lib/wee/html_canvas.rb', line 90

def select_list(items, &block)
  handle2(Brush::SelectListTag.new(items), &block)
end

#space(n = 1) ⇒ Object



96
97
98
# File 'lib/wee/html_canvas.rb', line 96

def space(n=1)
  text(HTML_NBSP*n)
end

#text(str) ⇒ Object Also known as: <<



100
101
102
103
104
105
# File 'lib/wee/html_canvas.rb', line 100

def text(str)
  @current_brush.close if @current_brush
  @current_brush = nil
  @document.text(str)
  nil
end

#try_divert(tag, txt = nil, &block) ⇒ Object

If the divert tag exists, divert, otherwise do nothing.



166
167
168
169
170
171
172
173
# File 'lib/wee/html_canvas.rb', line 166

def try_divert(tag, txt=nil, &block)
  if @document.has_divert?(tag)
    divert(tag, txt, &block)
    true
  else
    false
  end
end

#url_for_callback(callback, type = :action, hash = nil) ⇒ Object



196
197
198
# File 'lib/wee/html_canvas.rb', line 196

def url_for_callback(callback, type=:action, hash=nil)
  url_for_callback_id(register_callback(type, callback), hash)
end

#url_for_callback_id(callback_id, hash = nil) ⇒ Object



200
201
202
203
204
205
206
# File 'lib/wee/html_canvas.rb', line 200

def url_for_callback_id(callback_id, hash=nil)
  if hash
    build_url(hash.update(:callback_id => callback_id))
  else
    build_url(:callback_id => callback_id)
  end
end