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



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

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



114
115
116
117
118
119
120
121
122
# File 'lib/wee/html_canvas.rb', line 114

def css(str)
  @current_brush.close if @current_brush
  @current_brush = nil
  @document.start_tag(:style, 'type' => 'text/css')
  @document.write("<!--\n")
  @document.text(str)
  @document.write("-->\n")
  @document.end_tag(:style)
end

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

Define a divert location or change into an existing divert location (and append txt or the contents of block).



148
149
150
# File 'lib/wee/html_canvas.rb', line 148

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

#encode_text(str) ⇒ Object



107
108
109
110
111
112
# File 'lib/wee/html_canvas.rb', line 107

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


175
176
177
# File 'lib/wee/html_canvas.rb', line 175

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/>



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

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



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

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.



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

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

#register_callback(type, callback) ⇒ Object



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

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

#select_list(items, &block) ⇒ Object



88
89
90
# File 'lib/wee/html_canvas.rb', line 88

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

#space(n = 1) ⇒ Object



94
95
96
# File 'lib/wee/html_canvas.rb', line 94

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

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



98
99
100
101
102
103
# File 'lib/wee/html_canvas.rb', line 98

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

#update(*components) ⇒ Object

To update components in an AJAX request.



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

def update(*components)
  components.each {|c|
    self.callbacks.unregister(c)
    self.render_decoration(c)
  }
end

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



183
184
185
# File 'lib/wee/html_canvas.rb', line 183

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



187
188
189
190
191
192
193
# File 'lib/wee/html_canvas.rb', line 187

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